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/lib/Test/Pear/LocalLoop.pm

105 lines
1.9 KiB
Perl
Raw Normal View History

package Test::Pear::LocalLoop;
use Mojo::Base -base;
2017-04-23 15:59:35 +00:00
use Test::More;
use File::Temp;
use Test::Mojo;
2017-04-23 15:59:35 +00:00
use DateTime::Format::Strptime;
has config => sub {
my $file = File::Temp->new;
print $file <<'END';
{
dsn => "dbi:SQLite::memory:",
user => undef,
pass => undef,
}
END
$file->seek( 0, SEEK_END );
return $file;
};
has framework => sub {
my $self = shift;
$ENV{MOJO_CONFIG} = $self->config->filename;
my $t = Test::Mojo->new('Pear::LocalLoop');
my $schema = $t->app->schema;
$schema->deploy;
$schema->resultset('AgeRange')->populate([
[ qw/ string / ],
[ '20-35' ],
[ '35-50' ],
[ '50+' ],
]);
return $t;
};
2017-04-23 15:59:35 +00:00
sub dump_error {
return sub {
my $self = shift;
if ( my $error = $self->tx->res->dom->at('pre[id="error"]') ) {
diag $error->text;
} else {
diag $self->tx->res->to_string;
}
};
}
sub register_customer {
my $self = shift;
my $args = shift;
my $json = {
usertype => 'customer',
%$args,
};
$self->framework->post_ok('/api/register' => json => $json)
->status_is(200)->or($self->dump_error)
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
}
2017-05-16 20:30:38 +00:00
sub register_organisation {
my ( $self, $args ) = @_;
$args->{usertype} = 'organisation';
$self->framework->post_ok('/api/register' => json => $args)
->status_is(200)->or($self->dump_error)
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
}
2017-04-23 15:59:35 +00:00
sub login {
my $self = shift;
my $args = shift;
$self->framework->post_ok('/api/login' => json => $args)
->status_is(200)->or($self->dump_error)
->json_is('/success', Mojo::JSON->true)->or($self->dump_error);
return $self->framework->tx->res->json->{session_key};
}
2017-05-16 20:30:38 +00:00
sub gen_upload {
my ( $self, $args ) = @_;
my $file = {
content => '',
filename => 'text.jpg',
'Content-Type' => 'image/jpeg',
};
return {
json => Mojo::JSON::encode_json($args),
file => $file,
};
}
1;