Skip to content

Commit 0ddd1ad

Browse files
committed
Add tests
1 parent 33a4150 commit 0ddd1ad

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

webapp/src/Controller/API/ContestController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public function problemsetAction(Request $request, string $cid): Response
384384
* Change the start time or unfreeze (thaw) time of the given contest.
385385
* @throws NonUniqueResultException
386386
*/
387-
#[IsGranted('ROLE_API_WRITER')]
387+
#[IsGranted(new Expression("is_granted('ROLE_API_WRITER') or is_granted('ROLE_API_CONTEST_CHANGE')"))]
388388
#[Rest\Patch('/{cid}')]
389389
#[OA\RequestBody(
390390
required: true,

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

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
abstract class BaseTestCase extends BaseBaseTestCase
1313
{
1414
protected static array $rootEndpoints = ['contests', 'judgehosts', 'users'];
15+
protected static string $testedRole = 'unset';
1516

1617
/** @var KernelBrowser */
1718
protected KernelBrowser $client;
@@ -373,4 +374,10 @@ public function provideSingleNotFound(): Generator
373374
yield [$id];
374375
}
375376
}
377+
378+
protected function provideAllowedUsers(): Generator
379+
{
380+
yield ['admin', ['admin']];
381+
yield ['team', [static::$testedRole]];
382+
}
376383
}

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

+35-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class ContestControllerAdminTest extends ContestControllerTest
2222
{
2323
protected ?string $apiUser = 'admin';
24+
protected static string $testedRole = 'api_contest_change';
2425

2526
private function parseSortYaml(string $yamlString): array
2627
{
@@ -29,7 +30,10 @@ private function parseSortYaml(string $yamlString): array
2930
return $new;
3031
}
3132

32-
public function testAddYaml(): void
33+
/**
34+
* @dataProvider provideAllowedUsers
35+
*/
36+
public function testAddYaml(string $user, array $newRoles): void
3337
{
3438
$yaml = <<<EOF
3539
duration: 2:00:00
@@ -69,6 +73,8 @@ public function testAddYaml(): void
6973
scoreboard_freeze_duration: 0:30:00
7074
EOF;
7175

76+
$this->roles = $newRoles;
77+
self::setUp();
7278
$url = $this->helperGetEndpointURL($this->apiEndpoint);
7379
$tempYamlFile = tempnam(sys_get_temp_dir(), "/contest-yaml-");
7480
file_put_contents($tempYamlFile, $yaml);
@@ -89,7 +95,10 @@ public function testAddYaml(): void
8995
self::assertNull($this->getContest($cid)->getDeactivatetime());
9096
}
9197

92-
public function testAddJson(): void
98+
/**
99+
* @dataProvider provideAllowedUsers
100+
*/
101+
public function testAddJson(string $user, array $newRoles): void
93102
{
94103
$json = <<<EOF
95104
{
@@ -103,6 +112,8 @@ public function testAddJson(): void
103112
}
104113
EOF;
105114

115+
$this->roles = $newRoles;
116+
self::setUp();
106117
$url = $this->helperGetEndpointURL($this->apiEndpoint);
107118
$tempJsonFile = tempnam(sys_get_temp_dir(), "/contest-json-");
108119
file_put_contents($tempJsonFile, $json);
@@ -121,8 +132,13 @@ protected function getContest(int|string $cid): Contest
121132
return static::getContainer()->get(EntityManagerInterface::class)->getRepository(Contest::class)->findOneBy(['externalid' => $cid]);
122133
}
123134

124-
public function testBannerManagement(): void
135+
/**
136+
* @dataProvider provideAllowedUsers
137+
*/
138+
public function testBannerManagement(string $user, array $newRoles): void
125139
{
140+
$this->roles = $newRoles;
141+
self::setUp();
126142
// First, make sure we have no banner
127143
$id = 1;
128144
if ($this->objectClassForExternalId !== null) {
@@ -163,8 +179,13 @@ public function testBannerManagement(): void
163179
self::assertArrayNotHasKey('banner', $object);
164180
}
165181

166-
public function testProblemsetManagement(): void
182+
/**
183+
* @dataProvider provideAllowedUsers
184+
*/
185+
public function testProblemsetManagement(string $user, array $newRoles): void
167186
{
187+
$this->roles = $newRoles;
188+
self::setUp();
168189
// First, make sure we have no problemset document
169190
$id = 1;
170191
if ($this->objectClassForExternalId !== null) {
@@ -233,7 +254,10 @@ public function testChangeTimes(
233254
array $extraFixtures = [],
234255
bool $checkUnfreezeTime = false,
235256
bool $convertRelativeTimes = false,
257+
array $newRoles = [],
236258
): void {
259+
$this->roles = $newRoles;
260+
self::setUp();
237261
$this->loadFixture(DemoPreStartContestFixture::class);
238262
$this->loadFixtures($extraFixtures);
239263
$id = 1;
@@ -299,14 +323,18 @@ public function provideChangeTimes(): Generator
299323
yield [['id' => 1, 'scoreboard_thaw_time' => '+15 seconds', 'force' => true], 204, null, [DemoPostUnfreezeContestFixture::class], false, true];
300324
yield [['id' => 1, 'scoreboard_thaw_time' => '+15 seconds'], 204, null, [], false, true];
301325
yield [['id' => 1, 'scoreboard_thaw_time' => '-15 seconds'], 200, 'Demo contest', [], true, true];
326+
327+
// Show that this works for both roles
328+
yield [['id' => 1, 'scoreboard_thaw_time' => '-14 seconds'], 200, 'Demo contest', [], true, true, ['admin']];
329+
yield [['id' => 1, 'scoreboard_thaw_time' => '-13 seconds'], 200, 'Demo contest', [], true, true, ['api_contest_change']];
302330
}
303331

304332
/**
305333
* @dataProvider provideNewContest
306334
*/
307335
public function testActivateTimeContestYaml(
308336
string $activateTime, string $startTime, ?string $deactivateTime,
309-
bool $setActivate, bool $setDeactivate
337+
bool $setActivate, bool $setDeactivate, array $newRoles = [],
310338
): void {
311339
$yaml = <<<EOF
312340
duration: 2:00:00
@@ -322,6 +350,8 @@ public function testActivateTimeContestYaml(
322350
id: anothereruption
323351
EOF;
324352

353+
$this->roles = $newRoles;
354+
self::setUp();
325355
if ($setActivate) {
326356
$yaml = "activate_time: ".$activateTime."\n".$yaml;
327357
}

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
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
1312
{
1413
protected ?string $apiUser = 'admin';
14+
protected static string $testedRole = 'api_problem_change';
1515

1616
protected function setUp(): void
1717
{
@@ -234,10 +234,4 @@ public function testDeleteFromLocked(string $user, array $newRoles): void
234234
$problemResponse = $this->verifyApiJsonResponse('DELETE', $url, 403, $this->apiUser);
235235
self::assertStringContainsString('Contest is locked', $problemResponse['message']);
236236
}
237-
238-
private function provideAllowedUsers(): Generator
239-
{
240-
yield ['admin', ['admin']];
241-
yield ['team', ['api_problem_change']];
242-
}
243237
}

0 commit comments

Comments
 (0)