diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index c2e5972..df43bf5 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -175,7 +175,6 @@ sub startup { $admin_routes->get('/users/:id')->to('admin-users#read'); $admin_routes->post('/users/:id')->to('admin-users#update'); $admin_routes->post('/users/:id/delete')->to('admin-users#delete'); - $admin_routes->post('/users/:id/edit')->to('admin-users#edit'); $admin_routes->get('/organisations')->to('admin-organisations#list'); $admin_routes->get('/organisations/add')->to('admin-organisations#add_org'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Users.pm b/lib/Pear/LocalLoop/Controller/Admin/Users.pm index 064ffba..803b68e 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Users.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Users.pm @@ -40,7 +40,7 @@ sub read { } } -sub edit { +sub update { my $c = shift; my $id = $c->param('id'); @@ -61,10 +61,10 @@ sub edit { $validation->required('postcode')->postcode; $validation->optional('new_password'); - if ( defined $user->customer_id ) { + if ( $user->type eq 'customer' ) { $validation->required('display_name'); $validation->required('full_name'); - } elsif ( defined $user->organisation_id ) { + } elsif ( $user->type eq 'organisation' ) { $validation->required('name'); $validation->required('street_name'); $validation->required('town'); @@ -73,15 +73,14 @@ sub edit { if ( $validation->has_error ) { $c->flash( error => 'The validation has failed' ); - $c->app->log->warn(Dumper $validation); return $c->redirect_to( '/admin/users/' . $id ); } - if ( defined $user->customer_id ){ + if ( $user->type eq 'customer' ){ try { $c->schema->txn_do( sub { - $user->customer->update({ + $user->entity->customer->update({ full_name => $validation->param('full_name'), display_name => $validation->param('display_name'), postcode => $validation->param('postcode'), @@ -100,11 +99,11 @@ sub edit { }; } } - elsif ( defined $user->organisation_id ) { + elsif ( $user->type eq 'organisation' ) { try { $c->schema->txn_do( sub { - $user->organisation->update({ + $user->entity->organisation->update({ name => $validation->param('name'), street_name => $validation->param('street_name'), town => $validation->param('town'), @@ -129,9 +128,4 @@ sub edit { $c->redirect_to( '/admin/users/' . $id ); } -sub update { - my $c = shift; - $c->redirect_to( '/admin/users' ); -} - 1; diff --git a/t/admin/user.t b/t/admin/user.t index 129dce6..58ba1e0 100644 --- a/t/admin/user.t +++ b/t/admin/user.t @@ -1,49 +1,17 @@ use Mojo::Base -strict; +use FindBin qw/ $Bin /; + use Test::More; -use Mojo::JSON; use Test::Pear::LocalLoop; -my $framework = Test::Pear::LocalLoop->new; +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); 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', - sector => 'A', - 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 => { @@ -51,27 +19,36 @@ $t->post_ok('/admin', form => { password => 'abc123', })->status_is(200); +$t->get_ok('/admin/users') + ->status_is(200) + ->or($framework->dump_error); + #Read customer user -$t->get_ok('/admin/users/2/') +$t->get_ok('/admin/users/1') ->status_is(200); #Read organisation user -$t->get_ok('/admin/users/3/') +$t->get_ok('/admin/users/5') ->status_is(200); #Valid customer user update -$t->post_ok('/admin/users/2/edit', form => { - email => 'test12@example.com', - new_password => 'abc123', - full_name => 'Test User1', - display_name => 'Test User1', - town => 'Midgar', - sector => 'A', - postcode => 'WC1E 6AD', -})->status_is(200)->content_like(qr/Updated User/); +$t->post_ok( + '/admin/users/1', + form => { + email => 'test12@example.com', + new_password => 'abc123', + full_name => 'Test User1', + display_name => 'Test User1', + town => 'Midgar', + sector => 'A', + postcode => 'WC1E 6AD', + }) + ->status_is(200) + ->or($framework->dump_error) + ->content_like(qr/Updated User/); #Failed validation on customer user from no postcode -$t->post_ok('/admin/users/2/edit', form => { +$t->post_ok('/admin/users/2', form => { email => 'test12@example.com', new_password => 'abc123', full_name => 'Test User1', @@ -81,7 +58,7 @@ $t->post_ok('/admin/users/2/edit', form => { })->content_like(qr/The validation has failed/); #Failed validation on customer user from no display name -$t->post_ok('/admin/users/2/edit', form => { +$t->post_ok('/admin/users/2', form => { email => 'test12@example.com', new_password => 'abc123', full_name => 'Test User1', @@ -91,7 +68,7 @@ $t->post_ok('/admin/users/2/edit', form => { })->content_like(qr/The validation has failed/); #Valid organisation user update -$t->post_ok('/admin/users/3/edit', form => { +$t->post_ok('/admin/users/5', form => { email => 'test51@example.com', new_password => 'abc123', name => '7th Heaven', @@ -102,7 +79,7 @@ $t->post_ok('/admin/users/3/edit', form => { })->status_is(200)->content_like(qr/Updated User/); #Failed validation on organisation user from no postcode -$t->post_ok('/admin/users/3/edit', form => { +$t->post_ok('/admin/users/5', form => { email => 'test50@example.com', new_password => 'abc123', name => '7th Heaven', @@ -112,7 +89,7 @@ $t->post_ok('/admin/users/3/edit', form => { })->content_like(qr/The validation has failed/); #Failed validation on organisation user from no street name -$t->post_ok('/admin/users/3/edit', form => { +$t->post_ok('/admin/users/5', form => { email => 'test50@example.com', new_password => 'abc123', name => '7th Heaven', diff --git a/templates/admin/users/read.html.ep b/templates/admin/users/read.html.ep index 8e6cb47..eafe62e 100644 --- a/templates/admin/users/read.html.ep +++ b/templates/admin/users/read.html.ep @@ -25,14 +25,14 @@
Leave blank unless you want to change their password