Skip to content

Commit

Permalink
Allow selfregistration property for groups via API
Browse files Browse the repository at this point in the history
Expose domjudge only group properties to verify for API upload

The intent is that only priviliged users can view the properties but not
expose those to the generic users (Teams, Public).
  • Loading branch information
vmcj committed Nov 1, 2023
1 parent f691a60 commit 204611f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion webapp/src/Entity/TeamCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TeamCategory extends BaseApiEntity implements Stringable
'comment' => 'Are self-registered teams allowed to choose this category?',
'default' => 0,
])]
#[Serializer\Exclude]
#[Serializer\Groups([ARC::GROUP_NONSTRICT])]
private bool $allow_self_registration = false;

/**
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/Service/ImportExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ protected function importGroupsTsv(array $content, ?string &$message = null): in
$line = Utils::parseTsvLine(trim($line));
$groupData[] = [
'categoryid' => @$line[0],
'name' => @$line[1]
'name' => @$line[1],
'allow_self_registration' => false,
];
}

Expand All @@ -618,6 +619,7 @@ public function importGroupsJson(array $data, ?string &$message = null, ?array &
'visible' => !($group['hidden'] ?? false),
'sortorder' => @$group['sortorder'],
'color' => @$group['color'],
'allow_self_registration' => $group['allow_self_registration'] ?? false,
];
}

Expand Down Expand Up @@ -661,6 +663,7 @@ protected function importGroupData(array $groupData, ?array &$saved = null): int
->setSortorder($groupItem['sortorder'] ?? 0)
->setColor($groupItem['color'] ?? null)
->setIcpcid($groupItem['icpc_id'] ?? null);
$teamCategory->setAllowSelfRegistration($groupItem['allow_self_registration']);
$this->em->flush();
$this->dj->auditlog('team_category', $teamCategory->getCategoryid(), 'replaced',
'imported from tsv / json');
Expand Down
40 changes: 29 additions & 11 deletions webapp/tests/Unit/Controller/API/GroupControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Tests\Unit\Controller\API;

use Generator;

class GroupControllerTest extends BaseTestCase
{
protected ?string $apiEndpoint = 'groups';
Expand Down Expand Up @@ -33,43 +35,59 @@ class GroupControllerTest extends BaseTestCase
]
];

protected array $newGroupPostData = [
'name' => 'newGroup',
'icpc_id' => 'icpc100',
'hidden' => false,
'sortorder' => 1,
'color' => '#0077B3'
protected array $newGroupsPostData = [
['name' => 'newGroup',
'icpc_id' => 'icpc100',
'hidden' => false,
'sortorder' => 1,
'color' => '#0077B3'],
['name' => 'newSelfRegister',
'hidden' => false,
'sortorder' => 2,
'color' => 'red',
'allow_self_registration' => true],
];
protected array $restrictedProperties = ['hidden', 'allow_self_registration'];

// We test explicitly for groups 1 and 5 here, which are hidden groups and
// should not be returned for non-admin users.
protected array $expectedAbsent = ['4242', 'nonexistent', '1', '5'];

public function testNewAddedGroup(): void
/**
* @dataProvider provideNewAddedGroup
*/
public function testNewAddedGroup(array $newGroupPostData): void
{
$url = $this->helperGetEndpointURL($this->apiEndpoint);
$objectsBeforeTest = $this->verifyApiJsonResponse('GET', $url, 200, $this->apiUser);

$returnedObject = $this->verifyApiJsonResponse('POST', $url, 201, 'admin', $this->newGroupPostData);
foreach ($this->newGroupPostData as $key => $value) {
$returnedObject = $this->verifyApiJsonResponse('POST', $url, 201, 'admin', $newGroupPostData);
foreach ($newGroupPostData as $key => $value) {
self::assertEquals($value, $returnedObject[$key]);
}

$objectsAfterTest = $this->verifyApiJsonResponse('GET', $url, 200, $this->apiUser);
$newItems = array_map('unserialize', array_diff(array_map('serialize', $objectsAfterTest), array_map('serialize', $objectsBeforeTest)));
self::assertEquals(1, count($newItems));
$listKey = array_keys($newItems)[0];
foreach ($this->newGroupPostData as $key => $value) {
foreach ($newGroupPostData as $key => $value) {
self::assertEquals($value, $newItems[$listKey][$key]);
}
}

public function testNewAddedGroupPostWithId(): void
{
$url = $this->helperGetEndpointURL($this->apiEndpoint);
$postWithId = $this->newGroupPostData;
$postWithId = $this->newGroupsPostData[0];
$postWithId['id'] = '1';
// CLICS does not allow POST to set the id value
$this->verifyApiJsonResponse('POST', $url, 400, 'admin', $postWithId);
}

public function provideNewAddedGroup(): Generator
{
foreach ($this->newGroupsPostData as $group) {
yield [$group];
}
}
}

0 comments on commit 204611f

Please sign in to comment.