Skip to content

Commit 14e06d2

Browse files
committed
More tests!
1 parent a7e4063 commit 14e06d2

File tree

6 files changed

+177
-32
lines changed

6 files changed

+177
-32
lines changed

Web/lib/Test/Mojo/BlogDB.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ sub stash_has {
8989
my ( $t, $expect, $desc ) = @_;
9090

9191
cmp_deeply( $t->stash, superhashof($expect), $desc);
92+
93+
return $t;
9294
}
9395

9496
1;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
6+
7+
# Try creating a valid account, ensure it exists in the DB.
8+
$t->post_ok( '/register', form => {
9+
username => 'fred',
10+
email => 'fred@blog.com',
11+
password => 'SuperSecure',
12+
confirm => 'SuperSecure',
13+
})->status_is( 302
14+
)->code_block( sub {
15+
is( scalar(@{shift->stash->{errors}}), 0, 'No errors' );
16+
})->code_block( sub {
17+
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 1, 'User created.');
18+
})->get_ok( '/'
19+
)->code_block( sub {
20+
is(shift->stash->{person}->username, 'fred', 'Got the fred after login...');
21+
})->stash_has( { blogs => [ ]}, "Blog array ref exists.");
22+
23+
# New Session, verify it isn't logged in.
24+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
25+
26+
$t->get_ok( '/' )->code_block( sub { is(shift->stash->{person}, undef, "Not logged in.") });
27+
28+
$t->post_ok( '/login', form => { username => 'fred', password => 'SuperSecure'})
29+
->get_ok( '/')
30+
->code_block( sub { is(shift->stash->{person}->username, 'fred', 'Logged in')});
31+
32+
done_testing();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
6+
7+
# Try creating a valid account, ensure it exists in the DB.
8+
$t->post_ok( '/register', form => {
9+
username => 'fred',
10+
email => 'fred@blog.com',
11+
password => 'SuperSecure',
12+
confirm => 'SuperSecure',
13+
})->status_is( 302
14+
)->code_block( sub {
15+
is( scalar(@{shift->stash->{errors}}), 0, 'No errors' );
16+
})->code_block( sub {
17+
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 1, 'User created.');
18+
})->get_ok( '/'
19+
)->code_block( sub {
20+
is(shift->stash->{person}->username, 'fred', 'Got the fred after login...');
21+
})->stash_has( { blogs => [ ]}, "Blog array ref exists.");
22+
23+
# New Session, verify it isn't logged in.
24+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
25+
26+
$t->get_ok( '/' )->code_block( sub { is(shift->stash->{person}, undef, "Not logged in.") });
27+
28+
$t->post_ok( '/login', form => { username => 'fred', password => 'SuperSecure'})
29+
->get_ok( '/')
30+
->code_block( sub { is(shift->stash->{person}->username, 'fred', 'Logged in')});
31+
32+
$t->post_ok('/logout')
33+
->get_ok( '/')
34+
->code_block( sub { is(shift->stash->{person}, undef, 'User logged out.')});
35+
36+
done_testing();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
6+
7+
# Try creating a valid account, ensure it exists in the DB.
8+
$t->post_ok( '/register', form => {
9+
username => 'fred',
10+
email => 'fred@blog.com',
11+
password => 'SuperSecure',
12+
confirm => 'SuperSecure',
13+
})->status_is( 302
14+
)->code_block( sub {
15+
is( scalar(@{shift->stash->{errors}}), 0, 'No errors' );
16+
})->code_block( sub {
17+
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 1, 'User created.');
18+
})->get_ok( '/'
19+
)->code_block( sub {
20+
is(shift->stash->{person}->username, 'fred', 'Got the fred after login...');
21+
})->stash_has( { blogs => [ ]}, "Blog array ref exists.");
22+
23+
# New Session, verify it isn't logged in.
24+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
25+
26+
$t->get_ok( '/' )->code_block( sub { is(shift->stash->{person}, undef, "Not logged in.") });
27+
28+
$t->post_ok( '/forgot', form => { username => 'noone' })
29+
->status_is( 200 )
30+
->code_block( sub {
31+
is(shift->stash->{errors}->[0], 'No such username or email address.', 'Invalid addresses raise errors.');
32+
});
33+
34+
$t->post_ok( '/forgot', form => { username => 'fred' })
35+
->status_is( 200 )
36+
->stash_has( { success => 1}, "Finished setting token.")
37+
->code_block( sub {
38+
my $t = shift;
39+
ok my $fred = $t->app->db->resultset('Person')->find( { username => 'fred' }), "Found fred in DB.";
40+
ok my $reset_token = $fred->search_related('password_tokens')->first->token, "Found reset token in DB.";
41+
$t->stash( { %{$t->stash}, token => $reset_token } )
42+
});
43+
44+
warn "I have reset token: " . $t->stash->{token} . "\n";
45+
46+
done_testing();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
6+
7+
# Try creating a valid account, ensure it exists in the DB.
8+
$t->post_ok( '/register', form => {
9+
username => 'fred',
10+
email => 'fred@blog.com',
11+
password => 'SuperSecure',
12+
confirm => 'SuperSecure',
13+
})->status_is( 302
14+
)->code_block( sub {
15+
is( scalar(@{shift->stash->{errors}}), 0, 'No errors' );
16+
})->code_block( sub {
17+
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 1, 'User created.');
18+
})->get_ok( '/'
19+
)->code_block( sub {
20+
is(shift->stash->{person}->username, 'fred', 'Got the fred after login...');
21+
})->stash_has( { blogs => [ ]}, "Blog array ref exists.");
22+
23+
# New Session, verify it isn't logged in.
24+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
25+
$t->get_ok( '/' )->code_block( sub { is(shift->stash->{person}, undef, "Not logged in.") });
26+
27+
# Fill out the forgot password form to get a token.
28+
$t->post_ok( '/forgot', form => { username => 'fred' })
29+
->status_is( 200 )
30+
->stash_has( { success => 1}, "Finished setting token.")
31+
->code_block( sub {
32+
my $t = shift;
33+
ok my $fred = $t->app->db->resultset('Person')->find( { username => 'fred' }), "Found fred in DB.";
34+
ok my $reset_token = $fred->search_related('password_tokens')->first->token, "Found reset token in DB.";
35+
$t->stash( { %{$t->stash}, token => $reset_token } )
36+
});
37+
38+
my $reset_token = $t->stash->{token};
39+
40+
# Use the reset password form to reset the password.
41+
$t->post_ok( "/forgot/$reset_token", form => {
42+
reset_token => $reset_token,
43+
password => 'NewPassword',
44+
confirm => 'NewPassword',
45+
})->stash_has( { success => 1 });
46+
47+
# New Session, verify it isn't logged in.
48+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
49+
$t->get_ok( '/' )->code_block( sub { is(shift->stash->{person}, undef, "Not logged in.") });
50+
51+
# Prove the login doesn't work with the old password.
52+
$t->post_ok( '/login', form => { username => 'fred', password => 'SuperSecure'})
53+
->get_ok( '/')
54+
->code_block( sub { is( shift->stash->{person}, undef, "Old password doesn't work.")});
55+
56+
# Prove the login works with the new password.
57+
$t->post_ok( '/login', form => { username => 'fred', password => 'NewPassword'})
58+
->get_ok( '/')
59+
->code_block( sub { is( shift->stash->{person}->username, 'fred', "New password does work.")});
60+
61+
done_testing();

Web/t/01_index.t

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)