janova-clearance 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/CHANGELOG.md +278 -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/authentication.rb +131 -0
  44. data/lib/clearance/configuration.rb +25 -0
  45. data/lib/clearance/extensions/errors.rb +6 -0
  46. data/lib/clearance/extensions/rescue.rb +5 -0
  47. data/lib/clearance/routes.rb +49 -0
  48. data/lib/clearance/user.rb +207 -0
  49. data/lib/clearance.rb +7 -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 +260 -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/config/boot.rb +110 -0
  64. data/test/rails_root/config/environment.rb +17 -0
  65. data/test/rails_root/config/environments/development.rb +19 -0
  66. data/test/rails_root/config/environments/production.rb +1 -0
  67. data/test/rails_root/config/environments/test.rb +36 -0
  68. data/test/rails_root/config/initializers/clearance.rb +3 -0
  69. data/test/rails_root/config/initializers/inflections.rb +10 -0
  70. data/test/rails_root/config/initializers/mime_types.rb +5 -0
  71. data/test/rails_root/config/initializers/requires.rb +13 -0
  72. data/test/rails_root/config/initializers/time_formats.rb +4 -0
  73. data/test/rails_root/config/routes.rb +6 -0
  74. data/test/rails_root/features/step_definitions/web_steps.rb +259 -0
  75. data/test/rails_root/features/support/env.rb +47 -0
  76. data/test/rails_root/public/dispatch.rb +10 -0
  77. data/test/rails_root/script/create_project.rb +52 -0
  78. data/test/rails_root/test/functional/accounts_controller_test.rb +23 -0
  79. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +21 -0
  80. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/formtastic.rb +1236 -0
  81. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/justin_french/formtastic.rb +10 -0
  82. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/rails/init.rb +3 -0
  83. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/formtastic_spec.rb +2900 -0
  84. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/test_helper.rb +14 -0
  85. data/test/test_helper.rb +19 -0
  86. metadata +202 -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,260 @@
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)
9
+ should validate_presence_of(:password)
10
+ should allow_value("foo@example.co.uk").for(:email)
11
+ should allow_value("foo@example.com").for(:email)
12
+ should_not allow_value("foo@").for(:email)
13
+ should_not allow_value("foo@example..com").for(:email)
14
+ should_not allow_value("foo@.example.com").for(:email)
15
+ should_not allow_value("foo").for(:email)
16
+ should_not allow_value("example.com").for(:email)
17
+
18
+ should "require password confirmation on create" do
19
+ user = Factory.build(:user, :password => 'blah',
20
+ :password_confirmation => 'boogidy')
21
+ assert ! user.save
22
+ assert user.errors.on(:password)
23
+ end
24
+
25
+ should "require non blank password confirmation on create" do
26
+ user = Factory.build(:user, :password => 'blah',
27
+ :password_confirmation => '')
28
+ assert ! user.save
29
+ assert user.errors.on(:password)
30
+ end
31
+
32
+ should "initialize salt" do
33
+ assert_not_nil Factory(:user).salt
34
+ end
35
+
36
+ should "initialize confirmation token" do
37
+ assert_not_nil Factory(:user).confirmation_token
38
+ end
39
+
40
+ context "encrypt password" do
41
+ setup do
42
+ @salt = "salt"
43
+ @user = Factory.build(:user, :salt => @salt)
44
+ def @user.initialize_salt; end
45
+ @user.save!
46
+ @password = @user.password
47
+
48
+ @user.send(:encrypt, @password)
49
+ @expected = Digest::SHA1.hexdigest("--#{@salt}--#{@password}--")
50
+ end
51
+
52
+ should "create an encrypted password using SHA1 encryption" do
53
+ assert_equal @expected, @user.encrypted_password
54
+ end
55
+ end
56
+
57
+ should "store email in exact case" do
58
+ user = Factory(:user, :email => "John.Doe@example.com")
59
+ assert_equal "John.Doe@example.com", user.email
60
+ end
61
+
62
+ should "send the confirmation email" do
63
+ assert_sent_email do |email|
64
+ email.subject =~ /account confirmation/i
65
+ end
66
+ end
67
+ end
68
+
69
+ context "When signing up with email already confirmed" do
70
+ setup do
71
+ ActionMailer::Base.deliveries.clear
72
+ Factory(:user, :email_confirmed => true)
73
+ end
74
+
75
+ should "not send the confirmation email" do
76
+ assert_did_not_send_email
77
+ end
78
+ end
79
+
80
+ context "When multiple users have signed up" do
81
+ setup { Factory(:user) }
82
+ should_validate_uniqueness_of :email
83
+ end
84
+
85
+ # confirming email
86
+
87
+ context "A user without email confirmation" do
88
+ setup do
89
+ @user = Factory(:user)
90
+ assert ! @user.email_confirmed?
91
+ end
92
+
93
+ context "after #confirm_email!" do
94
+ setup do
95
+ assert @user.confirm_email!
96
+ @user.reload
97
+ end
98
+
99
+ should "have confirmed their email" do
100
+ assert @user.email_confirmed?
101
+ end
102
+
103
+ should "reset confirmation token" do
104
+ assert_nil @user.confirmation_token
105
+ end
106
+ end
107
+ end
108
+
109
+ # authenticating
110
+
111
+ context "A user" do
112
+ setup do
113
+ @user = Factory(:user)
114
+ @password = @user.password
115
+ end
116
+
117
+ should "authenticate with good credentials" do
118
+ assert ::User.authenticate(@user.email, @password)
119
+ assert @user.authenticated?(@password)
120
+ end
121
+
122
+ should "not authenticate with bad credentials" do
123
+ assert ! ::User.authenticate(@user.email, 'bad_password')
124
+ assert ! @user.authenticated?('bad_password')
125
+ end
126
+ end
127
+
128
+ # resetting remember token
129
+
130
+ context "When resetting authentication with reset_remember_token!" do
131
+ setup do
132
+ @user = Factory(:email_confirmed_user)
133
+ @user.remember_token = "old-token"
134
+ @user.reset_remember_token!
135
+ end
136
+
137
+ should "change the remember token" do
138
+ assert_not_equal "old-token", @user.remember_token
139
+ end
140
+ end
141
+
142
+ # updating password
143
+
144
+ context "An email confirmed user" do
145
+ setup do
146
+ @user = Factory(:email_confirmed_user)
147
+ @old_encrypted_password = @user.encrypted_password
148
+ end
149
+
150
+ context "who updates password with confirmation" do
151
+ setup do
152
+ @user.update_password("new_password", "new_password")
153
+ end
154
+
155
+ should "change encrypted password" do
156
+ assert_not_equal @user.encrypted_password,
157
+ @old_encrypted_password
158
+ end
159
+ end
160
+ end
161
+
162
+ should "not generate the same remember token for users with the same password at the same time" do
163
+ Time.stubs(:now => Time.now)
164
+ password = 'secret'
165
+ first_user = Factory(:email_confirmed_user,
166
+ :password => password,
167
+ :password_confirmation => password)
168
+ second_user = Factory(:email_confirmed_user,
169
+ :password => password,
170
+ :password_confirmation => password)
171
+
172
+ assert_not_equal first_user.remember_token, second_user.remember_token
173
+ end
174
+
175
+ # recovering forgotten password
176
+
177
+ context "An email confirmed user" do
178
+ setup do
179
+ @user = Factory(:email_confirmed_user)
180
+ @old_encrypted_password = @user.encrypted_password
181
+ @user.confirm_email!
182
+ end
183
+
184
+ context "who requests password reminder" do
185
+ setup do
186
+ assert_nil @user.confirmation_token
187
+ @user.forgot_password!
188
+ end
189
+
190
+ should "generate confirmation token" do
191
+ assert_not_nil @user.confirmation_token
192
+ end
193
+
194
+ context "and then updates password" do
195
+ context 'with confirmation' do
196
+ setup do
197
+ @user.update_password("new_password", "new_password")
198
+ end
199
+
200
+ should "change encrypted password" do
201
+ assert_not_equal @user.encrypted_password,
202
+ @old_encrypted_password
203
+ end
204
+
205
+ should "clear confirmation token" do
206
+ assert_nil @user.confirmation_token
207
+ end
208
+ end
209
+
210
+ context 'without confirmation' do
211
+ setup do
212
+ @user.update_password("new_password", "")
213
+ end
214
+
215
+ should "not change encrypted password" do
216
+ assert_equal @user.encrypted_password,
217
+ @old_encrypted_password
218
+ end
219
+
220
+ should "not clear confirmation token" do
221
+ assert_not_nil @user.confirmation_token
222
+ end
223
+ end
224
+ end
225
+ end
226
+
227
+ end
228
+
229
+ # optional email/password fields
230
+ context "a user with an optional email" do
231
+ setup do
232
+ @user = User.new
233
+ class << @user
234
+ def email_optional?
235
+ true
236
+ end
237
+ end
238
+ end
239
+
240
+ subject { @user }
241
+
242
+ should_allow_values_for :email, nil, ""
243
+ end
244
+
245
+ context "a user with an optional password" do
246
+ setup do
247
+ @user = User.new
248
+ class << @user
249
+ def password_optional?
250
+ true
251
+ end
252
+ end
253
+ end
254
+
255
+ subject { @user }
256
+
257
+ should_allow_values_for :password, nil, ""
258
+ end
259
+
260
+ end