Skip to content

Commit 5015dba

Browse files
committed
Add tests
1 parent b47f786 commit 5015dba

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

webapp/tests/Unit/Controller/API/BaseTestCase.php

+6
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,10 @@ public function provideSingleNotFound(): Generator
373373
yield [$id];
374374
}
375375
}
376+
377+
protected function provideAllowedUsers(): Generator
378+
{
379+
yield ['admin', ['admin']];
380+
yield ['team', ['api_problem_change']];
381+
}
376382
}

webapp/tests/Unit/Controller/API/ContestControllerAdminTest.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ private function parseSortYaml(string $yamlString): array
2929
return $new;
3030
}
3131

32-
public function testAddYaml(): void
32+
/**
33+
* @dataProvider provideAllowedUsers
34+
*/
35+
public function testAddYaml(string $user, array $newRoles): void
3336
{
3437
$yaml = <<<EOF
3538
duration: 2:00:00
@@ -69,6 +72,8 @@ public function testAddYaml(): void
6972
scoreboard_freeze_duration: 0:30:00
7073
EOF;
7174

75+
$this->roles = $newRoles;
76+
self::setUp();
7277
$url = $this->helperGetEndpointURL($this->apiEndpoint);
7378
$tempYamlFile = tempnam(sys_get_temp_dir(), "/contest-yaml-");
7479
file_put_contents($tempYamlFile, $yaml);
@@ -89,7 +94,10 @@ public function testAddYaml(): void
8994
self::assertNull($this->getContest($cid)->getDeactivatetime());
9095
}
9196

92-
public function testAddJson(): void
97+
/**
98+
* @dataProvider provideAllowedUsers
99+
*/
100+
public function testAddJson(string $user, array $newRoles): void
93101
{
94102
$json = <<<EOF
95103
{
@@ -103,6 +111,8 @@ public function testAddJson(): void
103111
}
104112
EOF;
105113

114+
$this->roles = $newRoles;
115+
self::setUp();
106116
$url = $this->helperGetEndpointURL($this->apiEndpoint);
107117
$tempJsonFile = tempnam(sys_get_temp_dir(), "/contest-json-");
108118
file_put_contents($tempJsonFile, $json);
@@ -121,8 +131,13 @@ protected function getContest(int|string $cid): Contest
121131
return static::getContainer()->get(EntityManagerInterface::class)->getRepository(Contest::class)->findOneBy(['externalid' => $cid]);
122132
}
123133

124-
public function testBannerManagement(): void
134+
/**
135+
* @dataProvider provideAllowedUsers
136+
*/
137+
public function testBannerManagement(string $user, array $newRoles): void
125138
{
139+
$this->roles = $newRoles;
140+
self::setUp();
126141
// First, make sure we have no banner
127142
$id = 1;
128143
if ($this->objectClassForExternalId !== null) {
@@ -163,8 +178,13 @@ public function testBannerManagement(): void
163178
self::assertArrayNotHasKey('banner', $object);
164179
}
165180

166-
public function testProblemsetManagement(): void
181+
/**
182+
* @dataProvider provideAllowedUsers
183+
*/
184+
public function testProblemsetManagement(string $user, array $newRoles): void
167185
{
186+
$this->roles = $newRoles;
187+
self::setUp();
168188
// First, make sure we have no problemset document
169189
$id = 1;
170190
if ($this->objectClassForExternalId !== null) {
@@ -233,7 +253,10 @@ public function testChangeTimes(
233253
array $extraFixtures = [],
234254
bool $checkUnfreezeTime = false,
235255
bool $convertRelativeTimes = false,
256+
array $newRoles = [],
236257
): void {
258+
$this->roles = $newRoles;
259+
self::setUp();
237260
$this->loadFixture(DemoPreStartContestFixture::class);
238261
$this->loadFixtures($extraFixtures);
239262
$id = 1;
@@ -299,14 +322,18 @@ public function provideChangeTimes(): Generator
299322
yield [['id' => 1, 'scoreboard_thaw_time' => '+15 seconds', 'force' => true], 204, null, [DemoPostUnfreezeContestFixture::class], false, true];
300323
yield [['id' => 1, 'scoreboard_thaw_time' => '+15 seconds'], 204, null, [], false, true];
301324
yield [['id' => 1, 'scoreboard_thaw_time' => '-15 seconds'], 200, 'Demo contest', [], true, true];
325+
326+
// Show that this works for both roles
327+
yield [['id' => 1, 'scoreboard_thaw_time' => '-14 seconds'], 200, 'Demo contest', [], true, true, ['admin']];
328+
yield [['id' => 1, 'scoreboard_thaw_time' => '-13 seconds'], 200, 'Demo contest', [], true, true, ['api_contest_change']];
302329
}
303330

304331
/**
305332
* @dataProvider provideNewContest
306333
*/
307334
public function testActivateTimeContestYaml(
308335
string $activateTime, string $startTime, ?string $deactivateTime,
309-
bool $setActivate, bool $setDeactivate
336+
bool $setActivate, bool $setDeactivate, array $newRoles = [],
310337
): void {
311338
$yaml = <<<EOF
312339
duration: 2:00:00
@@ -322,6 +349,8 @@ public function testActivateTimeContestYaml(
322349
id: anothereruption
323350
EOF;
324351

352+
$this->roles = $newRoles;
353+
self::setUp();
325354
if ($setActivate) {
326355
$yaml = "activate_time: ".$activateTime."\n".$yaml;
327356
}

webapp/tests/Unit/Controller/API/ProblemControllerAdminTest.php

-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\DataFixtures\Test\LockedContestFixture;
77
use App\Entity\Problem;
88
use Doctrine\ORM\EntityManagerInterface;
9-
use Generator;
109
use Symfony\Component\HttpFoundation\File\UploadedFile;
1110

1211
class ProblemControllerAdminTest extends ProblemControllerTest
@@ -234,10 +233,4 @@ public function testDeleteFromLocked(string $user, array $newRoles): void
234233
$problemResponse = $this->verifyApiJsonResponse('DELETE', $url, 403, $this->apiUser);
235234
self::assertStringContainsString('Contest is locked', $problemResponse['message']);
236235
}
237-
238-
private function provideAllowedUsers(): Generator
239-
{
240-
yield ['admin', ['admin']];
241-
yield ['team', ['api_problem_change']];
242-
}
243236
}

0 commit comments

Comments
 (0)