]> iEval git - plack-app-gruntmaster.git/blame - t/mech.t
Direct people to the archive when viewing a problem in a finished contest
[plack-app-gruntmaster.git] / t / mech.t
CommitLineData
b7bd0bad
MG
1#!/usr/bin/perl -w
2use strict;
3use warnings;
4
a2431c94
MG
5use Test::MockTime ':all';
6use Test::More tests => 14;
b7bd0bad
MG
7use Test::WWW::Mechanize::PSGI;
8
a2431c94
MG
9use Gruntmaster::Data;
10use JSON::MaybeXS qw/decode_json/;
11use Log::Log4perl qw/:easy/;
12
13set_fixed_time 25;
14
b7bd0bad 15my $mech = Test::WWW::Mechanize::PSGI->new(app => do 'app.psgi');
a2431c94
MG
16my $result;
17my $content;
18
19Log::Log4perl->easy_init($OFF);
20
21sub get {
22 my ($url, $expect_fail) = @_;
23 my $param = $url =~ /[?]/ ? '&format=json' : '?format=json';
24 $expect_fail ? $mech->get("$url$param") : $mech->get_ok("$url$param");
25 $result = $mech->response->code;
26 $content = $result == 200 ? decode_json $mech->content(decoded_by_headers => 1) : '';
27}
28
29our $db = Gruntmaster::Data->connect('dbi:SQLite:dbname=:memory:');
30$db->deploy;
31
32$db->users->create({id => 'MGV', admin => 1});
33$db->contests->create({id => 'fc', start => 10, stop => 20, name => 'Finished contest', owner => 'MGV'});
34$db->contests->create({id => 'rc', start => 20, stop => 30, name => 'Running contest', owner => 'MGV'});
35$db->contests->create({id => 'pc', start => 30, stop => 40, name => 'Pending contest', owner => 'MGV'});
36
37my @pbjunk = (
38 name => 'Problem',
39 generator => 'Undef',
40 runner => 'File',
41 judge => 'Absolute',
42 level => 'beginner',
43 value => 100,
44 owner => 'MGV',
45 statement => '...',
46 testcnt => 1,
47 timeout => 1
48);
49
50$db->problems->create({id => 'fca', private => 0, @pbjunk});
51$db->problems->create({id => 'rca', private => 0, @pbjunk});
52$db->problems->create({id => 'pca', private => 0, @pbjunk});
53$db->problems->create({id => 'arc', private => 0, @pbjunk});
54$db->problems->create({id => 'prv', private => 1, @pbjunk});
55$db->contest_problems->create({problem => "${_}a", contest => $_}) for qw/fc rc pc/;
56
57sub problem_list () { [sort map {$_->{id}} @{$content->{beginner}}] }
58get '/pb/';
59is_deeply problem_list, [qw/arc fca/], '/pb/ has correct problems';
60get '/pb/?contest=pc', 1;
61is $result, 401, '/pb/?contest=pc returns 401';
62get '/pb/?contest=rc';
63is_deeply problem_list, [qw/rca/], '/pb/?contest=rc has correct problems';
64get '/pb/?contest=fc';
65is_deeply problem_list, [qw/fca/], '/pb/?contest=fc has correct problems';
66
67get '/us/';
68my ($mgv) = @{$content->{us}};
69is $mgv->{id}, 'MGV', 'first (and only) user is MGV';
70is $mgv->{admin}, 1, 'MGV is an admin';
b7bd0bad 71
a2431c94
MG
72get '/ct/';
73my $pc = $content->{pending}->[0];
74my $rc = $content->{running}->[0];
75my $fc = $content->{finished}->[0];
76is $pc->{id}, 'pc', 'pc is pending';
77is $rc->{id}, 'rc', 'rc is running';
78is $fc->{id}, 'fc', 'fc is running';
This page took 0.041856 seconds and 4 git commands to generate.