From a78de6ea8e71001d93edca72fa892d9e9e413dee Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Sat, 8 Apr 2017 13:38:54 +0100 Subject: [PATCH] Added admin test and logout function --- cpanfile | 1 + lib/Pear/LocalLoop.pm | 1 + lib/Pear/LocalLoop/Controller/Root.pm | 8 +++ t/admin/login.t | 96 +++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 t/admin/login.t diff --git a/cpanfile b/cpanfile index 309701b..d3f50a4 100644 --- a/cpanfile +++ b/cpanfile @@ -12,4 +12,5 @@ requires 'DBIx::Class'; requires 'DBIx::Class::Schema::Loader'; requires 'SQL::Translator'; requires 'DateTime'; +requires 'DateTime::Format::SQLite'; requires 'TryCatch'; diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index f295faf..ccab394 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -49,6 +49,7 @@ sub startup { my $r = $self->routes; $r->any('/')->to('root#index'); + $r->any('/logout')->to('root#auth_logout'); $r->get('/admin')->to('admin#index'); $r->post('/admin')->to('admin#login'); my $api = $r->under('/api' => sub { diff --git a/lib/Pear/LocalLoop/Controller/Root.pm b/lib/Pear/LocalLoop/Controller/Root.pm index fb0ca18..8de9259 100644 --- a/lib/Pear/LocalLoop/Controller/Root.pm +++ b/lib/Pear/LocalLoop/Controller/Root.pm @@ -6,4 +6,12 @@ sub index { } +sub auth_logout { + my $c = shift; + + $c->logout; + $c->redirect_to('/'); +} + + 1; diff --git a/t/admin/login.t b/t/admin/login.t new file mode 100644 index 0000000..24ef175 --- /dev/null +++ b/t/admin/login.t @@ -0,0 +1,96 @@ +use strict; +use warnings; + +use FindBin qw/ $Bin /; +use lib "$Bin/../../lib"; + +use File::Temp; +use Test::More; +use Test::Mojo; +use DateTime; +my $file = File::Temp->new; + +print $file <<'END'; +{ + dsn => "dbi:SQLite::memory:", + user => undef, + pass => undef, +} +END +$file->seek( 0, SEEK_END ); + +$ENV{MOJO_CONFIG} = $file->filename; + +my $t = Test::Mojo->new('Pear::LocalLoop'); +my $schema = $t->app->schema; +$schema->deploy; + +$schema->resultset('User')->create({ + email => 'admin@example.com', + hashedpassword => $t->app->generate_hashed_password('abc123'), + administrator => {}, + joindate => DateTime->now, +}); + +$schema->resultset('User')->create({ + email => 'user@example.com', + hashedpassword => $t->app->generate_hashed_password('abc123'), + joindate => DateTime->now, +}); + +is $schema->resultset('User')->count, 2, 'Users Created'; +is $schema->resultset('Administrator')->count, 1, 'Admin Created'; + +my $location_is = sub { + my ($t, $value, $desc) = @_; + $desc ||= "Location: $value"; + local $Test::Builder::Level = $Test::Builder::Level + 1; + return $t->success(is($t->tx->res->headers->location, $value, $desc)); +}; + +$t->get_ok('/admin') + ->status_is(200) + ->content_like(qr/Login/); + +$t->ua->max_redirects(10); +$t->post_ok('/admin', form => { + email => 'user@example.com', + password => 'abc123', +})->status_is(200) + ->content_like(qr/Hello!/, 'Redirected to root as not an admin'); + +$t->ua->max_redirects(0); +$t->get_ok('/admin/home') + ->status_is(302) + ->$location_is('/'); + +$t->get_ok('/logout') + ->status_is(302) + ->$location_is('/'); + +$t->get_ok('/admin/home') + ->status_is(302) + ->$location_is('/', 'Logged out'); + +$t->ua->max_redirects(10); +$t->post_ok('/admin', form => { + email => 'admin@example.com', + password => 'abc123', +})->status_is(200) + ->content_like(qr/Admin/); + +$t->ua->max_redirects(0); +$t->get_ok('/admin/home') + ->status_is(200) + ->content_like(qr/Admin/); + +$t->get_ok('/logout') + ->status_is(302) + ->$location_is('/'); + +$t->get_ok('/admin/home') + ->status_is(302) + ->$location_is('/', 'Logged out'); + +done_testing; +