2017-04-18 22:31:08 +01:00
package Test::Pear::LocalLoop ;
2017-09-13 15:23:23 +01:00
use Moo ;
2017-04-18 22:31:08 +01:00
2017-04-23 16:59:35 +01:00
use Test::More ;
2017-04-18 22:31:08 +01:00
use File::Temp ;
use Test::Mojo ;
2017-04-23 16:59:35 +01:00
use DateTime::Format::Strptime ;
2017-08-25 17:12:12 +01:00
use DBIx::Class::Fixtures ;
2017-04-18 22:31:08 +01:00
2017-09-13 15:23:23 +01:00
# Conditionally require Test::PostgreSQL
sub BUILD {
2021-03-20 12:09:50 +00:00
if ( $ ENV { PEAR_TEST_PG } ) {
require Test::PostgreSQL
or die "you need Test::PostgreSQL to run PG testing" ;
Test::PostgreSQL - > import ;
}
2021-03-20 15:02:00 +00:00
return 1 ;
2017-09-13 15:23:23 +01:00
}
sub DEMOLISH {
2021-03-20 12:09:50 +00:00
my ( $ self , $ in_global_destruction ) = @ _ ;
2017-09-13 15:23:23 +01:00
2021-03-20 12:09:50 +00:00
if ( $ ENV { PEAR_TEST_PG } && ! $ in_global_destruction ) {
$ self - > mojo - > app - > schema - > storage - > dbh - > disconnect ;
$ self - > pg - > stop ;
}
2021-03-20 15:02:00 +00:00
return 1 ;
2017-09-13 15:23:23 +01:00
}
has pg = > (
2021-03-20 12:09:50 +00:00
is = > 'lazy' ,
builder = > sub {
return Test::PostgreSQL - > new ( ) ;
} ,
2017-09-13 15:23:23 +01:00
) ;
has config = > (
2021-03-20 12:09:50 +00:00
is = > 'lazy' ,
builder = > sub {
my $ self = shift ;
my $ file = File::Temp - > new ;
2017-04-18 22:31:08 +01:00
2021-03-20 12:09:50 +00:00
my $ dsn ;
2017-09-13 15:23:23 +01:00
2021-03-20 12:09:50 +00:00
if ( $ ENV { PEAR_TEST_PG } ) {
$ dsn = $ self - > pg - > dsn ;
}
else {
$ dsn = "dbi:SQLite::memory:" ;
}
2017-09-13 15:23:23 +01:00
2021-03-20 12:09:50 +00:00
print $ file << "END" ;
2017-04-18 22:31:08 +01:00
{
2017-09-13 15:23:23 +01:00
dsn = > "$dsn" ,
2017-04-18 22:31:08 +01:00
user = > undef ,
pass = > undef ,
}
END
2021-03-20 12:09:50 +00:00
$ file - > seek ( 0 , SEEK_END ) ;
return $ file ;
} ,
2017-09-13 15:23:23 +01:00
) ;
2017-04-18 22:31:08 +01:00
2017-09-13 15:23:23 +01:00
has mojo = > (
2021-03-20 12:09:50 +00:00
is = > 'lazy' ,
builder = > sub {
my $ self = shift ;
2017-04-18 22:31:08 +01:00
2021-03-20 12:09:50 +00:00
$ ENV { MOJO_CONFIG } = $ self - > config - > filename ;
2017-04-18 22:31:08 +01:00
2021-03-20 12:09:50 +00:00
my $ t = Test::Mojo - > new ( 'Pear::LocalLoop' ) ;
$ t - > app - > schema - > deploy ;
2017-08-25 17:12:12 +01:00
2021-03-20 12:09:50 +00:00
return $ t ;
} ,
2017-09-13 15:23:23 +01:00
) ;
has etc_dir = > (
2021-03-20 12:09:50 +00:00
is = > 'lazy' ,
builder = > sub { die "etc dir not set" } ,
2017-09-13 15:23:23 +01:00
) ;
2017-08-25 17:12:12 +01:00
2017-09-13 15:23:23 +01:00
has _deployed = > (
2021-03-20 12:09:50 +00:00
is = > 'rwp' ,
default = > 0 ,
2017-09-13 15:23:23 +01:00
) ;
2017-08-25 17:12:12 +01:00
sub framework {
2021-03-20 12:09:50 +00:00
my $ self = shift ;
my $ no_populate = shift ;
my $ t = $ self - > mojo ;
my $ schema = $ t - > app - > schema ;
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 - > _set__deployed ( 1 ) ;
return $ t ;
}
2017-04-18 22:31:08 +01:00
2017-04-23 16:59:35 +01:00
sub dump_error {
2021-03-20 12:09:50 +00:00
return sub {
my $ self = shift ;
if ( my $ error = $ self - > tx - > res - > dom - > at ( 'pre[id="error"]' ) ) {
diag $ error - > text ;
}
elsif ( my $ route_error =
$ self - > tx - > res - > dom - > at ( 'div[id="routes"] > p' ) )
{
diag $ route_error - > content ;
}
else {
diag $ self - > tx - > res - > to_string ;
}
} ;
2017-04-23 16:59:35 +01:00
}
sub register_customer {
2021-03-20 12:09:50 +00:00
my $ self = shift ;
my $ args = shift ;
2017-04-23 16:59:35 +01:00
2021-03-20 12:09:50 +00:00
my $ json = {
usertype = > 'customer' ,
%$ args ,
} ;
2017-04-23 16:59:35 +01:00
2021-03-20 12:09:50 +00:00
$ 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 ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2017-04-23 16:59:35 +01:00
}
2017-05-16 21:30:38 +01:00
sub register_organisation {
2021-03-20 12:09:50 +00:00
my ( $ self , $ args ) = @ _ ;
2017-05-16 21:30:38 +01:00
2021-03-20 12:09:50 +00:00
$ args - > { usertype } = 'organisation' ;
2017-05-16 21:30:38 +01:00
2021-03-20 12:09:50 +00:00
$ 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 ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2017-05-16 21:30:38 +01:00
}
2017-04-23 16:59:35 +01:00
sub login {
2021-03-20 12:09:50 +00:00
my $ self = shift ;
my $ args = shift ;
2017-04-23 16:59:35 +01:00
2021-03-20 12:09:50 +00:00
$ 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 ) ;
2017-04-23 16:59:35 +01:00
2021-03-20 12:09:50 +00:00
return $ self - > framework - > tx - > res - > json - > { session_key } ;
2017-04-23 16:59:35 +01:00
}
2017-08-29 12:42:27 +01:00
sub logout {
2021-03-20 12:09:50 +00:00
my $ self = shift ;
my $ session_key = shift ;
2017-08-29 12:42:27 +01:00
2021-03-20 12:09:50 +00:00
$ 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/ ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2017-08-29 12:42:27 +01:00
}
2017-05-16 21:30:38 +01:00
sub gen_upload {
2021-03-20 12:09:50 +00:00
my ( $ self , $ args ) = @ _ ;
my $ file = {
content = > '' ,
filename = > 'text.jpg' ,
'Content-Type' = > 'image/jpeg' ,
} ;
return {
json = > Mojo::JSON:: encode_json ( $ args ) ,
file = > $ file ,
} ;
2021-03-20 15:02:00 +00:00
return 1 ;
2017-05-16 21:30:38 +01:00
}
2017-08-25 17:12:12 +01:00
sub install_fixtures {
2021-03-20 12:09:50 +00:00
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 ) ;
my $ schema = $ t - > app - > schema ;
$ fixtures - > populate (
{
directory = > File::Spec - > catdir (
$ self - > etc_dir , 'fixtures' , 'data' , $ fixture_name
) ,
no_deploy = > 1 ,
schema = > $ schema ,
}
) ;
# Reset table id sequences
if ( $ ENV { PEAR_TEST_PG } ) {
$ schema - > storage - > dbh_do (
sub {
my ( $ storage , $ dbh , $ sets ) = @ _ ;
for my $ table ( keys %$ sets ) {
my $ seq = $ sets - > { $ table } ;
$ dbh - > do (
qq /
2017-09-13 15:23:23 +01:00
SELECT setval (
'$seq' ,
COALESCE (
( SELECT MAX ( id ) + 1 FROM $ table ) ,
1
) ,
false
) ;
2021-03-20 12:09:50 +00:00
/
) ;
}
} ,
{
entities = > 'entities_id_seq' ,
organisations = > 'organisations_id_seq' ,
users = > 'users_id_seq' ,
customers = > 'customers_id_seq' ,
}
) ;
}
2021-03-20 15:02:00 +00:00
return 1 ;
2017-08-25 17:12:12 +01:00
}
2017-04-18 22:31:08 +01:00
1 ;