sorcery 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +29 -104
  3. data/CHANGELOG.md +13 -1
  4. data/Gemfile +2 -16
  5. data/README.md +124 -272
  6. data/Rakefile +2 -2
  7. data/gemfiles/{mongoid-rails40.gemfile → active_record-rails42.gemfile} +1 -3
  8. data/lib/generators/sorcery/helpers.rb +4 -4
  9. data/lib/generators/sorcery/install_generator.rb +25 -19
  10. data/lib/generators/sorcery/templates/initializer.rb +27 -52
  11. data/lib/generators/sorcery/templates/migration/activity_logging.rb +2 -2
  12. data/lib/generators/sorcery/templates/migration/brute_force_protection.rb +1 -1
  13. data/lib/generators/sorcery/templates/migration/core.rb +3 -3
  14. data/lib/generators/sorcery/templates/migration/external.rb +2 -2
  15. data/lib/generators/sorcery/templates/migration/remember_me.rb +2 -2
  16. data/lib/generators/sorcery/templates/migration/reset_password.rb +2 -2
  17. data/lib/generators/sorcery/templates/migration/user_activation.rb +2 -2
  18. data/lib/sorcery.rb +0 -28
  19. data/lib/sorcery/adapters/active_record_adapter.rb +7 -18
  20. data/lib/sorcery/controller.rb +19 -21
  21. data/lib/sorcery/controller/config.rb +20 -18
  22. data/lib/sorcery/controller/submodules/activity_logging.rb +7 -15
  23. data/lib/sorcery/controller/submodules/brute_force_protection.rb +1 -2
  24. data/lib/sorcery/controller/submodules/external.rb +22 -14
  25. data/lib/sorcery/controller/submodules/http_basic_auth.rb +16 -19
  26. data/lib/sorcery/controller/submodules/remember_me.rb +15 -10
  27. data/lib/sorcery/controller/submodules/session_timeout.rb +7 -8
  28. data/lib/sorcery/crypto_providers/aes256.rb +15 -15
  29. data/lib/sorcery/crypto_providers/bcrypt.rb +19 -21
  30. data/lib/sorcery/crypto_providers/common.rb +1 -1
  31. data/lib/sorcery/crypto_providers/md5.rb +5 -5
  32. data/lib/sorcery/crypto_providers/sha1.rb +5 -5
  33. data/lib/sorcery/crypto_providers/sha256.rb +2 -2
  34. data/lib/sorcery/crypto_providers/sha512.rb +3 -3
  35. data/lib/sorcery/engine.rb +3 -8
  36. data/lib/sorcery/model.rb +24 -32
  37. data/lib/sorcery/model/config.rb +64 -49
  38. data/lib/sorcery/model/submodules/activity_logging.rb +31 -12
  39. data/lib/sorcery/model/submodules/brute_force_protection.rb +23 -23
  40. data/lib/sorcery/model/submodules/external.rb +3 -7
  41. data/lib/sorcery/model/submodules/remember_me.rb +19 -7
  42. data/lib/sorcery/model/submodules/reset_password.rb +32 -36
  43. data/lib/sorcery/model/submodules/user_activation.rb +38 -50
  44. data/lib/sorcery/model/temporary_token.rb +2 -2
  45. data/lib/sorcery/protocols/oauth.rb +3 -9
  46. data/lib/sorcery/protocols/oauth2.rb +0 -2
  47. data/lib/sorcery/providers/base.rb +4 -4
  48. data/lib/sorcery/providers/facebook.rb +5 -8
  49. data/lib/sorcery/providers/github.rb +5 -7
  50. data/lib/sorcery/providers/google.rb +3 -5
  51. data/lib/sorcery/providers/heroku.rb +6 -8
  52. data/lib/sorcery/providers/jira.rb +12 -17
  53. data/lib/sorcery/providers/linkedin.rb +6 -8
  54. data/lib/sorcery/providers/liveid.rb +4 -7
  55. data/lib/sorcery/providers/paypal.rb +60 -0
  56. data/lib/sorcery/providers/salesforce.rb +3 -5
  57. data/lib/sorcery/providers/slack.rb +45 -0
  58. data/lib/sorcery/providers/twitter.rb +4 -6
  59. data/lib/sorcery/providers/vk.rb +3 -5
  60. data/lib/sorcery/providers/wechat.rb +79 -0
  61. data/lib/sorcery/providers/xing.rb +7 -10
  62. data/lib/sorcery/test_helpers/internal.rb +10 -10
  63. data/lib/sorcery/test_helpers/internal/rails.rb +16 -8
  64. data/lib/sorcery/test_helpers/rails/controller.rb +1 -1
  65. data/lib/sorcery/test_helpers/rails/integration.rb +5 -6
  66. data/lib/sorcery/version.rb +1 -1
  67. data/sorcery.gemspec +25 -27
  68. data/spec/active_record/user_activation_spec.rb +2 -3
  69. data/spec/active_record/user_activity_logging_spec.rb +2 -4
  70. data/spec/active_record/user_brute_force_protection_spec.rb +3 -4
  71. data/spec/active_record/user_oauth_spec.rb +3 -4
  72. data/spec/active_record/user_remember_me_spec.rb +3 -4
  73. data/spec/active_record/user_reset_password_spec.rb +2 -3
  74. data/spec/active_record/user_spec.rb +7 -7
  75. data/spec/controllers/controller_activity_logging_spec.rb +13 -24
  76. data/spec/controllers/controller_brute_force_protection_spec.rb +6 -8
  77. data/spec/controllers/controller_http_basic_auth_spec.rb +19 -20
  78. data/spec/controllers/controller_oauth2_spec.rb +125 -100
  79. data/spec/controllers/controller_oauth_spec.rb +86 -66
  80. data/spec/controllers/controller_remember_me_spec.rb +35 -30
  81. data/spec/controllers/controller_session_timeout_spec.rb +14 -15
  82. data/spec/controllers/controller_spec.rb +77 -111
  83. data/spec/orm/active_record.rb +1 -1
  84. data/spec/rails_app/app/active_record/authentication.rb +1 -1
  85. data/spec/rails_app/app/active_record/user.rb +2 -2
  86. data/spec/rails_app/app/controllers/sorcery_controller.rb +89 -24
  87. data/spec/rails_app/app/mailers/sorcery_mailer.rb +16 -17
  88. data/spec/rails_app/config.ru +1 -1
  89. data/spec/rails_app/config/application.rb +7 -7
  90. data/spec/rails_app/config/boot.rb +1 -1
  91. data/spec/rails_app/config/environments/test.rb +1 -1
  92. data/spec/rails_app/config/initializers/compatible_legacy_migration.rb +11 -0
  93. data/spec/rails_app/config/initializers/session_store.rb +3 -3
  94. data/spec/rails_app/config/routes.rb +11 -1
  95. data/spec/rails_app/db/migrate/activation/20101224223622_add_activation_to_users.rb +4 -4
  96. data/spec/rails_app/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +8 -8
  97. data/spec/rails_app/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +5 -5
  98. data/spec/rails_app/db/migrate/core/20101224223620_create_users.rb +5 -5
  99. data/spec/rails_app/db/migrate/external/20101224223628_create_authentications_and_user_providers.rb +3 -3
  100. data/spec/rails_app/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +6 -6
  101. data/spec/rails_app/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +5 -5
  102. data/spec/shared_examples/user_activation_shared_examples.rb +99 -58
  103. data/spec/shared_examples/user_activity_logging_shared_examples.rb +47 -41
  104. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +19 -24
  105. data/spec/shared_examples/user_oauth_shared_examples.rb +7 -10
  106. data/spec/shared_examples/user_remember_me_shared_examples.rb +90 -21
  107. data/spec/shared_examples/user_reset_password_shared_examples.rb +52 -54
  108. data/spec/shared_examples/user_shared_examples.rb +215 -118
  109. data/spec/sorcery_crypto_providers_spec.rb +63 -76
  110. data/spec/spec_helper.rb +17 -13
  111. metadata +28 -83
  112. data/gemfiles/mongo_mapper-rails40.gemfile +0 -9
  113. data/gemfiles/mongo_mapper-rails41.gemfile +0 -9
  114. data/gemfiles/mongoid-rails41.gemfile +0 -9
  115. data/gemfiles/mongoid3-rails32.gemfile +0 -9
  116. data/lib/sorcery/adapters/data_mapper_adapter.rb +0 -176
  117. data/lib/sorcery/adapters/mongo_mapper_adapter.rb +0 -110
  118. data/lib/sorcery/adapters/mongoid_adapter.rb +0 -97
  119. data/lib/sorcery/railties/tasks.rake +0 -6
  120. data/spec/data_mapper/user_activation_spec.rb +0 -10
  121. data/spec/data_mapper/user_activity_logging_spec.rb +0 -14
  122. data/spec/data_mapper/user_brute_force_protection_spec.rb +0 -9
  123. data/spec/data_mapper/user_oauth_spec.rb +0 -9
  124. data/spec/data_mapper/user_remember_me_spec.rb +0 -8
  125. data/spec/data_mapper/user_reset_password_spec.rb +0 -8
  126. data/spec/data_mapper/user_spec.rb +0 -27
  127. data/spec/mongo_mapper/user_activation_spec.rb +0 -9
  128. data/spec/mongo_mapper/user_activity_logging_spec.rb +0 -8
  129. data/spec/mongo_mapper/user_brute_force_protection_spec.rb +0 -8
  130. data/spec/mongo_mapper/user_oauth_spec.rb +0 -8
  131. data/spec/mongo_mapper/user_remember_me_spec.rb +0 -8
  132. data/spec/mongo_mapper/user_reset_password_spec.rb +0 -8
  133. data/spec/mongo_mapper/user_spec.rb +0 -37
  134. data/spec/mongoid/user_activation_spec.rb +0 -9
  135. data/spec/mongoid/user_activity_logging_spec.rb +0 -8
  136. data/spec/mongoid/user_brute_force_protection_spec.rb +0 -8
  137. data/spec/mongoid/user_oauth_spec.rb +0 -8
  138. data/spec/mongoid/user_remember_me_spec.rb +0 -8
  139. data/spec/mongoid/user_reset_password_spec.rb +0 -8
  140. data/spec/mongoid/user_spec.rb +0 -51
  141. data/spec/orm/data_mapper.rb +0 -48
  142. data/spec/orm/mongo_mapper.rb +0 -10
  143. data/spec/orm/mongoid.rb +0 -22
  144. data/spec/rails_app/app/data_mapper/authentication.rb +0 -8
  145. data/spec/rails_app/app/data_mapper/user.rb +0 -7
  146. data/spec/rails_app/app/mongo_mapper/authentication.rb +0 -6
  147. data/spec/rails_app/app/mongo_mapper/user.rb +0 -7
  148. data/spec/rails_app/app/mongoid/authentication.rb +0 -7
  149. data/spec/rails_app/app/mongoid/user.rb +0 -7
@@ -1,10 +1,8 @@
1
- shared_examples_for "rails_3_core_model" do
2
-
1
+ shared_examples_for 'rails_3_core_model' do
3
2
  let(:user) { create_new_user }
4
3
  let(:crypted_password) { user.send User.sorcery_config.crypted_password_attribute_name }
5
4
 
6
- describe "loaded plugin configuration" do
7
-
5
+ describe 'loaded plugin configuration' do
8
6
  after(:each) { User.sorcery_config.reset! }
9
7
 
10
8
  it "enables configuration option 'username_attribute_names'" do
@@ -57,7 +55,7 @@ shared_examples_for "rails_3_core_model" do
57
55
  end
58
56
 
59
57
  it "enables configuration option 'salt_join_token'" do
60
- salt_join_token = "--%%*&-"
58
+ salt_join_token = '--%%*&-'
61
59
  sorcery_model_property_set(:salt_join_token, salt_join_token)
62
60
 
63
61
  expect(User.sorcery_config.salt_join_token).to eq salt_join_token
@@ -70,101 +68,126 @@ shared_examples_for "rails_3_core_model" do
70
68
  expect(User.sorcery_config.stretches).to eq stretches
71
69
  end
72
70
 
71
+ it "enables configuration option 'deliver_later_enabled" do
72
+ sorcery_model_property_set(:email_delivery_method, :deliver_later)
73
+ expect(User.sorcery_config.email_delivery_method).to eq :deliver_later
74
+ end
75
+
73
76
  it 'respond to username=' do
74
77
  expect(User.new).to respond_to(:username=)
75
78
  end
76
79
  end
77
80
 
78
- describe "when activated with sorcery" do
81
+ describe 'when activated with sorcery' do
79
82
  before(:all) { sorcery_reload! }
80
83
  before(:each) { User.sorcery_adapter.delete_all }
81
84
 
82
- it "does not add authenticate method to base class", active_record: true do
85
+ it 'does not add authenticate method to base class', active_record: true do
83
86
  expect(ActiveRecord::Base).not_to respond_to(:authenticate) if defined?(ActiveRecord)
84
87
  end
85
88
 
86
- it "responds to class method authenticate" do
89
+ it 'responds to class method authenticate' do
87
90
  expect(User).to respond_to :authenticate
88
91
  end
89
92
 
90
- describe "#authenticate" do
91
- it "returns user if credentials are good" do
92
- expect(User.authenticate user.email, 'secret').to eq user
93
+ describe '#authenticate' do
94
+ it 'returns user if credentials are good' do
95
+ expect(User.authenticate(user.email, 'secret')).to eq user
93
96
  end
94
97
 
95
- it "returns nil if credentials are bad" do
96
- expect(User.authenticate user.email, 'wrong!').to be nil
98
+ it 'returns nil if credentials are bad' do
99
+ expect(User.authenticate(user.email, 'wrong!')).to be nil
97
100
  end
98
101
 
99
- context "with empty credentials" do
100
- before do
101
- sorcery_model_property_set(:downcase_username_before_authenticating, true)
102
- end
103
-
102
+ context 'downcasing username' do
104
103
  after do
105
104
  sorcery_reload!
106
105
  end
107
106
 
108
- it "don't downcase empty credentials" do
109
- expect(User.authenticate(nil, 'wrong!')).to be_falsy
107
+ context 'when downcasing set to false' do
108
+ before do
109
+ sorcery_model_property_set(:downcase_username_before_authenticating, false)
110
+ end
111
+
112
+ it 'does not find user with wrongly capitalized username' do
113
+ expect(User.authenticate(user.email.capitalize, 'secret')).to be_nil
114
+ end
115
+
116
+ it 'finds user with correctly capitalized username' do
117
+ expect(User.authenticate(user.email, 'secret')).to eq user
118
+ end
110
119
  end
111
- end
112
120
 
113
- context "and model implements active_for_authentication?" do
121
+ context 'when downcasing set to true' do
122
+ before do
123
+ sorcery_model_property_set(:downcase_username_before_authenticating, true)
124
+ end
125
+
126
+ it 'does not find user with wrongly capitalized username' do
127
+ expect(User.authenticate(user.email.capitalize, 'secret')).to eq user
128
+ end
114
129
 
115
- it "authenticates returns user if active_for_authentication? returns true" do
130
+ it 'finds user with correctly capitalized username' do
131
+ expect(User.authenticate(user.email, 'secret')).to eq user
132
+ end
133
+ end
134
+ end
135
+
136
+ context 'and model implements active_for_authentication?' do
137
+ it 'authenticates returns user if active_for_authentication? returns true' do
116
138
  allow_any_instance_of(User).to receive(:active_for_authentication?) { true }
117
139
 
118
- expect(User.authenticate user.email, 'secret').to eq user
140
+ expect(User.authenticate(user.email, 'secret')).to eq user
119
141
  end
120
142
 
121
- it "authenticate returns nil if active_for_authentication? returns false" do
143
+ it 'authenticate returns nil if active_for_authentication? returns false' do
122
144
  allow_any_instance_of(User).to receive(:active_for_authentication?) { false }
123
145
 
124
- expect(User.authenticate user.email, 'secret').to be_nil
146
+ expect(User.authenticate(user.email, 'secret')).to be_nil
125
147
  end
126
148
  end
127
149
  end
128
150
 
129
151
  specify { expect(User).to respond_to(:encrypt) }
130
152
 
131
- it "subclass inherits config if defined so" do
132
- sorcery_reload!([],{:subclasses_inherit_config => true})
153
+ it 'subclass inherits config if defined so' do
154
+ sorcery_reload!([], subclasses_inherit_config: true)
133
155
  class Admin < User; end
134
156
 
135
157
  expect(Admin.sorcery_config).not_to be_nil
136
158
  expect(Admin.sorcery_config).to eq User.sorcery_config
137
159
  end
138
160
 
139
- it "subclass does not inherit config if not defined so" do
140
- sorcery_reload!([],{:subclasses_inherit_config => false})
161
+ it 'subclass does not inherit config if not defined so' do
162
+ sorcery_reload!([], subclasses_inherit_config: false)
141
163
  class Admin2 < User; end
142
164
 
143
165
  expect(Admin2.sorcery_config).to be_nil
144
166
  end
145
167
  end
146
168
 
147
-
148
- describe "registration" do
149
-
169
+ describe 'registration' do
150
170
  before(:all) { sorcery_reload! }
151
171
  before(:each) { User.sorcery_adapter.delete_all }
152
172
 
153
- it "by default, encryption_provider is not nil" do
173
+ it 'by default, encryption_provider is not nil' do
154
174
  expect(User.sorcery_config.encryption_provider).not_to be_nil
155
175
  end
156
176
 
157
- it "encrypts password when a new user is saved" do
158
- expect(User.sorcery_config.encryption_provider.matches? crypted_password, 'secret', user.salt).to be true
177
+ it 'encrypts password when a new user is saved' do
178
+ expect(
179
+ User.sorcery_config.encryption_provider.matches?(crypted_password, 'secret', user.salt)
180
+ ).to be true
159
181
  end
160
182
 
161
- it "clears the virtual password field if the encryption process worked" do
183
+ it 'clears the virtual password field if the encryption process worked' do
162
184
  expect(user.password).to be_nil
163
185
  end
164
186
 
165
- it "does not clear the virtual password field if save failed due to validity" do
187
+ it 'does not clear the virtual password field if save failed due to validity' do
166
188
  User.class_eval do
167
- validates_format_of :email, :with => /\A(.)+@(.)+\Z/, :if => Proc.new {|r| r.email}, :message => "is invalid"
189
+ validates_format_of :email, with: /\A(.)+@(.)+\Z/,
190
+ if: proc { |r| r.email }, message: 'is invalid'
168
191
  end
169
192
 
170
193
  user.password = 'blupush'
@@ -174,7 +197,7 @@ shared_examples_for "rails_3_core_model" do
174
197
  expect(user.password).not_to be_nil
175
198
  end
176
199
 
177
- it "does not clear the virtual password field if save failed due to exception" do
200
+ it 'does not clear the virtual password field if save failed due to exception' do
178
201
  user.password = '4blupush'
179
202
  user.username = nil
180
203
 
@@ -188,21 +211,25 @@ shared_examples_for "rails_3_core_model" do
188
211
  expect(user.password).not_to be_nil
189
212
  end
190
213
 
191
- it "does not encrypt the password twice when a user is updated" do
192
- user.email = "blup@bla.com"
214
+ it 'does not encrypt the password twice when a user is updated' do
215
+ user.email = 'blup@bla.com'
193
216
  user.save
194
217
 
195
- expect(User.sorcery_config.encryption_provider.matches? crypted_password, 'secret', user.salt).to be true
218
+ expect(
219
+ User.sorcery_config.encryption_provider.matches?(crypted_password, 'secret', user.salt)
220
+ ).to be true
196
221
  end
197
222
 
198
- it "replaces the crypted_password in case a new password is set" do
223
+ it 'replaces the crypted_password in case a new password is set' do
199
224
  user.password = 'new_secret'
200
225
  user.save
201
226
 
202
- expect(User.sorcery_config.encryption_provider.matches? crypted_password, 'secret', user.salt).to be false
227
+ expect(
228
+ User.sorcery_config.encryption_provider.matches?(crypted_password, 'secret', user.salt)
229
+ ).to be false
203
230
  end
204
231
 
205
- describe "when user has password_confirmation_defined" do
232
+ describe 'when user has password_confirmation_defined' do
206
233
  before(:all) do
207
234
  update_model { attr_accessor :password_confirmation }
208
235
  end
@@ -212,17 +239,25 @@ shared_examples_for "rails_3_core_model" do
212
239
  User.send(:remove_method, :password_confirmation=)
213
240
  end
214
241
 
215
- it "clears the virtual password field if the encryption process worked" do
216
- user = create_new_user(username: "u", password: "secret", password_confirmation: "secret", email: "email@example.com")
242
+ it 'clears the virtual password field if the encryption process worked' do
243
+ user = create_new_user(
244
+ username: 'u',
245
+ password: 'secret', password_confirmation: 'secret',
246
+ email: 'email@example.com'
247
+ )
217
248
 
218
249
  expect(user.password_confirmation).to be_nil
219
250
  end
220
251
 
221
- it "does not clear the virtual password field if save failed due to validity" do
252
+ it 'does not clear the virtual password field if save failed due to validity' do
222
253
  User.class_eval do
223
- validates_format_of :email, :with => /\A(.)+@(.)+\Z/
254
+ validates_format_of :email, with: /\A(.)+@(.)+\Z/
224
255
  end
225
- user = build_new_user(username: "u", password: "secret", password_confirmation: "secret", email: "asd")
256
+ user = build_new_user(
257
+ username: 'u',
258
+ password: 'secret', password_confirmation: 'secret',
259
+ email: 'asd'
260
+ )
226
261
  user.save
227
262
 
228
263
  expect(user.password_confirmation).not_to be_nil
@@ -230,25 +265,89 @@ shared_examples_for "rails_3_core_model" do
230
265
  end
231
266
  end
232
267
 
233
- describe "password validation" do
234
-
235
- let(:user_with_pass) { create_new_user({:username => 'foo_bar', :email => "foo@bar.com", :password => 'foobar'})}
268
+ describe 'password validation' do
269
+ let(:user_with_pass) do
270
+ create_new_user(username: 'foo_bar', email: 'foo@bar.com', password: 'foobar')
271
+ end
236
272
 
237
273
  specify { expect(user_with_pass).to respond_to :valid_password? }
238
-
239
- it "returns true if password is correct" do
240
- expect(user_with_pass.valid_password?("foobar")).to be true
274
+
275
+ it 'returns true if password is correct' do
276
+ expect(user_with_pass.valid_password?('foobar')).to be true
241
277
  end
242
-
243
- it "returns false if password is incorrect" do
244
- expect(user_with_pass.valid_password?("foobug")).to be false
278
+
279
+ it 'returns false if password is incorrect' do
280
+ expect(user_with_pass.valid_password?('foobug')).to be false
245
281
  end
246
282
  end
247
283
 
248
- describe "special encryption cases" do
284
+ describe 'generic send email' do
249
285
  before(:all) do
250
- sorcery_reload!()
251
- @text = "Some Text!"
286
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
287
+ User.reset_column_information
288
+ end
289
+
290
+ after(:all) do
291
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
292
+ end
293
+
294
+ before do
295
+ @mail = double('mail')
296
+ allow(::SorceryMailer).to receive(:activation_success_email).and_return(@mail)
297
+ end
298
+
299
+ it 'use deliver_later' do
300
+ sorcery_reload!(
301
+ [
302
+ :user_activation, :user_activation_mailer,
303
+ :activation_needed_email_method_name, :email_delivery_method
304
+ ],
305
+ user_activation_mailer: SorceryMailer,
306
+ activation_needed_email_method_name: nil,
307
+ email_delivery_method: :deliver_later
308
+ )
309
+
310
+ expect(@mail).to receive(:deliver_later).once
311
+ user.activate!
312
+ end
313
+
314
+ describe 'email_delivery_method is default' do
315
+ it 'use deliver_now if rails version 4.2+' do
316
+ allow(Rails).to receive(:version).and_return('4.2.0')
317
+ sorcery_reload!(
318
+ [
319
+ :user_activation, :user_activation_mailer,
320
+ :activation_needed_email_method_name
321
+ ],
322
+ user_activation_mailer: SorceryMailer,
323
+ activation_needed_email_method_name: nil
324
+ )
325
+
326
+ expect(@mail).to receive(:deliver_now).once
327
+ user.activate!
328
+ end
329
+
330
+ it 'use deliver if rails version < 4.2' do
331
+ allow(Rails).to receive(:version).and_return('4.1.0')
332
+ sorcery_reload!(
333
+ [
334
+ :user_activation, :user_activation_mailer,
335
+ :activation_needed_email_method_name
336
+ ],
337
+ user_activation_mailer: SorceryMailer,
338
+ activation_needed_email_method_name: nil
339
+ )
340
+
341
+ expect(@mail).to receive(:deliver).once
342
+ user.activate!
343
+ end
344
+ end
345
+ end
346
+
347
+ describe 'special encryption cases' do
348
+ before(:all) do
349
+ sorcery_reload!
350
+ @text = 'Some Text!'
252
351
  end
253
352
 
254
353
  before(:each) do
@@ -259,20 +358,20 @@ shared_examples_for "rails_3_core_model" do
259
358
  User.sorcery_config.reset!
260
359
  end
261
360
 
262
- it "works with no password encryption" do
361
+ it 'works with no password encryption' do
263
362
  sorcery_model_property_set(:encryption_algorithm, :none)
264
363
  username = user.send(User.sorcery_config.username_attribute_names.first)
265
364
 
266
- expect(User.authenticate username, 'secret').to be_truthy
365
+ expect(User.authenticate(username, 'secret')).to be_truthy
267
366
  end
268
367
 
269
- it "works with custom password encryption" do
368
+ it 'works with custom password encryption' do
270
369
  class MyCrypto
271
370
  def self.encrypt(*tokens)
272
- tokens.flatten.join('').gsub(/e/,'A')
371
+ tokens.flatten.join('').tr('e', 'A')
273
372
  end
274
373
 
275
- def self.matches?(crypted,*tokens)
374
+ def self.matches?(crypted, *tokens)
276
375
  crypted == encrypt(*tokens)
277
376
  end
278
377
  end
@@ -281,87 +380,87 @@ shared_examples_for "rails_3_core_model" do
281
380
 
282
381
  username = user.send(User.sorcery_config.username_attribute_names.first)
283
382
 
284
- expect(User.authenticate username, 'secret').to be_truthy
383
+ expect(User.authenticate(username, 'secret')).to be_truthy
285
384
  end
286
385
 
287
- it "if encryption algo is aes256, it sets key to crypto provider" do
386
+ it 'if encryption algo is aes256, it sets key to crypto provider' do
288
387
  sorcery_model_property_set(:encryption_algorithm, :aes256)
289
388
  sorcery_model_property_set(:encryption_key, nil)
290
389
 
291
390
  expect { User.encrypt @text }.to raise_error(ArgumentError)
292
391
 
293
- sorcery_model_property_set(:encryption_key, "asd234dfs423fddsmndsflktsdf32343")
392
+ sorcery_model_property_set(:encryption_key, 'asd234dfs423fddsmndsflktsdf32343')
294
393
 
295
394
  expect { User.encrypt @text }.not_to raise_error
296
395
  end
297
396
 
298
- it "if encryption algo is aes256, it sets key to crypto provider, even if attributes are set in reverse" do
397
+ it 'if encryption algo is aes256, it sets key to crypto provider, even if attributes are set in reverse' do
299
398
  sorcery_model_property_set(:encryption_key, nil)
300
399
  sorcery_model_property_set(:encryption_algorithm, :none)
301
- sorcery_model_property_set(:encryption_key, "asd234dfs423fddsmndsflktsdf32343")
400
+ sorcery_model_property_set(:encryption_key, 'asd234dfs423fddsmndsflktsdf32343')
302
401
  sorcery_model_property_set(:encryption_algorithm, :aes256)
303
402
 
304
403
  expect { User.encrypt @text }.not_to raise_error
305
404
  end
306
405
 
307
- it "if encryption algo is md5 it works" do
406
+ it 'if encryption algo is md5 it works' do
308
407
  sorcery_model_property_set(:encryption_algorithm, :md5)
309
408
 
310
- expect(User.encrypt @text).to eq Sorcery::CryptoProviders::MD5.encrypt(@text)
409
+ expect(User.encrypt(@text)).to eq Sorcery::CryptoProviders::MD5.encrypt(@text)
311
410
  end
312
411
 
313
- it "if encryption algo is sha1 it works" do
412
+ it 'if encryption algo is sha1 it works' do
314
413
  sorcery_model_property_set(:encryption_algorithm, :sha1)
315
414
 
316
- expect(User.encrypt @text).to eq Sorcery::CryptoProviders::SHA1.encrypt(@text)
415
+ expect(User.encrypt(@text)).to eq Sorcery::CryptoProviders::SHA1.encrypt(@text)
317
416
  end
318
417
 
319
- it "if encryption algo is sha256 it works" do
418
+ it 'if encryption algo is sha256 it works' do
320
419
  sorcery_model_property_set(:encryption_algorithm, :sha256)
321
420
 
322
- expect(User.encrypt @text).to eq Sorcery::CryptoProviders::SHA256.encrypt(@text)
421
+ expect(User.encrypt(@text)).to eq Sorcery::CryptoProviders::SHA256.encrypt(@text)
323
422
  end
324
423
 
325
- it "if encryption algo is sha512 it works" do
424
+ it 'if encryption algo is sha512 it works' do
326
425
  sorcery_model_property_set(:encryption_algorithm, :sha512)
327
426
 
328
- expect(User.encrypt @text).to eq Sorcery::CryptoProviders::SHA512.encrypt(@text)
427
+ expect(User.encrypt(@text)).to eq Sorcery::CryptoProviders::SHA512.encrypt(@text)
329
428
  end
330
429
 
331
- it "salt is random for each user and saved in db" do
430
+ it 'salt is random for each user and saved in db' do
332
431
  sorcery_model_property_set(:salt_attribute_name, :salt)
333
432
 
334
433
  expect(user.salt).not_to be_nil
335
434
  end
336
435
 
337
- it "if salt is set uses it to encrypt" do
436
+ it 'if salt is set uses it to encrypt' do
338
437
  sorcery_model_property_set(:salt_attribute_name, :salt)
339
438
  sorcery_model_property_set(:encryption_algorithm, :sha512)
340
439
 
341
440
  expect(user.crypted_password).not_to eq Sorcery::CryptoProviders::SHA512.encrypt('secret')
342
- expect(user.crypted_password).to eq Sorcery::CryptoProviders::SHA512.encrypt('secret',user.salt)
441
+ expect(user.crypted_password).to eq Sorcery::CryptoProviders::SHA512.encrypt('secret', user.salt)
343
442
  end
344
443
 
345
- it "if salt_join_token is set uses it to encrypt" do
444
+ it 'if salt_join_token is set uses it to encrypt' do
346
445
  sorcery_model_property_set(:salt_attribute_name, :salt)
347
- sorcery_model_property_set(:salt_join_token, "-@=>")
446
+ sorcery_model_property_set(:salt_join_token, '-@=>')
348
447
  sorcery_model_property_set(:encryption_algorithm, :sha512)
349
448
 
350
449
  expect(user.crypted_password).not_to eq Sorcery::CryptoProviders::SHA512.encrypt('secret')
351
450
 
352
- Sorcery::CryptoProviders::SHA512.join_token = ""
451
+ Sorcery::CryptoProviders::SHA512.join_token = ''
353
452
 
354
- expect(user.crypted_password).not_to eq Sorcery::CryptoProviders::SHA512.encrypt('secret',user.salt)
453
+ expect(user.crypted_password).not_to eq Sorcery::CryptoProviders::SHA512.encrypt('secret', user.salt)
355
454
 
356
455
  Sorcery::CryptoProviders::SHA512.join_token = User.sorcery_config.salt_join_token
357
456
 
358
- expect(user.crypted_password).to eq Sorcery::CryptoProviders::SHA512.encrypt('secret',user.salt)
457
+ expect(user.crypted_password).to eq Sorcery::CryptoProviders::SHA512.encrypt('secret', user.salt)
359
458
  end
360
459
  end
361
460
 
362
- describe "ORM adapter" do
461
+ describe 'ORM adapter' do
363
462
  before(:all) do
364
- sorcery_reload!()
463
+ sorcery_reload!
365
464
  User.sorcery_adapter.delete_all
366
465
  end
367
466
 
@@ -372,26 +471,25 @@ shared_examples_for "rails_3_core_model" do
372
471
  User.sorcery_config.reset!
373
472
  end
374
473
 
375
-
376
- it "find_by_username works as expected" do
474
+ it 'find_by_username works as expected' do
377
475
  sorcery_model_property_set(:username_attribute_names, [:username])
378
476
 
379
- expect(User.sorcery_adapter.find_by_username "gizmo").to eq user
477
+ expect(User.sorcery_adapter.find_by_username('gizmo')).to eq user
380
478
  end
381
479
 
382
- it "find_by_username works as expected with multiple username attributes" do
480
+ it 'find_by_username works as expected with multiple username attributes' do
383
481
  sorcery_model_property_set(:username_attribute_names, [:username, :email])
384
482
 
385
- expect(User.sorcery_adapter.find_by_username "gizmo").to eq user
483
+ expect(User.sorcery_adapter.find_by_username('gizmo')).to eq user
386
484
  end
387
485
 
388
- it "find_by_email works as expected" do
389
- expect(User.sorcery_adapter.find_by_email "bla@bla.com").to eq user
486
+ it 'find_by_email works as expected' do
487
+ expect(User.sorcery_adapter.find_by_email('bla@bla.com')).to eq user
390
488
  end
391
489
  end
392
490
  end
393
491
 
394
- shared_examples_for "external_user" do
492
+ shared_examples_for 'external_user' do
395
493
  let(:user) { create_new_user }
396
494
  let(:external_user) { create_new_external_user :twitter }
397
495
 
@@ -403,16 +501,15 @@ shared_examples_for "external_user" do
403
501
  expect(user).to respond_to(:external?)
404
502
  end
405
503
 
406
- it "external? is false for regular users" do
504
+ it 'external? is false for regular users' do
407
505
  expect(user.external?).to be false
408
506
  end
409
507
 
410
- it "external? is true for external users" do
508
+ it 'external? is true for external users' do
411
509
  expect(external_user.external?).to be true
412
510
  end
413
511
 
414
- describe ".create_from_provider" do
415
-
512
+ describe '.create_from_provider' do
416
513
  before(:all) do
417
514
  if SORCERY_ORM == :active_record
418
515
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
@@ -431,24 +528,26 @@ shared_examples_for "external_user" do
431
528
  it 'supports nested attributes' do
432
529
  sorcery_model_property_set(:authentications_class, Authentication)
433
530
 
434
- expect { User.create_from_provider('facebook', '123', {username: 'Noam Ben Ari'}) }.to change { User.count }.by(1)
531
+ expect do
532
+ User.create_from_provider('facebook', '123', username: 'Noam Ben Ari')
533
+ end.to change { User.count }.by(1)
534
+
435
535
  expect(User.first.username).to eq 'Noam Ben Ari'
436
536
  end
437
537
 
438
538
  context 'with block' do
439
539
  it 'create user when block return true' do
440
- expect {
441
- User.create_from_provider('facebook', '123', {username: 'Noam Ben Ari'}) { true }
442
- }.to change { User.count }.by(1)
540
+ expect do
541
+ User.create_from_provider('facebook', '123', username: 'Noam Ben Ari') { true }
542
+ end.to change { User.count }.by(1)
443
543
  end
444
544
 
445
545
  it 'does not create user when block return false' do
446
- expect {
447
- User.create_from_provider('facebook', '123', {username: 'Noam Ben Ari'}) { false }
448
- }.not_to change { User.count }
546
+ expect do
547
+ User.create_from_provider('facebook', '123', username: 'Noam Ben Ari') { false }
548
+ end.not_to change { User.count }
449
549
  end
450
550
  end
451
-
452
551
  end
453
552
 
454
553
  describe 'activation' do
@@ -458,13 +557,13 @@ shared_examples_for "external_user" do
458
557
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
459
558
  end
460
559
 
461
- sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
560
+ sorcery_reload!([:user_activation, :external], user_activation_mailer: ::SorceryMailer)
462
561
  end
463
562
 
464
563
  after(:all) do
465
564
  if SORCERY_ORM == :active_record
466
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
467
565
  ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
566
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
468
567
  end
469
568
  end
470
569
 
@@ -472,16 +571,15 @@ shared_examples_for "external_user" do
472
571
  User.sorcery_adapter.delete_all
473
572
  end
474
573
 
475
- [:facebook, :github, :google, :liveid].each do |provider|
476
-
477
- it "does not send activation email to external users" do
574
+ [:facebook, :github, :google, :liveid, :slack].each do |provider|
575
+ it 'does not send activation email to external users' do
478
576
  old_size = ActionMailer::Base.deliveries.size
479
577
  create_new_external_user(provider)
480
578
 
481
579
  expect(ActionMailer::Base.deliveries.size).to eq old_size
482
580
  end
483
581
 
484
- it "does not send external users an activation success email" do
582
+ it 'does not send external users an activation success email' do
485
583
  sorcery_model_property_set(:activation_success_email_method_name, nil)
486
584
  create_new_external_user(provider)
487
585
  old_size = ActionMailer::Base.deliveries.size
@@ -490,6 +588,5 @@ shared_examples_for "external_user" do
490
588
  expect(ActionMailer::Base.deliveries.size).to eq old_size
491
589
  end
492
590
  end
493
-
494
591
  end
495
592
  end