Add a few tests
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 27 Jan 2015 08:54:50 +0000 (10:54 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 27 Jan 2015 08:54:50 +0000 (10:54 +0200)
Makefile.PL
app.psgi
t/mech.t

index 02f3d25df0e4a1bfd1abdbf8be7c5b874c4c4343..f198fa63baf33155beafdff6d78e35346da5a2b8 100644 (file)
@@ -10,7 +10,8 @@ WriteMakefile(
        LICENSE           => 'AGPL_3',
        SIGN              => 1,
        BUILD_REQUIRES    => {
-               qw/Test::More                 0
+               qw/Test::MockTime             0
+                  Test::More                 0
                   Test::WWW::Mechanize::PSGI 0/,
        },
        PREREQ_PM         => {
index cdd6863cc3bd153f35d1117469bf4bb4e94239a3..058bfc171284f3ed3820e59aa2bc701afbb9c26b 100644 (file)
--- a/app.psgi
+++ b/app.psgi
@@ -15,7 +15,7 @@ use constant AUTH_TIMEOUT => 5 * 60;
 use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
 use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self'; connect-src 'self'; frame-src free.timeanddate.com,;
 
-my $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} //' dbi:Pg:');
+our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} //' dbi:Pg:');
 
 tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT};
 
index 6a30b18dccb8ae799ceafe42035f79040bb2b1df..990de7659332b1909e87bbc8b7614c6ea4a2d608 100644 (file)
--- a/t/mech.t
+++ b/t/mech.t
@@ -2,17 +2,77 @@
 use strict;
 use warnings;
 
-use Test::More;
-BEGIN {
-       plan skip_all => '$ENV{AUTHOR_TESTING} is false, skipping tests' unless $ENV{AUTHOR_TESTING};
-       plan tests    => 5;
-}
+use Test::MockTime ':all';
+use Test::More tests => 14;
 use Test::WWW::Mechanize::PSGI;
 
+use Gruntmaster::Data;
+use JSON::MaybeXS qw/decode_json/;
+use Log::Log4perl qw/:easy/;
+
+set_fixed_time 25;
+
 my $mech = Test::WWW::Mechanize::PSGI->new(app => do 'app.psgi');
-$mech->get_ok('/');
-$mech->title_is('Gruntmaster 6000');
+my $result;
+my $content;
+
+Log::Log4perl->easy_init($OFF);
+
+sub get {
+       my ($url, $expect_fail) = @_;
+       my $param = $url =~ /[?]/ ? '&format=json' : '?format=json';
+       $expect_fail ? $mech->get("$url$param") : $mech->get_ok("$url$param");
+       $result = $mech->response->code;
+       $content = $result == 200 ? decode_json $mech->content(decoded_by_headers => 1) : '';
+}
+
+our $db = Gruntmaster::Data->connect('dbi:SQLite:dbname=:memory:');
+$db->deploy;
+
+$db->users->create({id => 'MGV', admin => 1});
+$db->contests->create({id => 'fc', start => 10, stop => 20, name => 'Finished contest', owner => 'MGV'});
+$db->contests->create({id => 'rc', start => 20, stop => 30, name => 'Running contest', owner => 'MGV'});
+$db->contests->create({id => 'pc', start => 30, stop => 40, name => 'Pending contest', owner => 'MGV'});
+
+my @pbjunk = (
+       name      => 'Problem',
+       generator => 'Undef',
+       runner    => 'File',
+       judge     => 'Absolute',
+       level     => 'beginner',
+       value     => 100,
+       owner     => 'MGV',
+       statement => '...',
+       testcnt   => 1,
+       timeout   => 1
+);
+
+$db->problems->create({id => 'fca', private => 0, @pbjunk});
+$db->problems->create({id => 'rca', private => 0, @pbjunk});
+$db->problems->create({id => 'pca', private => 0, @pbjunk});
+$db->problems->create({id => 'arc', private => 0, @pbjunk});
+$db->problems->create({id => 'prv', private => 1, @pbjunk});
+$db->contest_problems->create({problem => "${_}a", contest => $_}) for qw/fc rc pc/;
+
+sub problem_list () { [sort map {$_->{id}} @{$content->{beginner}}] }
+get '/pb/';
+is_deeply problem_list, [qw/arc fca/], '/pb/ has correct problems';
+get '/pb/?contest=pc', 1;
+is $result, 401, '/pb/?contest=pc returns 401';
+get '/pb/?contest=rc';
+is_deeply problem_list, [qw/rca/], '/pb/?contest=rc has correct problems';
+get '/pb/?contest=fc';
+is_deeply problem_list, [qw/fca/], '/pb/?contest=fc has correct problems';
+
+get '/us/';
+my ($mgv) = @{$content->{us}};
+is $mgv->{id}, 'MGV', 'first (and only) user is MGV';
+is $mgv->{admin}, 1, 'MGV is an admin';
 
-$mech->get_ok('/pb/');
-$mech->title_is('Problems');
-$mech->content_contains('Spell');
+get '/ct/';
+my $pc = $content->{pending}->[0];
+my $rc = $content->{running}->[0];
+my $fc = $content->{finished}->[0];
+is $pc->{id}, 'pc', 'pc is pending';
+is $rc->{id}, 'rc', 'rc is running';
+is $fc->{id}, 'fc', 'fc is running';
This page took 0.01271 seconds and 4 git commands to generate.