Fix admin organisation management tests

This commit is contained in:
Tom Bloor 2017-08-31 14:52:45 +01:00
parent 0159ef91dc
commit 92afe1c531
3 changed files with 56 additions and 133 deletions

View file

@ -179,12 +179,9 @@ sub startup {
$admin_routes->get('/organisations')->to('admin-organisations#list'); $admin_routes->get('/organisations')->to('admin-organisations#list');
$admin_routes->get('/organisations/add')->to('admin-organisations#add_org'); $admin_routes->get('/organisations/add')->to('admin-organisations#add_org');
$admin_routes->post('/organisations/add/submit')->to('admin-organisations#add_org_submit'); $admin_routes->post('/organisations/add')->to('admin-organisations#add_org_submit');
$admin_routes->get('/organisations/valid/:id')->to('admin-organisations#valid_read'); $admin_routes->get('/organisations/:id')->to('admin-organisations#valid_read');
$admin_routes->post('/organisations/valid/:id/edit')->to('admin-organisations#valid_edit'); $admin_routes->post('/organisations/:id')->to('admin-organisations#valid_edit');
$admin_routes->get('/organisations/pending/:id')->to('admin-organisations#pending_read');
$admin_routes->post('/organisations/pending/:id/edit')->to('admin-organisations#pending_edit');
$admin_routes->get('/organisations/pending/:id/approve')->to('admin-organisations#pending_approve');
$admin_routes->get('/feedback')->to('admin-feedback#index'); $admin_routes->get('/feedback')->to('admin-feedback#index');
$admin_routes->get('/feedback/:id')->to('admin-feedback#read'); $admin_routes->get('/feedback/:id')->to('admin-feedback#read');

View file

@ -2,7 +2,6 @@ package Pear::LocalLoop::Controller::Admin::Organisations;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use Try::Tiny; use Try::Tiny;
use Data::Dumper;
sub list { sub list {
my $c = shift; my $c = shift;
@ -33,35 +32,38 @@ sub add_org_submit {
if ( $validation->has_error ) { if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' ); $c->flash( error => 'The validation has failed' );
$c->app->log->warn(Dumper $validation); return $c->redirect_to( '/admin/organisations/add' );
return $c->redirect_to( '/admin/organisations/add/' );
} }
my $organisation; my $organisation;
try { try {
$organisation = $c->schema->resultset('Organisation')->create({ my $entity = $c->schema->resultset('Entity')->create({
name => $validation->param('name'), organisation => {
street_name => $validation->param('street_name'), name => $validation->param('name'),
town => $validation->param('town'), street_name => $validation->param('street_name'),
sector => $validation->param('sector'), town => $validation->param('town'),
postcode => $validation->param('postcode'), sector => $validation->param('sector'),
postcode => $validation->param('postcode'),
},
type => 'organisation',
}); });
$organisation = $entity->organisation;
} finally { } finally {
if ( @_ ) { if ( @_ ) {
$c->flash( error => 'Something went wrong Adding the Organisation' ); $c->flash( error => 'Something went wrong Adding the Organisation' );
$c->app->log->warn(Dumper @_); $c->redirect_to( '/admin/organisations/add' );
} else { } else {
$c->flash( success => 'Added Organisation' ); $c->flash( success => 'Added Organisation' );
$c->redirect_to( '/admin/organisations/' . $organisation->id);
} }
}; };
$c->redirect_to( '/admin/organisations/add/' );
} }
sub valid_read { sub valid_read {
my $c = shift; my $c = shift;
my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') ); my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') );
my $transactions = $valid_org->transactions->search( my $transactions = $valid_org->entity->sales->search(
undef, { undef, {
page => $c->param('page') || 1, page => $c->param('page') || 1,
rows => 10, rows => 10,
@ -86,8 +88,7 @@ sub valid_edit {
if ( $validation->has_error ) { if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' ); $c->flash( error => 'The validation has failed' );
$c->app->log->warn(Dumper $validation); return $c->redirect_to( '/admin/organisations/' . $c->param('id') );
return $c->redirect_to( '/admin/organisations/valid/' . $c->param('id') );
} }
my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') ); my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') );
@ -105,90 +106,11 @@ sub valid_edit {
} finally { } finally {
if ( @_ ) { if ( @_ ) {
$c->flash( error => 'Something went wrong Updating the Organisation' ); $c->flash( error => 'Something went wrong Updating the Organisation' );
$c->app->log->warn(Dumper @_);
} else { } else {
$c->flash( success => 'Updated Organisation' ); $c->flash( success => 'Updated Organisation' );
} }
}; };
$c->redirect_to( '/admin/organisations/valid/' . $valid_org->id ); $c->redirect_to( '/admin/organisations/' . $valid_org->id );
}
sub pending_read {
my $c = shift;
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );
my $transactions = $pending_org->transactions->search(
undef, {
page => $c->param('page') || 1,
rows => 10,
},
);
$c->stash(
pending_org => $pending_org,
transactions => $transactions,
);
}
sub pending_edit {
my $c = shift;
my $validation = $c->validation;
$validation->required('name');
$validation->required('street_name');
$validation->required('town');
$validation->required('postcode')->postcode;
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
$c->app->log->warn(Dumper $validation);
return $c->redirect_to( '/admin/organisations/pending/' . $c->param('id') );
}
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );
try {
$c->schema->storage->txn_do( sub {
$pending_org->update({
name => $validation->param('name'),
street_name => $validation->param('street_name'),
town => $validation->param('town'),
postcode => $validation->param('postcode'),
});
} );
} finally {
if ( @_ ) {
$c->flash( error => 'Something went wrong Updating the Organisation' );
$c->app->log->warn(Dumper @_);
} else {
$c->flash( success => 'Updated Organisation' );
}
};
$c->redirect_to( '/admin/organisations/pending/' . $pending_org->id );
}
sub pending_approve {
my $c = shift;
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );
my $valid_org;
try {
$c->schema->storage->txn_do( sub {
$valid_org = $c->schema->resultset('Organisation')->create({
name => $pending_org->name,
street_name => $pending_org->street_name,
town => $pending_org->town,
postcode => $pending_org->postcode,
});
$c->copy_transactions_and_delete( $pending_org, $valid_org );
} );
} finally {
if ( @_ ) {
$c->flash( error => 'Something went wrong Validating the Organisation' );
$c->redirect_to( '/admin/organisations/pending/' . $pending_org->id );
} else {
$c->flash( success => 'Validated Organisation' );
$c->redirect_to( '/admin/organisations/valid/' . $valid_org->id );
}
}
} }
1; 1;

View file

@ -1,38 +1,42 @@
use Mojo::Base -strict; use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More; use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop; 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 $t = $framework->framework;
my $schema = $t->app->schema; my $schema = $t->app->schema;
my $user = $schema->resultset('User')->create({ my $valid_entity = $schema->resultset('Entity')->create({
email => 'admin@example.com', organisation => {
password => 'abc123', name => 'Shinra Electric Power Company',
administrator => {}, street_name => 'Sector 0, Midgar, Eastern Continent',
town => 'Gaia',
sector => 'A',
postcode => 'WC1E 6AD',
},
type => "organisation",
}); });
is $schema->resultset('Administrator')->count, 1, 'Admin Created'; my $pending_entity = $schema->resultset('Entity')->create({
organisation => {
$schema->resultset('Organisation')->create({ name => '7th Heaven',
id => 1, street_name => 'Slums, Sector 7',
name => 'Shinra Electric Power Company', town => 'Midgar',
street_name => 'Sector 0, Midgar, Eastern Continent', sector => 'A',
town => 'Gaia', postcode => 'WC1E 6AD',
sector => 'A', pending => \"1",
postcode => 'WC1E 6AD', },
type => "organisation",
}); });
$schema->resultset('PendingOrganisation')->create({ my $valid_id = $valid_entity->organisation->id;
id => 2, my $pending_id = $pending_entity->organisation->id;
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
postcode => 'WC1E 6AD',
submitted_by_id => $user->id,
});
#login to admin #login to admin
$t->ua->max_redirects(10); $t->ua->max_redirects(10);
@ -42,15 +46,15 @@ $t->post_ok('/admin', form => {
})->status_is(200); })->status_is(200);
#Read approved organisation #Read approved organisation
$t->get_ok('/admin/organisations/valid/1/') $t->get_ok("/admin/organisations/$valid_id")
->status_is(200); ->status_is(200)->or($framework->dump_error);
#Read pending organisation #Read pending organisation
$t->get_ok('/admin/organisations/pending/2/') $t->get_ok("/admin/organisations/$pending_id")
->status_is(200); ->status_is(200)->or($framework->dump_error);
#Valid approved organisation update #Valid approved organisation update
$t->post_ok('/admin/organisations/valid/1/edit', form => { $t->post_ok("/admin/organisations/$valid_id", form => {
name => 'Shinra Electric Power Company', name => 'Shinra Electric Power Company',
street_name => 'Sector 0, Midgar, Eastern Continent', street_name => 'Sector 0, Midgar, Eastern Continent',
town => 'Gaia', town => 'Gaia',
@ -59,7 +63,7 @@ $t->post_ok('/admin/organisations/valid/1/edit', form => {
})->status_is(200)->content_like(qr/Updated Organisation/); })->status_is(200)->content_like(qr/Updated Organisation/);
#Failed validation on approved organisation #Failed validation on approved organisation
$t->post_ok('/admin/organisations/valid/1/edit', form => { $t->post_ok("/admin/organisations/$valid_id", form => {
name => 'Shinra Electric Power Company', name => 'Shinra Electric Power Company',
street_name => 'Sector 0, Midgar, Eastern Continent', street_name => 'Sector 0, Midgar, Eastern Continent',
sector => 'A', sector => 'A',
@ -67,7 +71,7 @@ $t->post_ok('/admin/organisations/valid/1/edit', form => {
})->content_like(qr/The validation has failed/); })->content_like(qr/The validation has failed/);
#Valid pending organisation update #Valid pending organisation update
$t->post_ok('/admin/organisations/pending/2/edit', form => { $t->post_ok("/admin/organisations/$pending_id", form => {
name => '7th Heaven', name => '7th Heaven',
street_name => 'Slums, Sector 7', street_name => 'Slums, Sector 7',
town => 'Midgar', town => 'Midgar',
@ -75,14 +79,14 @@ $t->post_ok('/admin/organisations/pending/2/edit', form => {
})->status_is(200)->content_like(qr/Updated Organisation/); })->status_is(200)->content_like(qr/Updated Organisation/);
#Failed validation on pending organisation #Failed validation on pending organisation
$t->post_ok('/admin/organisations/pending/2/edit', form => { $t->post_ok("/admin/organisations/$pending_id", form => {
name => '7th Heaven', name => '7th Heaven',
street_name => 'Slums, Sector 7', street_name => 'Slums, Sector 7',
postcode => 'WC1E 6AD', postcode => 'WC1E 6AD',
})->content_like(qr/The validation has failed/); })->content_like(qr/The validation has failed/);
#Valid adding organisation #Valid adding organisation
$t->post_ok('/admin/organisations/add/submit', form => { $t->post_ok('/admin/organisations/add', form => {
name => 'Wall Market', name => 'Wall Market',
street_name => 'Slums, Sector 6', street_name => 'Slums, Sector 6',
town => 'Midgar', town => 'Midgar',
@ -91,7 +95,7 @@ $t->post_ok('/admin/organisations/add/submit', form => {
})->status_is(200)->content_like(qr/Added Organisation/); })->status_is(200)->content_like(qr/Added Organisation/);
#Failed validation on adding organisation #Failed validation on adding organisation
$t->post_ok('/admin/organisations/add/submit', form => { $t->post_ok('/admin/organisations/add', form => {
name => 'Wall Market', name => 'Wall Market',
street_name => 'Slums, Sector 6', street_name => 'Slums, Sector 6',
postcode => 'TN35 5AQ', postcode => 'TN35 5AQ',