+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';