From 2828998a3240cf1cac1382c92957c5961d1d8a7b Mon Sep 17 00:00:00 2001 From: Paul Dill Date: Fri, 24 Feb 2017 17:09:37 +0000 Subject: [PATCH] Admin approve API is done but test is broken. --- dropschema.sql | 1 + foodloopserver.pl | 107 +++++++++++++ schema.sql | 7 +- t/admin-approve.t | 378 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 t/admin-approve.t diff --git a/dropschema.sql b/dropschema.sql index 4be21c4..123e6b4 100644 --- a/dropschema.sql +++ b/dropschema.sql @@ -1,3 +1,4 @@ +DROP TABLE IF EXISTS Administrators; DROP TABLE IF EXISTS PendingTransactions; DROP TABLE IF EXISTS PendingOrganisations; DROP TABLE IF EXISTS SessionTokens; diff --git a/foodloopserver.pl b/foodloopserver.pl index bfe3b52..50c184a 100644 --- a/foodloopserver.pl +++ b/foodloopserver.pl @@ -36,6 +36,104 @@ any '/' => sub { return $self->render(text => 'If you are seeing this, then the server is running.', success => Mojo::JSON->true); }; +post '/admin-approve' => sub{ + my $self = shift; + + 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.', + }, + 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) = $dbh->selectrow_array("SELECT PendingOrganisationId, Name, FullAddress, Postcode FROM PendingOrganisations WHERE PendingOrganisationId = ?", undef, ($unvalidatedOrganisationId)); + + #It does not exist. + if (! defined $id) { + 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") . "\n"; + + my $statementSelectPendingTrans = $dbh->prepare("SELECT BuyerUserId_FK, ValueMicroCurrency, ProofImage, TimeDateSubmitted FROM PendingTransactions WHERE PendingSellerOrganisationId_FK = ?"); + $statementSelectPendingTrans->execute($organisationalId); + + 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,); + +}; + #TODO this should limit the number of responses returned, when location is implemented that would be the main way of filtering. post '/search' => sub { my $self = shift; @@ -799,6 +897,15 @@ post '/fetchuser' => sub { }); }; + +helper is_admin => sub{ + my ($self, $userId) = @_; + + my ($rowCount) = $self->db->selectrow_array("SELECT COUNT(UserId) FROM Administrators WHERE UserId = ?", undef, ($userId)); + + return $rowCount != 0; +}; + helper create_hash => sub{ my ($self, $id, $name, $fullAddress, $postcode) = @_; diff --git a/schema.sql b/schema.sql index a8d0707..078fecb 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,5 @@ CREATE TABLE Organisations ( - OrganisationalId INTEGER PRIMARY KEY UNIQUE NOT NULL, + OrganisationalId INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, Name TEXT NOT NULL COLLATE nocase, FullAddress TEXT NOT NULL COLLATE nocase, PostCode TEXT NOT NULL COLLATE nocase @@ -79,3 +79,8 @@ CREATE TABLE PendingTransactions ( FOREIGN KEY (BuyerUserId_FK) REFERENCES Users (UserId), FOREIGN KEY (PendingSellerOrganisationId_FK) REFERENCES PendingOrganisations (PendingOrganisationId) ); + +CREATE TABLE Administrators ( + UserId INTEGER PRIMARY KEY UNIQUE NOT NULL, + FOREIGN KEY (UserId) REFERENCES Users (UserId) +) diff --git a/t/admin-approve.t b/t/admin-approve.t new file mode 100644 index 0000000..ed6cf50 --- /dev/null +++ b/t/admin-approve.t @@ -0,0 +1,378 @@ +use Test::More skip_all => "Test is currently broken"; +use Test::Mojo; +use Mojo::JSON; + +use FindBin; + +$ENV{MOJO_MODE} = 'development'; +$ENV{MOJO_LOG_LEVEL} = 'debug'; + +require "$FindBin::Bin/../foodloopserver.pl"; + +my $t = Test::Mojo->new; + +my $dbh = $t->app->db; + +#Dump all pf the test tables and start again. +my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../dropschema.sql")->slurp; +for (split ';', $sqlDeployment){ + $dbh->do($_) or die $dbh->errstr; +} + +my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp; +for (split ';', $sqlDeployment){ + $dbh->do($_) or die $dbh->errstr; +} + +my @accountTokens = ('a', 'b', 'c'); +my $tokenStatement = $dbh->prepare('INSERT INTO AccountTokens (AccountTokenName) VALUES (?)'); +foreach (@accountTokens){ + my $rowsAdded = $tokenStatement->execute($_); +} + + +#This depends on "register.t", "login.t" and "upload.t" working. + +#Valid customer, this also tests that redirects are disabled for register. +print "test 1 - Create customer user account (Rufus)\n"; +my $emailReno = 'reno@shinra.energy'; +my $passwordReno = 'turks'; +my $testJson = { + 'usertype' => 'customer', + 'token' => shift(@accountTokens), + 'username' => 'Rude', + 'email' => $emailReno, + 'postcode' => 'E1 MP01', + 'password' => $passwordReno, + 'age' => '20-35' +}; +$t->post_ok('/register' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +print "test 2 - Create organisation user account (Choco Billy)\n"; +my $emailBilly = 'choco.billy@chocofarm.org'; +my $passwordBilly = 'Choco'; +my $testJson = { + 'usertype' => 'organisation', + 'token' => shift(@accountTokens), + 'username' => 'ChocoBillysGreens', + 'email' => $emailBilly, + 'postcode' => 'E4 C12', + 'password' => $passwordBilly, + 'fulladdress' => 'Chocobo Farm, Eastern Continent, Gaia' +}; +$t->post_ok('/register' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + + +print "test 3 - Create admin account\n"; +my $emailAdmin = 'admin@foodloop.net'; +my $passwordAdmin = 'ethics'; +my $testJson = { + 'usertype' => 'customer', + 'token' => shift(@accountTokens), + 'username' => 'admin', + 'email' => $emailAdmin, + 'postcode' => 'NW1 W01', + 'password' => $passwordAdmin, + 'age' => '35-50' +}; +$t->post_ok('/register' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +print "test 3 - Making 'admin' an Admin\n"; +my $rufusUserId = $t->app->db->selectrow_array("SELECT UserId FROM Users WHERE Email = ?", undef, ($emailAdmin)); +is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],0,"No admins"; +$t->app->db->prepare("INSERT INTO Administrators (UserId) VALUES (?)")->execute($rufusUserId); +is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],1,"1 admin"; + + +###################################################### + +#Login as non-admin Reno + +print "test 4 - Login - non-admin Reno (cookies, customer)\n"; +$testJson = { + 'email' => $emailReno, + 'password' => $passwordReno, +}; +$t->post_ok('/login' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +print "test 5 - 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." ; +my $nameToTestTurtle = 'Turtle\'s Paradise'; +$json = { + microCurrencyValue => 20, + transactionAdditionType => 3, + organisationName => $nameToTestTurtle, + streetName => "Town centre", + town => " Wutai", + postcode => "NW1 W01" +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/upload' => form => $upload ) + ->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],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." ; + +my $newPendingTurtleOrgId = $t->app->db->last_insert_id(undef,undef, "PendingOrganisations", "PendingOrganisationId"); +print "Turtle Id: " . $newPendingTurtleOrgId; + +print "test 6 - Non-admin (customer) tries to approve their organisation and fails.\n"; +$json = { + unvalidatedOrganisationId => $newPendingTurtleOrgId, +}; +$t->post_ok('/admin-approve' => json => $json) + ->status_is(403) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/You are not an admin/i); + + +print "test 7 - Logout Reno\n"; +$t->post_ok('/logout') + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +#End of non-admin Reno + +###################################################### + +#Login as non-admin Choco Billy + +print "test 8 - Login - non-admin Choco Billy (cookies, organisation)\n"; +$testJson = { + 'email' => $emailBilly, + 'password' => $passwordBilly, +}; +$t->post_ok('/login' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + + +print "test 9 - 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." ; +my $nameToTestKalm = 'Kalm Inn'; +$json = { + microCurrencyValue => 10, + transactionAdditionType => 3, + organisationName => $nameToTestKalm, + streetName => "Town centre", + town => "Kalm", + postcode => "E2 M02" +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/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->db->last_insert_id(undef,undef, "PendingOrganisations", "PendingOrganisationId"); +print "Kalm Id: " . $newPendingKalmOrgId . "\n"; + + +print "test 10 - add valid transaction (type 2: unvalidated organisation)\n"; +$json = { + microCurrencyValue => 10, + transactionAdditionType => 2, + addUnvalidatedId => $newPendingKalmOrgId, +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/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],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." ; + + +print "test 11 - add valid transaction (type 3: new organisation)\n"; + +my $nameToTestJunon = 'Store'; +$json = { + microCurrencyValue => 10, + transactionAdditionType => 3, + organisationName => $nameToTestJunon, + streetName => "Main street", + town => "Under Junon", + postcode => "E6 M02" +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/upload' => form => $upload) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +my $newPendingJunonOrgId = $t->app->db->last_insert_id(undef,undef, "PendingOrganisations", "PendingOrganisationId"); +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." ; + + + +print "test 12 - add valid transaction (type 2: unvalidated organisation)\n"; +$json = { + microCurrencyValue => 20, + transactionAdditionType => 2, + addUnvalidatedId => $newPendingJunonOrgId, +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/upload' => form => $upload ) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); +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." ; + +done_testing(); +exit; + +print "test 13 - add valid transaction (type 2: unvalidated organisation)\n"; +$json = { + microCurrencyValue => 30, + transactionAdditionType => 2, + addUnvalidatedId => $newPendingJunonOrgId, +}; +my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}}; +$t->post_ok('/upload' => form => $upload ) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); +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." ; + +print "test 13 - Non-admin (organisation) tries to approve their organisation and fails.\n"; +$json = { + unvalidatedOrganisationId => $newPendingKalmOrgId, +}; +$t->post_ok('/admin-approve' => json => $json) + ->status_is(403) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/You are not an admin/i); + +print "test 12 - Logout Choco Billy\n"; +$t->post_ok('/logout') + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +#End of non-admin Choco Billy + +###################################################### + +#Login as Admin + +print "test 13 - Login - admin\n"; +$testJson = { + 'email' => $emailAdmin, + 'password' => $passwordAdmin, +}; +$t->post_ok('/login' => json => $testJson) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + + +print "test 14 - JSON is missing.\n"; +$t->post_ok('/admin-approve' => json) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/JSON is missing/i); + +print "test 15 - unvalidatedOrganisationId missing (non-modify).\n"; +$json = { +}; +$t->post_ok('/admin-approve' => json => $json) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/unvalidatedOrganisationId is missing/i); + +print "test 16 - unvalidatedOrganisationId not number (non-modify).\n"; +$json = { + unvalidatedOrganisationId => 'Abc', +}; +$t->post_ok('/admin-approve' => json => $json) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/unvalidatedOrganisationId does not look like a number/i); + + +print "test 17 - unvalidatedOrganisationId does not exist (non-modify).\n"; +my ($maxPendingId) = $t->app->db->selectrow_array("SELECT MAX(PendingOrganisationId) FROM PendingOrganisations", undef,()); +$json = { + unvalidatedOrganisationId => ($maxPendingId + 1), +}; +$t->post_ok('/admin-approve' => json => $json) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/the specified unvalidatedOrganisationId does not exist/i); + +#TODO add text to see the specific one has moved. + +print "test 18 - 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."; +$json = { + unvalidatedOrganisationId => $newPendingKalmOrgId, +}; +$t->post_ok('/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],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."; + +#TODO check with values missing + +print "test 18 - valid approval (non-modify).\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"; +$json = { + unvalidatedOrganisationId => $newPendingTurtleOrgId, + name => $testName, + fullAddress => $testFullAddress, + postCode => $testPostCode, +}; +$t->post_ok('/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],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, ($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, it been renamed."; +is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations WHERE Name = ? AND FullAddress = ? AND PostCode = ?", undef, ($testName, $testFullAddress, $testPostCode))}[0],0,"Turtle exists and has been renamed in verified orgs."; + +############################################## + + + +done_testing();