|
| 1 | +#!/usr/bin/env perl |
| 2 | +use Mojo::Base -strict; |
| 3 | +use Test::Mojo; |
| 4 | +use Test::More; |
| 5 | +use Test::Postgresql58; |
| 6 | +use DBI; |
| 7 | + |
| 8 | +my $t = Test::Mojo->new('BlogDB::Web'); |
| 9 | + |
| 10 | +$t->get_ok( '/' ); |
| 11 | + |
| 12 | +our $pgsql = Test::Postgresql58->new() |
| 13 | + or BAILOUT( "PSQL Error: " . $Test::Postgresql58::errstr ); |
| 14 | + |
| 15 | +load_psql_file("/home/symkat/Code/BlogDB/DB/etc/schema.sql"); |
| 16 | + |
| 17 | +# Create an account. |
| 18 | +$t->post_ok( '/register', form => { |
| 19 | + username => 'fred', |
| 20 | + email => 'fred@blog.com', |
| 21 | + password => 'SuperSecure', |
| 22 | + confirm => 'SuperSecure', |
| 23 | + })->status_is( 200 ); |
| 24 | + |
| 25 | +# Login works and redirects the user. |
| 26 | +$t->post_ok( '/login', form => { |
| 27 | + username => 'fred', |
| 28 | + password => 'SuperSecure', |
| 29 | + confirm => 'SuperSecure', |
| 30 | + return => '/', |
| 31 | + })->status_is( 302 ); |
| 32 | + |
| 33 | +# Login without account throws an error |
| 34 | +$t->post_ok( '/login', form => { |
| 35 | + username => 'james', |
| 36 | + password => 'NoPassword', |
| 37 | + confirm => 'NoPassword', |
| 38 | + return => '/', |
| 39 | + })->status_is( 500 ); |
| 40 | + |
| 41 | +done_testing(); |
| 42 | + |
| 43 | +sub load_psql_file { |
| 44 | + my ( $file ) = @_; |
| 45 | + |
| 46 | + open my $lf, "<", $file |
| 47 | + or die "Failed to open $file for reading: $!"; |
| 48 | + my $content; |
| 49 | + while ( defined( my $line = <$lf> ) ) { |
| 50 | + next unless $line !~ /^\s*--/; |
| 51 | + $content .= $line; |
| 52 | + } |
| 53 | + close $lf; |
| 54 | + |
| 55 | + my $dbh = DBI->connect( $pgsql->dsn ); |
| 56 | + for my $command ( split( /;/, $content ) ) { |
| 57 | + next if $command =~ /^\s*$/; |
| 58 | + $dbh->do( $command ) |
| 59 | + or BAIL_OUT( "PSQL Error($file): $command: " . $dbh->errstr); |
| 60 | + } |
| 61 | + undef $dbh; |
| 62 | +} |
0 commit comments