Refactored Pending and Valid orgs and admin-approve test

This commit is contained in:
Tom Bloor 2017-04-21 14:14:51 +01:00
parent 99ef319c3b
commit c40a480361
7 changed files with 174 additions and 208 deletions

View file

@ -91,12 +91,15 @@ sub startup {
});
$api->post('/upload')->to('api-upload#post_upload');
$api->post('/search')->to('api-upload#post_search');
$api->post('/admin-approve')->to('api-admin#post_admin_approve');
$api->post('/admin-merge')->to('api-admin#post_admin_merge');
$api->post('/edit')->to('api-api#post_edit');
$api->post('/fetchuser')->to('api-api#post_fetchuser');
$api->post('/user-history')->to('api-user#post_user_history');
my $api_admin = $api->under('/')->to('api-admin#auth');
$api_admin->post('/admin-approve')->to('api-admin#post_admin_approve');
$api_admin->post('/admin-merge')->to('api-admin#post_admin_merge');
my $admin_routes = $r->under('/admin')->to('admin#under');
$admin_routes->get('/')->to('admin#home');

View file

@ -1,6 +1,18 @@
package Pear::LocalLoop::Controller::Api::Admin;
use Mojo::Base 'Mojolicious::Controller';
use Data::Dumper;
has error_messages => sub {
return {
pending_organisation_id => {
required => { message => 'pending_organisation_id is missing', status => 400 },
number => { message => 'pending_organisation_id is not a number', status => 400 },
in_resultset => { message => 'pending_organisation_id does not exist in the database', status => 400 },
},
postcode => {
postcode => { message => 'postcode is not a valid uk postcode', status => 400 },
},
};
};
sub auth {
my $c = shift;
@ -23,103 +35,50 @@ sub post_admin_approve {
my $c = shift;
my $self = $c;
my $user = $c->stash->{ api_user };
my $validation = $c->validation;
my $userId = $self->get_active_user_id();
if ( ! $self->is_admin($userId) ) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'You are not an admin.',
$validation->input( $c->stash->{api_json} );
my $pending_org_rs = $c->schema->resultset('PendingOrganisation');
$validation->required('pending_organisation_id')->number->in_resultset('id', $pending_org_rs);
$validation->optional('name');
$validation->optional('street_name');
$validation->optional('town');
$validation->optional('postcode')->postcode;
return $c->api_validation_error if $validation->has_error;
my $pending_org = $pending_org_rs->find( $validation->param('pending_organisation_id') );
my $valid_org = $c->schema->resultset('Organisation')->create({
name => $validation->param('name') || $pending_org->name,
street_name => $validation->param('street_name') || $pending_org->street_name,
town => $validation->param('town') || $pending_org->town,
postcode => $validation->param('postcode') || $pending_org->postcode,
});
my $pending_transaction_rs = $pending_org->pending_transactions;
while ( my $pending_transaction = $pending_transaction_rs->next ) {
$valid_org->create_related(
'transactions', {
buyeruserid_fk => $pending_transaction->buyeruserid_fk,
valuemicrocurrency => $pending_transaction->valuemicrocurrency,
proofimage => $pending_transaction->proofimage,
timedatesubmitted => $pending_transaction->timedatesubmitted,
}
);
}
$pending_org->delete;
return $self->render(
json => {
success => Mojo::JSON->true,
message => 'Successfully Approved Organisation',
},
status => 403,); #Forbidden request
}
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 $unvalidatedOrganisationId = $json->{unvalidatedOrganisationId};
if ( ! defined $unvalidatedOrganisationId ) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'unvalidatedOrganisationId is missing.',
},
status => 400,); #Malformed request
}
elsif (! Scalar::Util::looks_like_number($unvalidatedOrganisationId)){
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'unvalidatedOrganisationId does not look like a number.',
},
status => 400,); #Malformed request
}
my ($id, $name, $fullAddress, $postcode) = $self->db->selectrow_array("SELECT PendingOrganisationId, Name, FullAddress, Postcode FROM PendingOrganisations WHERE PendingOrganisationId = ?", undef, ($unvalidatedOrganisationId));
#It does not exist.
if (! defined $id) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->false,
message => 'the specified unvalidatedOrganisationId does not exist.',
},
status => 400,); #Malformed request
}
my $nameJson = $json->{name};
if (defined $nameJson) {
$name = $nameJson;
}
my $fullAddressJson = $json->{fullAddress};
if (defined $fullAddressJson) {
$fullAddress = $fullAddressJson;
}
my $postCodeJson = $json->{postCode};
if (defined $postCodeJson) {
$postcode = $postCodeJson;
}
#FIXME there may be race conditions here, so may get the wrong number, mutux is needed.
my $statementInsOrg = $self->db->prepare("INSERT INTO Organisations (Name, FullAddress, PostCode) VALUES (?, ?, ?)");
$statementInsOrg->execute($name, $fullAddress, $postcode);
my $organisationalId = $self->db->last_insert_id(undef,undef, "Organisations", "OrganisationalId");
#print "OrgId: " . $organisationalId . "\n";
my $statementSelectPendingTrans = $self->db->prepare("SELECT BuyerUserId_FK, ValueMicroCurrency, ProofImage, TimeDateSubmitted FROM PendingTransactions WHERE PendingSellerOrganisationId_FK = ?");
$statementSelectPendingTrans->execute($unvalidatedOrganisationId);
my $statementInsTrans = $self->db->prepare("INSERT INTO Transactions (BuyerUserId_FK, SellerOrganisationId_FK, ValueMicroCurrency, ProofImage, TimeDateSubmitted) VALUES (?, ?, ?, ?, ?)");
#Move all transactions from pending onto verified.
while (my ($buyerUserId, $value, $imgName, $timeDate) = $statementSelectPendingTrans->fetchrow_array()) {
$statementInsTrans->execute($buyerUserId, $organisationalId, $value, $imgName, $timeDate);
}
#Delete transactions first, so there is no dependancy when deleting the row from PendingOrganisations.
$self->db->prepare("DELETE FROM PendingTransactions WHERE PendingSellerOrganisationId_FK = ?")->execute($unvalidatedOrganisationId);
$self->db->prepare("DELETE FROM PendingOrganisations WHERE PendingOrganisationId = ?")->execute($unvalidatedOrganisationId);
$self->app->log->debug('Path Success: file:' . __FILE__ . ', line: ' . __LINE__);
return $self->render( json => {
success => Mojo::JSON->true,
validatedOrganisationId => $organisationalId,
},
status => 200,);
status => 200,
);
}

View file

@ -32,8 +32,8 @@ has error_messages => sub {
required => { message => 'No age sent.', status => 400 },
in_resultset => { message => 'Age range is invalid.', status => 400 },
},
street_address => {
required => { message => 'No street_address sent.', status => 400 },
street_name => {
required => { message => 'No street_name sent.', status => 400 },
},
town => {
required => { message => 'No town sent.', status => 400 },
@ -83,7 +83,7 @@ sub post_register{
} elsif ( $usertype eq 'organisation' ) {
$validation->required('street_address');
$validation->required('street_name');
$validation->required('town');
}
@ -135,7 +135,7 @@ sub post_register{
$c->schema->resultset('User')->create({
organisation => {
name => $validation->param('username'),
street_address => $validation->param('street_address'),
street_name => $validation->param('street_name'),
town => $validation->param('town'),
postcode => $validation->param('postcode'),
},

View file

@ -97,8 +97,8 @@ sub post_upload {
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );
} elsif ( $type == 2 ) {
# Unvalidated Organisation
my $valid_org_rs = $c->schema->resultset('PendingOrganisation')->search({ usersubmitted_fk => $user->id });
$validation->required('organisation_id')->number->in_resultset( 'pendingorganisationid', $valid_org_rs );
my $valid_org_rs = $c->schema->resultset('PendingOrganisation')->search({ submitted_by_id => $user->id });
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );
} elsif ( $type == 3 ) {
# Unknown Organisation
$validation->required('organisation_name');
@ -162,16 +162,17 @@ sub post_upload {
}
my $pending_org = $c->schema->resultset('PendingOrganisation')->create({
usersubmitted_fk => $user->id,
timedatesubmitted => DateTime->now,
name => $organisation_name,
fulladdress => $fullAddress,
postcode => $postcode,
submitted_by => $user,
submitted_at => DateTime->now,
name => $organisation_name,
street_name => $street_name,
town => $town,
postcode => $postcode,
});
$c->schema->resultset('PendingTransaction')->create({
buyeruserid_fk => $user->id,
pendingsellerorganisationid_fk => $pending_org->pendingorganisationid,
pendingsellerorganisationid_fk => $pending_org->id,
valuemicrocurrency => $transaction_value,
proofimage => $filename,
timedatesubmitted => DateTime->now,

View file

@ -20,7 +20,7 @@ __PACKAGE__->add_columns(
size => 255,
is_nullable => 0,
},
street_address => {
street_name => {
data_type => 'text',
is_nullable => 1,
},

View file

@ -21,7 +21,7 @@ __PACKAGE__->add_columns(
size => 255,
is_nullable => 0,
},
street_address => {
street_name => {
data_type => 'text',
is_nullable => 1,
},
@ -64,7 +64,7 @@ __PACKAGE__->has_many(
{
"foreign.pendingsellerorganisationid_fk" => "self.id",
},
{ cascade_copy => 0, cascade_delete => 0 },
{ cascade_copy => 0, cascade_delete => 1 },
);
=head2 usersubmitted_fk

View file

@ -45,7 +45,7 @@ $testJson = {
'email' => $emailBilly,
'postcode' => 'E4 C12',
'password' => $passwordBilly,
'street_address' => 'Chocobo Farm, Eastern Continent',
'street_name' => 'Chocobo Farm, Eastern Continent',
'town' => 'Gaia',
};
$t->post_ok('/api/register' => json => $testJson)
@ -90,11 +90,10 @@ $t->post_ok('/api/login' => json => $testJson)
my $session_key = $t->tx->res->json('/session_key');
print "test 6 - add valid transaction (type 3: new organisation)\n";
my ($test1) = $t->app->db->selectrow_array("SELECT COUNT(*) FROM PendingOrganisations", undef, ());
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],0,"No unverified organisations.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],0,"No unverified transactions." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],1,"1 verified organisation (choco billy)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],0,"No verified transactions." ;
is $schema->resultset('PendingOrganisation')->count, 0, "No unverified organisations";
is $schema->resultset('PendingTransaction')->count, 0, "No unverified transactions" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
my $nameToTestTurtle = 'Turtle\'s Paradise';
my $json = {
transaction_value => 20,
@ -107,25 +106,25 @@ my $json = {
};
my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(200)
->status_is(200)->or($dump_error)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],1,"1 unverified organisation." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],1,"1 unverified transaction." ;
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 $schema->resultset('PendingOrganisation')->count, 1, "1 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 1, "1 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
my $newPendingTurtleOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestTurtle })->pendingorganisationid;
my $newPendingTurtleOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestTurtle })->id;
#print "Turtle Id: " . $newPendingTurtleOrgId . "\n";
print "test 7 - Non-admin (customer) tries to approve their organisation and fails.\n";
$json = {
unvalidatedOrganisationId => $newPendingTurtleOrgId,
pending_organisation_id => $newPendingTurtleOrgId,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(403)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/You are not an admin/i);
->json_like('/message', qr/Not Authorised/i);
print "test 8 - Logout Reno\n";
@ -150,10 +149,11 @@ $t->post_ok('/api/login' => json => $testJson)
$session_key = $t->tx->res->json('/session_key');
print "test 10 - add valid transaction (type 3: new organisation)\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],1,"1 unverified organisations." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],1,"1 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 Transactions", undef, ())}[0],0,"No verified transactions." ;
is $schema->resultset('PendingOrganisation')->count, 1, "1 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 1, "1 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
my $nameToTestKalm = 'Kalm Inn';
$json = {
transaction_value => 10,
@ -168,14 +168,13 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload )
->status_is(200)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],2,"2 unverified organisations." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],2,"2 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 Transactions", undef, ())}[0],0,"No verified transactions." ;
my $newPendingKalmOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestKalm })->pendingorganisationid;
#print "Kalm Id: " . $newPendingKalmOrgId . "\n";
is $schema->resultset('PendingOrganisation')->count, 2, "2 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 2, "2 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
my $newPendingKalmOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestKalm })->id;
print "test 11 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
@ -186,12 +185,13 @@ $json = {
};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(200)
->status_is(200)->or($dump_error)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],2,"2 unverified organisations." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],3,"3 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 Transactions", undef, ())}[0],0,"No verified transactions." ;
is $schema->resultset('PendingOrganisation')->count, 2, "2 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 3, "3 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
print "test 12 - add valid transaction (type 3: new organisation)\n";
my $nameToTestJunon = 'Store';
@ -209,14 +209,12 @@ $t->post_ok('/api/upload' => form => $upload)
->status_is(200)
->json_is('/success', Mojo::JSON->true);
my $newPendingJunonOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestJunon })->pendingorganisationid;
my $newPendingJunonOrgId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestJunon })->id;
#print "Junon Id: " . $newPendingJunonOrgId . "\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 PendingTransactions", undef, ())}[0],4,"4 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 Transactions", undef, ())}[0],0,"No verified transactions." ;
is $schema->resultset('PendingOrganisation')->count, 3, "3 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 4, "4 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
print "test 13 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
@ -228,13 +226,11 @@ $json = {
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(200)
->json_is('/success', Mojo::JSON->true)
->json_hasnt('/unvalidatedOrganisationId');
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],5,"5 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 Transactions", undef, ())}[0],0,"No verified transactions." ;
->json_is('/success', Mojo::JSON->true);
is $schema->resultset('PendingOrganisation')->count, 3, "3 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 5, "5 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
print "test 14 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
@ -246,22 +242,21 @@ $json = {
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(200)
->json_is('/success', Mojo::JSON->true)
->json_hasnt('/unvalidatedOrganisationId');
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 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." ;
->json_is('/success', Mojo::JSON->true);
is $schema->resultset('PendingOrganisation')->count, 3, "3 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 6, "6 unverified transaction" ;
is $schema->resultset('Organisation')->count, 1, "1 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 0, "No verified transactions" ;
print "test 15 - Non-admin (organisation) tries to approve their organisation and fails.\n";
$json = {
unvalidatedOrganisationId => $newPendingKalmOrgId,
pending_organisation_id => $newPendingKalmOrgId,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(403)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/You are not an admin/i);
->json_like('/message', qr/Not Authorised/i);
print "test 16 - Logout Choco Billy\n";
$t->post_ok('/api/logout', json => { session_key => $session_key } )
@ -290,99 +285,107 @@ $t->post_ok('/api/admin-approve' => json => {})
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/JSON is missing/i);
print "test 19 - unvalidatedOrganisationId missing (non-modify).\n";
print "test 19 - pending_organisation_id missing (non-modify).\n";
$json = {
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/unvalidatedOrganisationId is missing/i);
->content_like(qr/pending_organisation_id is missing/i);
print "test 20 - unvalidatedOrganisationId not number (non-modify).\n";
print "test 20 - pending_organisation_id not number (non-modify).\n";
$json = {
unvalidatedOrganisationId => 'Abc',
pending_organisation_id => 'Abc',
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/unvalidatedOrganisationId does not look like a number/i);
->json_like('/message', qr/pending_organisation_id is not a number/i);
print "test 21 - pending_organisation_id does not exist (non-modify).\n";
print "test 21 - unvalidatedOrganisationId does not exist (non-modify).\n";
my ($maxPendingId) = $t->app->db->selectrow_array("SELECT MAX(PendingOrganisationId) FROM PendingOrganisations", undef,());
my $maxPendingId = $schema->resultset('PendingOrganisation')->get_column('id')->max;
$json = {
unvalidatedOrganisationId => ($maxPendingId + 1),
pending_organisation_id => $maxPendingId + 1,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/the specified unvalidatedOrganisationId does not exist/i);
->json_like('/message', qr/pending_organisation_id does not exist in the database/i);
#TODO add text to see the specific one has moved.
print "test 22 - 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 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 Transactions", undef, ())}[0],0,"No verified organisations.";
is $schema->resultset('PendingOrganisation')->count, 3, "3 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 6, "6 unverified transaction";
is $schema->resultset('Organisation')->count, 1, "1 verified organisation";
is $schema->resultset('Transaction')->count, 0, "No verified transactions";
$json = {
unvalidatedOrganisationId => $newPendingKalmOrgId,
pending_organisation_id => $newPendingKalmOrgId,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(200)->or($dump_error)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],2,"2 unverified organisation." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],4,"4 unverified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],2,"2 verified organisations (choco billy and kalm inn)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],2,"2 verified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations WHERE Name = ?", undef, ($nameToTestKalm))}[0],0,"Kalm does not exist in pending orgs.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ?", undef, ($nameToTestKalm))}[0],1,"Kalm exists in verified orgs.";
is $schema->resultset('PendingOrganisation')->count, 2, "2 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 4, "4 unverified transaction";
is $schema->resultset('Organisation')->count, 2, "2 verified organisations";
is $schema->resultset('Transaction')->count, 2, "2 verified transactions";
is $schema->resultset('PendingOrganisation')->find({ name => $nameToTestKalm }), undef, "Kalm does not exist in pending orgs.";
ok $schema->resultset('Organisation')->find({ name => $nameToTestKalm }), "Kalm exists in verified orgs.";
print "test 23 - valid approval (modify all).\n";
#TODO if we implement constraints on the input data this will fail
my $testName = "Change testing turtle name";
my $testFullAddress = "Change testing turtle address";
my $testPostCode = "Change testing turtle postcode";
my $test_name = "Change testing turtle name";
my $test_street_name = "Change testing turtle address";
my $test_town = "TestinTown";
my $test_postcode = "BN21 2RB";
$json = {
unvalidatedOrganisationId => $newPendingTurtleOrgId,
name => $testName,
fullAddress => $testFullAddress,
postCode => $testPostCode,
pending_organisation_id => $newPendingTurtleOrgId,
name => $test_name,
street_name => $test_street_name,
town => $test_town,
postcode => $test_postcode,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(200)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],1,"1 unverified organisation." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],3,"3 unverified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],3,"3 verified organisations (choco billy, kalm inn and turtle)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],3,"3 verified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations WHERE Name = ?", undef, ($nameToTestTurtle))}[0],0,"Turtle does not exist in pending orgs.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ?", undef, ($nameToTestTurtle))}[0],0,"Turtle does not exist in verified orgs, its been renamed.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ? AND FullAddress = ? AND PostCode = ?", undef, ($testName, $testFullAddress, $testPostCode))}[0],1,"Turtle exists and has been renamed in verified orgs.";
is $schema->resultset('PendingOrganisation')->count, 1, "1 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 3, "3 unverified transaction" ;
is $schema->resultset('Organisation')->count, 3, "3 verified organisation (choco billy)" ;
is $schema->resultset('Transaction')->count, 3, "3 verified transactions" ;
is $schema->resultset('PendingOrganisation')->find({ name => $nameToTestTurtle }), undef, "Turtle does not exist in pending orgs.";
is $schema->resultset('Organisation')->find({ name => $nameToTestTurtle }), undef, "original Turtle does not exists in verified orgs.";
my $turtle_result = $schema->resultset('Organisation')->find({ name => $test_name });
ok $turtle_result, "new Turtle exists in verified orgs.";
is $turtle_result->street_name, $test_street_name, 'street_name correct';
is $turtle_result->town, $test_town, 'town correct';
is $turtle_result->postcode, $test_postcode, 'postcode correct';
print "test 24 - valid approval (modify some).\n";
#TODO if we implement constraints on the input data this will fail
$testName = "Change testing junon name";
$test_name = "Change testing junon name";
$json = {
unvalidatedOrganisationId => $newPendingJunonOrgId,
name => $testName,
pending_organisation_id => $newPendingJunonOrgId,
name => $test_name,
session_key => $session_key,
};
$t->post_ok('/api/admin-approve' => json => $json)
->status_is(200)
->json_is('/success', Mojo::JSON->true);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],0,"0 unverified organisation." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],0,"0 unverified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef, ())}[0],4,"4 verified organisations (choco billy, kalm inn, turtle and junon)" ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef, ())}[0],6,"6 verified transaction." ;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations WHERE Name = ?", undef, ($nameToTestJunon))}[0],0,"Junon does not exist in pending orgs.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ?", undef, ($nameToTestJunon))}[0],0,"Junon does not exist in verified orgs, its been renamed.";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ?", undef, ($testName))}[0],1,"Junon exists and has been renamed in verified orgs.";
is $schema->resultset('PendingOrganisation')->count, 0, "0 unverified organisation";
is $schema->resultset('PendingTransaction')->count, 0, "0 unverified transaction";
is $schema->resultset('Organisation')->count, 4, "4 verified organisations";
is $schema->resultset('Transaction')->count, 6, "6 verified transactions";
is $schema->resultset('PendingOrganisation')->find({ name => $nameToTestJunon }), undef, "Junon does not exist in pending orgs.";
is $schema->resultset('Organisation')->find({ name => $nameToTestJunon }), undef, "original Junon does not exists in verified orgs.";
my $junon_result = $schema->resultset('Organisation')->find({ name => $test_name });
ok $junon_result, "new Junon exists in verified orgs.";
##############################################