goma 0.0.1.rc1 → 0.0.1.rc2

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.
@@ -1,227 +1,275 @@
1
1
  require "test_helper"
2
2
 
3
3
  class ConfirmableTest < ActiveSupport::TestCase
4
- context "A newly created user" do
5
- setup do
6
- @user = User.new(username: 'foo', email: 'foo@example.com', password: 'password')
7
- end
4
+ test 'should not be activated when user is created' do
5
+ user = User.create(username: 'foo', email: 'foo@example.com', password: 'password')
6
+ user.reload
7
+ refute user.activated?
8
+ end
9
+
10
+ test 'should send activation needed email when user is created' do
11
+ Goma.token_generator.expects(:friendly_token).returns('sesame')
8
12
 
9
- should "not be activated" do
10
- @user.save!
11
- @user.reload
12
- refute @user.activated?
13
+ user = User.new(username: 'foo', email: 'foo@example.com', password: 'password')
14
+ assert_emails 1 do
15
+ user.save!
13
16
  end
17
+ email = ActionMailer::Base.deliveries.last
18
+ assert_match %r{/confirmations/sesame}, email.body.encoded
19
+ end
14
20
 
15
- should "be sent activation needed email when saving" do
16
- assert_difference('ActionMailer::Base.deliveries.size', 1) do
17
- @user.save!
21
+ test 'should not send activation needed email when user is created if activation_needed_email_method_name == nil' do
22
+ swap Goma.config(:user), activation_needed_email_method_name: nil do
23
+ user = User.new(username: 'foo', email: 'foo@example.com', password: 'password')
24
+ assert_no_emails do
25
+ user.save!
18
26
  end
19
27
  end
20
28
  end
21
29
 
22
- context "User.load_from_activation_token!" do
23
- setup do
24
- @user = Fabricate(:unactivated_user)
25
- end
30
+ test 'load_from_activation_token_with_error with correct token' do
31
+ user = Fabricate(:unactivated_user)
32
+ raw_token = user.raw_confirmation_token
33
+ loaded_user, error = User.load_from_activation_token_with_error(raw_token)
34
+ assert_equal user, loaded_user
35
+ assert_nil error
36
+ end
26
37
 
27
- should "load user record with correct activation token" do
28
- raw_token = @user.raw_confirmation_token
29
- loaded_user = User.load_from_activation_token!(raw_token)
30
- assert_equal @user, loaded_user
31
- end
38
+ test 'load_from_activation_token_with_error with wrong token' do
39
+ user = Fabricate(:unactivated_user)
40
+ loaded_user, error = User.load_from_activation_token_with_error('beans')
41
+ assert_nil loaded_user
42
+ assert_equal :not_found, error
43
+ end
32
44
 
33
- should "raise exception with incorrect activation token" do
34
- assert_raise Goma::NotFound do
35
- User.load_from_activation_token!('blahblahblah')
36
- end
45
+ test 'load_from_activation_token_with_error with expired token' do
46
+ user = Fabricate(:unactivated_user)
47
+ raw_token = user.raw_confirmation_token
48
+ Timecop.travel 4.days.from_now do
49
+ loaded_user, error = User.load_from_activation_token_with_error(raw_token)
50
+ assert_nil loaded_user
51
+ assert_equal :token_expired, error
37
52
  end
53
+ end
38
54
 
39
- should "raise exception with correct activation token which is expired" do
40
- raw_token = @user.raw_confirmation_token
41
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
55
+ test 'load_from_activation_token! with correct token' do
56
+ user = Fabricate(:unactivated_user)
57
+ raw_token = user.raw_confirmation_token
58
+ assert_equal user, User.load_from_activation_token!(raw_token)
59
+ end
42
60
 
61
+ test 'load_from_activation_token! with wrong token' do
62
+ user = Fabricate(:unactivated_user)
63
+ assert_raise Goma::NotFound do
64
+ User.load_from_activation_token!('beans')
65
+ end
66
+ end
67
+
68
+ test 'load_from_activation_token! with expired token' do
69
+ user = Fabricate(:unactivated_user)
70
+ raw_token = user.raw_confirmation_token
71
+ Timecop.travel 4.days.from_now do
43
72
  assert_raise Goma::TokenExpired do
44
73
  User.load_from_activation_token!(raw_token)
45
74
  end
46
75
  end
47
76
  end
48
77
 
49
- context "User.load_from_activation_token_with_error" do
50
- setup do
51
- @user = Fabricate(:unactivated_user)
52
- end
78
+ test 'load_from_activation_token with correct token' do
79
+ user = Fabricate(:unactivated_user)
80
+ raw_token = user.raw_confirmation_token
81
+ assert_equal user, User.load_from_activation_token(raw_token)
82
+ end
53
83
 
54
- should "return user record and nil with correct activation token" do
55
- raw_token = @user.raw_confirmation_token
56
- loaded_user, error = User.load_from_activation_token_with_error(raw_token)
57
- assert_equal @user, loaded_user
58
- assert_nil error
59
- end
84
+ test 'load_from_activation_token with wrong token' do
85
+ user = Fabricate(:unactivated_user)
86
+ assert_nil User.load_from_activation_token('beans')
87
+ end
60
88
 
61
- should "return nil and :not_found with incorrect activation token" do
62
- loaded_user, error = User.load_from_activation_token_with_error('blahblahblah')
63
- assert_nil loaded_user
64
- assert_equal :not_found, error
89
+ test 'load_from_activation_token with expired token' do
90
+ user = Fabricate(:unactivated_user)
91
+ raw_token = user.raw_confirmation_token
92
+ Timecop.travel 4.days.from_now do
93
+ assert_nil User.load_from_activation_token(raw_token)
65
94
  end
95
+ end
66
96
 
67
- should "return nil and :token_expired with correct activation token which is expired" do
68
- raw_token = @user.raw_confirmation_token
69
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
97
+ test 'activate!' do
98
+ user = Fabricate(:unactivated_user)
99
+ refute user.activated?
70
100
 
71
- loaded_user, error = User.load_from_activation_token_with_error(raw_token)
72
- assert_nil loaded_user
73
- assert_equal :token_expired, error
74
- end
101
+ user.activate!
102
+ user.reload
103
+ assert user.activated?
104
+ assert_nil user.confirmation_token
105
+ assert_nil user.confirmation_token_sent_at
75
106
  end
76
107
 
77
- context "User.load_from_activation_token" do
78
- setup do
79
- @user = Fabricate(:unactivated_user)
108
+ test 'should send activation success email when activating' do
109
+ user = Fabricate(:unactivated_user)
110
+ assert_emails 1 do
111
+ user.activate!
80
112
  end
113
+ email = ActionMailer::Base.deliveries.last
114
+ assert_match /You have successfully activated your account/, email.body.encoded
115
+ end
81
116
 
82
- should "return user record with correct activation token" do
83
- raw_token = @user.raw_confirmation_token
84
- loaded_user = User.load_from_activation_token(raw_token)
85
- assert_equal @user, loaded_user
117
+ test 'should not send activation success email when activating if activation_success_email_method_name == nil' do
118
+ swap Goma.config(:user), activation_success_email_method_name: nil do
119
+ user = Fabricate(:unactivated_user)
120
+ assert_no_emails do
121
+ user.activate!
122
+ end
86
123
  end
124
+ end
87
125
 
88
- should "return nil with incorrect activation token" do
89
- loaded_user = User.load_from_activation_token('blahblahblah')
90
- assert_nil loaded_user
126
+ test 'should change email immediately if email_confirmation_enabled == false' do
127
+ swap Goma.config(:user), email_confirmation_enabled: false do
128
+ user = Fabricate(:user)
129
+ user.update(email: 'new@example.com')
130
+ user.reload
131
+ assert_equal 'new@example.com', user.email
91
132
  end
133
+ end
92
134
 
93
- should "return nil with correct activation token which is expired" do
94
- raw_token = @user.raw_confirmation_token
95
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
96
-
97
- loaded_user = User.load_from_activation_token(raw_token)
98
- assert_nil loaded_user
99
- end
135
+ test 'should postpone email change' do
136
+ user = Fabricate(:user)
137
+ old_email = user.email
138
+ user.update(email: 'new@example.com')
139
+ user.reload
140
+ assert_equal old_email, user.email
141
+ assert_equal 'new@example.com', user.unconfirmed_email
142
+ assert user.confirmation_token
143
+ assert user.confirmation_token_sent_at
100
144
  end
101
145
 
102
- context "A user" do
103
- setup do
104
- @user = Fabricate(:unactivated_user)
105
- end
146
+ test 'should send email confirmation needed email when email is changed' do
147
+ Goma.token_generator.stubs(:friendly_token).returns('sesame')
106
148
 
107
- should "be filled activated_at field when activate!ing" do
108
- refute @user.activated_at
109
- @user.activate!
110
- @user.reload
111
- assert @user.activated_at
149
+ user = Fabricate(:user)
150
+ assert_emails 1 do
151
+ user.update(email: 'new@example.com')
112
152
  end
153
+ email = ActionMailer::Base.deliveries.last
154
+ assert_match %r{/confirmations/sesame/email}, email.body.encoded
155
+ end
113
156
 
114
- should "be sent activation success email when activate!ing" do
115
- assert_difference('ActionMailer::Base.deliveries.size', 1) do
116
- @user.activate!
157
+ test 'should not send email confirmation needed email when email is changed if email_confirmation_needed_email_method_name == nil' do
158
+ swap Goma.config(:user), email_confirmation_needed_email_method_name: nil do
159
+ user = Fabricate(:user)
160
+ assert_no_emails do
161
+ user.update(email: 'new@example.com')
117
162
  end
118
163
  end
119
164
  end
120
165
 
121
- context "A user who changed email" do
122
- setup do
123
- @user = Fabricate(:user)
124
- @old_email = @user.email
125
- @user.email = 'another@example.com'
126
- end
127
-
128
- should "be postponed email change" do
129
- @user.save!
130
- assert_equal @old_email, @user.email
131
- assert_equal 'another@example.com', @user.unconfirmed_email
132
- end
166
+ test 'load_from_email_confirmation_token_with_error with correct token' do
167
+ user = Fabricate(:user)
168
+ user.update(email: 'new@example.com')
169
+ raw_token = user.raw_confirmation_token
170
+ loaded_user, error = User.load_from_email_confirmation_token_with_error(raw_token)
171
+ assert_equal user, loaded_user
172
+ assert_nil error
173
+ end
133
174
 
134
- should "be sent email confirmation needed email when saving" do
135
- assert_difference('ActionMailer::Base.deliveries.size', 1) do
136
- @user.save!
137
- end
138
- end
175
+ test 'load_from_email_confirmation_token_with_error with wrong token' do
176
+ user = Fabricate(:user)
177
+ user.update(email: 'new@example.com')
178
+ loaded_user, error = User.load_from_email_confirmation_token_with_error('beans')
179
+ assert_nil loaded_user
180
+ assert_equal :not_found, error
139
181
  end
140
182
 
141
- context "User.load_from_email_confirmation_token!" do
142
- setup do
143
- @user = Fabricate(:user)
144
- @user.email = 'another@example.com'
145
- @user.save!
183
+ test 'load_from_email_confirmation_token_with_error with expired token' do
184
+ user = Fabricate(:user)
185
+ user.update(email: 'new@example.com')
186
+ raw_token = user.raw_confirmation_token
187
+ Timecop.travel 4.days.from_now do
188
+ loaded_user, error = User.load_from_email_confirmation_token_with_error(raw_token)
189
+ assert_nil loaded_user
190
+ assert_equal :token_expired, error
146
191
  end
192
+ end
147
193
 
148
- should "find user record with correct email confirmation token" do
149
- raw_token = @user.raw_confirmation_token
150
- loaded_user = User.load_from_email_confirmation_token!(raw_token)
151
- assert_equal @user, loaded_user
152
- end
194
+ test 'load_from_email_confirmation_token! with correct token' do
195
+ user = Fabricate(:user)
196
+ user.update(email: 'new@example.com')
197
+ raw_token = user.raw_confirmation_token
198
+ assert_equal user, User.load_from_email_confirmation_token!(raw_token)
199
+ end
153
200
 
154
- should "not find user record with incorrect confirmation token" do
155
- assert_raise Goma::NotFound do
156
- User.load_from_email_confirmation_token!('blahblahblah')
157
- end
201
+ test 'load_from_email_confirmation_token! with wrong token' do
202
+ user = Fabricate(:user)
203
+ user.update(email: 'new@example.com')
204
+ assert_raise Goma::NotFound do
205
+ User.load_from_email_confirmation_token!('beans')
158
206
  end
207
+ end
159
208
 
160
- should "not find user record with correct confirmation token which is expired" do
161
- raw_token = @user.raw_confirmation_token
162
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
163
-
209
+ test 'load_from_email_confirmation_token! with expired token' do
210
+ user = Fabricate(:user)
211
+ user.update(email: 'new@example.com')
212
+ raw_token = user.raw_confirmation_token
213
+ Timecop.travel 4.days.from_now do
164
214
  assert_raise Goma::TokenExpired do
165
215
  User.load_from_email_confirmation_token!(raw_token)
166
216
  end
167
217
  end
168
218
  end
169
219
 
170
- context "User.load_from_email_confirmation_token_with_error" do
171
- setup do
172
- @user = Fabricate(:user)
173
- @user.email = 'another@example.com'
174
- @user.save!
175
- end
176
-
177
- should "return user record and nil with correct email_confirmation token" do
178
- raw_token = @user.raw_confirmation_token
179
- loaded_user, error = User.load_from_email_confirmation_token_with_error(raw_token)
180
- assert_equal @user, loaded_user
181
- assert_nil error
182
- end
183
-
184
- should "return nil and :not_found with incorrect email_confirmation token" do
185
- loaded_user, error = User.load_from_email_confirmation_token_with_error('blahblahblah')
186
- assert_nil loaded_user
187
- assert_equal :not_found, error
188
- end
189
-
190
- should "return nil and :token_expired with correct email_confirmation token which is expired" do
191
- raw_token = @user.raw_confirmation_token
192
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
220
+ test 'load_from_email_confirmation_token with correct token' do
221
+ user = Fabricate(:user)
222
+ user.update(email: 'new@example.com')
223
+ raw_token = user.raw_confirmation_token
224
+ assert_equal user, User.load_from_email_confirmation_token(raw_token)
225
+ end
193
226
 
194
- loaded_user, error = User.load_from_email_confirmation_token_with_error(raw_token)
195
- assert_nil loaded_user
196
- assert_equal :token_expired, error
197
- end
227
+ test 'load_from_email_confirmation_token with wrong token' do
228
+ user = Fabricate(:user)
229
+ user.update(email: 'new@example.com')
230
+ assert_nil User.load_from_email_confirmation_token('beans')
198
231
  end
199
232
 
200
- context "User.load_from_email_confirmation_token" do
201
- setup do
202
- @user = Fabricate(:user)
203
- @user.email = 'another@example.com'
204
- @user.save!
233
+ test 'load_from_email_confirmation_token with expired token' do
234
+ user = Fabricate(:user)
235
+ user.update(email: 'new@example.com')
236
+ raw_token = user.raw_confirmation_token
237
+ Timecop.travel 4.days.from_now do
238
+ assert_nil User.load_from_email_confirmation_token(raw_token)
205
239
  end
240
+ end
206
241
 
207
- should "return user record with correct email_confirmation token" do
208
- raw_token = @user.raw_confirmation_token
209
- loaded_user = User.load_from_email_confirmation_token(raw_token)
210
- assert_equal @user, loaded_user
211
- end
242
+ test 'confirm_email!' do
243
+ user = Fabricate(:user)
244
+ user.update(email: 'new@example.com')
245
+ user.reload
246
+ user.confirm_email!
247
+ user.reload
248
+ assert_nil user.unconfirmed_email
249
+ assert_equal 'new@example.com', user.email
250
+ assert_nil user.confirmation_token
251
+ assert_nil user.confirmation_token_sent_at
252
+ end
212
253
 
213
- should "return nil with incorrect email_confirmation token" do
214
- loaded_user = User.load_from_email_confirmation_token('blahblahblah')
215
- assert_nil loaded_user
254
+ test 'should send confirmation success email when confirm_email!' do
255
+ user = Fabricate(:user)
256
+ user.update(email: 'new@example.com')
257
+ user.reload
258
+ assert_emails 1 do
259
+ user.confirm_email!
216
260
  end
261
+ email = ActionMailer::Base.deliveries.last
262
+ assert_match /You have successfully changed your account email/, email.body.encoded
263
+ end
217
264
 
218
- should "return nil with correct email_confirmation token which is expired" do
219
- raw_token = @user.raw_confirmation_token
220
- @user.update_attribute(:confirmation_token_sent_at, 7.days.ago)
221
-
222
- loaded_user = User.load_from_email_confirmation_token(raw_token)
223
- assert_nil loaded_user
265
+ test 'should not send confirmation success email when confirm_email! if email_confirmation_success_email_method_name == nil' do
266
+ swap Goma.config(:user), email_confirmation_success_email_method_name: nil do
267
+ user = Fabricate(:user)
268
+ user.update(email: 'new@example.com')
269
+ user.reload
270
+ assert_no_emails do
271
+ user.confirm_email!
272
+ end
224
273
  end
225
274
  end
226
-
227
275
  end
@@ -3,11 +3,6 @@ require 'test_helper'
3
3
  class GomaTest < ActiveSupport::TestCase
4
4
  test "goma class method is introduced to ActiveRecord::Base" do
5
5
  assert ActiveRecord::Base.respond_to?(:goma)
6
- # assert User.respond_to?(:goma)
7
-
8
- # user = User.new
9
- # assert_equal "blahblah", user.blahblah
10
- # puts Goma.config.encryptor
11
6
  end
12
7
 
13
8
  test "should have module existence methods" do
@@ -0,0 +1,131 @@
1
+ require "test_helper"
2
+
3
+ class LockableTest < ActiveSupport::TestCase
4
+ test 'lock_access!' do
5
+ user = Fabricate(:user)
6
+ user.lock_access!
7
+ user.reload
8
+ assert user.access_locked?
9
+ assert user.unlock_token
10
+ assert user.unlock_token_sent_at
11
+ assert user.locked_at
12
+ end
13
+
14
+ test 'should send unlock token email when lock_access!' do
15
+ Goma.token_generator.expects(:friendly_token).returns('sesame')
16
+
17
+ user = Fabricate(:user)
18
+ assert_emails 1 do
19
+ user.lock_access!
20
+ end
21
+ email = ActionMailer::Base.deliveries.last
22
+ assert_match %r{/unlocks/sesame}, email.body.encoded
23
+ end
24
+
25
+ test 'should not send unlock token email when lock_access! if unlock_strategies does not include :email' do
26
+ swap Goma.config(:user), unlock_strategies: [:time] do
27
+ user = Fabricate(:user)
28
+ assert_nil user.locked_at
29
+ locked_at = Time.now.utc
30
+ assert_no_emails do
31
+ user.lock_access!
32
+ end
33
+ user.reload
34
+ assert user.locked_at
35
+ end
36
+ end
37
+
38
+ test 'load_from_unlock_token_with_error with correct token' do
39
+ user = Fabricate(:user)
40
+ user.lock_access!
41
+ raw_token = user.raw_unlock_token
42
+ loaded_user, error = User.load_from_unlock_token_with_error(raw_token)
43
+ assert_equal user, loaded_user
44
+ assert_nil error
45
+ end
46
+
47
+ test 'load_from_unlock_token_with_error with wrong token' do
48
+ user = Fabricate(:user)
49
+ user.lock_access!
50
+ loaded_user, error = User.load_from_unlock_token_with_error('beans')
51
+ assert_nil loaded_user
52
+ assert_equal :not_found, error
53
+ end
54
+
55
+ test 'load_from_unlock_token! with correct token' do
56
+ user = Fabricate(:user)
57
+ user.lock_access!
58
+ raw_token = user.raw_unlock_token
59
+ assert_equal user, User.load_from_unlock_token!(raw_token)
60
+ end
61
+
62
+ test 'load_from_unlock_token! with wrong token' do
63
+ user = Fabricate(:user)
64
+ user.lock_access!
65
+ assert_raise Goma::NotFound do
66
+ User.load_from_unlock_token!('beans')
67
+ end
68
+ end
69
+
70
+ test 'load_from_unlock_token with correct token' do
71
+ user = Fabricate(:user)
72
+ user.lock_access!
73
+ raw_token = user.raw_unlock_token
74
+ assert_equal user, User.load_from_unlock_token(raw_token)
75
+ end
76
+
77
+ test 'load_from_unlock_token with wrong token' do
78
+ user = Fabricate(:user)
79
+ user.lock_access!
80
+ assert_nil User.load_from_unlock_token('beans')
81
+ end
82
+
83
+ test 'unlock_access!' do
84
+ user = Fabricate(:user)
85
+ user.lock_access!
86
+ user.unlock_access!
87
+ user.reload
88
+ refute user.access_locked?
89
+ assert_nil user.unlock_token
90
+ assert_nil user.unlock_token_sent_at
91
+ assert_nil user.locked_at
92
+ end
93
+
94
+ test 'lock_expired?' do
95
+ user = Fabricate(:user)
96
+ user.lock_access!
97
+ user.reload
98
+ refute user.lock_expired?
99
+ Timecop.travel 1.hour.from_now
100
+ assert user.lock_expired?
101
+ Timecop.return
102
+ end
103
+
104
+ test 'valid_password?' do
105
+ swap Goma.config(:user), maximum_attempts: 3 do
106
+ user = Fabricate(:user)
107
+ 3.times do
108
+ user.valid_password? 'wrongpass'
109
+ end
110
+ user.reload
111
+ refute user.access_locked?
112
+
113
+ user.valid_password? 'wrongpass'
114
+ user.reload
115
+ assert user.access_locked?
116
+ end
117
+ end
118
+
119
+ test 'last_attempt?' do
120
+ swap Goma.config(:user), maximum_attempts: 3 do
121
+ user = Fabricate(:user)
122
+ 2.times do
123
+ user.valid_password? 'wrongpass'
124
+ refute user.last_attempt?
125
+ end
126
+
127
+ user.valid_password? 'wrongpass'
128
+ assert user.last_attempt?
129
+ end
130
+ end
131
+ end