From 9865130666757adc4b70b4fc2b11c88f9e9c43a2 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 23 Aug 2017 18:42:19 +0100 Subject: [PATCH] Added user account stuff test and some fixes --- lib/Pear/LocalLoop/Controller/Admin/Users.pm | 78 +++++++++------ t/admin/user.t | 99 ++++++++++++++++++++ 2 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 t/admin/user.t diff --git a/lib/Pear/LocalLoop/Controller/Admin/Users.pm b/lib/Pear/LocalLoop/Controller/Admin/Users.pm index 296a0cf..d48e259 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Users.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Users.pm @@ -1,6 +1,8 @@ package Pear::LocalLoop::Controller::Admin::Users; use Mojo::Base 'Mojolicious::Controller'; +use Data::Dumper; + has result_set => sub { my $c = shift; return $c->schema->resultset('User'); @@ -32,16 +34,19 @@ sub edit { my $id = $c->param('id'); - if ( my $user = $c->result_set->find($id) ) { - $c->stash( user => $user ); - } else { + my $user; + + unless ( $user = $c->result_set->find($id) ) { $c->flash( error => 'No User found' ); - $c->redirect_to( '/admin/users/' . $id ); + return $c->redirect_to( '/admin/users/' . $id ); } my $validation = $c->validation; - $validation->required('email')->not_in_resultset( 'email', $user->id ); + my $not_myself_user_rs = $c->result_set->search({ + id => { "!=" => $user->id }, + }); + $validation->required('email')->email->not_in_resultset( 'email', $not_myself_user_rs ); $validation->required('postcode')->postcode; $validation->optional('new_password'); @@ -62,34 +67,51 @@ sub edit { if ( defined $user->customer_id ){ - $c->schema->txn_do( sub { - $user->customer->update({ - full_name => $validation->param('full_name'), - display_name => $validation->param('display_name'), - postcode => $validation->param('postcode'), + try { + $c->schema->txn_do( sub { + $user->customer->update({ + full_name => $validation->param('full_name'), + display_name => $validation->param('display_name'), + postcode => $validation->param('postcode'), + }); + $user->update({ + email => $validation->param('email'), + ( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ), + }); }); - $user->update({ - email => $validation->param('email'), - ( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ), - }); - }); - + } finally { + if ( @_ ) { + $c->flash( error => 'Something went wrong Updating the User' ); + $c->app->log->warn(Dumper @_); + } else { + $c->flash( success => 'Updated User' ); + }; + } } elsif ( defined $user->organisation_id ) { - $c->schema->txn_do( sub { - $user->organisation->update({ - name => $validation->param('name'), - street_name => $validation->param('street_name'), - town => $validation->param('town'), - postcode => $validation->param('postcode'), + try { + $c->schema->txn_do( sub { + $user->organisation->update({ + name => $validation->param('name'), + street_name => $validation->param('street_name'), + town => $validation->param('town'), + postcode => $validation->param('postcode'), + }); + $user->update({ + email => $validation->param('email'), + ( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ), + }); }); - $user->update({ - email => $validation->param('email'), - ( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ), - }); - }); - } + } finally { + if ( @_ ) { + $c->flash( error => 'Something went wrong Updating the User' ); + $c->app->log->warn(Dumper @_); + } else { + $c->flash( success => 'Updated User' ); + } + } + }; $c->redirect_to( '/admin/users/' . $id ); } diff --git a/t/admin/user.t b/t/admin/user.t new file mode 100644 index 0000000..86386e0 --- /dev/null +++ b/t/admin/user.t @@ -0,0 +1,99 @@ +use Mojo::Base -strict; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; + +my $framework = Test::Pear::LocalLoop->new; +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $user = $schema->resultset('User')->create({ + email => 'admin@example.com', + password => 'abc123', + administrator => {}, +}); + +is $schema->resultset('Administrator')->count, 1, 'Admin Created'; + +my $user1 = { + token => 'a', + full_name => 'Test User1', + display_name => 'Test User1', + email => 'test1@example.com', + postcode => 'LA1 1AA', + password => 'abc123', + year_of_birth => 2006, +}; + +my $org = { + token => 'e', + email => 'test50@example.com', + name => '7th Heaven', + street_name => 'Slums, Sector 7', + town => 'Midgar', + postcode => 'WC1E 6AD', + password => 'abc123', +}; + +$schema->resultset('AccountToken')->create({ name => $_->{token} }) + for ( $user1, $org ); + +$framework->register_customer($user1); + +$framework->register_organisation($org); + +#login to admin +$t->ua->max_redirects(10); +$t->post_ok('/admin', form => { + email => 'admin@example.com', + password => 'abc123', +})->status_is(200); + +#Read customer user +$t->get_ok('/admin/users/1/') + ->status_is(200); + +#Read organisation user +$t->get_ok('/admin/users/2/') + ->status_is(200); + +#Valid organisation user update +$t->post_ok('/admin/users/1/edit', form => { + email => 'test51@example.com', + new_password => 'abc123', + name => '7th Heaven', + street_name => 'Slums, Sector 7', + town => 'Midgar', + postcode => 'WC1E 6AD', +})->status_is(200)->content_like(qr/Updated User/); + +#Failed validation on organisation user from wrong email +$t->post_ok('/admin/users/1/edit', form => { + email => 'test55@example.com', + new_password => 'abc123', + name => '7th Heaven', + street_name => 'Slums, Sector 7', + town => 'Midgar', + postcode => 'WC1E 6AD', +})->content_like(qr/The validation has failed/); + +#Failed validation on organisation user from no postcode +$t->post_ok('/admin/users/1/edit', form => { + email => 'test50@example.com', + new_password => 'abc123', + name => '7th Heaven', + street_name => 'Slums, Sector 7', + town => 'Midgar', +})->content_like(qr/The validation has failed/); + +#Failed validation on organisation user from no street name +$t->post_ok('/admin/users/1/edit', form => { + email => 'test50@example.com', + new_password => 'abc123', + name => '7th Heaven', + town => 'Midgar', + postcode => 'WC1E 6AD', +})->content_like(qr/The validation has failed/); + +done_testing();