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

249 lines
6.9 KiB
Perl
Raw Permalink Normal View History

use Mojo::Base -strict;
2017-04-21 16:23:41 +00:00
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;
my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework;
my $schema = $t->app->schema;
my $dump_error = sub { diag $t->tx->res->to_string };
2017-04-21 16:23:41 +00:00
my @account_tokens = ('a', 'b');
$schema->resultset('AccountToken')->populate([
2017-04-21 20:28:50 +00:00
[ qw/ name / ],
2017-04-21 16:23:41 +00:00
map { [ $_ ] } @account_tokens,
]);
$schema->resultset('Organisation')->populate([
[ qw/ name street_name town postcode / ],
[ "Avanti Bar & Restaurant", "57 Main St", "Kirkby Lonsdale", "LA6 2AH" ],
[ "Full House Noodle Bar", "21 Common Garden St", "Lancaster", "LA1 1XD" ],
[ "The Quay's Fishbar", "1 Adcliffe Rd", "Lancaster", "LA1 1SS" ],
[ "Dan's Fishop", "56 North Rd", "Lancaster", "LA1 1LT" ],
[ "Hodgeson's Chippy", "96 Prospect St", "Lancaster", "LA1 3BH" ],
]);
2017-02-20 04:23:14 +00:00
#test with a customer.
print "test 1 - Create customer user account (Rufus)\n";
my $emailRufus = 'rufus@shinra.energy';
my $passwordRufus = 'MakoGold';
my $testJson = {
'usertype' => 'customer',
'token' => shift(@account_tokens),
'full_name' => 'RufusShinra',
'display_name' => 'RufusShinra',
'email' => $emailRufus,
'postcode' => 'RG26 5NU',
'password' => $passwordRufus,
2017-06-13 20:06:53 +00:00
'year_of_birth' => 2006
2017-02-20 04:23:14 +00:00
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)->or($framework->dump_error)
2017-02-20 04:23:14 +00:00
->json_is('/success', Mojo::JSON->true);
#test with an organisation.
print "test 2 - Create organisation user account (Choco Billy)\n";
my $emailBilly = 'choco.billy@chocofarm.org';
my $passwordBilly = 'Choco';
$testJson = {
'usertype' => 'organisation',
'token' => shift(@account_tokens),
'name' => 'ChocoBillysGreens',
'email' => $emailBilly,
'postcode' => 'LA1 1HT',
'password' => $passwordBilly,
2017-04-21 16:23:41 +00:00
'street_name' => 'Market St',
'town' => 'Lancaster',
2017-02-20 04:23:14 +00:00
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)
2017-02-20 04:23:14 +00:00
->json_is('/success', Mojo::JSON->true);
2017-04-18 23:10:14 +00:00
my $session_key;
2017-02-20 04:23:14 +00:00
sub login_rufus {
$testJson = {
'email' => $emailRufus,
'password' => $passwordRufus,
};
$t->post_ok('/api/login' => json => $testJson)
2017-02-20 04:23:14 +00:00
->status_is(200)
->json_is('/success', Mojo::JSON->true);
2017-04-18 23:10:14 +00:00
$session_key = $t->tx->res->json('/session_key');
2017-02-20 04:23:14 +00:00
};
sub login_billy {
$testJson = {
'email' => $emailBilly,
'password' => $passwordBilly,
};
$t->post_ok('/api/login' => json => $testJson)
2017-02-20 04:23:14 +00:00
->status_is(200)
->json_is('/success', Mojo::JSON->true);
2017-04-18 23:10:14 +00:00
$session_key = $t->tx->res->json('/session_key');
2017-02-20 04:23:14 +00:00
};
sub log_out{
$t->post_ok('/api/logout', json => { session_key => $session_key })
2017-02-20 04:23:14 +00:00
->status_is(200)
->json_is('/success', Mojo::JSON->true);
}
######################################################
#Login as Rufus (customer)
print "test 3 - Login - Rufus (cookies, customer)\n";
login_rufus();
print "test 4 - Added something containing 'fish'\n";
my $json = {
transaction_value => 10,
transaction_type => 3,
organisation_name => 'Shoreway Fisheries',
street_name => "2 James St",
2017-02-20 04:23:14 +00:00
town => "Lancaster",
2017-04-18 23:10:14 +00:00
postcode => "LA1 1UP",
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
};
my $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 5 - Logout Rufus \n";
log_out();
#End of Rufus (customer)
######################################################
#Login as Choco billy (organisation)
print "test 6 - Login - Choco billy (cookies, organisation)\n";
login_billy();
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";
log_out();
#End of Choco billy (organisation)
######################################################
#Login as Rufus (customer)
print "test 10 - Login - Rufus (cookies, customer)\n";
login_rufus();
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)
->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";
check_vars(" ", 5, 1);
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";
log_out();
#End of Rufus (customer)
######################################################
#Login as Choco billy (organisation)
print "test 17 - Login - Choco billy (cookies, organisation)\n";
login_billy();
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";
log_out();
done_testing();