2017-04-18 21:31:08 +00:00
|
|
|
package Test::Pear::LocalLoop;
|
|
|
|
use Mojo::Base -base;
|
|
|
|
|
2017-04-23 15:59:35 +00:00
|
|
|
use Test::More;
|
2017-04-18 21:31:08 +00:00
|
|
|
use File::Temp;
|
|
|
|
use Test::Mojo;
|
2017-04-23 15:59:35 +00:00
|
|
|
use DateTime::Format::Strptime;
|
2017-08-25 16:12:12 +00:00
|
|
|
use DBIx::Class::Fixtures;
|
2017-04-18 21:31:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-08-25 16:12:12 +00:00
|
|
|
has mojo => sub {
|
2017-04-18 21:31:08 +00:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$ENV{MOJO_CONFIG} = $self->config->filename;
|
|
|
|
|
|
|
|
my $t = Test::Mojo->new('Pear::LocalLoop');
|
2017-08-25 16:12:12 +00:00
|
|
|
$t->app->schema->deploy;
|
|
|
|
|
|
|
|
return $t;
|
|
|
|
};
|
|
|
|
|
|
|
|
has _deployed => sub { 0 };
|
|
|
|
|
|
|
|
sub framework {
|
|
|
|
my $self = shift;
|
|
|
|
my $no_populate = shift;
|
|
|
|
|
|
|
|
my $t = $self->mojo;
|
2017-04-18 21:31:08 +00:00
|
|
|
my $schema = $t->app->schema;
|
2017-08-25 16:12:12 +00:00
|
|
|
|
|
|
|
unless ( $no_populate || $self->_deployed ) {
|
|
|
|
$schema->resultset('Leaderboard')->populate([
|
|
|
|
[ qw/ name type / ],
|
|
|
|
[ 'Daily Total', 'daily_total' ],
|
|
|
|
[ 'Daily Count', 'daily_count' ],
|
|
|
|
[ 'Weekly Total', 'weekly_total' ],
|
|
|
|
[ 'Weekly Count', 'weekly_count' ],
|
|
|
|
[ 'Monthly Total', 'monthly_total' ],
|
|
|
|
[ 'Monthly Count', 'monthly_count' ],
|
|
|
|
[ 'All Time Total', 'all_time_total' ],
|
|
|
|
[ 'All Time Count', 'all_time_count' ],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->_deployed(1);
|
2017-05-23 22:06:07 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
return $t;
|
|
|
|
};
|
|
|
|
|
2017-08-25 16:12:12 +00:00
|
|
|
has etc_dir => sub { die "etc dir not set" };
|
|
|
|
|
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-08-29 11:42:27 +00:00
|
|
|
sub logout {
|
|
|
|
my $self = shift;
|
|
|
|
my $session_key = shift;
|
|
|
|
|
|
|
|
$self->framework->post_ok('/api/logout' => json => { session_key => $session_key })
|
|
|
|
->status_is(200)
|
|
|
|
->json_is('/success', Mojo::JSON->true)
|
|
|
|
->json_like('/message', qr/Logged Out/);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-25 16:12:12 +00:00
|
|
|
sub install_fixtures {
|
|
|
|
my ( $self, $fixture_name ) = @_;
|
|
|
|
|
|
|
|
my $fixtures = DBIx::Class::Fixtures->new({
|
|
|
|
config_dir => File::Spec->catdir( $self->etc_dir, 'fixtures', 'config'),
|
|
|
|
});
|
|
|
|
|
|
|
|
my $t = $self->framework(1);
|
|
|
|
$fixtures->populate({
|
|
|
|
directory => File::Spec->catdir( $self->etc_dir, 'fixtures', 'data', $fixture_name ),
|
|
|
|
no_deploy => 1,
|
|
|
|
schema => $t->app->schema,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
1;
|