/
password.t
401 lines (366 loc) · 14.5 KB
/
password.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env perl -w
use 5.12.0;
use utf8;
use Test::More tests => 304;
#use Test::More 'no_plan';
use Plack::Test;
use HTTP::Request::Common;
use PGXN::Manager;
use PGXN::Manager::Router;
use HTTP::Message::PSGI;
use Test::XML;
use Test::XPath;
use MIME::Base64;
use lib 't/lib';
use TxnTest;
use XPathTest;
my $app = PGXN::Manager::Router->app;
my $mt = PGXN::Manager::Locale->accept('en');
my $uri = '/auth/account/password';
my $head = {
h1 => 'Change Your Password',
validate_form => '#passform',
page_title => 'Change your password',
};
# Connect without authenticating.
test_psgi $app => sub {
my $cb = shift;
ok my $res = $cb->(GET $uri), "GET $uri";
is $res->code, 401, 'Should get 401 response';
like $res->content, qr/Authorization required/,
'The body should indicate need for authentication';
};
# Connect as authenticated user.
test_psgi +PGXN::Manager::Router->app => sub {
my $cb = shift;
my $user = TxnTest->user;
my $req = GET $uri, Authorization => 'Basic ' . encode_base64("$user:****");
ok my $res = $cb->($req), "Get $uri with auth token";
ok $res->is_success, 'Response should be success';
is_well_formed_xml $res->content, 'The HTML should be well-formed';
my $tx = Test::XPath->new( xml => $res->content, is_html => 1 );
$req = PGXN::Manager::Request->new(req_to_psgi($req));
$req->env->{REMOTE_USER} = $user;
$req->env->{SCRIPT_NAME} = '/auth';
XPathTest->test_basics($tx, $req, $mt, $head);
$tx->ok('/html/body/div[@id="content"]', 'Look at the content', sub {
$tx->is('count(./*)', 3, '... It should have three subelements');
$tx->is(
'./p',
$mt->maketext(q{There's nothing better than the smell of a fresh password in the morning, don't you agree?}),
'... Intro paragraph should be set'
);
});
# Now examine the form.
$tx->ok('/html/body/div[@id="content"]/form[@id="passform"]', sub {
for my $attr (
[action => $req->uri_for('/account/password')],
[enctype => 'application/x-www-form-urlencoded; charset=UTF-8'],
[method => 'post']
) {
$tx->is(
"./\@$attr->[0]",
$attr->[1],
qq{... Its $attr->[0] attribute should be "$attr->[1]"},
);
}
$tx->is('count(./*)', 2, '... It should have two subelements');
$tx->ok('./fieldset', '... Test fieldset', sub {
$tx->is('./@id', 'accpass', '...... It should have the proper id');
$tx->is('count(./*)', 7, '...... It should have 7 subelements');
$tx->is(
'./legend',
$mt->maketext('Password'),
'...... Its legend should be correct'
);
my $i = 0;
for my $spec (
{
id => 'old_pass',
title => $mt->maketext(q{What's your current password?}),
label => $mt->maketext('Old Password'),
type => 'password',
class => 'required',
},
{
id => 'new_pass',
title => $mt->maketext(q{What would you like your new password to be?}),
label => $mt->maketext('New Password'),
type => 'password',
class => 'required',
},
{
id => 'new_pass2',
title => $mt->maketext(q{What was that again?}),
label => $mt->maketext('Verify Password'),
type => 'password',
class => 'required',
},
) {
++$i;
$tx->ok("./label[$i]", "...... Test $spec->{id} label", sub {
$_->is('./@for', $spec->{id}, '......... Check "for" attr' );
$_->is('./@title', $spec->{title}, '......... Check "title" attr' );
$_->is('./text()', $spec->{label}, '......... Check its value');
});
$tx->ok("./input[$i]", "...... Test $spec->{id} input", sub {
$_->is('./@id', $spec->{id}, '......... Check "id" attr' );
$_->is('./@name', $spec->{id}, '......... Check "name" attr' );
$_->is('./@type', $spec->{type}, '......... Check "type" attr' );
$_->is('./@title', $spec->{title}, '......... Check "title" attr' );
$_->is('./@class', $spec->{class}, '......... Check "class" attr' );
});
}
});
$tx->ok('./input[@type="submit"]', '... Test input', sub {
for my $attr (
[id => 'submit'],
[name => 'submit'],
[class => 'submit'],
[value => $mt->maketext('Ch-ch-ch-ch-change it!')],
) {
$_->is(
"./\@$attr->[0]",
$attr->[1],
qq{...... Its $attr->[0] attribute should be "$attr->[1]"},
);
}
});
});
};
# Okay, let's submit the form.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:****"),
Content => [
old_pass => '****',
new_pass => 'whatevs',
new_pass2 => 'whatevs',
],
)), "POST update to $uri";
ok $res->is_redirect, 'It should be a redirect response';
my $req = PGXN::Manager::Request->new(req_to_psgi($res->request));
is $res->headers->header('location'), $req->uri_for($uri),
"Should redirect to $uri";
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'whatevs'
)->[0], 'The password should have been changed';
});
};
# Try an update via an XMLHttpRequest.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:whatevs"),
'X-Requested-With' => 'XMLHttpRequest',
Content => [
old_pass => 'whatevs',
new_pass => 'dood',
new_pass2 => 'dood',
],
)), "POST XMLHttpRequest update to $uri";
ok $res->is_success, 'It should be a success';
is $res->content, 'Success', 'And the content should say so';
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password should have been changed';
});
};
# Awesome. Try mismatched passwords.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:dood"),
Content => [
old_pass => '****',
new_pass => 'whatevs',
new_pass2 => 'whatever',
],
)), "POST password mismatch update to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
# So check the content.
is_well_formed_xml $res->content, 'The HTML should be well-formed';
my $tx = Test::XPath->new( xml => $res->content, is_html => 1 );
my $req = PGXN::Manager::Request->new(req_to_psgi($res->request));
$req->env->{REMOTE_USER} = $user;
$req->env->{SCRIPT_NAME} = '/auth';
XPathTest->test_basics($tx, $req, $mt, $head);
# Make sure we've got an error message.
$tx->ok('/html/body/div[@id="content"]', 'Test the content', sub {
$tx->is('count(./*)', 4, '... It should have four subelements');
my $err = $mt->maketext(q{D'oh! The passwords you typed in don't match. Would you mind trying again? Thanks.});
$tx->is('./p[@class="error"]', $err, '... Error paragraph should be set');
});
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password should not have been changed';
});
};
# Try it again with an XMLHttpRequest.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:dood"),
'X-Requested-With' => 'XMLHttpRequest',
Content => [
old_pass => '****',
new_pass => 'whatevs',
new_pass2 => 'whatever',
],
)), "POST password mismatch XMLHttpRequest to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
my $err = $mt->maketext(q{D'oh! The passwords you typed in don't match. Would you mind trying again? Thanks.});
is $res->decoded_content, $err, 'And the content should say why';
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password still should not have been changed';
});
};
# Now try passwords that are too short.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:dood"),
Content => [
old_pass => '****',
new_pass => 'hi',
new_pass2 => 'hi',
],
)), "POST short passwords to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
# So check the content.
is_well_formed_xml $res->content, 'The HTML should be well-formed';
my $tx = Test::XPath->new( xml => $res->content, is_html => 1 );
my $req = PGXN::Manager::Request->new(req_to_psgi($res->request));
$req->env->{REMOTE_USER} = $user;
$req->env->{SCRIPT_NAME} = '/auth';
XPathTest->test_basics($tx, $req, $mt, $head);
# Make sure we've got an error message.
$tx->ok('/html/body/div[@id="content"]', 'Test the content', sub {
$tx->is('count(./*)', 4, '... It should have four subelements');
my $err = $mt->maketext(q{So sorry! Passwords must be at least four characters long.});
$tx->is('./p[@class="error"]', $err, '... Error paragraph should be set');
});
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password should not have been changed';
});
};
# Try it again with an XMLHttpRequest.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:dood"),
'X-Requested-With' => 'XMLHttpRequest',
Content => [
old_pass => '****',
new_pass => 'hi',
new_pass2 => 'hi',
],
)), "POST short passwords via XMLHttpRequest to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
my $err = $mt->maketext(q{So sorry! Passwords must be at least four characters long.});
is $res->decoded_content, $err, 'And the content should say why';
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password still should not have been changed';
});
};
# And finally, we have an invalid old password.
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:dood"),
Content => [
old_pass => '****',
new_pass => 'hihi',
new_pass2 => 'hihi',
],
)), "POST invalid old password to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
# So check the content.
is_well_formed_xml $res->content, 'The HTML should be well-formed';
my $tx = Test::XPath->new( xml => $res->content, is_html => 1 );
my $req = PGXN::Manager::Request->new(req_to_psgi($res->request));
$req->env->{REMOTE_USER} = $user;
$req->env->{SCRIPT_NAME} = '/auth';
XPathTest->test_basics($tx, $req, $mt, $head);
# Make sure we've got an error message.
$tx->ok('/html/body/div[@id="content"]', 'Test the content', sub {
$tx->is('count(./*)', 4, '... It should have four subelements');
my $err = $mt->maketext(q{I don't think that was really your existing password. Care to try again?});
$tx->is('./p[@class="error"]', $err, '... Error paragraph should be set');
});
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, 'dood'
)->[0], 'The password should not have been changed';
});
};
# Reset the transaction and try again with an XMLHttpRequest.
TxnTest->restart;
test_psgi $app => sub {
my $cb = shift;
my $user = TxnTest->user;
ok my $res = $cb->(POST(
$uri,
Authorization => 'Basic ' . encode_base64("$user:****"),
'X-Requested-With' => 'XMLHttpRequest',
Content => [
old_pass => 'dood',
new_pass => 'hihi',
new_pass2 => 'hihi',
],
)), "POST invalid old password via XMLHttpRequest to $uri";
ok !$res->is_success, 'It should not be a success';
is $res->code, 409, 'It should return code 409';
my $err = $mt->maketext(q{I don't think that was really your existing password. Care to try again?});
is $res->decoded_content, $err, 'And the content should say why';
# And the user's password should be updated.
PGXN::Manager->conn->run(sub {
ok $_->selectcol_arrayref(
'SELECT authenticate_user(?, ?)',
undef, $user, '****'
)->[0], 'The password still should not have been changed';
});
};