2017-04-20 17:15:34 +00:00
use Mojo::Base - strict ;
2017-04-21 11:42:31 +00:00
2017-03-05 17:32:38 +00:00
use Test::More ;
2017-02-24 17:09:37 +00:00
use Mojo::JSON ;
2017-04-21 11:42:31 +00:00
use Test::Pear::LocalLoop ;
2017-02-24 17:09:37 +00:00
2017-04-21 11:42:31 +00:00
my $ framework = Test::Pear::LocalLoop - > new ;
my $ t = $ framework - > framework ;
my $ schema = $ t - > app - > schema ;
my $ dump_error = sub { diag $ t - > tx - > res - > dom - > at ( 'pre[id="error"]' ) - > text } ;
2017-02-24 17:09:37 +00:00
2017-04-21 11:42:31 +00:00
my @ account_tokens = ( 'a' , 'b' , 'c' ) ;
2017-02-24 17:09:37 +00:00
2017-04-21 11:42:31 +00:00
$ schema - > resultset ( 'AccountToken' ) - > populate ( [
[ 'accounttokenname' ] ,
map { [ $ _ ] } @ account_tokens ,
] ) ;
2017-02-24 17:09:37 +00:00
#This depends on "register.t", "login.t" and "upload.t" working.
#Valid customer, this also tests that redirects are disabled for register.
2017-03-05 16:01:44 +00:00
print "test 1 - Create customer user account (Reno)\n" ;
2017-02-24 17:09:37 +00:00
my $ emailReno = 'reno@shinra.energy' ;
my $ passwordReno = 'turks' ;
my $ testJson = {
'usertype' = > 'customer' ,
2017-04-21 11:42:31 +00:00
'token' = > shift ( @ account_tokens ) ,
2017-03-05 16:01:44 +00:00
'username' = > 'Reno' ,
2017-02-24 17:09:37 +00:00
'email' = > $ emailReno ,
'postcode' = > 'E1 MP01' ,
'password' = > $ passwordReno ,
'age' = > '20-35'
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > 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' ;
2017-04-20 17:15:34 +00:00
$ testJson = {
2017-02-24 17:09:37 +00:00
'usertype' = > 'organisation' ,
2017-04-21 11:42:31 +00:00
'token' = > shift ( @ account_tokens ) ,
2017-02-24 17:09:37 +00:00
'username' = > 'ChocoBillysGreens' ,
'email' = > $ emailBilly ,
'postcode' = > 'E4 C12' ,
'password' = > $ passwordBilly ,
2017-04-21 11:42:31 +00:00
'street_address' = > 'Chocobo Farm, Eastern Continent' ,
'town' = > 'Gaia' ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
print "test 3 - Create admin account\n" ;
my $ emailAdmin = 'admin@foodloop.net' ;
my $ passwordAdmin = 'ethics' ;
2017-04-20 17:15:34 +00:00
$ testJson = {
2017-02-24 17:09:37 +00:00
'usertype' = > 'customer' ,
2017-04-21 11:42:31 +00:00
'token' = > shift ( @ account_tokens ) ,
2017-02-24 17:09:37 +00:00
'username' = > 'admin' ,
'email' = > $ emailAdmin ,
'postcode' = > 'NW1 W01' ,
'password' = > $ passwordAdmin ,
'age' = > '35-50'
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-03-05 19:07:32 +00:00
print "test 4 - Making 'admin' an Admin\n" ;
2017-04-21 11:42:31 +00:00
my $ rufus_user = $ schema - > resultset ( 'User' ) - > find ( { email = > $ emailAdmin } ) ;
is $ schema - > resultset ( 'Administrator' ) - > count , 0 , "No admins" ;
$ rufus_user - > find_or_create_related ( 'administrator' , { } ) ;
is $ schema - > resultset ( 'Administrator' ) - > count , 1 , "1 admin" ;
2017-02-24 17:09:37 +00:00
######################################################
#Login as non-admin Reno
2017-03-05 19:07:32 +00:00
print "test 5 - Login - non-admin Reno (cookies, customer)\n" ;
2017-02-24 17:09:37 +00:00
$ testJson = {
'email' = > $ emailReno ,
'password' = > $ passwordReno ,
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/login' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-04-18 22:50:19 +00:00
my $ session_key = $ t - > tx - > res - > json ( '/session_key' ) ;
2017-03-05 19:07:32 +00:00
print "test 6 - add valid transaction (type 3: new organisation)\n" ;
2017-02-24 17:09:37 +00:00
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' ;
2017-04-20 17:15:34 +00:00
my $ json = {
2017-04-20 11:33:56 +00:00
transaction_value = > 20 ,
transaction_type = > 3 ,
organisation_name = > $ nameToTestTurtle ,
street_name = > "Town centre" ,
2017-02-24 17:09:37 +00:00
town = > " Wutai" ,
2017-04-20 00:49:34 +00:00
postcode = > "NW1 5RU" ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 11:33:56 +00:00
my $ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-04-20 00:49:34 +00:00
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-02-24 17:09:37 +00:00
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." ;
2017-04-20 00:49:34 +00:00
my $ newPendingTurtleOrgId = $ t - > app - > schema - > resultset ( 'PendingOrganisation' ) - > find ( { name = > $ nameToTestTurtle } ) - > pendingorganisationid ;
2017-03-05 17:32:38 +00:00
#print "Turtle Id: " . $newPendingTurtleOrgId . "\n";
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 7 - Non-admin (customer) tries to approve their organisation and fails.\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
unvalidatedOrganisationId = > $ newPendingTurtleOrgId ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > status_is ( 403 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/You are not an admin/ i ) ;
2017-03-05 19:07:32 +00:00
print "test 8 - Logout Reno\n" ;
2017-04-20 00:27:18 +00:00
$ t - > post_ok ( '/api/logout' , json = > { session_key = > $ session_key } )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
#End of non-admin Reno
######################################################
#Login as non-admin Choco Billy
2017-03-05 19:07:32 +00:00
print "test 9 - Login - non-admin Choco Billy (cookies, organisation)\n" ;
2017-02-24 17:09:37 +00:00
$ testJson = {
'email' = > $ emailBilly ,
'password' = > $ passwordBilly ,
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/login' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-04-20 00:49:34 +00:00
$ session_key = $ t - > tx - > res - > json ( '/session_key' ) ;
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 10 - add valid transaction (type 3: new organisation)\n" ;
2017-02-24 17:09:37 +00:00
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 = {
2017-04-20 11:33:56 +00:00
transaction_value = > 10 ,
transaction_type = > 3 ,
organisation_name = > $ nameToTestKalm ,
street_name = > "Town centre" ,
2017-02-24 17:09:37 +00:00
town = > "Kalm" ,
2017-04-20 00:49:34 +00:00
postcode = > "NW11 7GZ" ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 17:15:34 +00:00
$ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-04-20 00:49:34 +00:00
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-02-24 17:09:37 +00:00
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." ;
2017-04-20 00:49:34 +00:00
my $ newPendingKalmOrgId = $ t - > app - > schema - > resultset ( 'PendingOrganisation' ) - > find ( { name = > $ nameToTestKalm } ) - > pendingorganisationid ;
2017-03-05 17:32:38 +00:00
#print "Kalm Id: " . $newPendingKalmOrgId . "\n";
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 11 - add valid transaction (type 2: unvalidated organisation)\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
2017-04-20 11:33:56 +00:00
transaction_value = > 10 ,
transaction_type = > 2 ,
organisation_id = > $ newPendingKalmOrgId ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 17:15:34 +00:00
$ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-04-20 00:49:34 +00:00
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-02-24 17:09:37 +00:00
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." ;
2017-03-05 19:07:32 +00:00
print "test 12 - add valid transaction (type 3: new organisation)\n" ;
2017-02-24 17:09:37 +00:00
my $ nameToTestJunon = 'Store' ;
$ json = {
2017-04-20 11:33:56 +00:00
transaction_value = > 10 ,
transaction_type = > 3 ,
organisation_name = > $ nameToTestJunon ,
street_name = > "Main street" ,
2017-02-24 17:09:37 +00:00
town = > "Under Junon" ,
2017-04-20 00:49:34 +00:00
postcode = > "NW1W 7GF" ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 17:15:34 +00:00
$ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-04-20 00:49:34 +00:00
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-02-24 17:09:37 +00:00
2017-04-20 00:49:34 +00:00
my $ newPendingJunonOrgId = $ t - > app - > schema - > resultset ( 'PendingOrganisation' ) - > find ( { name = > $ nameToTestJunon } ) - > pendingorganisationid ;
2017-03-05 17:32:38 +00:00
#print "Junon Id: " . $newPendingJunonOrgId . "\n";
2017-02-24 17:09:37 +00:00
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." ;
2017-03-05 19:07:32 +00:00
print "test 13 - add valid transaction (type 2: unvalidated organisation)\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
2017-04-20 11:33:56 +00:00
transaction_value = > 20 ,
transaction_type = > 2 ,
organisation_id = > $ newPendingJunonOrgId ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 17:15:34 +00:00
$ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-03-05 17:32:38 +00:00
- > json_is ( '/success' , Mojo::JSON - > true )
- > json_hasnt ( '/unvalidatedOrganisationId' ) ;
2017-02-24 17:09:37 +00:00
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." ;
2017-03-05 17:32:38 +00:00
2017-03-05 19:07:32 +00:00
print "test 14 - add valid transaction (type 2: unvalidated organisation)\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
2017-04-20 11:33:56 +00:00
transaction_value = > 30 ,
transaction_type = > 2 ,
organisation_id = > $ newPendingJunonOrgId ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-20 17:15:34 +00:00
$ upload = { json = > Mojo::JSON:: encode_json ( $ json ) , file = > { file = > './t/test.jpg' } } ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/upload' = > form = > $ upload )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
2017-03-05 17:32:38 +00:00
- > json_is ( '/success' , Mojo::JSON - > true )
- > json_hasnt ( '/unvalidatedOrganisationId' ) ;
2017-02-24 17:09:37 +00:00
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." ;
2017-03-05 19:07:32 +00:00
print "test 15 - Non-admin (organisation) tries to approve their organisation and fails.\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
unvalidatedOrganisationId = > $ newPendingKalmOrgId ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > status_is ( 403 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/You are not an admin/ i ) ;
2017-03-05 19:07:32 +00:00
print "test 16 - Logout Choco Billy\n" ;
2017-04-20 00:27:18 +00:00
$ t - > post_ok ( '/api/logout' , json = > { session_key = > $ session_key } )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
#End of non-admin Choco Billy
######################################################
#Login as Admin
2017-03-05 19:07:32 +00:00
print "test 17 - Login - admin\n" ;
2017-02-24 17:09:37 +00:00
$ testJson = {
'email' = > $ emailAdmin ,
'password' = > $ passwordAdmin ,
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/login' = > json = > $ testJson )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-04-18 22:50:19 +00:00
$ session_key = $ t - > tx - > res - > json ( '/session_key' ) ;
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 18 - JSON is missing.\n" ;
2017-04-20 17:15:34 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > { } )
2017-04-20 00:27:18 +00:00
- > status_is ( 400 )
2017-02-24 17:09:37 +00:00
- > json_is ( '/success' , Mojo::JSON - > false )
2017-04-20 00:27:18 +00:00
- > json_like ( '/message' , qr/JSON is missing/ i ) ;
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 19 - unvalidatedOrganisationId missing (non-modify).\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > status_is ( 400 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/unvalidatedOrganisationId is missing/ i ) ;
2017-03-05 19:07:32 +00:00
print "test 20 - unvalidatedOrganisationId not number (non-modify).\n" ;
2017-02-24 17:09:37 +00:00
$ json = {
unvalidatedOrganisationId = > 'Abc' ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > status_is ( 400 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/unvalidatedOrganisationId does not look like a number/ i ) ;
2017-03-05 19:07:32 +00:00
print "test 21 - unvalidatedOrganisationId does not exist (non-modify).\n" ;
2017-02-24 17:09:37 +00:00
my ( $ maxPendingId ) = $ t - > app - > db - > selectrow_array ( "SELECT MAX(PendingOrganisationId) FROM PendingOrganisations" , undef , ( ) ) ;
$ json = {
unvalidatedOrganisationId = > ( $ maxPendingId + 1 ) ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > 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.
2017-03-05 19:07:32 +00:00
print "test 22 - valid approval (non-modify).\n" ;
2017-02-24 17:09:37 +00:00
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 ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-04-21 11:42:31 +00:00
- > status_is ( 200 ) - > or ( $ dump_error )
2017-02-24 17:09:37 +00:00
- > 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." ;
2017-03-05 19:07:32 +00:00
print "test 23 - valid approval (modify all).\n" ;
2017-02-24 17:09:37 +00:00
#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 ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-02-24 17:09:37 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-02-24 17:09:37 +00:00
- > status_is ( 200 )
- > json_is ( '/success' , Mojo::JSON - > true ) ;
2017-03-05 17:32:38 +00:00
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." ;
2017-02-24 17:09:37 +00:00
is @ { $ t - > app - > db - > selectrow_arrayref ( "SELECT COUNT(*) FROM PendingOrganisations WHERE Name = ?" , undef , ( $ nameToTestTurtle ) ) } [ 0 ] , 0 , "Turtle does not exist in pending orgs." ;
2017-03-05 19:07:32 +00:00
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." ;
2017-03-05 16:01:44 +00:00
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." ;
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
print "test 24 - valid approval (modify some).\n" ;
#TODO if we implement constraints on the input data this will fail
2017-04-20 17:15:34 +00:00
$ testName = "Change testing junon name" ;
2017-03-05 19:07:32 +00:00
$ json = {
unvalidatedOrganisationId = > $ newPendingJunonOrgId ,
name = > $ testName ,
2017-04-18 22:50:19 +00:00
session_key = > $ session_key ,
2017-03-05 19:07:32 +00:00
} ;
2017-04-06 21:43:27 +00:00
$ t - > post_ok ( '/api/admin-approve' = > json = > $ json )
2017-03-05 19:07:32 +00:00
- > 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." ;
2017-02-24 17:09:37 +00:00
2017-03-05 19:07:32 +00:00
##############################################
2017-02-24 17:09:37 +00:00
done_testing ( ) ;