salted_login_generator 1.1.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,328 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= file_name %>_controller'
3
+ # ADD to fix advance_by_days= problem
4
+ require File.dirname(__FILE__) + '/../mocks/test/time'
5
+
6
+ # Raise errors beyond the default web-based presentation
7
+ class <%= class_name %>Controller; def rescue_action(e) raise e end; end
8
+
9
+ class <%= class_name %>ControllerTest < Test::Unit::TestCase
10
+
11
+ fixtures :<%= plural_name %>
12
+
13
+ def setup
14
+ super
15
+ @controller = <%= class_name %>Controller.new
16
+ @request = ActionController::TestRequest.new
17
+ @response = ActionController::TestResponse.new
18
+ end
19
+
20
+ # let's test our main index page
21
+ def test_index
22
+ get :index
23
+ assert_response :success
24
+ end
25
+
26
+
27
+ def test_auth_bob
28
+ @request.session['return-to'] = "/bogus/location"
29
+
30
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
31
+ assert_not_nil @response.session["<%= singular_name %>"]
32
+
33
+ assert_equal @bob, @response.session["<%= singular_name %>"]
34
+
35
+ assert_equal("http://#{@request.host}/bogus/location", @response.redirect_url)
36
+ end
37
+
38
+ def do_test_signup(bad_password, bad_email)
39
+ ActionMailer::Base.deliveries = []
40
+
41
+ @request.session['return-to'] = "/bogus/location"
42
+
43
+ if not bad_password and not bad_email
44
+ post :signup, "<%= singular_name %>" => { "login" => "newbob@test.com", "password" => "newpassword", "password_confirmation" => "newpassword" }
45
+ assert_nil session["<%= singular_name %>"]
46
+
47
+ assert_equal(@controller.url_for(:action => "login"), @response.redirect_url)
48
+ assert_equal 1, ActionMailer::Base.deliveries.size
49
+ mail = ActionMailer::Base.deliveries[0]
50
+ assert_equal "newbob@test.com", mail.to_addrs[0].to_s
51
+ # assert_match /login:\s+\w+\n/, mail.encoded
52
+ # assert_match /password:\s+\w+\n/, mail.encoded
53
+ mail.encoded =~ /key=(.*?)"/
54
+ key = $1
55
+
56
+ <%= singular_name %> = <%= class_name %>.find_by_login("newbob@test.com")
57
+ assert_not_nil <%= singular_name %>
58
+ assert_equal 0, <%= singular_name %>.verified
59
+
60
+ # First past the expiration.
61
+ Time.advance_by_days = 1
62
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
63
+ Time.advance_by_days = 0
64
+ <%= singular_name %> = <%= class_name %>.find_by_login("newbob@test.com")
65
+ assert_equal 0, <%= singular_name %>.verified
66
+
67
+ # Then a bogus key.
68
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "boguskey"
69
+ <%= singular_name %> = <%= class_name %>.find_by_login("newbob@test.com")
70
+ assert_equal 0, <%= singular_name %>.verified
71
+
72
+ # Now the real one.
73
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
74
+ <%= singular_name %> = <%= class_name %>.find_by_login("newbob@test.com")
75
+ assert_equal 1, <%= singular_name %>.verified
76
+
77
+ post :login, "<%= singular_name %>" => { "login" => "newbob@test.com", "password" => "newpassword" }
78
+ assert_not_nil session["<%= singular_name %>"]
79
+ get :logout
80
+ elsif bad_password
81
+ post :signup, "<%= singular_name %>" => { "login" => "newbob@test.com", "password" => "bad", "password_confirmation" => "bad"}
82
+ assert_nil session["<%= singular_name %>"]
83
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:password))
84
+
85
+ assert_response(:success)
86
+ assert_equal 0, ActionMailer::Base.deliveries.size
87
+ elsif bad_email
88
+ ActionMailer::Base.inject_one_error = true
89
+ post :signup, "<%= singular_name %>" => { "login" => "newbob@test.com", "password" => "newpassword", "password_confirmation" => "newpassword" }
90
+ assert_nil session["<%= singular_name %>"]
91
+ assert_equal 0, ActionMailer::Base.deliveries.size
92
+ else
93
+ # Invalid test case
94
+ assert false
95
+ end
96
+ end
97
+
98
+ def test_edit
99
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
100
+ assert_not_nil session["<%= singular_name %>"]
101
+
102
+ post :edit, "<%= singular_name %>" => { "firstname" => "Bob", "form" => "edit" }
103
+ assert_equal @response.session["<%= singular_name %>"].firstname, "Bob"
104
+
105
+ post :edit, "<%= singular_name %>" => { "firstname" => "", "form" => "edit" }
106
+ assert_equal @response.session["<%= singular_name %>"].firstname, ""
107
+
108
+ get :logout
109
+ end
110
+
111
+ def test_delete
112
+ ActionMailer::Base.deliveries = []
113
+
114
+ # Immediate delete
115
+ post :login, "<%= singular_name %>" => { "login" => "deletebob1@test.com", "password" => "alongtest" }
116
+ assert_not_nil session["<%= singular_name %>"]
117
+
118
+ <%= class_name %>System::CONFIG[:delayed_delete] = false
119
+ post :edit, "<%= singular_name %>" => { "form" => "delete" }
120
+ assert_equal 1, ActionMailer::Base.deliveries.size
121
+
122
+ assert_nil session["<%= singular_name %>"]
123
+ post :login, "<%= singular_name %>" => { "login" => "deletebob1@test.com", "password" => "alongtest" }
124
+ assert_nil session["<%= singular_name %>"]
125
+
126
+ # Now try delayed delete
127
+ ActionMailer::Base.deliveries = []
128
+
129
+ post :login, "<%= singular_name %>" => { "login" => "deletebob2@test.com", "password" => "alongtest" }
130
+ assert_not_nil session["<%= singular_name %>"]
131
+
132
+ <%= class_name %>System::CONFIG[:delayed_delete] = true
133
+ post :edit, "<%= singular_name %>" => { "form" => "delete" }
134
+ assert_equal 1, ActionMailer::Base.deliveries.size
135
+ mail = ActionMailer::Base.deliveries[0]
136
+ mail.encoded =~ /<%= singular_name %>\[id\]=(.*?)&key=(.*?)"/
137
+ id = $1
138
+ key = $2
139
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "badkey"
140
+ assert_nil session["<%= singular_name %>"]
141
+
142
+ # Advance the time past the delete date
143
+ Time.advance_by_days = <%= class_name %>System::CONFIG[:delayed_delete_days]
144
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "#{key}"
145
+ assert_nil session["<%= singular_name %>"]
146
+
147
+ Time.advance_by_days = 0
148
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "#{key}"
149
+ assert_not_nil session["<%= singular_name %>"]
150
+ get :logout
151
+ end
152
+
153
+ def test_signup
154
+ do_test_signup(true, false)
155
+ do_test_signup(false, true)
156
+ do_test_signup(false, false)
157
+ end
158
+
159
+ def do_change_password(bad_password, bad_email)
160
+ ActionMailer::Base.deliveries = []
161
+
162
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
163
+ assert_not_nil session["<%= singular_name %>"]
164
+
165
+ if not bad_password and not bad_email
166
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
167
+ assert_equal 1, ActionMailer::Base.deliveries.size
168
+ mail = ActionMailer::Base.deliveries[0]
169
+ assert_equal "bob@test.com", mail.to_addrs[0].to_s
170
+ # assert_match /login:\s+\w+\n/, mail.encoded
171
+ # assert_match /password:\s+\w+\n/, mail.encoded
172
+ elsif bad_password
173
+ post :change_password, "<%= singular_name %>" => { "password" => "bad", "password_confirmation" => "bad" }
174
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:password))
175
+ assert_response(:success)
176
+ assert_equal 0, ActionMailer::Base.deliveries.size
177
+ elsif bad_email
178
+ ActionMailer::Base.inject_one_error = true
179
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
180
+ assert_equal 0, ActionMailer::Base.deliveries.size
181
+ else
182
+ # Invalid test case
183
+ assert false
184
+ end
185
+
186
+ get :logout
187
+ assert_nil session["<%= singular_name %>"]
188
+
189
+ if not bad_password and not bad_email
190
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "changed_password" }
191
+ assert_not_nil session["<%= singular_name %>"]
192
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
193
+ get :logout
194
+ end
195
+
196
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
197
+ assert_not_nil session["<%= singular_name %>"]
198
+
199
+ get :logout
200
+ end
201
+
202
+ def test_change_password0
203
+ do_change_password(false, false)
204
+ end
205
+ def test_change_password1
206
+ do_change_password(true, false)
207
+ end
208
+ def test_change_password2
209
+ do_change_password(false, true)
210
+ end
211
+
212
+ def do_forgot_password(bad_address, bad_email, logged_in)
213
+ ActionMailer::Base.deliveries = []
214
+
215
+ if logged_in
216
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
217
+ assert_not_nil session["<%= singular_name %>"]
218
+ end
219
+
220
+ @request.session['return-to'] = "/bogus/location"
221
+ if not bad_address and not bad_email
222
+ post :forgot_password, "<%= singular_name %>" => { "login" => "bob@test.com" }
223
+ password = "anewpassword"
224
+ if logged_in
225
+ assert_equal 0, ActionMailer::Base.deliveries.size
226
+ assert_equal(@controller.url_for(:action => "change_password"), @response.redirect_url)
227
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}" }
228
+ else
229
+ assert_equal 1, ActionMailer::Base.deliveries.size
230
+ mail = ActionMailer::Base.deliveries[0]
231
+ assert_equal "bob@test.com", mail.to_addrs[0].to_s
232
+ mail.encoded =~ /<%= singular_name %>\[id\]=(.*?)&key=(.*?)"/
233
+ id = $1
234
+ key = $2
235
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}", "id" => "#{id}" }, "key" => "#{key}"
236
+ assert_not_nil session["<%= singular_name %>"]
237
+ get :logout
238
+ end
239
+ elsif bad_address
240
+ post :forgot_password, "<%= singular_name %>" => { "login" => "bademail@test.com" }
241
+ assert_equal 0, ActionMailer::Base.deliveries.size
242
+ elsif bad_email
243
+ ActionMailer::Base.inject_one_error = true
244
+ post :forgot_password, "<%= singular_name %>" => { "login" => "bob@test.com" }
245
+ assert_equal 0, ActionMailer::Base.deliveries.size
246
+ else
247
+ # Invalid test case
248
+ assert false
249
+ end
250
+
251
+ if not bad_address and not bad_email
252
+ if logged_in
253
+ get :logout
254
+ else
255
+ assert_equal(@controller.url_for(:action => "login"), @response.redirect_url)
256
+ end
257
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "#{password}" }
258
+ else
259
+ # Okay, make sure the database did not get changed
260
+ if logged_in
261
+ get :logout
262
+ end
263
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
264
+ end
265
+
266
+ assert_not_nil session["<%= singular_name %>"]
267
+
268
+ # Put the old settings back
269
+ if not bad_address and not bad_email
270
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
271
+ end
272
+
273
+ get :logout
274
+ end
275
+
276
+ def test_forgot_password
277
+ do_forgot_password(false, false, false)
278
+ do_forgot_password(false, false, true)
279
+ do_forgot_password(true, false, false)
280
+ do_forgot_password(false, true, false)
281
+ end
282
+
283
+ def test_bad_signup
284
+ @request.session['return-to'] = "/bogus/location"
285
+
286
+ post :signup, "<%= singular_name %>" => { "login" => "newbob@test.com", "password" => "newpassword", "password_confirmation" => "wrong" }
287
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:password))
288
+ assert_response(:success)
289
+
290
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "newpassword" }
291
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:login))
292
+ assert_response(:success)
293
+
294
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "wrong" }
295
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:login))
296
+ assert(find_record_in_template("<%= singular_name %>").errors.invalid?(:password))
297
+ assert_response(:success)
298
+ end
299
+
300
+ def test_invalid_login
301
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "not_correct" }
302
+
303
+ assert_nil session["<%= singular_name %>"]
304
+
305
+ assert(@response.has_template_object?("login"))
306
+ end
307
+
308
+ def test_login_logoff
309
+
310
+ post :login, "<%= singular_name %>" => { "login" => "bob@test.com", "password" => "atest" }
311
+ assert_not_nil session["<%= singular_name %>"]
312
+
313
+ get :logout
314
+ assert_nil session["<%= singular_name %>"]
315
+
316
+ end
317
+
318
+ # deprecated assertions
319
+ def find_record_in_template(key = nil)
320
+ assert_not_nil assigns(key)
321
+ record = @response.template_objects[key]
322
+
323
+ assert_not_nil(record)
324
+ assert_kind_of ActiveRecord::Base, record
325
+
326
+ return record
327
+ end
328
+ end
@@ -0,0 +1,303 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= file_name %>_controller'
3
+
4
+ # Raise errors beyond the default web-based presentation
5
+ class <%= class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= class_name %>ControllerTest < Test::Unit::TestCase
8
+
9
+ fixtures :<%= plural_name %>
10
+
11
+ def setup
12
+ super
13
+ @controller = <%= class_name %>Controller.new
14
+ @request = ActionController::TestRequest.new
15
+ @response = ActionController::TestResponse.new
16
+ end
17
+
18
+ def test_auth_bob
19
+ @request.session['return-to'] = "/bogus/location"
20
+
21
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
22
+ assert_session_has "<%= singular_name %>"
23
+
24
+ assert_equal @bob, @response.session["<%= singular_name %>"]
25
+
26
+ assert_redirect_url "http://#{@request.host}/bogus/location"
27
+ end
28
+
29
+ def do_test_signup(bad_password, bad_email)
30
+ ActionMailer::Base.deliveries = []
31
+
32
+ @request.session['return-to'] = "/bogus/location"
33
+
34
+ if not bad_password and not bad_email
35
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
36
+ assert_session_has_no "<%= singular_name %>"
37
+
38
+ assert_redirect_url(@controller.url_for(:action => "login"))
39
+ assert_equal 1, ActionMailer::Base.deliveries.size
40
+ mail = ActionMailer::Base.deliveries[0]
41
+ assert_equal "newbob@test.com", mail.to_addrs[0].to_s
42
+ assert_match /login:\s+\w+\n/, mail.encoded
43
+ assert_match /password:\s+\w+\n/, mail.encoded
44
+ mail.encoded =~ /key=(.*?)"/
45
+ key = $1
46
+
47
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
48
+ assert_not_nil <%= singular_name %>
49
+ assert_equal 0, <%= singular_name %>.verified
50
+
51
+ # First past the expiration.
52
+ Time.advance_by_days = 1
53
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
54
+ Time.advance_by_days = 0
55
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
56
+ assert_equal 0, <%= singular_name %>.verified
57
+
58
+ # Then a bogus key.
59
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "boguskey"
60
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
61
+ assert_equal 0, <%= singular_name %>.verified
62
+
63
+ # Now the real one.
64
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
65
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
66
+ assert_equal 1, <%= singular_name %>.verified
67
+
68
+ post :login, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword" }
69
+ assert_session_has "<%= singular_name %>"
70
+ get :logout
71
+ elsif bad_password
72
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "bad", "password_confirmation" => "bad", "email" => "newbob@test.com" }
73
+ assert_session_has_no "<%= singular_name %>"
74
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
75
+ assert_success
76
+ assert_equal 0, ActionMailer::Base.deliveries.size
77
+ elsif bad_email
78
+ ActionMailer::Base.inject_one_error = true
79
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
80
+ assert_session_has_no "<%= singular_name %>"
81
+ assert_equal 0, ActionMailer::Base.deliveries.size
82
+ else
83
+ # Invalid test case
84
+ assert false
85
+ end
86
+ end
87
+
88
+ def test_edit
89
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
90
+ assert_session_has "<%= singular_name %>"
91
+
92
+ post :edit, "<%= singular_name %>" => { "firstname" => "Bob", "form" => "edit" }
93
+ assert_equal @response.session['<%= singular_name %>'].firstname, "Bob"
94
+
95
+ post :edit, "<%= singular_name %>" => { "firstname" => "", "form" => "edit" }
96
+ assert_equal @response.session['<%= singular_name %>'].firstname, ""
97
+
98
+ get :logout
99
+ end
100
+
101
+ def test_delete
102
+ ActionMailer::Base.deliveries = []
103
+
104
+ # Immediate delete
105
+ post :login, "<%= singular_name %>" => { "login" => "deletebob1", "password" => "alongtest" }
106
+ assert_session_has "<%= singular_name %>"
107
+
108
+ <%= class_name %>System::CONFIG[:delayed_delete] = false
109
+ post :edit, "<%= singular_name %>" => { "form" => "delete" }
110
+ assert_equal 1, ActionMailer::Base.deliveries.size
111
+
112
+ assert_session_has_no "<%= singular_name %>"
113
+ post :login, "<%= singular_name %>" => { "login" => "deletebob1", "password" => "alongtest" }
114
+ assert_session_has_no "<%= singular_name %>"
115
+
116
+ # Now try delayed delete
117
+ ActionMailer::Base.deliveries = []
118
+
119
+ post :login, "<%= singular_name %>" => { "login" => "deletebob2", "password" => "alongtest" }
120
+ assert_session_has "<%= singular_name %>"
121
+
122
+ <%= class_name %>System::CONFIG[:delayed_delete] = true
123
+ post :edit, "<%= singular_name %>" => { "form" => "delete" }
124
+ assert_equal 1, ActionMailer::Base.deliveries.size
125
+ mail = ActionMailer::Base.deliveries[0]
126
+ mail.encoded =~ /<%= singular_name%>\[id\]=(.*?)&key=(.*?)"/
127
+ id = $1
128
+ key = $2
129
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "badkey"
130
+ assert_session_has_no "<%= singular_name %>"
131
+
132
+ # Advance the time past the delete date
133
+ Time.advance_by_days = <%= class_name %>System::CONFIG[:delayed_delete_days]
134
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "#{key}"
135
+ assert_session_has_no "<%= singular_name %>"
136
+ Time.advance_by_days = 0
137
+
138
+ post :restore_deleted, "<%= singular_name %>" => { "id" => "#{id}" }, "key" => "#{key}"
139
+ assert_session_has "<%= singular_name %>"
140
+ get :logout
141
+ end
142
+
143
+ def test_signup
144
+ do_test_signup(true, false)
145
+ do_test_signup(false, true)
146
+ do_test_signup(false, false)
147
+ end
148
+
149
+ def do_change_password(bad_password, bad_email)
150
+ ActionMailer::Base.deliveries = []
151
+
152
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
153
+ assert_session_has "<%= singular_name %>"
154
+
155
+ if not bad_password and not bad_email
156
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
157
+ assert_equal 1, ActionMailer::Base.deliveries.size
158
+ mail = ActionMailer::Base.deliveries[0]
159
+ assert_equal "bob@test.com", mail.to_addrs[0].to_s
160
+ assert_match /login:\s+\w+\n/, mail.encoded
161
+ assert_match /password:\s+\w+\n/, mail.encoded
162
+ elsif bad_password
163
+ post :change_password, "<%= singular_name %>" => { "password" => "bad", "password_confirmation" => "bad" }
164
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
165
+ assert_success
166
+ assert_equal 0, ActionMailer::Base.deliveries.size
167
+ elsif bad_email
168
+ ActionMailer::Base.inject_one_error = true
169
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
170
+ assert_equal 0, ActionMailer::Base.deliveries.size
171
+ else
172
+ # Invalid test case
173
+ assert false
174
+ end
175
+
176
+ get :logout
177
+ assert_session_has_no "<%= singular_name %>"
178
+
179
+ if not bad_password and not bad_email
180
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "changed_password" }
181
+ assert_session_has "<%= singular_name %>"
182
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
183
+ get :logout
184
+ end
185
+
186
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
187
+ assert_session_has "<%= singular_name %>"
188
+
189
+ get :logout
190
+ end
191
+
192
+ def test_change_password
193
+ do_change_password(false, false)
194
+ do_change_password(true, false)
195
+ do_change_password(false, true)
196
+ end
197
+
198
+ def do_forgot_password(bad_address, bad_email, logged_in)
199
+ ActionMailer::Base.deliveries = []
200
+
201
+ if logged_in
202
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
203
+ assert_session_has "<%= singular_name %>"
204
+ end
205
+
206
+ @request.session['return-to'] = "/bogus/location"
207
+ if not bad_address and not bad_email
208
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bob@test.com" }
209
+ password = "anewpassword"
210
+ if logged_in
211
+ assert_equal 0, ActionMailer::Base.deliveries.size
212
+ assert_redirect_url(@controller.url_for(:action => "change_password"))
213
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}" }
214
+ else
215
+ assert_equal 1, ActionMailer::Base.deliveries.size
216
+ mail = ActionMailer::Base.deliveries[0]
217
+ assert_equal "bob@test.com", mail.to_addrs[0].to_s
218
+ mail.encoded =~ /<%= singular_name %>\[id\]=(.*?)&key=(.*?)"/
219
+ id = $1
220
+ key = $2
221
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}", "id" => "#{id}" }, "key" => "#{key}"
222
+ assert_session_has "<%= singular_name %>"
223
+ get :logout
224
+ end
225
+ elsif bad_address
226
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bademail@test.com" }
227
+ assert_equal 0, ActionMailer::Base.deliveries.size
228
+ elsif bad_email
229
+ ActionMailer::Base.inject_one_error = true
230
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bob@test.com" }
231
+ assert_equal 0, ActionMailer::Base.deliveries.size
232
+ else
233
+ # Invalid test case
234
+ assert false
235
+ end
236
+
237
+ if not bad_address and not bad_email
238
+ if logged_in
239
+ get :logout
240
+ else
241
+ assert_redirect_url(@controller.url_for(:action => "login"))
242
+ end
243
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "#{password}" }
244
+ else
245
+ # Okay, make sure the database did not get changed
246
+ if logged_in
247
+ get :logout
248
+ end
249
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
250
+ end
251
+
252
+ assert_session_has "<%= singular_name %>"
253
+
254
+ # Put the old settings back
255
+ if not bad_address and not bad_email
256
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
257
+ end
258
+
259
+ get :logout
260
+ end
261
+
262
+ def test_forgot_password
263
+ do_forgot_password(false, false, false)
264
+ do_forgot_password(false, false, true)
265
+ do_forgot_password(true, false, false)
266
+ do_forgot_password(false, true, false)
267
+ end
268
+
269
+ def test_bad_signup
270
+ @request.session['return-to'] = "/bogus/location"
271
+
272
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "wrong" }
273
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
274
+ assert_success
275
+
276
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "newpassword" }
277
+ assert_invalid_column_on_record "<%= singular_name %>", "login"
278
+ assert_success
279
+
280
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "wrong" }
281
+ assert_invalid_column_on_record "<%= singular_name %>", ["login", "password"]
282
+ assert_success
283
+ end
284
+
285
+ def test_invalid_login
286
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "not_correct" }
287
+
288
+ assert_session_has_no "<%= singular_name %>"
289
+
290
+ assert_template_has "login"
291
+ end
292
+
293
+ def test_login_logoff
294
+
295
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
296
+ assert_session_has "<%= singular_name %>"
297
+
298
+ get :logout
299
+ assert_session_has_no "<%= singular_name %>"
300
+
301
+ end
302
+
303
+ end