sorcery 0.4.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. data/Gemfile +5 -2
  2. data/Gemfile.lock +12 -0
  3. data/README.rdoc +12 -7
  4. data/VERSION +1 -1
  5. data/lib/sorcery/controller/adapters/sinatra.rb +35 -28
  6. data/lib/sorcery/controller/submodules/brute_force_protection.rb +1 -1
  7. data/lib/sorcery/controller/submodules/http_basic_auth.rb +1 -1
  8. data/lib/sorcery/crypto_providers/bcrypt.rb +2 -4
  9. data/lib/sorcery/crypto_providers/common.rb +2 -2
  10. data/lib/sorcery/engine.rb +0 -11
  11. data/lib/sorcery/initializers/initializer.rb +5 -1
  12. data/lib/sorcery/model/adapters/active_record.rb +28 -0
  13. data/lib/sorcery/model/adapters/mongoid.rb +64 -0
  14. data/lib/sorcery/model/submodules/activity_logging.rb +12 -3
  15. data/lib/sorcery/model/submodules/brute_force_protection.rb +6 -1
  16. data/lib/sorcery/model/submodules/external.rb +1 -0
  17. data/lib/sorcery/model/submodules/remember_me.rb +15 -1
  18. data/lib/sorcery/model/submodules/reset_password.rb +14 -5
  19. data/lib/sorcery/model/submodules/user_activation.rb +11 -1
  20. data/lib/sorcery/model/temporary_token.rb +1 -1
  21. data/lib/sorcery/model.rb +37 -10
  22. data/lib/sorcery/sinatra.rb +0 -1
  23. data/lib/sorcery/test_helpers/internal/rails.rb +53 -0
  24. data/lib/sorcery/test_helpers/internal/sinatra.rb +74 -0
  25. data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +74 -0
  26. data/lib/sorcery/test_helpers/internal.rb +56 -0
  27. data/lib/sorcery/test_helpers/rails.rb +5 -43
  28. data/lib/sorcery/test_helpers/sinatra.rb +62 -102
  29. data/lib/sorcery/test_helpers.rb +0 -47
  30. data/lib/sorcery.rb +27 -1
  31. data/sorcery.gemspec +162 -10
  32. data/spec/Gemfile +1 -1
  33. data/spec/Gemfile.lock +9 -4
  34. data/spec/rails3/Gemfile +1 -1
  35. data/spec/rails3/Gemfile.lock +6 -9
  36. data/spec/rails3/spec/controller_activity_logging_spec.rb +2 -4
  37. data/spec/rails3/spec/controller_oauth_spec.rb +3 -3
  38. data/spec/rails3/spec/controller_session_timeout_spec.rb +20 -19
  39. data/spec/rails3/spec/controller_spec.rb +6 -16
  40. data/spec/rails3/spec/spec_helper.rb +2 -2
  41. data/spec/rails3/spec/user_activation_spec.rb +2 -12
  42. data/spec/rails3/spec/user_brute_force_protection_spec.rb +2 -30
  43. data/spec/rails3/spec/user_remember_me_spec.rb +3 -11
  44. data/spec/rails3/spec/user_reset_password_spec.rb +15 -16
  45. data/spec/rails3/spec/user_spec.rb +18 -13
  46. data/spec/rails3_mongoid/.gitignore +4 -0
  47. data/spec/rails3_mongoid/.rspec +1 -0
  48. data/spec/rails3_mongoid/Gemfile +14 -0
  49. data/spec/rails3_mongoid/Gemfile.lock +143 -0
  50. data/spec/rails3_mongoid/Rakefile +11 -0
  51. data/spec/rails3_mongoid/app/controllers/application_controller.rb +108 -0
  52. data/spec/rails3_mongoid/app/helpers/application_helper.rb +2 -0
  53. data/spec/rails3_mongoid/app/mailers/sorcery_mailer.rb +25 -0
  54. data/spec/rails3_mongoid/app/models/authentication.rb +7 -0
  55. data/spec/rails3_mongoid/app/models/user.rb +5 -0
  56. data/spec/rails3_mongoid/app/views/layouts/application.html.erb +14 -0
  57. data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_email.html.erb +17 -0
  58. data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_email.text.erb +9 -0
  59. data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_success_email.html.erb +17 -0
  60. data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_success_email.text.erb +9 -0
  61. data/spec/rails3_mongoid/app/views/sorcery_mailer/reset_password_email.html.erb +16 -0
  62. data/spec/rails3_mongoid/app/views/sorcery_mailer/reset_password_email.text.erb +8 -0
  63. data/spec/rails3_mongoid/config/application.rb +51 -0
  64. data/spec/rails3_mongoid/config/boot.rb +13 -0
  65. data/spec/rails3_mongoid/config/environment.rb +5 -0
  66. data/spec/rails3_mongoid/config/environments/development.rb +26 -0
  67. data/spec/rails3_mongoid/config/environments/in_memory.rb +0 -0
  68. data/spec/rails3_mongoid/config/environments/production.rb +49 -0
  69. data/spec/rails3_mongoid/config/environments/test.rb +35 -0
  70. data/spec/rails3_mongoid/config/initializers/backtrace_silencers.rb +7 -0
  71. data/spec/rails3_mongoid/config/initializers/inflections.rb +10 -0
  72. data/spec/rails3_mongoid/config/initializers/mime_types.rb +5 -0
  73. data/spec/rails3_mongoid/config/initializers/secret_token.rb +7 -0
  74. data/spec/rails3_mongoid/config/initializers/session_store.rb +8 -0
  75. data/spec/rails3_mongoid/config/locales/en.yml +5 -0
  76. data/spec/rails3_mongoid/config/mongoid.yml +7 -0
  77. data/spec/rails3_mongoid/config/routes.rb +59 -0
  78. data/spec/rails3_mongoid/config.ru +4 -0
  79. data/spec/rails3_mongoid/db/schema.rb +23 -0
  80. data/spec/rails3_mongoid/db/seeds.rb +7 -0
  81. data/spec/rails3_mongoid/lib/tasks/.gitkeep +0 -0
  82. data/spec/rails3_mongoid/public/404.html +26 -0
  83. data/spec/rails3_mongoid/public/422.html +26 -0
  84. data/spec/rails3_mongoid/public/500.html +26 -0
  85. data/spec/rails3_mongoid/public/favicon.ico +0 -0
  86. data/spec/rails3_mongoid/public/images/rails.png +0 -0
  87. data/spec/rails3_mongoid/public/javascripts/application.js +2 -0
  88. data/spec/rails3_mongoid/public/javascripts/controls.js +965 -0
  89. data/spec/rails3_mongoid/public/javascripts/dragdrop.js +974 -0
  90. data/spec/rails3_mongoid/public/javascripts/effects.js +1123 -0
  91. data/spec/rails3_mongoid/public/javascripts/prototype.js +6001 -0
  92. data/spec/rails3_mongoid/public/javascripts/rails.js +175 -0
  93. data/spec/rails3_mongoid/public/robots.txt +5 -0
  94. data/spec/rails3_mongoid/public/stylesheets/.gitkeep +0 -0
  95. data/spec/rails3_mongoid/script/rails +6 -0
  96. data/spec/rails3_mongoid/spec/spec.opts +2 -0
  97. data/spec/rails3_mongoid/spec/spec_helper.orig.rb +27 -0
  98. data/spec/rails3_mongoid/spec/spec_helper.rb +55 -0
  99. data/spec/rails3_mongoid/spec/user_activation_spec.rb +178 -0
  100. data/spec/rails3_mongoid/spec/user_activity_logging_spec.rb +31 -0
  101. data/spec/rails3_mongoid/spec/user_brute_force_protection_spec.rb +41 -0
  102. data/spec/rails3_mongoid/spec/user_oauth_spec.rb +34 -0
  103. data/spec/rails3_mongoid/spec/user_remember_me_spec.rb +51 -0
  104. data/spec/rails3_mongoid/spec/user_reset_password_spec.rb +182 -0
  105. data/spec/rails3_mongoid/spec/user_spec.rb +319 -0
  106. data/spec/rails3_mongoid/vendor/plugins/.gitkeep +0 -0
  107. data/spec/sinatra/Gemfile +4 -1
  108. data/spec/sinatra/Gemfile.lock +22 -39
  109. data/spec/sinatra/Rakefile +1 -1
  110. data/spec/sinatra/filters.rb +20 -14
  111. data/spec/sinatra/modular.rb +157 -0
  112. data/spec/sinatra/spec/controller_session_timeout_spec.rb +23 -22
  113. data/spec/sinatra/spec/spec_helper.rb +2 -2
  114. data/spec/sinatra_modular/Gemfile +15 -0
  115. data/spec/sinatra_modular/Gemfile.lock +117 -0
  116. data/spec/sinatra_modular/Rakefile +11 -0
  117. data/spec/sinatra_modular/authentication.rb +3 -0
  118. data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  119. data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  120. data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  121. data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +16 -0
  122. data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +14 -0
  123. data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  124. data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  125. data/spec/sinatra_modular/filters.rb +27 -0
  126. data/spec/sinatra_modular/modular.rb +157 -0
  127. data/spec/sinatra_modular/myapp.rb +133 -0
  128. data/spec/sinatra_modular/sorcery_mailer.rb +25 -0
  129. data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +85 -0
  130. data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +70 -0
  131. data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +53 -0
  132. data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +120 -0
  133. data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +121 -0
  134. data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +64 -0
  135. data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +57 -0
  136. data/spec/sinatra_modular/spec_modular/controller_spec.rb +116 -0
  137. data/spec/sinatra_modular/spec_modular/spec.opts +2 -0
  138. data/spec/sinatra_modular/spec_modular/spec_helper.rb +51 -0
  139. data/spec/sinatra_modular/user.rb +6 -0
  140. data/spec/sinatra_modular/views/test_login.erb +4 -0
  141. metadata +205 -37
  142. data/spec/untitled folder +0 -18
data/lib/sorcery/model.rb CHANGED
@@ -14,27 +14,38 @@ module Sorcery
14
14
  self.class_eval do
15
15
  extend ClassMethods # included here, before submodules, so they can be overriden by them.
16
16
  include InstanceMethods
17
-
18
- # set the user_class for the controller methods which call it
19
- ::Sorcery::Controller::Config.user_class = self
17
+
20
18
  @sorcery_config.submodules = ::Sorcery::Controller::Config.submodules
21
19
  @sorcery_config.submodules.each do |mod|
22
20
  begin
23
21
  include Submodules.const_get(mod.to_s.split("_").map {|p| p.capitalize}.join(""))
24
22
  rescue NameError
25
- # don't stop on a missing submodule.
23
+ # don't stop on a missing submodule. Needed because some submodules are only defined in the controller side.
26
24
  end
27
25
  end
28
26
  end
29
-
27
+
28
+ # This runs the options block set in the initializer on the model class.
30
29
  ::Sorcery::Controller::Config.user_config.tap{|blk| blk.call(@sorcery_config) if blk}
31
-
30
+
31
+ # Mongoid support
32
+ self.class_eval do
33
+ field sorcery_config.username_attribute_name, type: String
34
+ #field sorcery_config.password_attribute_name, type: String
35
+ field sorcery_config.email_attribute_name, type: String unless sorcery_config.username_attribute_name == sorcery_config.email_attribute_name
36
+ field sorcery_config.crypted_password_attribute_name, type: String
37
+ field sorcery_config.salt_attribute_name, type: String
38
+ end if defined?(Mongoid) and self.ancestors.include?(Mongoid::Document)
39
+
40
+ # add virtual password accessor and ORM callbacks
32
41
  self.class_eval do
33
42
  attr_accessor @sorcery_config.password_attribute_name
34
43
  attr_protected @sorcery_config.crypted_password_attribute_name, @sorcery_config.salt_attribute_name
35
44
  before_save :encrypt_password, :if => Proc.new { |record| record.send(sorcery_config.password_attribute_name).present? }
36
45
  after_save :clear_virtual_password, :if => Proc.new { |record| record.send(sorcery_config.password_attribute_name).present? }
37
46
  end
47
+
48
+ @sorcery_config.after_config << :add_config_inheritance if @sorcery_config.subclasses_inherit_config
38
49
  @sorcery_config.after_config.each { |c| send(c) }
39
50
  end
40
51
  end
@@ -53,7 +64,7 @@ module Sorcery
53
64
  # returns the user if success, nil otherwise.
54
65
  def authenticate(*credentials)
55
66
  raise ArgumentError, "at least 2 arguments required" if credentials.size < 2
56
- user = where("#{@sorcery_config.username_attribute_name} = ?", credentials[0]).first
67
+ user = find_by_credentials(credentials)
57
68
  _salt = user.send(@sorcery_config.salt_attribute_name) if user && !@sorcery_config.salt_attribute_name.nil? && !@sorcery_config.encryption_provider.nil?
58
69
  user if user && @sorcery_config.before_authenticate.all? {|c| user.send(c)} && credentials_match?(user.send(@sorcery_config.crypted_password_attribute_name),credentials[1],_salt)
59
70
  end
@@ -76,6 +87,20 @@ module Sorcery
76
87
  @sorcery_config.encryption_provider.matches?(crypted, *tokens)
77
88
  end
78
89
 
90
+ def add_config_inheritance
91
+ self.class_eval do
92
+ def self.inherited(subclass)
93
+ subclass.class_eval do
94
+ class << self
95
+ attr_accessor :sorcery_config
96
+ end
97
+ end
98
+ subclass.sorcery_config = sorcery_config
99
+ super
100
+ end
101
+ end
102
+ end
103
+
79
104
  end
80
105
 
81
106
  module InstanceMethods
@@ -93,10 +118,10 @@ module Sorcery
93
118
  protected
94
119
 
95
120
  # creates new salt and saves it.
96
- # encrypts password with salt and save it.
121
+ # encrypts password with salt and saves it.
97
122
  def encrypt_password
98
123
  config = sorcery_config
99
- new_salt = self.send(:"#{config.salt_attribute_name}=", generate_random_token) if !config.salt_attribute_name.nil?
124
+ self.send(:"#{config.salt_attribute_name}=", new_salt = generate_random_token) if !config.salt_attribute_name.nil?
100
125
  self.send(:"#{config.crypted_password_attribute_name}=", self.class.encrypt(self.send(config.password_attribute_name),new_salt))
101
126
  end
102
127
 
@@ -117,7 +142,7 @@ module Sorcery
117
142
 
118
143
  # Random code, used for salt and temporary tokens.
119
144
  def generate_random_token
120
- return Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
145
+ Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
121
146
  end
122
147
  end
123
148
 
@@ -133,6 +158,7 @@ module Sorcery
133
158
  :salt_attribute_name, # change default salt attribute.
134
159
  :stretches, # how many times to apply encryption to the password.
135
160
  :encryption_key, # encryption key used to encrypt reversible encryptions such as AES256.
161
+ :subclasses_inherit_config, # make this configuration inheritable for subclasses. Useful for ActiveRecord's STI.
136
162
 
137
163
  :submodules, # configured in config/application.rb
138
164
  :before_authenticate, # an array of method names to call before authentication completes. used internally.
@@ -156,6 +182,7 @@ module Sorcery
156
182
  :@salt_join_token => "",
157
183
  :@salt_attribute_name => :salt,
158
184
  :@stretches => nil,
185
+ :@subclasses_inherit_config => false,
159
186
  :@before_authenticate => [],
160
187
  :@after_config => []
161
188
  }
@@ -1,4 +1,3 @@
1
- ActiveRecord::Base.send(:include, Sorcery::Model) if defined?(ActiveRecord)
2
1
  if defined?(Sinatra::Base)
3
2
  Sinatra::Base.send(:include, Sorcery::Controller::Adapters::Sinatra)
4
3
  Sinatra::Base.send(:include, Sorcery::Controller)
@@ -0,0 +1,53 @@
1
+ module Sorcery
2
+ module TestHelpers
3
+ module Internal
4
+ module Rails
5
+ include ::Sorcery::TestHelpers::Rails
6
+
7
+ SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS = [:register_last_activity_time_to_db, :deny_banned_user, :validate_session]
8
+
9
+ def sorcery_reload!(submodules = [], options = {})
10
+ reload_user_class
11
+
12
+ # return to no-module configuration
13
+ ::Sorcery::Controller::Config.init!
14
+ ::Sorcery::Controller::Config.reset!
15
+
16
+ # remove all plugin before_filters so they won't fail other tests.
17
+ # I don't like this way, but I didn't find another.
18
+ # hopefully it won't break until Rails 4.
19
+ ApplicationController._process_action_callbacks.delete_if {|c| SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }
20
+
21
+ # configure
22
+ ::Sorcery::Controller::Config.submodules = submodules
23
+ ::Sorcery::Controller::Config.user_class = nil
24
+ ActionController::Base.send(:include,::Sorcery::Controller)
25
+ ::Sorcery::Controller::Config.user_class = User
26
+
27
+ ::Sorcery::Controller::Config.user_config do |user|
28
+ options.each do |property,value|
29
+ user.send(:"#{property}=", value)
30
+ end
31
+ end
32
+ User.authenticates_with_sorcery!
33
+ end
34
+
35
+ def sorcery_controller_property_set(property, value)
36
+ ::Sorcery::Controller::Config.send(:"#{property}=", value)
37
+ end
38
+
39
+ def sorcery_controller_external_property_set(provider, property, value)
40
+ ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
41
+ end
42
+
43
+ # This helper is used to fake multiple users signing in in tests.
44
+ # It does so by clearing @current_user, thus allowing a new user to login,
45
+ # all this without calling the :logout action explicitly.
46
+ # A dirty dirty hack.
47
+ def clear_user_without_logout
48
+ subject.instance_variable_set(:@current_user,nil)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,74 @@
1
+ module Sorcery
2
+ module TestHelpers
3
+ module Internal
4
+ module Sinatra
5
+ include ::Sorcery::TestHelpers::Sinatra::CookieSessionMethods
6
+ include ::Sorcery::TestHelpers::Sinatra::InstanceMethods
7
+
8
+ class ::Sinatra::Application
9
+ class << self
10
+ attr_accessor :sorcery_vars
11
+ end
12
+ @sorcery_vars = {}
13
+
14
+ # NOTE: see before and after test filters in filters.rb
15
+
16
+ def save_instance_vars
17
+ instance_variables.each do |var|
18
+ self.class.sorcery_vars[:"#{var.to_s.delete("@")}"] = instance_variable_get(var)
19
+ end
20
+ end
21
+ end
22
+
23
+ ::RSpec::Matchers.define :redirect_to do |expected|
24
+ match do |actual|
25
+ actual.status == 302 && actual.location == expected
26
+ end
27
+ end
28
+
29
+ def clear_user_without_logout
30
+ get_sinatra_app(subject).instance_variable_set(:@current_user, nil)
31
+ end
32
+
33
+ def assigns
34
+ ::Sinatra::Application.sorcery_vars
35
+ end
36
+
37
+ def sorcery_reload!(submodules = [], options = {})
38
+ reload_user_class
39
+
40
+ # return to no-module configuration
41
+ ::Sorcery::Controller::Config.init!
42
+ ::Sorcery::Controller::Config.reset!
43
+
44
+ # clear all filters
45
+ ::Sinatra::Application.instance_variable_set(:@filters, {:before => [], :after => []})
46
+ load File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec', 'sinatra', 'filters.rb')
47
+ ::Sinatra::Application.send(:include, ::Filters)
48
+
49
+ # configure
50
+ ::Sorcery::Controller::Config.submodules = submodules
51
+ ::Sorcery::Controller::Config.user_class = nil
52
+ ::Sinatra::Application.send(:include, Sorcery::Controller::Adapters::Sinatra)
53
+ ::Sinatra::Application.send(:include, Sorcery::Controller)
54
+ ::Sorcery::Controller::Config.user_class = User
55
+
56
+ ::Sorcery::Controller::Config.user_config do |user|
57
+ options.each do |property, value|
58
+ user.send(:"#{property}=", value)
59
+ end
60
+ end
61
+ User.authenticates_with_sorcery!
62
+ end
63
+
64
+ def sorcery_controller_property_set(property, value)
65
+ ::Sorcery::Controller::Config.send(:"#{property}=", value)
66
+ end
67
+
68
+ def sorcery_controller_external_property_set(provider, property, value)
69
+ ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,74 @@
1
+ module Sorcery
2
+ module TestHelpers
3
+ module Internal
4
+ module SinatraModular
5
+ include ::Sorcery::TestHelpers::Sinatra::CookieSessionMethods
6
+ include ::Sorcery::TestHelpers::Sinatra::InstanceMethods
7
+
8
+ class ::Modular
9
+ class << self
10
+ attr_accessor :sorcery_vars
11
+ end
12
+ @sorcery_vars = {}
13
+
14
+ # NOTE: see before and after test filters in filters.rb
15
+
16
+ def save_instance_vars
17
+ instance_variables.each do |var|
18
+ self.class.sorcery_vars[:"#{var.to_s.delete("@")}"] = instance_variable_get(var)
19
+ end
20
+ end
21
+ end
22
+
23
+ ::RSpec::Matchers.define :redirect_to do |expected|
24
+ match do |actual|
25
+ actual.status == 302 && actual.location == expected
26
+ end
27
+ end
28
+
29
+ def clear_user_without_logout
30
+ get_sinatra_app(subject).instance_variable_set(:@current_user, nil)
31
+ end
32
+
33
+ def assigns
34
+ ::Modular.sorcery_vars
35
+ end
36
+
37
+ def sorcery_reload!(submodules = [], options = {})
38
+ reload_user_class
39
+
40
+ # return to no-module configuration
41
+ ::Sorcery::Controller::Config.init!
42
+ ::Sorcery::Controller::Config.reset!
43
+
44
+ # clear all filters
45
+ ::Sinatra::Base.instance_variable_set(:@filters, {:before => [], :after => []})
46
+ require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec', 'sinatra', 'filters')
47
+ ::Sinatra::Base.send(:include, ::Filters)
48
+
49
+ # configure
50
+ ::Sorcery::Controller::Config.submodules = submodules
51
+ ::Sorcery::Controller::Config.user_class = nil
52
+ ::Sinatra::Base.send(:include, Sorcery::Controller::Adapters::Sinatra)
53
+ ::Sinatra::Base.send(:include, Sorcery::Controller)
54
+ ::Sorcery::Controller::Config.user_class = User
55
+
56
+ ::Sorcery::Controller::Config.user_config do |user|
57
+ options.each do |property, value|
58
+ user.send(:"#{property}=", value)
59
+ end
60
+ end
61
+ User.authenticates_with_sorcery!
62
+ end
63
+
64
+ def sorcery_controller_property_set(property, value)
65
+ ::Sorcery::Controller::Config.send(:"#{property}=", value)
66
+ end
67
+
68
+ def sorcery_controller_external_property_set(provider, property, value)
69
+ ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,56 @@
1
+ module Sorcery
2
+ module TestHelpers
3
+ # Internal TestHelpers are used to test the gem, internally, and should not be used to test apps *using* sorcery.
4
+ # This file will be included in the spec_helper file.
5
+ module Internal
6
+ def self.included(base)
7
+ # reducing default cost for specs speed
8
+ CryptoProviders::BCrypt.class_eval do
9
+ class << self
10
+ def cost
11
+ 1
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ # a patch to fix a bug in testing that happens when you 'destroy' a session twice.
18
+ # After the first destroy, the session is an ordinary hash, and then when destroy is called again there's an exception.
19
+ class ::Hash
20
+ def destroy
21
+ clear
22
+ end
23
+ end
24
+
25
+ def create_new_user(attributes_hash = nil)
26
+ user_attributes_hash = attributes_hash || {:username => 'gizmo', :email => "bla@bla.com", :password => 'secret'}
27
+ @user = User.new(user_attributes_hash)
28
+ @user.save!
29
+ @user
30
+ end
31
+
32
+ def create_new_external_user(provider, attributes_hash = nil)
33
+ user_attributes_hash = attributes_hash || {:username => 'gizmo'}
34
+ @user = User.new(user_attributes_hash)
35
+ @user.save!
36
+ @user.authentications.create!({:provider => provider, :uid => 123})
37
+ @user
38
+ end
39
+
40
+ def sorcery_model_property_set(property, *values)
41
+ User.class_eval do
42
+ sorcery_config.send(:"#{property}=", *values)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ # reload user class between specs
49
+ # so it will be possible to test the different submodules in isolation
50
+ def reload_user_class
51
+ Object.send(:remove_const,:User)
52
+ load 'user.rb'
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,54 +1,16 @@
1
1
  module Sorcery
2
2
  module TestHelpers
3
3
  module Rails
4
- SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS = [:register_last_activity_time_to_db, :deny_banned_user, :validate_session]
5
-
6
- def sorcery_reload!(submodules = [], options = {})
7
- reload_user_class
8
-
9
- # return to no-module configuration
10
- ::Sorcery::Controller::Config.init!
11
- ::Sorcery::Controller::Config.reset!
12
-
13
- # remove all plugin before_filters so they won't fail other tests.
14
- # I don't like this way, but I didn't find another.
15
- # hopefully it won't break until Rails 4.
16
- ApplicationController._process_action_callbacks.delete_if {|c| SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }
17
-
18
- # configure
19
- ::Sorcery::Controller::Config.submodules = submodules
20
- ::Sorcery::Controller::Config.user_class = nil
21
- ActionController::Base.send(:include,::Sorcery::Controller)
22
-
23
- ::Sorcery::Controller::Config.user_config do |user|
24
- options.each do |property,value|
25
- user.send(:"#{property}=", value)
26
- end
27
- end
28
- User.authenticates_with_sorcery!
29
- end
30
-
31
- def sorcery_controller_property_set(property, value)
32
- ::Sorcery::Controller::Config.send(:"#{property}=", value)
33
- end
34
-
35
- def sorcery_controller_external_property_set(provider, property, value)
36
- ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
37
- end
38
-
4
+ # logins a user and calls all callbacks
39
5
  def login_user(user = nil)
40
6
  user ||= @user
41
- subject.send(:login_user,user)
42
- subject.send(:after_login!,user,[user.username,'secret'])
7
+ @controller.send(:login_user,user)
8
+ @controller.send(:after_login!,user,[user.send(user.sorcery_config.username_attribute_name),'secret'])
43
9
  end
44
10
 
45
11
  def logout_user
46
- subject.send(:logout)
47
- end
48
-
49
- def clear_user_without_logout
50
- subject.instance_variable_set(:@current_user,nil)
12
+ @controller.send(:logout)
51
13
  end
52
14
  end
53
15
  end
54
- end
16
+ end
@@ -1,127 +1,87 @@
1
1
  module Sorcery
2
2
  module TestHelpers
3
3
  module Sinatra
4
-
5
- class MyApp
6
- class << self
7
- def new
8
- return this
4
+ def self.included(base)
5
+ base.send(:include, InstanceMethods)
6
+ base.send(:include, CookieSessionMethods)
7
+ ::Sinatra::Base.class_eval do
8
+ class << self
9
+ attr_accessor :rack_test_session
9
10
  end
10
- end
11
- end
12
-
13
- class ::Sinatra::Application
14
- class << self
15
- attr_accessor :sorcery_vars
16
- end
17
- @sorcery_vars = {}
18
11
 
19
- # NOTE: see before and after test filters in filters.rb
20
-
21
- def save_instance_vars
22
- instance_variables.each do |var|
23
- self.class.sorcery_vars[:"#{var.to_s.delete("@")}"] = instance_variable_get(var)
12
+ def self.inherited(subclass)
13
+ super
14
+ subclass.class_eval do
15
+ class << self
16
+ attr_accessor :rack_test_session
17
+ end
18
+ end
19
+ end
20
+
21
+ include CookieSessionMethods
22
+
23
+ def rack_test_session
24
+ self.class.rack_test_session
24
25
  end
25
26
  end
26
27
  end
27
-
28
- ::RSpec::Matchers.define :redirect_to do |expected|
29
- match do |actual|
30
- actual.status == 302 && actual.location == expected
31
- end
32
- end
33
-
34
- def get_sinatra_app(app)
35
- while app.class != ::Sinatra::Application do
36
- app = app.instance_variable_get(:@app)
37
- end
38
- app
39
- end
40
-
41
- def login_user(user=nil)
42
- user ||= @user
43
- get_sinatra_app(subject).send(:login_user,user)
44
- get_sinatra_app(subject).send(:after_login!,user,[user.username,'secret'])
45
- end
46
-
47
- def logout_user
48
- get_sinatra_app(subject).send(:logout)
49
- end
50
28
 
51
- def clear_user_without_logout
52
- get_sinatra_app(subject).instance_variable_set(:@current_user,nil)
53
- end
54
-
55
- def assigns
56
- ::Sinatra::Application.sorcery_vars
57
- end
58
-
59
- class SessionData
60
- def initialize(cookies)
61
- @cookies = cookies
62
- @data = cookies['rack.session']
63
- if @data
64
- @data = @data.unpack("m*").first
65
- @data = Marshal.load(@data)
66
- else
67
- @data = {}
29
+ module InstanceMethods
30
+ def get_sinatra_app(app)
31
+ while !app.kind_of? ::Sinatra::Base do
32
+ app = app.instance_variable_get(:@app)
68
33
  end
34
+ app
69
35
  end
70
36
 
71
- def [](key)
72
- @data[key]
37
+ def login_user(user=nil)
38
+ user ||= @user
39
+ get_sinatra_app(app).send(:login_user, user)
40
+ get_sinatra_app(app).send(:after_login!, user, [user.send(user.sorcery_config.username_attribute_name), 'secret'])
73
41
  end
74
42
 
75
- def []=(key, value)
76
- @data[key] = value
77
- session_data = Marshal.dump(@data)
78
- session_data = [session_data].pack("m*")
79
- @cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
80
- raise "session variable not set" unless @cookies['rack.session'] == session_data
43
+ def logout_user
44
+ get_sinatra_app(app).send(:logout)
81
45
  end
82
46
  end
83
47
 
84
- def session
85
- SessionData.new(rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar)
86
- end
87
-
88
- def cookies
89
- rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar
90
- end
91
-
92
- def sorcery_reload!(submodules = [], options = {})
93
- reload_user_class
48
+ module CookieSessionMethods
49
+ class SessionData
50
+ def initialize(cookies)
51
+ @cookies = cookies
52
+ @data = cookies['rack.session']
53
+ if @data
54
+ @data = @data.unpack("m*").first
55
+ @data = Marshal.load(@data)
56
+ else
57
+ @data = {}
58
+ end
59
+ end
94
60
 
95
- # return to no-module configuration
96
- ::Sorcery::Controller::Config.init!
97
- ::Sorcery::Controller::Config.reset!
61
+ def [](key)
62
+ @data[key]
63
+ end
98
64
 
99
- # clear all filters
100
- ::Sinatra::Application.instance_variable_set(:@filters,{:before => [],:after => []})
101
- ::Sinatra::Application.class_eval do
102
- load File.join(File.dirname(__FILE__),'..','..','..','spec','sinatra','filters.rb')
103
- end
104
-
105
- # configure
106
- ::Sorcery::Controller::Config.submodules = submodules
107
- ::Sorcery::Controller::Config.user_class = nil
108
- ::Sinatra::Application.send(:include, Sorcery::Controller::Adapters::Sinatra)
109
- ::Sinatra::Application.send(:include, Sorcery::Controller)
110
-
111
- ::Sorcery::Controller::Config.user_config do |user|
112
- options.each do |property,value|
113
- user.send(:"#{property}=", value)
65
+ def []=(key, value)
66
+ @data[key] = value
67
+ session_data = Marshal.dump(@data)
68
+ session_data = [session_data].pack("m*")
69
+ @cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
70
+ raise "session variable not set" unless @cookies['rack.session'] == session_data
71
+ end
72
+
73
+ def clear
74
+ @data = {}
114
75
  end
115
76
  end
116
- User.authenticates_with_sorcery!
117
- end
118
-
119
- def sorcery_controller_property_set(property, value)
120
- ::Sorcery::Controller::Config.send(:"#{property}=", value)
121
- end
122
77
 
123
- def sorcery_controller_external_property_set(provider, property, value)
124
- ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
78
+ def session
79
+ SessionData.new(cookies)
80
+ end
81
+
82
+ def cookies
83
+ rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar
84
+ end
125
85
  end
126
86
  end
127
87
  end
@@ -1,52 +1,5 @@
1
1
  module Sorcery
2
- # This file will be included in the spec_helper file.
3
2
  module TestHelpers
4
- def self.included(base)
5
- # reducing default cost for specs speed
6
- CryptoProviders::BCrypt.class_eval do
7
- class << self
8
- def cost
9
- 1
10
- end
11
- end
12
- end
13
- end
14
-
15
- # a patch to fix a bug in testing that happens when you 'destroy' a session twice.
16
- # After the first destroy, the session is an ordinary hash, and then when destroy is called again there's an exception.
17
- class ::Hash
18
- def destroy
19
- clear
20
- end
21
- end
22
-
23
- def create_new_user(attributes_hash = nil)
24
- user_attributes_hash = attributes_hash || {:username => 'gizmo', :email => "bla@bla.com", :password => 'secret'}
25
- @user = User.new(user_attributes_hash)
26
- @user.save!
27
- @user
28
- end
29
3
 
30
- def create_new_external_user(provider, attributes_hash = nil)
31
- user_attributes_hash = attributes_hash || {:username => 'gizmo', :authentications_attributes => [{:provider => provider, :uid => 123}]}
32
- @user = User.new(user_attributes_hash)
33
- @user.save!
34
- @user
35
- end
36
-
37
- def sorcery_model_property_set(property, *values)
38
- User.class_eval do
39
- sorcery_config.send(:"#{property}=", *values)
40
- end
41
- end
42
-
43
- private
44
-
45
- # reload user class between specs
46
- # so it will be possible to test the different submodules in isolation
47
- def reload_user_class
48
- Object.send(:remove_const,:User)
49
- load 'user.rb'
50
- end
51
4
  end
52
5
  end