Fix search test for entity upgrade

This commit is contained in:
Tom Bloor 2017-08-31 17:53:54 +01:00
parent 1ab2d0b71c
commit 2980fd129f
3 changed files with 73 additions and 133 deletions

View file

@ -137,14 +137,15 @@ sub post_upload {
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;
$organisation = $c->schema->resultset('PendingOrganisation')->create({ my $entity = $c->schema->resultset('Entity')->create_org({
submitted_by => $user, submitted_by_id => $user->id,
submitted_at => DateTime->now, name => $validation->param('organisation_name'),
name => $validation->param('organisation_name'), street_name => $validation->param('street_name'),
street_name => $validation->param('street_name'), town => $validation->param('town'),
town => $validation->param('town'), postcode => $validation->param('postcode'),
postcode => $validation->param('postcode'), pending => \"1"
}); });
$organisation = $entity->organisation;
} }
unless ( defined $organisation ) { unless ( defined $organisation ) {
@ -164,12 +165,12 @@ sub post_upload {
$purchase_time ||= DateTime->now(); $purchase_time ||= DateTime->now();
my $file = defined $upload ? $c->store_file_from_upload( $upload ) : undef; my $file = defined $upload ? $c->store_file_from_upload( $upload ) : undef;
my $new_transaction = $organisation->create_related( my $new_transaction = $organisation->entity->create_related(
'transactions', 'sales',
{ {
buyer => $user, buyer => $user->entity,
value => $transaction_value, value => $transaction_value,
( defined $file ? ( proof_image => $file ) : ( proof_image => 'a' ) ), ( defined $file ? ( proof_image => $file ) : () ),
purchase_time => $c->format_db_datetime($purchase_time), purchase_time => $c->format_db_datetime($purchase_time),
} }
); );
@ -209,11 +210,15 @@ sub post_search {
my $search_stmt = [ 'LOWER("name") LIKE ?', '%' . lc $search_name . '%' ]; my $search_stmt = [ 'LOWER("name") LIKE ?', '%' . lc $search_name . '%' ];
my $valid_orgs_rs = $c->schema->resultset('Organisation')->search( my $org_rs = $c->schema->resultset('Organisation');
my $valid_orgs_rs = $org_rs->search({ pending => 0 })->search(
\$search_stmt, \$search_stmt,
); );
my $pending_orgs_rs = $c->stash->{api_user}->pending_organisations->search( my $pending_orgs_rs = $org_rs->search({
pending => 1,
submitted_by_id => $c->stash->{api_user}->id,
})->search(
\$search_stmt, \$search_stmt,
); );

View file

@ -7,4 +7,13 @@ use base 'DBIx::Class::ResultSet';
sub sales { shift->search_related('sales', @_) } sub sales { shift->search_related('sales', @_) }
sub create_org {
my ( $self, $org ) = @_;
return $self->create({
organisation => $org,
type => 'organisation',
});
}
1; 1;

View file

@ -1,122 +1,46 @@
use Mojo::Base -strict; use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More; use Test::More;
use Mojo::JSON; use Mojo::JSON;
use Test::Pear::LocalLoop; use Test::Pear::LocalLoop;
my $framework = Test::Pear::LocalLoop->new; my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('search');
my $t = $framework->framework; my $t = $framework->framework;
my $schema = $t->app->schema; my $schema = $t->app->schema;
my $dump_error = sub { diag $t->tx->res->to_string };
my @account_tokens = ('a', 'b'); #Login as customer
$schema->resultset('AccountToken')->populate([ my $session_key = $framework->login({
[ qw/ name / ], 'email' => 'test1@example.com',
map { [ $_ ] } @account_tokens, 'password' => 'abc123',
]); });
$schema->resultset('Organisation')->populate([ my $json;
[ qw/ name street_name town postcode / ], my $upload;
[ "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" ],
]);
#test with a customer. $t->post_ok( '/api/upload', form => {
print "test 1 - Create customer user account (Rufus)\n"; json => Mojo::JSON::encode_json({
my $emailRufus = 'rufus@shinra.energy'; transaction_value => 10,
my $passwordRufus = 'MakoGold'; transaction_type => 3,
my $testJson = { organisation_name => 'Shoreway Fisheries',
'usertype' => 'customer', street_name => "2 James St",
'token' => shift(@account_tokens), town => "Lancaster",
'full_name' => 'RufusShinra', postcode => "LA1 1UP",
'display_name' => 'RufusShinra', purchase_time => "2017-08-14T11:29:07.965+01:00",
'email' => $emailRufus, session_key => $session_key,
'postcode' => 'RG26 5NU', }),
'password' => $passwordRufus, file => { file => './t/test.jpg' },
'year_of_birth' => 2006 })
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)->or($framework->dump_error)
->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,
'street_name' => 'Market St',
'town' => 'Lancaster',
'sector' => 'A',
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
my $session_key; $framework->logout( $session_key );
sub login_rufus {
$testJson = {
'email' => $emailRufus,
'password' => $passwordRufus,
};
$t->post_ok('/api/login' => json => $testJson)
->status_is(200)
->json_is('/success', Mojo::JSON->true);
$session_key = $t->tx->res->json('/session_key');
};
sub login_billy {
$testJson = {
'email' => $emailBilly,
'password' => $passwordBilly,
};
$t->post_ok('/api/login' => json => $testJson)
->status_is(200)
->json_is('/success', Mojo::JSON->true);
$session_key = $t->tx->res->json('/session_key');
};
sub log_out{
$t->post_ok('/api/logout', json => { session_key => $session_key })
->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",
town => "Lancaster",
postcode => "LA1 1UP",
purchase_time => "2017-08-14T11:29:07.965+01:00",
session_key => $session_key,
};
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 5 - Logout Rufus \n";
log_out();
#End of Rufus (customer) #End of Rufus (customer)
@ -125,7 +49,10 @@ log_out();
#Login as Choco billy (organisation) #Login as Choco billy (organisation)
print "test 6 - Login - Choco billy (cookies, organisation)\n"; print "test 6 - Login - Choco billy (cookies, organisation)\n";
login_billy(); $session_key = $framework->login({
'email' => 'org@example.com',
'password' => 'abc123',
});
print "test 7 - Added something containing 'bar'\n"; print "test 7 - Added something containing 'bar'\n";
$json = { $json = {
@ -160,16 +87,12 @@ $t->post_ok('/api/upload' => form => $upload )
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
print "test 9 - Logout Choco billy \n"; print "test 9 - Logout Choco billy \n";
log_out(); $framework->logout( $session_key );
#End of Choco billy (organisation) $session_key = $framework->login({
'email' => 'test1@example.com',
###################################################### 'password' => 'abc123',
});
#Login as Rufus (customer)
print "test 10 - Login - Rufus (cookies, customer)\n";
login_rufus();
sub check_vars{ sub check_vars{
my ($searchTerm, $numValidated, $numUnvalidated) = @_; my ($searchTerm, $numValidated, $numUnvalidated) = @_;
@ -179,6 +102,7 @@ sub check_vars{
session_key => $session_key, session_key => $session_key,
}) })
->status_is(200) ->status_is(200)
->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true) ->json_is('/success', Mojo::JSON->true)
->json_has("unvalidated") ->json_has("unvalidated")
->json_has("validated"); ->json_has("validated");
@ -196,7 +120,7 @@ sub check_vars{
}; };
print "test 11 - search blank\n"; print "test 11 - search blank\n";
check_vars(" ", 5, 1); check_vars(" ", 6, 1);
print "test 12 - Testing expected values with 'booths'\n"; print "test 12 - Testing expected values with 'booths'\n";
#Expect 0 validated and 0 unvalidated with "booths". #Expect 0 validated and 0 unvalidated with "booths".
@ -215,7 +139,7 @@ print "test 15 - Testing expected values with 'bar'\n";
check_vars("bar", 3, 0); check_vars("bar", 3, 0);
print "test 16 - Logout Rufus \n"; print "test 16 - Logout Rufus \n";
log_out(); $framework->logout( $session_key );
#End of Rufus (customer) #End of Rufus (customer)
@ -224,7 +148,10 @@ log_out();
#Login as Choco billy (organisation) #Login as Choco billy (organisation)
print "test 17 - Login - Choco billy (cookies, organisation)\n"; print "test 17 - Login - Choco billy (cookies, organisation)\n";
login_billy(); $session_key = $framework->login({
'email' => 'org@example.com',
'password' => 'abc123',
});
print "test 18 - Testing expected values with 'booths'\n"; print "test 18 - Testing expected values with 'booths'\n";
#Expect 0 validated and 0 unvalidated with "booths". #Expect 0 validated and 0 unvalidated with "booths".
@ -243,7 +170,6 @@ print "test 21 - Testing expected values with 'bar', with two unvalidated organi
check_vars("bar", 3, 2); check_vars("bar", 3, 2);
print "test 22 - Logout Choco billy \n"; print "test 22 - Logout Choco billy \n";
log_out(); $framework->logout( $session_key );
done_testing(); done_testing();