Merge pull request #60 from Pear-Trading/finn/FeedbackErrors

Added error rewrites and added feedback test
This commit is contained in:
Finn 2017-09-18 14:00:55 +01:00 committed by GitHub
commit 7366c5035b
3 changed files with 149 additions and 3 deletions

View file

@ -67,7 +67,7 @@ sub startup {
json => {
success => Mojo::JSON->false,
message => $c->error_messages->{$val}->{$check}->{message},
error => $check,
error => $c->error_messages->{$val}->{$check}->{error} || $check,
},
status => $c->error_messages->{$val}->{$check}->{status},
);

View file

@ -4,8 +4,8 @@ use Mojo::Base 'Mojolicious::Controller';
has error_messages => sub {
return {
email => {
required => { message => 'Email is required', status => 400 },
in_resultset => { message => 'Email does not exist in database', status => 400 },
required => { message => 'Email is required or not registered', status => 400 },
in_resultset => { message => 'Email is required or not registered', status => 400, error => "required" },
},
feedbacktext => {
required => { message => 'Feedback is required', status => 400 },

146
t/api/feedback.t Normal file
View file

@ -0,0 +1,146 @@
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('users');
my $t = $framework->framework;
my $schema = $t->app->schema;
#test email errors
$t->post_ok('/api/feedback' => json => {
email => '',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Email is required or not registered/);
$t->post_ok('/api/feedback' => json => {
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Email is required or not registered/);
$t->post_ok('/api/feedback' => json => {
email => 'banana',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Email is required or not registered/);
$t->post_ok('/api/feedback' => json => {
email => 'test21318432148@example.com',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Email is required or not registered/);
# Test for missing feedback
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Feedback is required/);
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => '',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Feedback is required/);
# Test for missing extra details
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => 'banana',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/App Name is required/);
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Package Name is required/);
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_number => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Version Code is required/);
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Version Number is required/);
# Valid Feedback
$t->post_ok('/api/feedback' => json => {
email => 'test1@example.com',
feedbacktext => 'banana',
app_name => 'Foodloop Web',
package_name => 'Foodloop Web',
version_code => 'dev',
version_number => 'dev',
})
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true);
done_testing;