clearance 0.10.3.2 → 0.10.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of clearance might be problematic. Click here for more details.
- data/.rspec +2 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -5
- data/Gemfile.lock +2 -11
- data/README.md +5 -8
- data/Rakefile +5 -7
- data/VERSION +1 -1
- data/app/views/passwords/new.html.erb +1 -1
- data/app/views/sessions/new.html.erb +1 -1
- data/app/views/users/_form.html.erb +1 -1
- data/features/engine/visitor_resets_password.feature +11 -0
- data/features/engine/visitor_signs_in.feature +10 -0
- data/features/engine/visitor_signs_up.feature +6 -4
- data/features/integration.feature +1 -1
- data/features/step_definitions/engine/clearance_steps.rb +8 -0
- data/lib/clearance/shoulda_macros.rb +5 -3
- data/lib/clearance/user.rb +12 -4
- data/lib/generators/clearance/install/install_generator.rb +2 -2
- data/spec/controllers/passwords_controller_spec.rb +177 -0
- data/spec/controllers/sessions_controller_spec.rb +160 -0
- data/spec/controllers/users_controller_spec.rb +64 -0
- data/{test → spec}/factories.rb +0 -0
- data/spec/models/clearance_mailer_spec.rb +27 -0
- data/spec/models/user_spec.rb +260 -0
- data/spec/spec_helper.rb +65 -0
- data/spec/support/authorization.rb +18 -0
- data/spec/support/clearance_redirects.rb +21 -0
- data/spec/support/cookies.rb +72 -0
- metadata +16 -27
- data/test/controllers/passwords_controller_test.rb +0 -198
- data/test/controllers/sessions_controller_test.rb +0 -150
- data/test/controllers/users_controller_test.rb +0 -64
- data/test/models/clearance_mailer_test.rb +0 -29
- data/test/models/user_test.rb +0 -244
- data/test/test_helper.rb +0 -59
@@ -1,150 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class SessionsControllerTest < ActionController::TestCase
|
4
|
-
tests Clearance::SessionsController
|
5
|
-
|
6
|
-
context "on GET to /sessions/new" do
|
7
|
-
setup { get :new }
|
8
|
-
|
9
|
-
should respond_with(:success)
|
10
|
-
should render_template(:new)
|
11
|
-
should_not set_the_flash
|
12
|
-
end
|
13
|
-
|
14
|
-
context "on POST to #create with good credentials" do
|
15
|
-
setup do
|
16
|
-
@user = Factory(:user)
|
17
|
-
@user.update_attribute(:remember_token, "old-token")
|
18
|
-
post :create, :session => {
|
19
|
-
:email => @user.email,
|
20
|
-
:password => @user.password }
|
21
|
-
end
|
22
|
-
|
23
|
-
should set_the_flash.to(/signed in/i)
|
24
|
-
should_redirect_to_url_after_create
|
25
|
-
|
26
|
-
should_set_cookie("remember_token", "old-token", Clearance.configuration.cookie_expiration.call)
|
27
|
-
|
28
|
-
should "have a default of 1 year from now" do
|
29
|
-
assert_in_delta Clearance.configuration.cookie_expiration.call, 1.year.from_now, 100
|
30
|
-
end
|
31
|
-
|
32
|
-
should "not change the remember token" do
|
33
|
-
assert_equal "old-token", @user.reload.remember_token
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "on POST to #create with good credentials - cookie duration set to 2 weeks" do
|
38
|
-
custom_duration = 2.weeks.from_now.utc
|
39
|
-
|
40
|
-
setup do
|
41
|
-
Clearance.configuration.cookie_expiration = lambda { custom_duration }
|
42
|
-
@user = Factory(:user)
|
43
|
-
@user.update_attribute(:remember_token, "old-token2")
|
44
|
-
post :create, :session => {
|
45
|
-
:email => @user.email,
|
46
|
-
:password => @user.password }
|
47
|
-
end
|
48
|
-
|
49
|
-
should_set_cookie("remember_token", "old-token2", custom_duration)
|
50
|
-
|
51
|
-
teardown do
|
52
|
-
# restore default Clearance configuration
|
53
|
-
Clearance.configuration = nil
|
54
|
-
Clearance.configure {}
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "on POST to #create with good credentials - cookie expiration set to nil (session cookie)" do
|
59
|
-
setup do
|
60
|
-
Clearance.configuration.cookie_expiration = lambda { nil }
|
61
|
-
@user = Factory(:user)
|
62
|
-
@user.update_attribute(:remember_token, "old-token3")
|
63
|
-
post :create, :session => {
|
64
|
-
:email => @user.email,
|
65
|
-
:password => @user.password }
|
66
|
-
end
|
67
|
-
|
68
|
-
should_set_cookie("remember_token", "old-token3", nil)
|
69
|
-
|
70
|
-
teardown do
|
71
|
-
# restore default Clearance configuration
|
72
|
-
Clearance.configuration = nil
|
73
|
-
Clearance.configure {}
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context "on POST to #create with good credentials and a session return url" do
|
78
|
-
setup do
|
79
|
-
@user = Factory(:user)
|
80
|
-
@return_url = '/url_in_the_session'
|
81
|
-
@request.session[:return_to] = @return_url
|
82
|
-
post :create, :session => {
|
83
|
-
:email => @user.email,
|
84
|
-
:password => @user.password }
|
85
|
-
end
|
86
|
-
|
87
|
-
should redirect_to("the return URL") { @return_url }
|
88
|
-
end
|
89
|
-
|
90
|
-
context "on POST to #create with good credentials and a request return url" do
|
91
|
-
setup do
|
92
|
-
@user = Factory(:user)
|
93
|
-
@return_url = '/url_in_the_request'
|
94
|
-
post :create, :session => {
|
95
|
-
:email => @user.email,
|
96
|
-
:password => @user.password },
|
97
|
-
:return_to => @return_url
|
98
|
-
end
|
99
|
-
|
100
|
-
should redirect_to("the return URL") { @return_url }
|
101
|
-
end
|
102
|
-
|
103
|
-
context "on POST to #create with good credentials and a session return url and request return url" do
|
104
|
-
setup do
|
105
|
-
@user = Factory(:user)
|
106
|
-
@return_url = '/url_in_the_session'
|
107
|
-
@request.session[:return_to] = @return_url
|
108
|
-
post :create, :session => {
|
109
|
-
:email => @user.email,
|
110
|
-
:password => @user.password },
|
111
|
-
:return_to => '/url_in_the_request'
|
112
|
-
end
|
113
|
-
|
114
|
-
should redirect_to("the return URL") { @return_url }
|
115
|
-
end
|
116
|
-
|
117
|
-
context "on DELETE to #destroy given a signed out user" do
|
118
|
-
setup do
|
119
|
-
sign_out
|
120
|
-
delete :destroy
|
121
|
-
end
|
122
|
-
should set_the_flash.to(/signed out/i)
|
123
|
-
should_redirect_to_url_after_destroy
|
124
|
-
end
|
125
|
-
|
126
|
-
context "on DELETE to #destroy with a cookie" do
|
127
|
-
setup do
|
128
|
-
@user = Factory(:user)
|
129
|
-
@user.update_attribute(:remember_token, "old-token")
|
130
|
-
@request.cookies["remember_token"] = "old-token"
|
131
|
-
delete :destroy
|
132
|
-
end
|
133
|
-
|
134
|
-
should set_the_flash.to(/signed out/i)
|
135
|
-
should_redirect_to_url_after_destroy
|
136
|
-
|
137
|
-
should "delete the cookie token" do
|
138
|
-
assert_nil cookies['remember_token']
|
139
|
-
end
|
140
|
-
|
141
|
-
should "reset the remember token" do
|
142
|
-
assert_not_equal "old-token", @user.reload.remember_token
|
143
|
-
end
|
144
|
-
|
145
|
-
should "unset the current user" do
|
146
|
-
assert_nil @controller.current_user
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class UsersControllerTest < ActionController::TestCase
|
4
|
-
|
5
|
-
tests Clearance::UsersController
|
6
|
-
|
7
|
-
context "when signed out" do
|
8
|
-
setup { sign_out }
|
9
|
-
|
10
|
-
context "on GET to #new" do
|
11
|
-
setup { get :new }
|
12
|
-
|
13
|
-
should respond_with(:success)
|
14
|
-
should render_template(:new)
|
15
|
-
should_not set_the_flash
|
16
|
-
end
|
17
|
-
|
18
|
-
context "on GET to #new with email" do
|
19
|
-
setup do
|
20
|
-
@email = "a@example.com"
|
21
|
-
get :new, :user => { :email => @email }
|
22
|
-
end
|
23
|
-
|
24
|
-
should "set assigned user's email" do
|
25
|
-
assert_equal @email, assigns(:user).email
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context "on POST to #create with valid attributes" do
|
30
|
-
setup do
|
31
|
-
user_attributes = Factory.attributes_for(:user)
|
32
|
-
@old_user_count = User.count
|
33
|
-
post :create, :user => user_attributes
|
34
|
-
end
|
35
|
-
|
36
|
-
should assign_to(:user)
|
37
|
-
|
38
|
-
should "create a new user" do
|
39
|
-
assert_equal @old_user_count + 1, User.count
|
40
|
-
end
|
41
|
-
|
42
|
-
should set_the_flash.to(/signed up/i)
|
43
|
-
should_redirect_to_url_after_create
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context "A signed-in user" do
|
48
|
-
setup do
|
49
|
-
@user = Factory(:user)
|
50
|
-
sign_in_as @user
|
51
|
-
end
|
52
|
-
|
53
|
-
context "GET to new" do
|
54
|
-
setup { get :new }
|
55
|
-
should redirect_to("the home page") { root_url }
|
56
|
-
end
|
57
|
-
|
58
|
-
context "POST to create" do
|
59
|
-
setup { post :create, :user => {} }
|
60
|
-
should redirect_to("the home page") { root_url }
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ClearanceMailerTest < ActiveSupport::TestCase
|
4
|
-
context "A change password email" do
|
5
|
-
setup do
|
6
|
-
@user = Factory(:user)
|
7
|
-
@user.forgot_password!
|
8
|
-
@email = ClearanceMailer.change_password @user
|
9
|
-
end
|
10
|
-
|
11
|
-
should "be from DO_NOT_REPLY" do
|
12
|
-
assert_match /#{@email.from[0]}/i, Clearance.configuration.mailer_sender
|
13
|
-
end
|
14
|
-
|
15
|
-
should "be sent to user" do
|
16
|
-
assert_match /#{@user.email}/i, @email.to.first
|
17
|
-
end
|
18
|
-
|
19
|
-
should "contain a link to edit the user's password" do
|
20
|
-
host = ActionMailer::Base.default_url_options[:host]
|
21
|
-
regexp = %r{http://#{host}/users/#{@user.id}/password/edit\?token=#{@user.confirmation_token}}
|
22
|
-
assert_match regexp, @email.body.to_s
|
23
|
-
end
|
24
|
-
|
25
|
-
should "set its subject" do
|
26
|
-
assert_match /Change your password/, @email.subject
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/test/models/user_test.rb
DELETED
@@ -1,244 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class UserTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
# db
|
6
|
-
|
7
|
-
should have_db_index(:email)
|
8
|
-
should have_db_index(:remember_token)
|
9
|
-
|
10
|
-
# signing up
|
11
|
-
|
12
|
-
context "When signing up" do
|
13
|
-
should validate_presence_of(:email)
|
14
|
-
should validate_presence_of(:password)
|
15
|
-
should allow_value("foo@example.co.uk").for(:email)
|
16
|
-
should allow_value("foo@example.com").for(:email)
|
17
|
-
should_not allow_value("foo@").for(:email)
|
18
|
-
should_not allow_value("foo@example..com").for(:email)
|
19
|
-
should_not allow_value("foo@.example.com").for(:email)
|
20
|
-
should_not allow_value("foo").for(:email)
|
21
|
-
should_not allow_value("example.com").for(:email)
|
22
|
-
|
23
|
-
should "require password confirmation on create" do
|
24
|
-
user = Factory.build(:user, :password => 'blah',
|
25
|
-
:password_confirmation => 'boogidy')
|
26
|
-
assert ! user.save
|
27
|
-
assert user.errors[:password].any?
|
28
|
-
end
|
29
|
-
|
30
|
-
should "require non blank password confirmation on create" do
|
31
|
-
user = Factory.build(:user, :password => 'blah',
|
32
|
-
:password_confirmation => '')
|
33
|
-
assert ! user.save
|
34
|
-
assert user.errors[:password].any?
|
35
|
-
end
|
36
|
-
|
37
|
-
should "initialize salt" do
|
38
|
-
assert_not_nil Factory(:user).salt
|
39
|
-
end
|
40
|
-
|
41
|
-
context "encrypt password" do
|
42
|
-
setup do
|
43
|
-
@salt = "salt"
|
44
|
-
@user = Factory.build(:user, :salt => @salt)
|
45
|
-
def @user.initialize_salt; end
|
46
|
-
@user.save!
|
47
|
-
@password = @user.password
|
48
|
-
|
49
|
-
@user.send(:encrypt, @password)
|
50
|
-
@expected = Digest::SHA1.hexdigest("--#{@salt}--#{@password}--")
|
51
|
-
end
|
52
|
-
|
53
|
-
should "create an encrypted password using SHA1 encryption" do
|
54
|
-
assert_equal @expected, @user.encrypted_password
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
should "store email in exact case" do
|
59
|
-
user = Factory(:user, :email => "John.Doe@example.com")
|
60
|
-
assert_equal "John.Doe@example.com", user.email
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context "When multiple users have signed up" do
|
65
|
-
setup { Factory(:user) }
|
66
|
-
should validate_uniqueness_of(:email)
|
67
|
-
end
|
68
|
-
|
69
|
-
# authenticating
|
70
|
-
|
71
|
-
context "A user" do
|
72
|
-
setup do
|
73
|
-
@user = Factory(:user)
|
74
|
-
@password = @user.password
|
75
|
-
end
|
76
|
-
|
77
|
-
should "authenticate with good credentials" do
|
78
|
-
assert ::User.authenticate(@user.email, @password)
|
79
|
-
assert @user.authenticated?(@password)
|
80
|
-
end
|
81
|
-
|
82
|
-
should "not authenticate with bad credentials" do
|
83
|
-
assert ! ::User.authenticate(@user.email, 'bad_password')
|
84
|
-
assert ! @user.authenticated?('bad_password')
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
# resetting remember token
|
89
|
-
|
90
|
-
context "When resetting authentication with reset_remember_token!" do
|
91
|
-
setup do
|
92
|
-
@user = Factory(:user)
|
93
|
-
@user.remember_token = "old-token"
|
94
|
-
@user.reset_remember_token!
|
95
|
-
end
|
96
|
-
|
97
|
-
should "change the remember token" do
|
98
|
-
assert_not_equal "old-token", @user.remember_token
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# updating password
|
103
|
-
|
104
|
-
context "An email confirmed user" do
|
105
|
-
setup do
|
106
|
-
@user = Factory(:user)
|
107
|
-
@old_encrypted_password = @user.encrypted_password
|
108
|
-
end
|
109
|
-
|
110
|
-
context "who updates password with confirmation" do
|
111
|
-
setup do
|
112
|
-
@user.update_password("new_password", "new_password")
|
113
|
-
end
|
114
|
-
|
115
|
-
should "change encrypted password" do
|
116
|
-
assert_not_equal @user.encrypted_password,
|
117
|
-
@old_encrypted_password
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
should "not generate the same remember token for users with the same password at the same time" do
|
123
|
-
Time.stubs(:now => Time.now)
|
124
|
-
password = 'secret'
|
125
|
-
first_user = Factory(:user,
|
126
|
-
:password => password,
|
127
|
-
:password_confirmation => password)
|
128
|
-
second_user = Factory(:user,
|
129
|
-
:password => password,
|
130
|
-
:password_confirmation => password)
|
131
|
-
|
132
|
-
assert_not_equal first_user.remember_token, second_user.remember_token
|
133
|
-
end
|
134
|
-
|
135
|
-
# recovering forgotten password
|
136
|
-
|
137
|
-
context "An user" do
|
138
|
-
setup do
|
139
|
-
@user = Factory(:user)
|
140
|
-
@old_encrypted_password = @user.encrypted_password
|
141
|
-
end
|
142
|
-
|
143
|
-
context "who requests password reminder" do
|
144
|
-
setup do
|
145
|
-
assert_nil @user.confirmation_token
|
146
|
-
@user.forgot_password!
|
147
|
-
end
|
148
|
-
|
149
|
-
should "generate confirmation token" do
|
150
|
-
assert_not_nil @user.confirmation_token
|
151
|
-
end
|
152
|
-
|
153
|
-
context "and then updates password" do
|
154
|
-
context 'with confirmation' do
|
155
|
-
setup do
|
156
|
-
@user.update_password("new_password", "new_password")
|
157
|
-
end
|
158
|
-
|
159
|
-
should "change encrypted password" do
|
160
|
-
assert_not_equal @user.encrypted_password,
|
161
|
-
@old_encrypted_password
|
162
|
-
end
|
163
|
-
|
164
|
-
should "clear confirmation token" do
|
165
|
-
assert_nil @user.confirmation_token
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
context 'without confirmation' do
|
170
|
-
setup do
|
171
|
-
@user.update_password("new_password", "")
|
172
|
-
end
|
173
|
-
|
174
|
-
should "not change encrypted password" do
|
175
|
-
assert_equal @user.encrypted_password,
|
176
|
-
@old_encrypted_password
|
177
|
-
end
|
178
|
-
|
179
|
-
should "not clear confirmation token" do
|
180
|
-
assert_not_nil @user.confirmation_token
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
end
|
187
|
-
|
188
|
-
# optional email/password fields
|
189
|
-
context "a user with an optional email" do
|
190
|
-
setup do
|
191
|
-
@user = User.new
|
192
|
-
class << @user
|
193
|
-
def email_optional?
|
194
|
-
true
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
subject { @user }
|
200
|
-
|
201
|
-
should allow_value(nil).for(:email)
|
202
|
-
should allow_value("").for(:email)
|
203
|
-
end
|
204
|
-
|
205
|
-
context "a user with an optional password" do
|
206
|
-
setup do
|
207
|
-
@user = User.new
|
208
|
-
class << @user
|
209
|
-
def password_optional?
|
210
|
-
true
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
subject { @user }
|
216
|
-
|
217
|
-
should allow_value(nil).for(:password)
|
218
|
-
should allow_value("").for(:password)
|
219
|
-
end
|
220
|
-
|
221
|
-
context "user factory" do
|
222
|
-
should "create a valid user with just an overridden password" do
|
223
|
-
assert Factory.build(:user, :password => "test").valid?
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
context "when user exists before Clearance was installed" do
|
228
|
-
setup do
|
229
|
-
@user = Factory(:user)
|
230
|
-
sql = "update users set salt = NULL, encrypted_password = NULL, remember_token = NULL where id = #{@user.id}"
|
231
|
-
ActiveRecord::Base.connection.update(sql)
|
232
|
-
assert_nil @user.reload.salt
|
233
|
-
assert_nil @user.reload.encrypted_password
|
234
|
-
assert_nil @user.reload.remember_token
|
235
|
-
end
|
236
|
-
|
237
|
-
should "initialize salt, generate remember token, and save encrypted password on update_password" do
|
238
|
-
@user.update_password('password', 'password')
|
239
|
-
assert_not_nil @user.salt
|
240
|
-
assert_not_nil @user.encrypted_password
|
241
|
-
assert_not_nil @user.remember_token
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|