This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/t/api/search.t

176 lines
4.8 KiB
Perl
Raw Normal View History

use Mojo::Base -strict;
2017-04-21 16:23:41 +00:00
2017-08-31 16:53:54 +00:00
use FindBin qw/ $Bin /;
2017-02-20 04:23:14 +00:00
use Test::More;
use Mojo::JSON;
2017-04-21 16:23:41 +00:00
use Test::Pear::LocalLoop;
2017-08-31 16:53:54 +00:00
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('search');
2017-04-21 16:23:41 +00:00
my $t = $framework->framework;
my $schema = $t->app->schema;
2017-02-20 04:23:14 +00:00
2017-08-31 16:53:54 +00:00
#Login as customer
my $session_key = $framework->login({
'email' => 'test1@example.com',
'password' => 'abc123',
});
my $json;
my $upload;
$t->post_ok( '/api/upload', form => {
json => Mojo::JSON::encode_json({
transaction_value => 10,
transaction_type => 3,
organisation_name => 'Shoreway Fisheries',
street_name => "2 James St",
town => "Lancaster",
postcode => "LA1 1UP",
purchase_time => "2017-08-14T11:29:07.965+01:00",
session_key => $session_key,
}),
file => { file => './t/test.jpg' },
})
2017-02-20 04:23:14 +00:00
->status_is(200)
2017-08-31 16:53:54 +00:00
->or($framework->dump_error)
2017-02-20 04:23:14 +00:00
->json_is('/success', Mojo::JSON->true);
2017-08-31 16:53:54 +00:00
$framework->logout( $session_key );
2017-02-20 04:23:14 +00:00
#End of Rufus (customer)
######################################################
#Login as Choco billy (organisation)
print "test 6 - Login - Choco billy (cookies, organisation)\n";
2017-08-31 16:53:54 +00:00
$session_key = $framework->login({
'email' => 'org@example.com',
'password' => 'abc123',
});
2017-02-20 04:23:14 +00:00
print "test 7 - Added something containing 'bar'\n";
$json = {
transaction_value => 10,
transaction_type => 3,
organisation_name => 'The Palatine Bar',
street_name => "The Crescent",
2017-02-20 04:23:14 +00:00
town => "Morecambe",
2017-04-18 23:10:14 +00:00
postcode => "LA4 5BZ",
purchase_time => "2017-08-14T11:29:07.965+01:00",
2017-04-18 23:10:14 +00:00
session_key => $session_key,
2017-02-20 04:23:14 +00:00
};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
2017-02-20 04:23:14 +00:00
->status_is(200)
->json_is('/success', Mojo::JSON->true);
print "test 8 - Added another thing containing 'bar'\n";
$json = {
transaction_value => 10,
transaction_type => 3,
organisation_name => 'The Sun Hotel & Bar',
street_name => "63-65 Church Street",
2017-02-20 04:23:14 +00:00
town => "Lancaster",
2017-04-18 23:10:14 +00:00
postcode => "LA1 1ET",
purchase_time => "2017-08-14T11:29:07.965+01:00",
2017-04-18 23:10:14 +00:00
session_key => $session_key,
2017-02-20 04:23:14 +00:00
};
$upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}};
$t->post_ok('/api/upload' => form => $upload )
2017-02-20 04:23:14 +00:00
->status_is(200)
->json_is('/success', Mojo::JSON->true);
print "test 9 - Logout Choco billy \n";
2017-08-31 16:53:54 +00:00
$framework->logout( $session_key );
2017-02-20 04:23:14 +00:00
2017-08-31 16:53:54 +00:00
$session_key = $framework->login({
'email' => 'test1@example.com',
'password' => 'abc123',
});
2017-02-20 04:23:14 +00:00
sub check_vars{
my ($searchTerm, $numValidated, $numUnvalidated) = @_;
2017-04-18 23:10:14 +00:00
$t->post_ok('/api/search' => json => {
2017-04-21 16:23:41 +00:00
search_name => $searchTerm,
2017-04-18 23:10:14 +00:00
session_key => $session_key,
})
2017-02-20 04:23:14 +00:00
->status_is(200)
2017-08-31 16:53:54 +00:00
->or($framework->dump_error)
2017-02-20 04:23:14 +00:00
->json_is('/success', Mojo::JSON->true)
->json_has("unvalidated")
->json_has("validated");
my $sessionJsonTest = $t->tx->res->json;
my $validated = $sessionJsonTest->{validated};
2017-02-20 04:23:14 +00:00
my $unvalidated = $sessionJsonTest->{unvalidated};
my $validSize = scalar @$validated;
my $unvalidSize = scalar @$unvalidated;
is $validSize,$numValidated,"validated returned - " . $searchTerm;
is $unvalidSize,$numUnvalidated,"unvalidated returned - " . $searchTerm;
2017-02-20 04:23:14 +00:00
};
2017-04-21 16:23:41 +00:00
print "test 11 - search blank\n";
2017-08-31 16:53:54 +00:00
check_vars(" ", 6, 1);
2017-04-21 16:23:41 +00:00
2017-02-20 04:23:14 +00:00
print "test 12 - Testing expected values with 'booths'\n";
2017-03-10 19:07:23 +00:00
#Expect 0 validated and 0 unvalidated with "booths".
2017-02-20 04:23:14 +00:00
check_vars("booths", 0, 0);
print "test 13 - Testing expected values with 'chip'\n";
2017-03-10 19:07:23 +00:00
#Expect 1 validated and 0 unvalidated with "chip".
2017-02-20 04:23:14 +00:00
check_vars("chip", 1, 0);
print "test 14 - Testing expected values with 'fish, with one unvalidated organisation'\n";
#Expect 2 validated and 1 unvalidated with "fish".
check_vars("fish", 2, 1);
print "test 15 - Testing expected values with 'bar'\n";
#Expect 3 validated and 0 unvalidated with "bar".
check_vars("bar", 3, 0);
print "test 16 - Logout Rufus \n";
2017-08-31 16:53:54 +00:00
$framework->logout( $session_key );
2017-02-20 04:23:14 +00:00
#End of Rufus (customer)
######################################################
#Login as Choco billy (organisation)
print "test 17 - Login - Choco billy (cookies, organisation)\n";
2017-08-31 16:53:54 +00:00
$session_key = $framework->login({
'email' => 'org@example.com',
'password' => 'abc123',
});
2017-02-20 04:23:14 +00:00
print "test 18 - Testing expected values with 'booths'\n";
2017-03-10 19:07:23 +00:00
#Expect 0 validated and 0 unvalidated with "booths".
2017-02-20 04:23:14 +00:00
check_vars("booths", 0, 0);
print "test 19 - Testing expected values with 'chip'\n";
2017-03-10 19:07:23 +00:00
#Expect 1 validated and 0 unvalidated with "chip".
2017-02-20 04:23:14 +00:00
check_vars("chip", 1, 0);
print "test 20 - Testing expected values with 'fish'\n";
#Expect 2 validated and 0 unvalidated with "fish".
check_vars("fish", 2, 0);
print "test 21 - Testing expected values with 'bar', with two unvalidated organisations\n";
#Expect 3 validated and 2 unvalidated with "bar".
check_vars("bar", 3, 2);
print "test 22 - Logout Choco billy \n";
2017-08-31 16:53:54 +00:00
$framework->logout( $session_key );
2017-02-20 04:23:14 +00:00
done_testing();