thoughtbot-clearance 0.3.8 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/README.textile +17 -16
  2. data/Rakefile +8 -7
  3. data/TODO.textile +3 -3
  4. data/generators/clearance/clearance_generator.rb +1 -1
  5. data/generators/clearance/templates/app/views/clearance_mailer/change_password.html.erb +1 -1
  6. data/generators/clearance/templates/app/views/passwords/edit.html.erb +1 -1
  7. data/generators/clearance/templates/app/views/sessions/new.html.erb +2 -2
  8. data/generators/clearance/templates/db/migrate/create_users_with_clearance_columns.rb +3 -3
  9. data/generators/clearance/templates/db/migrate/update_users_with_clearance_columns.rb +14 -13
  10. data/generators/clearance/templates/test/factories/clearance.rb +16 -0
  11. data/lib/clearance/app/controllers/application_controller.rb +26 -14
  12. data/lib/clearance/app/controllers/confirmations_controller.rb +6 -3
  13. data/lib/clearance/app/controllers/passwords_controller.rb +15 -9
  14. data/lib/clearance/app/controllers/sessions_controller.rb +21 -17
  15. data/lib/clearance/app/controllers/users_controller.rb +4 -3
  16. data/lib/clearance/app/models/clearance_mailer.rb +1 -1
  17. data/lib/clearance/app/models/user.rb +15 -16
  18. data/lib/clearance/test/functional/confirmations_controller_test.rb +18 -30
  19. data/lib/clearance/test/functional/passwords_controller_test.rb +27 -45
  20. data/lib/clearance/test/functional/sessions_controller_test.rb +23 -27
  21. data/lib/clearance/test/functional/users_controller_test.rb +38 -28
  22. data/lib/clearance/test/test_helper.rb +7 -2
  23. data/lib/clearance/test/unit/clearance_mailer_test.rb +7 -5
  24. data/lib/clearance/test/unit/user_test.rb +107 -138
  25. data/shoulda_macros/clearance.rb +134 -6
  26. metadata +7 -3
  27. data/generators/clearance/templates/test/factories/clearance_user.rb +0 -9
@@ -3,15 +3,19 @@ module Clearance
3
3
  module Functional
4
4
  module UsersControllerTest
5
5
 
6
- def self.included(base)
7
- base.class_eval do
6
+ def self.included(controller_test)
7
+ controller_test.class_eval do
8
+
9
+ should_filter_params :password
10
+
8
11
  public_context do
9
-
10
- context "on GET to /users/new" do
12
+ context "When getting new User view" do
11
13
  setup { get :new }
14
+
12
15
  should_respond_with :success
13
16
  should_render_template :new
14
17
  should_not_set_the_flash
18
+
15
19
  should "display a form to register" do
16
20
  assert_select "form[action=#{users_path}][method=post]",
17
21
  true, "There must be a form to register" do
@@ -25,48 +29,54 @@ module Clearance
25
29
  "There must be a submit button"
26
30
  end
27
31
  end
28
-
29
- context "with params" do
30
- setup do
31
- @email = 'a@example.com'
32
- get :new, :user => {:email => @email}
33
- end
32
+ end
33
+
34
+ context "Given email parameter when getting new User view" do
35
+ setup do
36
+ @email = "a@example.com"
37
+ get :new, :user => { :email => @email }
38
+ end
34
39
 
35
- should_assign_to :user
36
- should "set the @user's params" do
37
- assert_equal @email, assigns(:user).email
38
- end
40
+ should "set assigned user's email" do
41
+ assert_equal @email, assigns(:user).email
39
42
  end
40
43
  end
41
44
 
42
- context "on POST to /users" do
45
+ context "Given valid attributes when creating a new user" do
43
46
  setup do
44
- post :create, :user => Factory.build(:clearance_user).attributes.merge(
45
- {:password => 'skerit',
46
- :password_confirmation => 'skerit'})
47
+ user_attributes = Factory.attributes_for(:registered_user)
48
+ post :create, :user => user_attributes
47
49
  end
48
50
 
49
- should_set_the_flash_to /confirm/i
50
- should_redirect_to "@controller.send(:url_after_create)"
51
- should_assign_to :user
52
- should_change 'User.count', :by => 1
51
+ should_create_user_successfully
52
+ end
53
+
54
+ context "Given valid email confirmation attributes when creating a new user" do
55
+ setup do
56
+ user_attributes = Factory.attributes_for(:email_confirmed_user)
57
+ post :create, :user => user_attributes
58
+ end
59
+
60
+ should_create_user_successfully
61
+
62
+ should "not confirm email" do
63
+ assert ! assigns(:user).email_confirmed
64
+ end
53
65
  end
54
-
55
66
  end
56
67
 
57
- logged_in_user_context do
68
+ signed_in_user_context do
58
69
  context "GET to new" do
59
70
  setup { get :new }
60
- should_redirect_to 'root_url'
71
+ should_redirect_to "root_url"
61
72
  end
62
73
 
63
74
  context "POST to create" do
64
75
  setup { post :create, :user => {} }
65
- should_redirect_to 'root_url'
76
+ should_redirect_to "root_url"
66
77
  end
67
-
68
- should_filter_params :password
69
78
  end
79
+
70
80
  end
71
81
  end
72
82
  end
@@ -5,14 +5,19 @@ module Clearance
5
5
  def self.included(test_helper)
6
6
  test_helper.class_eval do
7
7
 
8
- def login_as(user = nil)
9
- user ||= Factory(:clearance_user)
8
+ def sign_in_as(user = nil)
9
+ unless user
10
+ user = Factory(:registered_user)
11
+ user.confirm_email!
12
+ end
10
13
  @request.session[:user_id] = user.id
14
+ @request.session[:salt] = user.salt
11
15
  return user
12
16
  end
13
17
 
14
18
  def logout
15
19
  @request.session[:user_id] = nil
20
+ @request.session[:salt] = nil
16
21
  end
17
22
 
18
23
  end
@@ -3,11 +3,12 @@ module Clearance
3
3
  module Unit
4
4
  module ClearanceMailerTest
5
5
 
6
- def self.included(base)
7
- base.class_eval do
6
+ def self.included(mailer_test)
7
+ mailer_test.class_eval do
8
+
8
9
  context "A change password email" do
9
10
  setup do
10
- @user = Factory :clearance_user
11
+ @user = Factory(:registered_user)
11
12
  @email = ClearanceMailer.create_change_password @user
12
13
  end
13
14
 
@@ -17,7 +18,7 @@ module Clearance
17
18
 
18
19
  should "contain a link to edit the user's password" do
19
20
  host = ActionMailer::Base.default_url_options[:host]
20
- regexp = %r{http://#{host}/users/#{@user.id}/password/edit\?email=#{@user.email.gsub("@", "%40")}&password=#{@user.crypted_password}}
21
+ regexp = %r{http://#{host}/users/#{@user.id}/password/edit\?email=#{@user.email.gsub("@", "%40")}&password=#{@user.encrypted_password}}
21
22
  assert_match regexp, @email.body
22
23
  end
23
24
 
@@ -32,7 +33,7 @@ module Clearance
32
33
 
33
34
  context "A confirmation email" do
34
35
  setup do
35
- @user = Factory :clearance_user
36
+ @user = Factory(:registered_user)
36
37
  @email = ClearanceMailer.create_confirmation @user
37
38
  end
38
39
 
@@ -54,6 +55,7 @@ module Clearance
54
55
  assert_match regexp, @email.body
55
56
  end
56
57
  end
58
+
57
59
  end
58
60
  end
59
61
 
@@ -3,188 +3,157 @@ module Clearance
3
3
  module Unit
4
4
  module UserTest
5
5
 
6
- def self.included(base)
7
- base.class_eval do
8
- should_require_attributes :email, :password
9
- should_allow_values_for :email, 'foo@example.com'
10
- should_not_allow_values_for :email, 'foo'
11
- should_not_allow_values_for :email, 'example.com'
6
+ def self.included(unit_test)
7
+ unit_test.class_eval do
8
+
9
+ # registering
10
+
11
+ context "When registering" do
12
+ should_require_attributes :email
13
+ should_allow_values_for :email, "foo@example.com"
14
+ should_not_allow_values_for :email, "foo"
15
+ should_not_allow_values_for :email, "example.com"
16
+
17
+ should_validate_confirmation_of :password,
18
+ :factory => :registered_user
19
+
20
+ should "initialize salt" do
21
+ assert_not_nil Factory(:registered_user).salt
22
+ end
23
+
24
+ context "encrypt password" do
25
+ setup do
26
+ @salt = "salt"
27
+ User.any_instance.stubs(:initialize_salt)
12
28
 
13
- should "require password validation on create" do
14
- user = Factory.build(:clearance_user, :password => 'blah', :password_confirmation => 'boogidy')
15
- assert !user.save
16
- assert_match(/confirmation/i, user.errors.on(:password))
17
- end
29
+ @user = Factory(:registered_user, :salt => @salt)
30
+ @password = @user.password
18
31
 
19
- should "create a crypted_password on save" do
20
- assert_not_nil Factory(:clearance_user, :crypted_password => nil).crypted_password
21
- end
32
+ @user.encrypt(@password)
33
+ @expected = Digest::SHA512.hexdigest("--#{@salt}--#{@password}--")
34
+ end
22
35
 
23
- context 'updating a password' do
36
+ should "create an encrypted password using SHA512 encryption" do
37
+ assert_equal @expected, @user.encrypted_password
38
+ assert_not_equal @password, @user.encrypted_password
39
+ end
40
+ end
41
+
42
+ should "store email in lower case" do
43
+ user = Factory(:registered_user, :email => "John.Doe@example.com")
44
+ assert_equal "john.doe@example.com", user.email
45
+ end
46
+ end
47
+
48
+ context "When multiple users have registerd" do
49
+ setup { @user = Factory(:registered_user) }
50
+
51
+ should_require_unique_attributes :email
52
+ end
53
+
54
+ # confirming email
55
+
56
+ context "A registered user without email confirmation" do
24
57
  setup do
25
- @user = Factory(:clearance_user)
26
- assert_not_nil @user.crypted_password
27
- @crypt = @user.crypted_password
28
- assert_not_nil @user.salt
29
- @salt = @user.salt
30
- @user.password = 'a_new_password'
31
- @user.password_confirmation = 'a_new_password'
32
- assert @user.save
58
+ @user = Factory(:registered_user)
59
+ assert ! @user.email_confirmed?
33
60
  end
34
61
 
35
- should 'update a crypted_password' do
36
- @user.reload
37
- assert @user.crypted_password != @crypt
62
+ context "after #confirm_email!" do
63
+ setup do
64
+ assert @user.confirm_email!
65
+ @user.reload
66
+ end
67
+
68
+ should "have confirmed their email" do
69
+ assert @user.email_confirmed?
70
+ end
38
71
  end
39
72
  end
73
+
74
+ # authenticating
40
75
 
41
- context 'A user' do
76
+ context "A user" do
42
77
  setup do
43
- @salt = 'salt'
44
- User.any_instance.stubs(:initialize_salt)
45
-
46
- @user = Factory(:clearance_user, :salt => @salt)
78
+ @user = Factory(:registered_user)
47
79
  @password = @user.password
48
80
  end
49
-
50
- should "require password validation on update" do
51
- @user.update_attributes :password => "blah",
52
- :password_confirmation => "boogidy"
53
- assert !@user.save
54
- assert_match(/confirmation/i, @user.errors.on(:password))
55
- end
56
-
57
- should_require_unique_attributes :email
58
-
59
- should 'store email in lower case' do
60
- @user.update_attributes(:email => 'John.Doe@example.com')
61
- assert_equal 'john.doe@example.com', @user.email
62
- end
63
81
 
64
82
  should "authenticate with good credentials" do
65
83
  assert User.authenticate(@user.email, @password)
84
+ assert @user.authenticated?(@password)
66
85
  end
67
86
 
68
87
  should "authenticate with good credentials, email in uppercase" do
69
88
  assert User.authenticate(@user.email.upcase, @password)
89
+ assert @user.authenticated?(@password)
70
90
  end
71
91
 
72
92
  should "not authenticate with bad credentials" do
73
93
  assert ! User.authenticate(@user.email, 'horribly_wrong_password')
74
- end
75
-
76
- should "be authenticated with a good password" do
77
- assert @user.authenticated?(@password)
78
- end
79
-
80
- should "not be authenticated with a bad password" do
81
94
  assert ! @user.authenticated?('horribly_wrong_password')
82
95
  end
96
+ end
83
97
 
84
- context 'encrypt' do
85
- setup do
86
- @crypted = @user.encrypt(@password)
87
- @expected = Digest::SHA512.hexdigest("--#{@salt}--#{@password}--")
88
- end
89
-
90
- should 'create a Hash using SHA512 encryption' do
91
- assert_equal @expected, @crypted
92
- assert_not_equal @password, @crypted
93
- end
98
+ # remember me
99
+
100
+ context "When registering with remember_me!" do
101
+ setup do
102
+ @user = Factory(:registered_user)
103
+ assert_nil @user.remember_token
104
+ assert_nil @user.remember_token_expires_at
105
+ @user.remember_me!
94
106
  end
95
107
 
96
- context 'remember_me!' do
97
- setup do
98
- assert_nil @user.remember_token
99
- assert_nil @user.remember_token_expires_at
100
- @user.remember_me!
101
- end
102
-
103
- should 'set the remember token and expiration date' do
104
- assert_not_nil @user.remember_token
105
- assert_not_nil @user.remember_token_expires_at
106
- end
107
-
108
- should 'remember_token?' do
109
- assert @user.remember_token?
110
- end
111
-
112
- context 'forget_me!' do
113
- setup { @user.forget_me! }
114
-
115
- should 'unset the remember token and expiration date' do
116
- assert_nil @user.remember_token
117
- assert_nil @user.remember_token_expires_at
118
- end
119
-
120
- should 'not remember_token?' do
121
- assert ! @user.remember_token?
122
- end
123
- end
108
+ should "set the remember token and expiration date" do
109
+ assert_not_nil @user.remember_token
110
+ assert_not_nil @user.remember_token_expires_at
124
111
  end
125
-
126
- context 'remember_token?' do
127
- context 'when token expires in the future' do
128
- setup do
129
- @user.update_attribute :remember_token_expires_at,
130
- 2.weeks.from_now.utc
131
- end
132
-
133
- should 'be true' do
134
- assert @user.remember_token?
135
- end
136
- end
137
-
138
- context 'when token expired' do
139
- setup do
140
- @user.update_attribute :remember_token_expires_at,
141
- 2.weeks.ago.utc
142
- end
143
-
144
- should 'be false' do
145
- assert ! @user.remember_token?
146
- end
147
- end
112
+
113
+ should "remember user when token expires in the future" do
114
+ @user.update_attribute :remember_token_expires_at,
115
+ 2.weeks.from_now.utc
116
+ assert @user.remember?
148
117
  end
149
118
 
150
- context "User.authenticate with a valid email and password" do
151
- setup do
152
- @found_user = User.authenticate(@user.email, @user.password)
153
- end
154
-
155
- should "find that user" do
156
- assert_equal @user, @found_user
157
- end
119
+ should "not remember user when token has already expired" do
120
+ @user.update_attribute :remember_token_expires_at,
121
+ 2.weeks.ago.utc
122
+ assert ! @user.remember?
158
123
  end
124
+
125
+ # logging out
126
+
127
+ context "forget_me!" do
128
+ setup { @user.forget_me! }
159
129
 
160
- context "When sent authenticate with an invalid email and password" do
161
- setup do
162
- @found_user = User.authenticate("not", "valid")
130
+ should "unset the remember token and expiration date" do
131
+ assert_nil @user.remember_token
132
+ assert_nil @user.remember_token_expires_at
163
133
  end
164
134
 
165
- should "find nothing" do
166
- assert_nil @found_user
135
+ should "not remember user" do
136
+ assert ! @user.remember?
167
137
  end
168
138
  end
169
139
  end
140
+
141
+ # updating password
142
+
143
+ context "An email confirmed user" do
144
+ setup { @user = Factory(:email_confirmed_user) }
170
145
 
171
- context "A user" do
172
- setup do
173
- @user = Factory(:clearance_user)
174
- end
175
-
176
- context 'when sent #confirm!' do
146
+ context "who changes and confirms password" do
177
147
  setup do
178
- assert ! @user.confirmed?
179
- assert @user.confirm!
180
- @user.reload
148
+ @user.password = "new_password"
149
+ @user.password_confirmation = "new_password"
150
+ @user.save
181
151
  end
182
152
 
183
- should 'mark the User record as confirmed' do
184
- assert @user.confirmed?
185
- end
153
+ should_change "@user.encrypted_password"
186
154
  end
187
155
  end
156
+
188
157
  end
189
158
  end
190
159