Refactor upload API to easier to read variables

This commit is contained in:
Tom Bloor 2017-04-20 12:33:56 +01:00
parent ab9bfdf221
commit 7f18aa7173
6 changed files with 220 additions and 226 deletions

View file

@ -4,7 +4,7 @@ use Data::Dumper;
=head2 post_upload
Takes a file upload, with a file key of 'file2', and a json string under the
Takes a file upload, with a file key of 'file', and a json string under the
'json' key.
The json string should be an object, with the following keys:
@ -45,30 +45,25 @@ The postcode of an organisation, optional key. Used when transaction_Type is 3.
has error_messages => sub {
return {
transactionAdditionType => {
required => { message => 'transactionAdditionType is missing.', status => 400 },
in => { message => 'transactionAdditionType is not a valid value.', status => 400 },
transaction_type => {
required => { message => 'transaction_type is missing.', status => 400 },
in => { message => 'transaction_type is not a valid value.', status => 400 },
},
microCurrencyValue => {
required => { message => 'microCurrencyValue is missing', status => 400 },
number => { message => 'microCurrencyValue does not look like a number', status => 400 },
gt_num => { message => 'microCurrencyValue cannot be equal to or less than zero', status => 400 },
transaction_value => {
required => { message => 'transaction_value is missing', status => 400 },
number => { message => 'transaction_value does not look like a number', status => 400 },
gt_num => { message => 'transaction_value cannot be equal to or less than zero', status => 400 },
},
file2 => {
file => {
required => { message => 'No file uploaded', status => 400 },
},
addValidatedId => {
required => { message => 'addValidatedId is missing', status => 400 },
organisation_id => {
required => { message => 'organisation_id is missing', status => 400 },
number => { message => 'organisation_id is not a number', status => 400 },
in_resultset => { message => 'addValidatedId does not exist in the database', status => 400 },
in_resultset => { message => 'organisation_id does not exist in the database', status => 400 },
},
addUnvalidatedId => {
required => { message => 'addUnvalidatedId is missing', status => 400 },
number => { message => 'addUnvalidatedId does not look like a number', status => 400 },
in_resultset => { message => 'addUnvalidatedId does not exist in the database for the user', status => 400 },
},
organisationName => {
required => { message => 'organisationName is missing', status => 400 },
organisation_name => {
required => { message => 'organisation_name is missing', status => 400 },
},
};
};
@ -82,44 +77,43 @@ sub post_upload {
my $validation = $c->validation;
# Test for file before loading the JSON in to the validator
$validation->required('file2');
$validation->required('file');
$validation->input( $c->stash->{api_json} );
$validation->required('microCurrencyValue')->number->gt_num(0);
$validation->required('transactionAdditionType')->in( 1, 2, 3 );
$validation->required('transaction_value')->number->gt_num(0);
$validation->required('transaction_type')->in( 1, 2, 3 );
# First pass of required items
return $c->api_validation_error if $validation->has_error;
my $type = $validation->param('transactionAdditionType');
my $type = $validation->param('transaction_type');
if ( $type == 1 ) {
# Validated Organisation
my $valid_org_rs = $c->schema->resultset('Organisation');
$validation->required('addValidatedId')->number->in_resultset( 'organisationalid', $valid_org_rs );
$validation->required('organisation_id')->number->in_resultset( 'organisationalid', $valid_org_rs );
} elsif ( $type == 2 ) {
# Unvalidated Organisation
my $valid_org_rs = $c->schema->resultset('PendingOrganisation')->search({ usersubmitted_fk => $user->id });
$validation->required('addUnvalidatedId')->number->in_resultset( 'pendingorganisationid', $valid_org_rs );
$validation->required('organisation_id')->number->in_resultset( 'pendingorganisationid', $valid_org_rs );
} elsif ( $type == 3 ) {
# Unknown Organisation
$validation->required('organisationName');
$validation->optional('streetName');
$validation->required('organisation_name');
$validation->optional('street_name');
$validation->optional('town');
$validation->optional('postcode')->postcode;
}
return $c->api_validation_error if $validation->has_error;
my $transactionAdditionType = $type;
my $microCurrencyValue = $validation->param('microCurrencyValue');
my $transaction_value = $validation->param('transaction_value');
my $json = $c->stash->{api_json};
my $userId = $user->id;
my $file = $self->req->upload('file2');
my $file = $self->req->upload('file');
my $ext = '.jpg';
my $uuid = Data::UUID->new->create_str;
@ -140,8 +134,8 @@ sub post_upload {
# Validated organisation
$c->schema->resultset('Transaction')->create({
buyeruserid_fk => $user->id,
sellerorganisationid_fk => $validation->param('addValidatedId'),
valuemicrocurrency => $microCurrencyValue,
sellerorganisationid_fk => $validation->param('organisation_id'),
valuemicrocurrency => $transaction_value,
proofimage => $filename,
timedatesubmitted => DateTime->now,
});
@ -151,23 +145,23 @@ sub post_upload {
# Unvalidated Organisation
$c->schema->resultset('PendingTransaction')->create({
buyeruserid_fk => $user->id,
pendingsellerorganisationid_fk => $validation->param('addUnvalidatedId'),
valuemicrocurrency => $microCurrencyValue,
pendingsellerorganisationid_fk => $validation->param('organisation_id'),
valuemicrocurrency => $transaction_value,
proofimage => $filename,
timedatesubmitted => DateTime->now,
});
$file->move_to('images/' . $filename);
} elsif ( $type == 3 ) {
my $organisationName = $validation->param('organisationName');
my $streetName = $validation->param('streetName');
my $organisation_name = $validation->param('organisation_name');
my $street_name = $validation->param('street_name');
my $town = $validation->param('town');
my $postcode = $validation->param('postcode');
my $fullAddress = "";
if ( defined $streetName && ! ($streetName =~ m/^\s*$/) ){
$fullAddress = $streetName;
if ( defined $street_name && ! ($street_name =~ m/^\s*$/) ){
$fullAddress = $street_name;
}
if ( defined $town && ! ($town =~ m/^\s*$/) ){
@ -183,7 +177,7 @@ sub post_upload {
my $pending_org = $c->schema->resultset('PendingOrganisation')->create({
usersubmitted_fk => $user->id,
timedatesubmitted => DateTime->now,
name => $organisationName,
name => $organisation_name,
fulladdress => $fullAddress,
postcode => $postcode,
});
@ -191,7 +185,7 @@ sub post_upload {
$c->schema->resultset('PendingTransaction')->create({
buyeruserid_fk => $user->id,
pendingsellerorganisationid_fk => $pending_org->pendingorganisationid,
valuemicrocurrency => $microCurrencyValue,
valuemicrocurrency => $transaction_value,
proofimage => $filename,
timedatesubmitted => DateTime->now,
});

View file

@ -113,15 +113,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef
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",
transaction_value => 20,
transaction_type => 3,
organisation_name => $nameToTestTurtle,
street_name => "Town centre",
town => " Wutai",
postcode => "NW1 5RU",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -172,15 +172,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef
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",
transaction_value => 10,
transaction_type => 3,
organisation_name => $nameToTestKalm,
street_name => "Town centre",
town => "Kalm",
postcode => "NW11 7GZ",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -195,12 +195,12 @@ my $newPendingKalmOrgId = $t->app->schema->resultset('PendingOrganisation')->fin
print "test 11 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingKalmOrgId,
transaction_value => 10,
transaction_type => 2,
organisation_id => $newPendingKalmOrgId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -212,15 +212,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 12 - add valid transaction (type 3: new organisation)\n";
my $nameToTestJunon = 'Store';
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => $nameToTestJunon,
streetName => "Main street",
transaction_value => 10,
transaction_type => 3,
organisation_name => $nameToTestJunon,
street_name => "Main street",
town => "Under Junon",
postcode => "NW1W 7GF",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -236,12 +236,12 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 13 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 20,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingJunonOrgId,
transaction_value => 20,
transaction_type => 2,
organisation_id => $newPendingJunonOrgId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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)
@ -254,12 +254,12 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 14 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 30,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingJunonOrgId,
transaction_value => 30,
transaction_type => 2,
organisation_id => $newPendingJunonOrgId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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)

View file

@ -111,15 +111,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Organisations", undef
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",
transaction_value => 20,
transaction_type => 3,
organisation_name => $nameToTestTurtle,
street_name => "Town centre",
town => " Wutai",
postcode => "NW10 8HH",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -161,15 +161,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
my $nameToTestTurtlePartial = 'Turtle\'s Paradise2';
$json = {
microCurrencyValue => 20,
transactionAdditionType => 3,
organisationName => $nameToTestTurtlePartial,
streetName => "",
transaction_value => 20,
transaction_type => 3,
organisation_name => $nameToTestTurtlePartial,
street_name => "",
town => "",
postcode => "",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -187,12 +187,12 @@ print "Turtle Id 2: " . $newPendingTurtleOrgIdPartial . "\n";
print "test 10 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingTurtleOrgIdPartial,
transaction_value => 10,
transaction_type => 2,
organisation_id => $newPendingTurtleOrgIdPartial,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -205,15 +205,15 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 11 - add valid transaction (type 3: new organisation)\n";
my $nameToTestJunon = 'Store';
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => $nameToTestJunon,
streetName => "Main street",
transaction_value => 10,
transaction_type => 3,
organisation_name => $nameToTestJunon,
street_name => "Main street",
town => "Under Junon",
postcode => "NW9 5EB",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -229,12 +229,12 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 12 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 20,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingJunonOrgId,
transaction_value => 20,
transaction_type => 2,
organisation_id => $newPendingJunonOrgId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -246,12 +246,12 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions", undef,
print "test 13 - add valid transaction (type 2: unvalidated organisation)\n";
$json = {
microCurrencyValue => 30,
transactionAdditionType => 2,
addUnvalidatedId => $newPendingJunonOrgId,
transaction_value => 30,
transaction_type => 2,
organisation_id => $newPendingJunonOrgId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);

View file

@ -132,15 +132,15 @@ login_rufus();
print "test 4 - Added something containing 'fish'\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => 'Shoreway Fisheries',
streetName => "2 James St",
transaction_value => 10,
transaction_type => 3,
organisation_name => 'Shoreway Fisheries',
street_name => "2 James St",
town => "Lancaster",
postcode => "LA1 1UP",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -159,30 +159,30 @@ login_billy();
print "test 7 - Added something containing 'bar'\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => 'The Palatine Bar',
streetName => "The Crescent",
transaction_value => 10,
transaction_type => 3,
organisation_name => 'The Palatine Bar',
street_name => "The Crescent",
town => "Morecambe",
postcode => "LA4 5BZ",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
print "test 8 - Added another thing containing 'bar'\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => 'The Sun Hotel & Bar',
streetName => "63-65 Church Street",
transaction_value => 10,
transaction_type => 3,
organisation_name => 'The Sun Hotel & Bar',
street_name => "63-65 Church Street",
town => "Lancaster",
postcode => "LA1 1ET",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);

View file

@ -98,7 +98,7 @@ $t->post_ok('/api/login' => json => $testJson)
->json_is('/success', Mojo::JSON->true);
my $session_key = $t->tx->res->json('/session_key');
print "test 5 - JSON missing\n";
my $upload = {file2 => {file => './t/test.jpg'}};
my $upload = {file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
@ -106,86 +106,86 @@ $t->post_ok('/api/upload' => form => $upload )
#TODO Check for malformed JSON.
print "test 6 - microCurrencyValue missing\n";
print "test 6 - transaction_value missing\n";
my $json = {
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/microCurrencyValue is missing/i);
->content_like(qr/transaction_value is missing/i);
print "test 7 - microCurrencyValue non-numbers\n";
print "test 7 - transaction_value non-numbers\n";
$json = {
microCurrencyValue => 'Abc',
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_value => 'Abc',
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/microCurrencyValue does not look like a number/i);
->content_like(qr/transaction_value does not look like a number/i);
print "test 8 - microCurrencyValue equal to zero\n";
print "test 8 - transaction_value equal to zero\n";
$json = {
microCurrencyValue => 0,
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_value => 0,
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/microCurrencyValue cannot be equal to or less than zero/i);
->content_like(qr/transaction_value cannot be equal to or less than zero/i);
print "test 9 - microCurrencyValue less than zero\n";
print "test 9 - transaction_value less than zero\n";
$json = {
microCurrencyValue => -1,
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_value => -1,
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/microCurrencyValue cannot be equal to or less than zero/i);
->content_like(qr/transaction_value cannot be equal to or less than zero/i);
print "test 10 - transactionAdditionType missing\n";
print "test 10 - transaction_type missing\n";
$json = {
microCurrencyValue => 10,
transaction_value => 10,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/transactionAdditionType is missing/i);
->content_like(qr/transaction_type is missing/i);
print "test 11 - transactionAdditionType invalid.\n";
print "test 11 - transaction_type invalid.\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 4,
transaction_value => 10,
transaction_type => 4,
session_key => $session_key,
# addValidatedId => $companyIdNumShinra
# organisation_id => $companyIdNumShinra
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/transactionAdditionType is not a valid value/i);
->content_like(qr/transaction_type is not a valid value/i);
print "test 12 - file not uploaded.\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 1,
addValidatedId => 1,
transaction_value => 10,
transaction_type => 1,
organisation_id => 1,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json)};
@ -194,41 +194,41 @@ $t->post_ok('/api/upload' => form => $upload )
->json_is('/success', Mojo::JSON->false)
->content_like(qr/no file uploaded/i);
print "test 13 - addValidatedId missing (type 1: already validated)\n";
print "test 13 - organisation_id missing (type 1: already validated)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 1,
transaction_value => 10,
transaction_type => 1,
session_key => $session_key,
# addValidatedId => $companyIdNumShinra
# organisation_id => $companyIdNumShinra
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addValidatedId is missing/i);
->content_like(qr/organisation_id is missing/i);
print "test 14 - addValidatedId for non-existent id. (type 1: already validated)\n";
print "test 14 - organisation_id for non-existent id. (type 1: already validated)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 1,
addValidatedId => ($companyIdNumShinra + 100),
transaction_value => 10,
transaction_type => 1,
organisation_id => ($companyIdNumShinra + 100),
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addValidatedId does not exist in the database/i);
->content_like(qr/organisation_id does not exist in the database/i);
print "test 15 - valid addition. (type 1: already validated)\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions")}[0],0,"no transactions";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_value => 10,
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$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)
@ -239,33 +239,33 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions")}[0],1,
print "test 16 - organsation missing (type 3: new organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
streetName => "Slums, Sector 7",
transaction_value => 10,
transaction_type => 3,
street_name => "Slums, Sector 7",
town => "Midgar",
postcode => "E1 0AA",
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisationName is missing/i);
->content_like(qr/organisation_name is missing/i);
print "test 17 - add valid transaction (type 3: new organisation)\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations")}[0],0,"No pending organisations";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions")}[0],0,"No pending transactions";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => '7th Heaven',
streetName => "Slums, Sector 7",
transaction_value => 10,
transaction_type => 3,
organisation_name => '7th Heaven',
street_name => "Slums, Sector 7",
town => "Midgar",
postcode => "E1 0AA",
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$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)
@ -275,43 +275,43 @@ is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions")
# Add type 2 (unverified organisation) checking.
print "test 18 - addUnvalidatedId missing (type 2: existing organisation)\n";
print "test 18 - organisation_id missing (type 2: existing organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
transaction_value => 10,
transaction_type => 2,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addUnvalidatedId is missing/i);
->content_like(qr/organisation_id is missing/i);
print "test 19 - addUnvalidatedId not a number (type 2: existing organisation)\n";
print "test 19 - organisation_id not a number (type 2: existing organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => "Abc",
transaction_value => 10,
transaction_type => 2,
organisation_id => "Abc",
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addUnvalidatedId does not look like a number/i);
->content_like(qr/organisation_id is not a number/i);
print "test 20 - id does not exist (type 2: existing organisation)\n";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => 1000, #Id that does not exist
transaction_value => 10,
transaction_type => 2,
organisation_id => 1000, #Id that does not exist
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addUnvalidatedId does not exist in the database for the user/i);
->content_like(qr/organisation_id does not exist in the database/i);
print "test 21 - Logout Rufus (type 2: existing organisation)\n";
$t->post_ok('/api/logout', json => { session_key => $session_key } )
@ -339,16 +339,16 @@ my $org_result = $schema->resultset('PendingOrganisation')->find({ name => '7th
my $unvalidatedOrganisationId = $org_result->pendingorganisationid;
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions")}[0],1,"1 pending transaction";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 10,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->content_like(qr/addUnvalidatedId does not exist in the database for the user/i);
->content_like(qr/organisation_id does not exist in the database/i);
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions")}[0],1,"1 pending transaction";
print "test 24 - Logout Hojo\n";
@ -375,12 +375,12 @@ $session_key = $t->tx->res->json('/session_key');
print "test 26 - add valid transaction (type 2: existing organisation)\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions")}[0],1,"1 pending transaction";
$json = {
microCurrencyValue => 10,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 10,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$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)
@ -412,12 +412,12 @@ $session_key = $t->tx->res->json('/session_key');
print "test 29 - organisation buy from another organisation\n";
is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Transactions")}[0],1,"1 transaction";
$json = {
microCurrencyValue => 100000,
transactionAdditionType => 1,
addValidatedId => $companyIdNumShinra,
transaction_value => 100000,
transaction_type => 1,
organisation_id => $companyIdNumShinra,
session_key => $session_key,
};
$upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
$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)

View file

@ -159,15 +159,15 @@ login_reno();
print "test 6 - Reno spends at Turtle\'s Paradise\n";
my $nameToTestTurtle = 'Turtle\'s Paradise';
$json = {
microCurrencyValue => 10,
transactionAdditionType => 3,
organisationName => $nameToTestTurtle,
streetName => "Town centre",
transaction_value => 10,
transaction_type => 3,
organisation_name => $nameToTestTurtle,
street_name => "Town centre",
town => " Wutai",
postcode => "NW11 8AD",
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -178,24 +178,24 @@ Time::Fake->offset("+" . $dateTimePlusTwoDaysSecondsDiff . "s");
print "test 7 - Reno spends at Turtle\'s Paradise, 2 days later, transaction 1/2\n";
$json = {
microCurrencyValue => 20,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 20,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
print "test 8 - Reno spends at Turtle\'s Paradise, 2 days later, transaction 2/2\n";
$json = {
microCurrencyValue => 40,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 40,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -211,12 +211,12 @@ login_reno();
print "test 11 - Reno spends at Turtle\'s Paradise, 1 month later\n";
$json = {
microCurrencyValue => 80,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 80,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -232,12 +232,12 @@ login_reno();
print "test 14 - Reno spends at Turtle\'s Paradise, 1 year later\n";
$json = {
microCurrencyValue => 160,
transactionAdditionType => 2,
addUnvalidatedId => $unvalidatedOrganisationId,
transaction_value => 160,
transaction_type => 2,
organisation_id => $unvalidatedOrganisationId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);
@ -271,12 +271,12 @@ login_chocobilly();
print "test 20 - Chocobilly spends at Turtle\'s Paradise, 2 days later\n";
#Added to test and see if the later values from different users merge together. They shouldn't
$json = {
microCurrencyValue => 320,
transactionAdditionType => 1,
addValidatedId => $validatedOrganisationId,
transaction_value => 320,
transaction_type => 1,
organisation_id => $validatedOrganisationId,
session_key => $session_key,
};
my $upload = {json => Mojo::JSON::encode_json($json), file2 => {file => './t/test.jpg'}};
my $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);