Merge pull request #59 from Pear-Trading/finn/betterErrors

Errors for API improved
This commit is contained in:
Finn 2017-09-18 12:48:15 +01:00 committed by GitHub
commit 3b9383fc3d
6 changed files with 49 additions and 46 deletions

View file

@ -5,7 +5,7 @@ has error_messages => sub {
return { return {
email => { email => {
required => { message => 'Email is required', status => 400 }, required => { message => 'Email is required', status => 400 },
in_resultset => { message => 'Change meeee', status => 400 }, in_resultset => { message => 'Email does not exist in database', status => 400 },
}, },
feedbacktext => { feedbacktext => {
required => { message => 'Feedback is required', status => 400 }, required => { message => 'Feedback is required', status => 400 },

View file

@ -9,18 +9,18 @@ has error_messages => sub {
in_resultset => { message => 'Token invalid or has been used.', status => 401 }, in_resultset => { message => 'Token invalid or has been used.', status => 401 },
}, },
name => { name => {
required => { message => 'No name sent or was blank.', status => 400 }, required => { message => 'No organisation name sent or was blank.', status => 400 },
}, },
display_name => { display_name => {
required => { message => 'No name sent or was blank.', status => 400 }, required => { message => 'No display name sent or was blank.', status => 400 },
}, },
full_name => { full_name => {
required => { message => 'No name sent or was blank.', status => 400 }, required => { message => 'No full name sent or was blank.', status => 400 },
}, },
email => { email => {
required => { message => 'No email sent.', status => 400 }, required => { message => 'No email sent.', status => 400 },
email => { message => 'Email is invalid.', status => 400 }, email => { message => 'Email is invalid.', status => 400 },
not_in_resultset => { message => 'Email exists.', status => 403 }, not_in_resultset => { message => 'Email already in use.', status => 403 },
}, },
postcode => { postcode => {
required => { message => 'No postcode sent.', status => 400 }, required => { message => 'No postcode sent.', status => 400 },
@ -34,16 +34,16 @@ has error_messages => sub {
in => { message => '"usertype" is invalid.', status => 400 }, in => { message => '"usertype" is invalid.', status => 400 },
}, },
year_of_birth => { year_of_birth => {
required => { message => 'No year_of_birth sent.', status => 400 }, required => { message => 'No year of birth sent.', status => 400 },
number => { message => 'year_of_birth is invalid', status => 400 }, number => { message => 'year of birth is invalid', status => 400 },
gt_num => { message => 'year_of_birth must be within last 150 years', status => 400 }, gt_num => { message => 'year of birth must be within last 150 years', status => 400 },
lt_num => { message => 'year_of_birth must be atleast 10 years ago', status => 400 }, lt_num => { message => 'year of birth must be atleast 10 years ago', status => 400 },
}, },
street_name => { street_name => {
required => { message => 'No street_name sent.', status => 400 }, required => { message => 'No street name sent.', status => 400 },
}, },
town => { town => {
required => { message => 'No town sent.', status => 400 }, required => { message => 'No town/city sent.', status => 400 },
}, },
}; };
}; };

View file

@ -50,17 +50,17 @@ The postcode of an organisation, optional key. Used when transaction_Type is 3.
has error_messages => sub { has error_messages => sub {
return { return {
transaction_type => { transaction_type => {
required => { message => 'transaction_type is missing.', status => 400 }, required => { message => 'transaction type is missing.', status => 400 },
in => { message => 'transaction_type is not a valid value.', status => 400 }, in => { message => 'transaction type is not a valid value.', status => 400 },
}, },
transaction_value => { transaction_value => {
required => { message => 'transaction_value is missing', status => 400 }, required => { message => 'transaction amount is missing', status => 400 },
number => { message => 'transaction_value does not look like a number', status => 400 }, number => { message => 'transaction amount does not look like a number', status => 400 },
gt_num => { message => 'transaction_value cannot be equal to or less than zero', status => 400 }, gt_num => { message => 'transaction amount cannot be equal to or less than zero', status => 400 },
}, },
purchase_time => { purchase_time => {
required => { message => 'purchase_time is missing', status => 400 }, required => { message => 'purchase time is missing', status => 400 },
is_full_iso_datetime => { message => 'purchase_time is in incorrect format', status => 400 }, is_full_iso_datetime => { message => 'purchase time is in incorrect format', status => 400 },
}, },
file => { file => {
required => { message => 'No file uploaded', status => 400 }, required => { message => 'No file uploaded', status => 400 },
@ -68,15 +68,18 @@ has error_messages => sub {
filetype => { message => 'File must be of type image/jpeg', status => 400 }, filetype => { message => 'File must be of type image/jpeg', status => 400 },
}, },
organisation_id => { organisation_id => {
required => { message => 'organisation_id is missing', status => 400 }, required => { message => 'existing organisation ID is missing', status => 400 },
number => { message => 'organisation_id is not a number', status => 400 }, number => { message => 'organisation ID is not a number', status => 400 },
in_resultset => { message => 'organisation_id does not exist in the database', status => 400 }, in_resultset => { message => 'organisation ID does not exist in the database', status => 400 },
}, },
organisation_name => { organisation_name => {
required => { message => 'organisation_name is missing', status => 400 }, required => { message => 'organisation name is missing', status => 400 },
},
town => {
required => { message => 'town/city is missing', status => 400 },
}, },
search_name => { search_name => {
required => { message => 'search_name is missing', status => 400 }, required => { message => 'search name is missing', status => 400 },
}, },
postcode => { postcode => {
required => { message => 'postcode is missing', status => 400 }, required => { message => 'postcode is missing', status => 400 },
@ -139,7 +142,7 @@ sub post_upload {
# Unknown Organisation # Unknown Organisation
$validation->required('organisation_name'); $validation->required('organisation_name');
$validation->optional('street_name'); $validation->optional('street_name');
$validation->optional('town'); $validation->required('town');
$validation->optional('postcode')->postcode; $validation->optional('postcode')->postcode;
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;

View file

@ -28,10 +28,10 @@ has error_messages => sub {
required => { message => 'No password sent.', status => 400 }, required => { message => 'No password sent.', status => 400 },
}, },
street_name => { street_name => {
required => { message => 'No street_name sent.', status => 400 }, required => { message => 'No street name sent.', status => 400 },
}, },
town => { town => {
required => { message => 'No town sent.', status => 400 }, required => { message => 'No town/city sent.', status => 400 },
}, },
sector => { sector => {
required => { message => 'No sector sent.', status => 400 }, required => { message => 'No sector sent.', status => 400 },

View file

@ -73,7 +73,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no name sent/i); ->content_like(qr/no display name sent/i);
#name missing JSON #name missing JSON
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
@ -87,7 +87,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no name sent/i); ->content_like(qr/no full name sent/i);
#Blank name #Blank name
$testJson = { $testJson = {
@ -235,7 +235,7 @@ $t->post_ok('/api/register' => json => $testJson)
->status_is(403) ->status_is(403)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/email/i) ->content_like(qr/email/i)
->content_like(qr/exists/i); ->content_like(qr/already in use/i);
#postcode missing JSON #postcode missing JSON
$testJson = { $testJson = {
@ -315,7 +315,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no year_of_birth sent/i); ->content_like(qr/no year of birth sent/i);
#Age is invalid #Age is invalid
$testJson = { $testJson = {
@ -331,7 +331,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/year_of_birth/i) ->content_like(qr/year of birth/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#full address missing JSON #full address missing JSON
@ -347,7 +347,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no street_name sent/i); ->content_like(qr/no street name sent/i);
#TODO Validation of full address #TODO Validation of full address

View file

@ -123,7 +123,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_value is missing/i); ->content_like(qr/transaction amount is missing/i);
print "test 7 - transaction_value non-numbers\n"; print "test 7 - transaction_value non-numbers\n";
$json = { $json = {
@ -137,7 +137,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_value does not look like a number/i); ->content_like(qr/transaction amount does not look like a number/i);
print "test 8 - transaction_value equal to zero\n"; print "test 8 - transaction_value equal to zero\n";
$json = { $json = {
@ -151,7 +151,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_value cannot be equal to or less than zero/i); ->content_like(qr/transaction amount cannot be equal to or less than zero/i);
print "test 9 - transaction_value less than zero\n"; print "test 9 - transaction_value less than zero\n";
$json = { $json = {
@ -165,7 +165,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_value cannot be equal to or less than zero/i); ->content_like(qr/transaction amount cannot be equal to or less than zero/i);
print "test 10 - transaction_type missing\n"; print "test 10 - transaction_type missing\n";
$json = { $json = {
@ -177,7 +177,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_type is missing/i); ->content_like(qr/transaction type is missing/i);
print "test 11 - transaction_type invalid.\n"; print "test 11 - transaction_type invalid.\n";
$json = { $json = {
@ -191,7 +191,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/transaction_type is not a valid value/i); ->content_like(qr/transaction type is not a valid value/i);
print "test 12 - file not uploaded.\n"; print "test 12 - file not uploaded.\n";
$json = { $json = {
@ -220,7 +220,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id is missing/i); ->content_like(qr/organisation ID is missing/i);
print "test 14 - organisation_id for non-existent id. (type 1: already validated)\n"; print "test 14 - organisation_id for non-existent id. (type 1: already validated)\n";
$json = { $json = {
@ -234,7 +234,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id 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"; print "test 15 - valid addition. (type 1: already validated)\n";
is $schema->resultset('Transaction')->count, 1, "1 transaction"; is $schema->resultset('Transaction')->count, 1, "1 transaction";
@ -268,7 +268,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_name is missing/i); ->content_like(qr/organisation name is missing/i);
print "test 17 - add valid transaction (type 3: new organisation)\n"; print "test 17 - add valid transaction (type 3: new organisation)\n";
is $schema->resultset('Organisation')->search({ pending => 1 })->count, 0, "No pending organisations"; is $schema->resultset('Organisation')->search({ pending => 1 })->count, 0, "No pending organisations";
@ -306,7 +306,7 @@ $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->or($framework->dump_error) ->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id is missing/i); ->content_like(qr/organisation ID is missing/i);
print "test 19 - organisation_id not a number (type 2: existing organisation)\n"; print "test 19 - organisation_id not a number (type 2: existing organisation)\n";
$json = { $json = {
@ -320,7 +320,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id is not 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"; print "test 20 - id does not exist (type 2: existing organisation)\n";
$json = { $json = {
@ -334,7 +334,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id does not exist in the database/i); ->content_like(qr/organisation ID does not exist in the database/i);
print "test 21 - purchase_time is missing\n"; print "test 21 - purchase_time is missing\n";
is $schema->resultset('Organisation')->search({ pending => 1 })->entity->sales->count, 1, "1 pending transactions"; is $schema->resultset('Organisation')->search({ pending => 1 })->entity->sales->count, 1, "1 pending transactions";
@ -385,7 +385,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/organisation_id does not exist in the database/i); ->content_like(qr/organisation ID does not exist in the database/i);
is $schema->resultset('Organisation')->search({ pending => 1 })->entity->sales->count, 1, "1 pending transactions"; is $schema->resultset('Organisation')->search({ pending => 1 })->entity->sales->count, 1, "1 pending transactions";
print "test 25 - Logout Hojo\n"; print "test 25 - Logout Hojo\n";
@ -475,7 +475,7 @@ $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jp
$t->post_ok('/api/upload' => form => $upload ) $t->post_ok('/api/upload' => form => $upload )
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/organisation_id does not exist in the database/); ->json_like('/message', qr/organisation ID does not exist in the database/);
is $schema->resultset('Transaction')->count, 6, "6 transaction"; is $schema->resultset('Transaction')->count, 6, "6 transaction";
done_testing(); done_testing();