Code cleanup

This commit is contained in:
Paul Dill 2017-03-02 15:46:35 +00:00
parent 1eafaf4ab1
commit 7bfa65f4c3
3 changed files with 13 additions and 82 deletions

View file

@ -38,6 +38,7 @@ Dwarn $config;
# shortcut for use in template # shortcut for use in template
$self->helper( db => sub { $dbh }); $self->helper( db => sub { $dbh });
$self->helper( schema => sub { $schema });
my $r = $self->routes; my $r = $self->routes;
@ -62,77 +63,6 @@ $r->any( '/' => sub {
}); });
#TODO this should limit the number of responses returned, when location is implemented that would be the main way of filtering.
$r->post ('/search' => sub {
my $self = shift;
my $userId = $self->get_active_user_id();
my $json = $self->req->json;
if ( ! defined $json ) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'JSON is missing.',
},
status => 400,); #Malformed request
}
my $searchName = $json->{searchName};
if ( ! defined $searchName ) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'searchName is missing.',
},
status => 400,); #Malformed request
}
#Is blank
elsif ( $searchName =~ m/^\s*$/) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'searchName is blank.',
},
status => 400,); #Malformed request
}
#Currently ignored
#TODO implement further.
my $searchLocation = $json->{searchLocation};
my @validatedOrgs = ();
{
my $statementValidated = $dbh->prepare("SELECT OrganisationalId, Name, FullAddress, PostCode FROM Organisations WHERE Name LIKE ?");
$statementValidated->execute('%'.$searchName.'%');
while (my ($id, $name, $address, $postcode) = $statementValidated->fetchrow_array()) {
push(@validatedOrgs, $self->create_hash($id,$name,$address,$postcode));
}
}
#$self->app->log->debug( "Orgs: " . Dumper @validatedOrgs );
my @unvalidatedOrgs = ();
{
my $statementUnvalidated = $dbh->prepare("SELECT PendingOrganisationId, Name, FullAddress, Postcode FROM PendingOrganisations WHERE Name LIKE ? AND UserSubmitted_FK = ?");
$statementUnvalidated->execute('%'.$searchName.'%', $userId);
while (my ($id, $name, $fullAddress, $postcode) = $statementUnvalidated->fetchrow_array()) {
push(@unvalidatedOrgs, $self->create_hash($id, $name, $fullAddress, $postcode));
}
}
$self->app->log->debug('Path Success: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->true,
unvalidated => \@unvalidatedOrgs,
validated => \@validatedOrgs,
},
status => 200,);
});
$self->hook( before_dispatch => sub { $self->hook( before_dispatch => sub {

View file

@ -102,5 +102,6 @@ sub post_admin_approve {
} }
1; 1;

View file

@ -243,8 +243,8 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions",
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],0,"No verified transactions." ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],0,"No verified transactions." ;
done_testing(); #done_testing();
exit; #exit;
print "test 13 - add valid transaction (type 2: unvalidated organisation)\n"; print "test 13 - add valid transaction (type 2: unvalidated organisation)\n";
$json = { $json = {
@ -261,7 +261,7 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions",
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],0,"No verified transactions." ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],0,"No verified transactions." ;
print "test 13 - Non-admin (organisation) tries to approve their organisation and fails.\n"; print "test 14 - Non-admin (organisation) tries to approve their organisation and fails.\n";
$json = { $json = {
unvalidatedOrganisationId => $newPendingKalmOrgId, unvalidatedOrganisationId => $newPendingKalmOrgId,
}; };
@ -270,7 +270,7 @@ $t->post_ok('/admin-approve' => json => $json)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/You are not an admin/i); ->content_like(qr/You are not an admin/i);
print "test 12 - Logout Choco Billy\n"; print "test 15 - Logout Choco Billy\n";
$t->post_ok('/logout') $t->post_ok('/logout')
->status_is(200) ->status_is(200)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
@ -281,7 +281,7 @@ $t->post_ok('/logout')
#Login as Admin #Login as Admin
print "test 13 - Login - admin\n"; print "test 16 - Login - admin\n";
$testJson = { $testJson = {
'email' => $emailAdmin, 'email' => $emailAdmin,
'password' => $passwordAdmin, 'password' => $passwordAdmin,
@ -291,13 +291,13 @@ $t->post_ok('/login' => json => $testJson)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
print "test 14 - JSON is missing.\n"; print "test 17 - JSON is missing.\n";
$t->post_ok('/admin-approve' => json) $t->post_ok('/admin-approve' => json)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/JSON is missing/i); ->content_like(qr/JSON is missing/i);
print "test 15 - unvalidatedOrganisationId missing (non-modify).\n"; print "test 18 - unvalidatedOrganisationId missing (non-modify).\n";
$json = { $json = {
}; };
$t->post_ok('/admin-approve' => json => $json) $t->post_ok('/admin-approve' => json => $json)
@ -305,7 +305,7 @@ $t->post_ok('/admin-approve' => json => $json)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/unvalidatedOrganisationId is missing/i); ->content_like(qr/unvalidatedOrganisationId is missing/i);
print "test 16 - unvalidatedOrganisationId not number (non-modify).\n"; print "test 19 - unvalidatedOrganisationId not number (non-modify).\n";
$json = { $json = {
unvalidatedOrganisationId => 'Abc', unvalidatedOrganisationId => 'Abc',
}; };
@ -315,7 +315,7 @@ $t->post_ok('/admin-approve' => json => $json)
->content_like(qr/unvalidatedOrganisationId does not look like a number/i); ->content_like(qr/unvalidatedOrganisationId does not look like a number/i);
print "test 17 - unvalidatedOrganisationId does not exist (non-modify).\n"; print "test 20 - unvalidatedOrganisationId does not exist (non-modify).\n";
my ($maxPendingId) = $t->app->db->selectrow_array("SELECT MAX(PendingOrganisationId) FROM PendingOrganisations", undef,()); my ($maxPendingId) = $t->app->db->selectrow_array("SELECT MAX(PendingOrganisationId) FROM PendingOrganisations", undef,());
$json = { $json = {
unvalidatedOrganisationId => ($maxPendingId + 1), unvalidatedOrganisationId => ($maxPendingId + 1),
@ -327,7 +327,7 @@ $t->post_ok('/admin-approve' => json => $json)
#TODO add text to see the specific one has moved. #TODO add text to see the specific one has moved.
print "test 18 - valid approval (non-modify).\n"; print "test 21 - valid approval (non-modify).\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],3,"3 unverified organisations."; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],3,"3 unverified organisations.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],6,"6 unverified transactions." ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],6,"6 unverified transactions." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisations (choco billy)" ;
@ -347,7 +347,7 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE N
#TODO check with values missing #TODO check with values missing
print "test 18 - valid approval (non-modify).\n"; print "test 22 - valid approval (non-modify).\n";
#TODO if we implement constraints on the input data this will fail #TODO if we implement constraints on the input data this will fail
my $testName = "Change testing turtle name"; my $testName = "Change testing turtle name";
my $testFullAddress = "Change testing turtle address"; my $testFullAddress = "Change testing turtle address";