benaldred-clearance 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/CHANGELOG.md +274 -0
  2. data/LICENSE +21 -0
  3. data/README.md +129 -0
  4. data/Rakefile +87 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/clearance/confirmations_controller.rb +76 -0
  7. data/app/controllers/clearance/passwords_controller.rb +85 -0
  8. data/app/controllers/clearance/sessions_controller.rb +67 -0
  9. data/app/controllers/clearance/users_controller.rb +35 -0
  10. data/app/models/clearance_mailer.rb +21 -0
  11. data/app/views/clearance_mailer/change_password.html.erb +9 -0
  12. data/app/views/clearance_mailer/confirmation.html.erb +5 -0
  13. data/app/views/passwords/edit.html.erb +23 -0
  14. data/app/views/passwords/new.html.erb +15 -0
  15. data/app/views/sessions/new.html.erb +24 -0
  16. data/app/views/users/_form.html.erb +13 -0
  17. data/app/views/users/new.html.erb +6 -0
  18. data/generators/clearance/USAGE +1 -0
  19. data/generators/clearance/clearance_generator.rb +68 -0
  20. data/generators/clearance/lib/insert_commands.rb +33 -0
  21. data/generators/clearance/lib/rake_commands.rb +22 -0
  22. data/generators/clearance/templates/README +24 -0
  23. data/generators/clearance/templates/clearance.rb +3 -0
  24. data/generators/clearance/templates/factories.rb +13 -0
  25. data/generators/clearance/templates/migrations/create_users.rb +21 -0
  26. data/generators/clearance/templates/migrations/update_users.rb +41 -0
  27. data/generators/clearance/templates/user.rb +3 -0
  28. data/generators/clearance_features/USAGE +1 -0
  29. data/generators/clearance_features/clearance_features_generator.rb +19 -0
  30. data/generators/clearance_features/templates/features/password_reset.feature +33 -0
  31. data/generators/clearance_features/templates/features/sign_in.feature +35 -0
  32. data/generators/clearance_features/templates/features/sign_out.feature +15 -0
  33. data/generators/clearance_features/templates/features/sign_up.feature +45 -0
  34. data/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb +122 -0
  35. data/generators/clearance_features/templates/features/support/paths.rb +23 -0
  36. data/generators/clearance_views/USAGE +0 -0
  37. data/generators/clearance_views/clearance_views_generator.rb +27 -0
  38. data/generators/clearance_views/templates/formtastic/passwords/edit.html.erb +21 -0
  39. data/generators/clearance_views/templates/formtastic/passwords/new.html.erb +15 -0
  40. data/generators/clearance_views/templates/formtastic/sessions/new.html.erb +21 -0
  41. data/generators/clearance_views/templates/formtastic/users/_inputs.html.erb +6 -0
  42. data/generators/clearance_views/templates/formtastic/users/new.html.erb +10 -0
  43. data/lib/clearance.rb +7 -0
  44. data/lib/clearance/authentication.rb +131 -0
  45. data/lib/clearance/configuration.rb +26 -0
  46. data/lib/clearance/extensions/errors.rb +6 -0
  47. data/lib/clearance/extensions/rescue.rb +5 -0
  48. data/lib/clearance/routes.rb +49 -0
  49. data/lib/clearance/user.rb +215 -0
  50. data/rails/init.rb +1 -0
  51. data/shoulda_macros/clearance.rb +266 -0
  52. data/test/controllers/confirmations_controller_test.rb +104 -0
  53. data/test/controllers/passwords_controller_test.rb +183 -0
  54. data/test/controllers/sessions_controller_test.rb +146 -0
  55. data/test/controllers/users_controller_test.rb +65 -0
  56. data/test/models/clearance_mailer_test.rb +55 -0
  57. data/test/models/user_test.rb +255 -0
  58. data/test/rails_root/app/controllers/accounts_controller.rb +10 -0
  59. data/test/rails_root/app/controllers/application_controller.rb +6 -0
  60. data/test/rails_root/app/helpers/application_helper.rb +5 -0
  61. data/test/rails_root/app/helpers/confirmations_helper.rb +2 -0
  62. data/test/rails_root/app/helpers/passwords_helper.rb +2 -0
  63. data/test/rails_root/app/models/user.rb +3 -0
  64. data/test/rails_root/config/boot.rb +110 -0
  65. data/test/rails_root/config/environment.rb +17 -0
  66. data/test/rails_root/config/environments/development.rb +19 -0
  67. data/test/rails_root/config/environments/production.rb +1 -0
  68. data/test/rails_root/config/environments/test.rb +36 -0
  69. data/test/rails_root/config/initializers/clearance.rb +3 -0
  70. data/test/rails_root/config/initializers/inflections.rb +10 -0
  71. data/test/rails_root/config/initializers/mime_types.rb +5 -0
  72. data/test/rails_root/config/initializers/requires.rb +13 -0
  73. data/test/rails_root/config/initializers/time_formats.rb +4 -0
  74. data/test/rails_root/config/routes.rb +6 -0
  75. data/test/rails_root/features/step_definitions/clearance_steps.rb +122 -0
  76. data/test/rails_root/features/step_definitions/web_steps.rb +259 -0
  77. data/test/rails_root/features/support/env.rb +47 -0
  78. data/test/rails_root/features/support/paths.rb +23 -0
  79. data/test/rails_root/public/dispatch.rb +10 -0
  80. data/test/rails_root/script/create_project.rb +52 -0
  81. data/test/rails_root/test/factories/clearance.rb +13 -0
  82. data/test/rails_root/test/functional/accounts_controller_test.rb +23 -0
  83. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +21 -0
  84. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/formtastic.rb +1236 -0
  85. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/justin_french/formtastic.rb +10 -0
  86. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/rails/init.rb +3 -0
  87. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/formtastic_spec.rb +2900 -0
  88. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/test_helper.rb +14 -0
  89. data/test/test_helper.rb +19 -0
  90. metadata +160 -0
@@ -0,0 +1,146 @@
1
+ require 'test_helper'
2
+
3
+ class SessionsControllerTest < ActionController::TestCase
4
+
5
+ tests Clearance::SessionsController
6
+
7
+ should_filter_params :password
8
+
9
+ context "on GET to /sessions/new" do
10
+ setup { get :new }
11
+
12
+ should_respond_with :success
13
+ should_render_template :new
14
+ should_not_set_the_flash
15
+ should_display_a_sign_in_form
16
+ end
17
+
18
+ context "on POST to #create with unconfirmed credentials" do
19
+ setup do
20
+ @user = Factory(:user)
21
+ ActionMailer::Base.deliveries.clear
22
+ post :create, :session => {
23
+ :email => @user.email,
24
+ :password => @user.password }
25
+ end
26
+
27
+ should_deny_access(:flash => /User has not confirmed email. Confirmation email will be resent./i)
28
+
29
+ should "send the confirmation email" do
30
+ assert_not_nil email = ActionMailer::Base.deliveries[0]
31
+ assert_match /account confirmation/i, email.subject
32
+ end
33
+ end
34
+
35
+ context "on POST to #create with good credentials" do
36
+ setup do
37
+ @user = Factory(:email_confirmed_user)
38
+ @user.update_attribute(:remember_token, "old-token")
39
+ post :create, :session => {
40
+ :email => @user.email,
41
+ :password => @user.password }
42
+ end
43
+
44
+ should_set_the_flash_to /signed in/i
45
+ should_redirect_to_url_after_create
46
+
47
+ should 'set the cookie' do
48
+ assert ! cookies['remember_token'].empty?
49
+ end
50
+
51
+ should "not change the remember token" do
52
+ assert_equal "old-token", @user.reload.remember_token
53
+ end
54
+ end
55
+
56
+ context "on POST to #create with good credentials and a session return url" do
57
+ setup do
58
+ @user = Factory(:email_confirmed_user)
59
+ @return_url = '/url_in_the_session'
60
+ @request.session[:return_to] = @return_url
61
+ post :create, :session => {
62
+ :email => @user.email,
63
+ :password => @user.password }
64
+ end
65
+
66
+ should_redirect_to("the return URL") { @return_url }
67
+ end
68
+
69
+ context "on POST to #create with good credentials and a request return url" do
70
+ setup do
71
+ @user = Factory(:email_confirmed_user)
72
+ @return_url = '/url_in_the_request'
73
+ post :create, :session => {
74
+ :email => @user.email,
75
+ :password => @user.password },
76
+ :return_to => @return_url
77
+ end
78
+
79
+ should_redirect_to("the return URL") { @return_url }
80
+ end
81
+
82
+ context "on POST to #create with good credentials and a session return url and request return url" do
83
+ setup do
84
+ @user = Factory(:email_confirmed_user)
85
+ @return_url = '/url_in_the_session'
86
+ @request.session[:return_to] = @return_url
87
+ post :create, :session => {
88
+ :email => @user.email,
89
+ :password => @user.password },
90
+ :return_to => '/url_in_the_request'
91
+ end
92
+
93
+ should_redirect_to("the return URL") { @return_url }
94
+ end
95
+
96
+ context "on POST to #create with bad credentials" do
97
+ setup do
98
+ post :create, :session => {
99
+ :email => 'bad.email@example.com',
100
+ :password => "bad value" }
101
+ end
102
+
103
+ should_set_the_flash_to /bad/i
104
+ should_respond_with :unauthorized
105
+ should_render_template :new
106
+ should_not_be_signed_in
107
+
108
+ should 'not create the cookie' do
109
+ assert_nil cookies['remember_token']
110
+ end
111
+ end
112
+
113
+ context "on DELETE to #destroy given a signed out user" do
114
+ setup do
115
+ sign_out
116
+ delete :destroy
117
+ end
118
+ should_set_the_flash_to(/signed out/i)
119
+ should_redirect_to_url_after_destroy
120
+ end
121
+
122
+ context "on DELETE to #destroy with a cookie" do
123
+ setup do
124
+ @user = Factory(:email_confirmed_user)
125
+ @user.update_attribute(:remember_token, "old-token")
126
+ @request.cookies["remember_token"] = "old-token"
127
+ delete :destroy
128
+ end
129
+
130
+ should_set_the_flash_to(/signed out/i)
131
+ should_redirect_to_url_after_destroy
132
+
133
+ should "delete the cookie token" do
134
+ assert_nil cookies['remember_token']
135
+ end
136
+
137
+ should "reset the remember token" do
138
+ assert_not_equal "old-token", @user.reload.remember_token
139
+ end
140
+
141
+ should "unset the current user" do
142
+ assert_nil @controller.current_user
143
+ end
144
+ end
145
+
146
+ end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class UsersControllerTest < ActionController::TestCase
4
+
5
+ tests Clearance::UsersController
6
+
7
+ should_filter_params :password
8
+
9
+ context "when signed out" do
10
+ setup { sign_out }
11
+
12
+ context "on GET to #new" do
13
+ setup { get :new }
14
+
15
+ should_respond_with :success
16
+ should_render_template :new
17
+ should_not_set_the_flash
18
+
19
+ should_display_a_sign_up_form
20
+ end
21
+
22
+ context "on GET to #new with email" do
23
+ setup do
24
+ @email = "a@example.com"
25
+ get :new, :user => { :email => @email }
26
+ end
27
+
28
+ should "set assigned user's email" do
29
+ assert_equal @email, assigns(:user).email
30
+ end
31
+ end
32
+
33
+ context "on POST to #create with valid attributes" do
34
+ setup do
35
+ user_attributes = Factory.attributes_for(:user)
36
+ post :create, :user => user_attributes
37
+ end
38
+
39
+ should_assign_to :user
40
+ should_change 'User.count', :by => 1
41
+
42
+ should "send the confirmation email" do
43
+ assert_sent_email do |email|
44
+ email.subject =~ /account confirmation/i
45
+ end
46
+ end
47
+
48
+ should_set_the_flash_to /confirm/i
49
+ should_redirect_to_url_after_create
50
+ end
51
+ end
52
+
53
+ signed_in_user_context do
54
+ context "GET to new" do
55
+ setup { get :new }
56
+ should_redirect_to("the home page") { root_url }
57
+ end
58
+
59
+ context "POST to create" do
60
+ setup { post :create, :user => {} }
61
+ should_redirect_to("the home page") { root_url }
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class ClearanceMailerTest < ActiveSupport::TestCase
4
+
5
+ context "A change password email" do
6
+ setup do
7
+ @user = Factory(:user)
8
+ @email = ClearanceMailer.create_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
23
+ end
24
+
25
+ should "set its subject" do
26
+ assert_match /Change your password/, @email.subject
27
+ end
28
+ end
29
+
30
+ context "A confirmation email" do
31
+ setup do
32
+ @user = Factory(:user)
33
+ @email = ClearanceMailer.create_confirmation @user
34
+ end
35
+
36
+ should "be from DO_NOT_REPLY" do
37
+ assert_match /#{@email.from[0]}/i, Clearance.configuration.mailer_sender
38
+ end
39
+
40
+ should "be sent to user" do
41
+ assert_match /#{@user.email}/i, @email.to.first
42
+ end
43
+
44
+ should "set its subject" do
45
+ assert_match /Account confirmation/, @email.subject
46
+ end
47
+
48
+ should "contain a link to confirm the user's account" do
49
+ host = ActionMailer::Base.default_url_options[:host]
50
+ regexp = %r{http://#{host}/users/#{@user.id}/confirmation/new\?token=#{@user.confirmation_token}}
51
+ assert_match regexp, @email.body
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,255 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+
5
+ # signing up
6
+
7
+ context "When signing up" do
8
+ should_validate_presence_of :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"
12
+
13
+ should "require password confirmation on create" do
14
+ user = Factory.build(:user, :password => 'blah',
15
+ :password_confirmation => 'boogidy')
16
+ assert ! user.save
17
+ assert user.errors.on(:password)
18
+ end
19
+
20
+ should "require non blank password confirmation on create" do
21
+ user = Factory.build(:user, :password => 'blah',
22
+ :password_confirmation => '')
23
+ assert ! user.save
24
+ assert user.errors.on(:password)
25
+ end
26
+
27
+ should "initialize salt" do
28
+ assert_not_nil Factory(:user).salt
29
+ end
30
+
31
+ should "initialize confirmation token" do
32
+ assert_not_nil Factory(:user).confirmation_token
33
+ end
34
+
35
+ context "encrypt password" do
36
+ setup do
37
+ @salt = "salt"
38
+ @user = Factory.build(:user, :salt => @salt)
39
+ def @user.initialize_salt; end
40
+ @user.save!
41
+ @password = @user.password
42
+
43
+ @user.send(:encrypt, @password)
44
+ @expected = Digest::SHA1.hexdigest("--#{@salt}--#{@password}--")
45
+ end
46
+
47
+ should "create an encrypted password using SHA1 encryption" do
48
+ assert_equal @expected, @user.encrypted_password
49
+ end
50
+ end
51
+
52
+ should "store email in exact case" do
53
+ user = Factory(:user, :email => "John.Doe@example.com")
54
+ assert_equal "John.Doe@example.com", user.email
55
+ end
56
+
57
+ should "send the confirmation email" do
58
+ assert_sent_email do |email|
59
+ email.subject =~ /account confirmation/i
60
+ end
61
+ end
62
+ end
63
+
64
+ context "When signing up with email already confirmed" do
65
+ setup do
66
+ ActionMailer::Base.deliveries.clear
67
+ Factory(:user, :email_confirmed => true)
68
+ end
69
+
70
+ should "not send the confirmation email" do
71
+ assert_did_not_send_email
72
+ end
73
+ end
74
+
75
+ context "When multiple users have signed up" do
76
+ setup { Factory(:user) }
77
+ should_validate_uniqueness_of :email
78
+ end
79
+
80
+ # confirming email
81
+
82
+ context "A user without email confirmation" do
83
+ setup do
84
+ @user = Factory(:user)
85
+ assert ! @user.email_confirmed?
86
+ end
87
+
88
+ context "after #confirm_email!" do
89
+ setup do
90
+ assert @user.confirm_email!
91
+ @user.reload
92
+ end
93
+
94
+ should "have confirmed their email" do
95
+ assert @user.email_confirmed?
96
+ end
97
+
98
+ should "reset confirmation token" do
99
+ assert_nil @user.confirmation_token
100
+ end
101
+ end
102
+ end
103
+
104
+ # authenticating
105
+
106
+ context "A user" do
107
+ setup do
108
+ @user = Factory(:user)
109
+ @password = @user.password
110
+ end
111
+
112
+ should "authenticate with good credentials" do
113
+ assert ::User.authenticate(@user.email, @password)
114
+ assert @user.authenticated?(@password)
115
+ end
116
+
117
+ should "not authenticate with bad credentials" do
118
+ assert ! ::User.authenticate(@user.email, 'bad_password')
119
+ assert ! @user.authenticated?('bad_password')
120
+ end
121
+ end
122
+
123
+ # resetting remember token
124
+
125
+ context "When resetting authentication with reset_remember_token!" do
126
+ setup do
127
+ @user = Factory(:email_confirmed_user)
128
+ @user.remember_token = "old-token"
129
+ @user.reset_remember_token!
130
+ end
131
+
132
+ should "change the remember token" do
133
+ assert_not_equal "old-token", @user.remember_token
134
+ end
135
+ end
136
+
137
+ # updating password
138
+
139
+ context "An email confirmed user" do
140
+ setup do
141
+ @user = Factory(:email_confirmed_user)
142
+ @old_encrypted_password = @user.encrypted_password
143
+ end
144
+
145
+ context "who updates password with confirmation" do
146
+ setup do
147
+ @user.update_password("new_password", "new_password")
148
+ end
149
+
150
+ should "change encrypted password" do
151
+ assert_not_equal @user.encrypted_password,
152
+ @old_encrypted_password
153
+ end
154
+ end
155
+ end
156
+
157
+ should "not generate the same remember token for users with the same password at the same time" do
158
+ Time.stubs(:now => Time.now)
159
+ password = 'secret'
160
+ first_user = Factory(:email_confirmed_user,
161
+ :password => password,
162
+ :password_confirmation => password)
163
+ second_user = Factory(:email_confirmed_user,
164
+ :password => password,
165
+ :password_confirmation => password)
166
+
167
+ assert_not_equal first_user.remember_token, second_user.remember_token
168
+ end
169
+
170
+ # recovering forgotten password
171
+
172
+ context "An email confirmed user" do
173
+ setup do
174
+ @user = Factory(:email_confirmed_user)
175
+ @old_encrypted_password = @user.encrypted_password
176
+ @user.confirm_email!
177
+ end
178
+
179
+ context "who requests password reminder" do
180
+ setup do
181
+ assert_nil @user.confirmation_token
182
+ @user.forgot_password!
183
+ end
184
+
185
+ should "generate confirmation token" do
186
+ assert_not_nil @user.confirmation_token
187
+ end
188
+
189
+ context "and then updates password" do
190
+ context 'with confirmation' do
191
+ setup do
192
+ @user.update_password("new_password", "new_password")
193
+ end
194
+
195
+ should "change encrypted password" do
196
+ assert_not_equal @user.encrypted_password,
197
+ @old_encrypted_password
198
+ end
199
+
200
+ should "clear confirmation token" do
201
+ assert_nil @user.confirmation_token
202
+ end
203
+ end
204
+
205
+ context 'without confirmation' do
206
+ setup do
207
+ @user.update_password("new_password", "")
208
+ end
209
+
210
+ should "not change encrypted password" do
211
+ assert_equal @user.encrypted_password,
212
+ @old_encrypted_password
213
+ end
214
+
215
+ should "not clear confirmation token" do
216
+ assert_not_nil @user.confirmation_token
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ end
223
+
224
+ # optional email/password fields
225
+ context "a user with an optional email" do
226
+ setup do
227
+ @user = User.new
228
+ class << @user
229
+ def email_optional?
230
+ true
231
+ end
232
+ end
233
+ end
234
+
235
+ subject { @user }
236
+
237
+ should_allow_values_for :email, nil, ""
238
+ end
239
+
240
+ context "a user with an optional password" do
241
+ setup do
242
+ @user = User.new
243
+ class << @user
244
+ def password_optional?
245
+ true
246
+ end
247
+ end
248
+ end
249
+
250
+ subject { @user }
251
+
252
+ should_allow_values_for :password, nil, ""
253
+ end
254
+
255
+ end