From a4ce60407ecf717225351565b66f863401ce2172 Mon Sep 17 00:00:00 2001 From: piratefinn Date: Mon, 24 Apr 2017 17:42:07 +0100 Subject: [PATCH] Did test and routing fixes and dependency fix --- cpanfile | 2 +- lib/Pear/LocalLoop.pm | 21 ++++----- lib/Pear/LocalLoop/Controller/Admin.pm | 21 +++++++-- lib/Pear/LocalLoop/Controller/Root.pm | 6 +-- script/deploy_db | 2 +- t/admin/login.t | 53 +++++++---------------- t/basic.t | 2 +- t/search.t | 2 +- templates/admin/index.html.ep | 55 ++++++++++++++++++++++++ templates/layouts/admin.html.ep | 6 +-- templates/layouts/user.html.ep | 3 -- templates/root/index.html.ep | 59 +++----------------------- 12 files changed, 116 insertions(+), 116 deletions(-) create mode 100644 templates/admin/index.html.ep diff --git a/cpanfile b/cpanfile index 01c7fc2..4d40bc6 100644 --- a/cpanfile +++ b/cpanfile @@ -14,5 +14,5 @@ requires 'DBIx::Class::TimeStamp'; requires 'DBIx::Class::Schema::Loader'; requires 'SQL::Translator'; requires 'DateTime'; -requires 'DateTime::Format::Strptime'; +requires 'DateTime::Format::Strptime', "1.73"; requires 'DateTime::Format::SQLite'; diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index bfd43ce..b999724 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -104,10 +104,11 @@ sub startup { my $r = $self->routes; $r->get('/')->to('root#index'); - $r->post('/')->to('root#auth_login'); - $r->get('/register')->to('register#index'); - $r->post('/register')->to('register#register'); - $r->any('/logout')->to('root#auth_logout'); + $r->get('/admin')->to('admin#index'); + $r->post('/admin')->to('admin#auth_login'); +# $r->get('/register')->to('register#index'); +# $r->post('/register')->to('register#register'); + $r->any('/admin/logout')->to('admin#auth_logout'); my $api_public_get = $r->under('/api'); $api_public_get->get('/info/ages')->to('api-info#get_ages'); @@ -142,7 +143,7 @@ sub startup { my $admin_routes = $r->under('/admin')->to('admin#under'); - $admin_routes->get('/')->to('admin#home'); + $admin_routes->get('/home')->to('admin#home'); $admin_routes->get('/tokens')->to('admin-tokens#index'); $admin_routes->post('/tokens')->to('admin-tokens#create'); @@ -160,14 +161,14 @@ sub startup { $admin_routes->get('/organisations/pending/:id')->to('admin-organisations#pending_read'); $admin_routes->get('/organisations/pending/:id/approve')->to('admin-organisations#pending_approve'); - my $user_routes = $r->under('/')->to('root#under'); +# my $user_routes = $r->under('/')->to('root#under'); - $user_routes->get('/home')->to('root#home'); +# $user_routes->get('/home')->to('root#home'); - my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under'); +# my $portal_api = $r->under('/portal')->to('api-auth#check_json')->under('/')->to('portal#under'); - $portal_api->post('/upload')->to('api-upload#post_upload'); - $portal_api->post('/search')->to('api-upload#post_search'); +# $portal_api->post('/upload')->to('api-upload#post_upload'); +# $portal_api->post('/search')->to('api-upload#post_search'); $self->hook( before_dispatch => sub { my $self = shift; diff --git a/lib/Pear/LocalLoop/Controller/Admin.pm b/lib/Pear/LocalLoop/Controller/Admin.pm index b7bb204..005707b 100644 --- a/lib/Pear/LocalLoop/Controller/Admin.pm +++ b/lib/Pear/LocalLoop/Controller/Admin.pm @@ -6,10 +6,8 @@ sub under { if ( $c->is_user_authenticated ) { return 1 if defined $c->current_user->administrator; - $c->redirect_to('/home'); - } else { - $c->redirect_to('/'); } + $c->redirect_to('/'); return undef; } @@ -31,4 +29,21 @@ sub home { ); } +sub auth_login { + my $c = shift; + + if ( $c->authenticate($c->param('email'), $c->param('password')) ) { + $c->redirect_to('/admin/home'); + } else { + $c->redirect_to('/admin'); + } +} + +sub auth_logout { + my $c = shift; + + $c->logout; + $c->redirect_to('/admin'); +} + 1; diff --git a/lib/Pear/LocalLoop/Controller/Root.pm b/lib/Pear/LocalLoop/Controller/Root.pm index 4b5d333..ef7b4d3 100644 --- a/lib/Pear/LocalLoop/Controller/Root.pm +++ b/lib/Pear/LocalLoop/Controller/Root.pm @@ -4,9 +4,9 @@ use Mojo::Base 'Mojolicious::Controller'; sub index { my $c = shift; - if ( $c->is_user_authenticated ) { - $c->redirect_to('/home'); - } +# if ( $c->is_user_authenticated ) { +# $c->redirect_to('/home'); +# } } sub under { diff --git a/script/deploy_db b/script/deploy_db index bce12d8..9354a6d 100755 --- a/script/deploy_db +++ b/script/deploy_db @@ -21,7 +21,7 @@ $schema->resultset('AgeRange')->populate([ [ '50+' ], ]); -if ( $ENV{MOJO_MODE} eq 'development' ) { +if (defined $ENV{MOJO_MODE} && $ENV{MOJO_MODE} eq 'development' ) { $schema->resultset('User')->create({ email => 'test@example.com', diff --git a/t/admin/login.t b/t/admin/login.t index 1f0eeff..1f7211a 100644 --- a/t/admin/login.t +++ b/t/admin/login.t @@ -1,29 +1,12 @@ -use strict; -use warnings; +use Mojo::Base -strict; -use FindBin qw/ $Bin /; -use lib "$Bin/../../lib"; - -use File::Temp; use Test::More; -use Test::Mojo; -use DateTime; -my $file = File::Temp->new; +use Mojo::JSON; +use Test::Pear::LocalLoop; -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 $framework = Test::Pear::LocalLoop->new; +my $t = $framework->framework; my $schema = $t->app->schema; -$schema->deploy; $schema->resultset('User')->create({ email => 'admin@example.com', @@ -47,46 +30,42 @@ my $location_is = sub { }; $t->get_ok('/admin') - ->status_is(302) - ->$location_is('/'); + ->status_is(200)->or($framework->dump_error); $t->ua->max_redirects(10); -$t->post_ok('/', form => { +$t->post_ok('/admin', form => { email => 'user@example.com', password => 'abc123', })->status_is(200); $t->ua->max_redirects(0); $t->get_ok('/admin') - ->status_is(302) - ->$location_is('/home'); + ->status_is(200); -$t->get_ok('/logout') +$t->get_ok('/admin/logout') ->status_is(302) - ->$location_is('/'); + ->$location_is('/admin'); $t->get_ok('/admin') - ->status_is(302) - ->$location_is('/', 'Logged out'); + ->status_is(200); $t->ua->max_redirects(10); -$t->post_ok('/', form => { +$t->post_ok('/admin', form => { email => 'admin@example.com', password => 'abc123', })->status_is(200); $t->ua->max_redirects(0); -$t->get_ok('/admin') +$t->get_ok('/admin/home') ->status_is(200) ->content_like(qr/Admin/); -$t->get_ok('/logout') +$t->get_ok('/admin/logout') ->status_is(302) - ->$location_is('/'); + ->$location_is('/admin'); $t->get_ok('/admin') - ->status_is(302) - ->$location_is('/', 'Logged out'); + ->status_is(200); done_testing; diff --git a/t/basic.t b/t/basic.t index 3a84078..9a14ba3 100644 --- a/t/basic.t +++ b/t/basic.t @@ -10,6 +10,6 @@ BEGIN { } my $t = Test::Mojo->new("Pear::LocalLoop"); -$t->get_ok('/')->status_is(200)->content_like(qr/login/i); +$t->get_ok('/')->status_is(200); done_testing(); diff --git a/t/search.t b/t/search.t index b0aa1a8..d8f0f5c 100644 --- a/t/search.t +++ b/t/search.t @@ -38,7 +38,7 @@ my $testJson = { 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(200)->or($dump_error) + ->status_is(200)->or($framework->dump_error) ->json_is('/success', Mojo::JSON->true); #test with an organisation. diff --git a/templates/admin/index.html.ep b/templates/admin/index.html.ep new file mode 100644 index 0000000..d540396 --- /dev/null +++ b/templates/admin/index.html.ep @@ -0,0 +1,55 @@ +% layout 'default'; +% title 'Login'; +% content_for css => begin + +% end +% content_for javascript => begin +% end +
+ % if ( my $error = flash 'error' ) { + + % } elsif ( my $success = flash 'success' ) { + + % } +
+ +
+ + +
+
+
+ diff --git a/templates/layouts/admin.html.ep b/templates/layouts/admin.html.ep index 213b6ef..f1eef4f 100644 --- a/templates/layouts/admin.html.ep +++ b/templates/layouts/admin.html.ep @@ -25,14 +25,14 @@ - LocalLoop Admin + LocalLoop Admin diff --git a/templates/layouts/user.html.ep b/templates/layouts/user.html.ep index 76f7dd3..0c03be2 100644 --- a/templates/layouts/user.html.ep +++ b/templates/layouts/user.html.ep @@ -28,9 +28,6 @@ LocalLoop