Skip to content

Commit

Permalink
Add tests for encoded group id
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Apr 9, 2020
1 parent 5bed5f5 commit 8d3f2ed
Showing 1 changed file with 76 additions and 11 deletions.
87 changes: 76 additions & 11 deletions apps/provisioning_api/tests/Controller/GroupsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ protected function setUp(): void {
$this->userSession = $this->createMock(IUserSession::class);
$this->accountManager = $this->createMock(AccountManager::class);
$this->logger = $this->createMock(ILogger::class);

$this->subAdminManager = $this->createMock(SubAdmin::class);

$this->groupManager
->method('getSubAdmin')
->willReturn($this->subAdminManager);

$this->api = $this->getMockBuilder(GroupsController::class)
->setConstructorArgs([
'provisioning_api',
Expand Down Expand Up @@ -249,15 +249,15 @@ public function testGetGroupsDetails($search, $limit, $offset) {
'disabled' => 11,
'canAdd' => true,
'canRemove' => true
],
],
[
'id' => 'group2',
'displayname' => 'group2-name',
'usercount' => 123,
'disabled' => 11,
'canAdd' => true,
'canRemove' => true

]
]], $result->getData());

Expand Down Expand Up @@ -287,7 +287,7 @@ public function testGetGroupAsSubadmin() {
$this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
}


public function testGetGroupAsIrrelevantSubadmin() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(403);
Expand Down Expand Up @@ -332,7 +332,7 @@ public function testGetGroupAsAdmin() {
$this->assertEquals(['users' => ['user1', 'user2']], $result->getData());
}


public function testGetGroupNonExisting() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionMessage('The requested group could not be found');
Expand All @@ -343,7 +343,7 @@ public function testGetGroupNonExisting() {
$this->api->getGroup($this->getUniqueID());
}


public function testGetSubAdminsOfGroupsNotExists() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionMessage('Group does not exist');
Expand Down Expand Up @@ -390,7 +390,7 @@ public function testGetSubAdminsOfGroupEmptyList() {
$this->assertEquals([], $result->getData());
}


public function testAddGroupEmptyGroup() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionMessage('Invalid group name');
Expand All @@ -399,7 +399,7 @@ public function testAddGroupEmptyGroup() {
$this->api->addGroup('');
}


public function testAddGroupExistingGroup() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(102);
Expand Down Expand Up @@ -440,15 +440,15 @@ public function testAddGroupWithSpecialChar() {
$this->api->addGroup('Iñtërnâtiônàlizætiøn');
}


public function testDeleteGroupNonExisting() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(101);

$this->api->deleteGroup('NonExistingGroup');
}


public function testDeleteAdminGroup() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(102);
Expand Down Expand Up @@ -480,6 +480,25 @@ public function testDeleteGroup() {
$this->api->deleteGroup('ExistingGroup');
}

public function testDeleteGroupEncoding() {
$this->groupManager
->method('groupExists')
->with('ExistingGroup A/B')
->willReturn('true');

$group = $this->createGroup('ExistingGroup');
$this->groupManager
->method('get')
->with('ExistingGroup A/B')
->willReturn($group);
$group
->expects($this->once())
->method('delete')
->willReturn(true);

$this->api->deleteGroup(urlencode('ExistingGroup A/B'));
}

public function testGetGroupUsersDetails() {
$gid = 'ncg1';

Expand Down Expand Up @@ -525,4 +544,50 @@ public function testGetGroupUsersDetails() {

$this->api->getGroupUsersDetails($gid);
}

public function testGetGroupUsersDetailsEncoded() {
$gid = 'Department A/B C/D';

$this->asAdmin();
$this->useAccountManager();

$users = [
'ncu1' => $this->createUser('ncu1'), # regular
'ncu2' => $this->createUser('ncu2'), # the zombie
];
$users['ncu2']->expects($this->atLeastOnce())
->method('getHome')
->willThrowException(new NoUserException());

$this->userManager->expects($this->any())
->method('get')
->willReturnCallback(function(string $uid) use ($users) {
return isset($users[$uid]) ? $users[$uid] : null;
});

$group = $this->createGroup($gid);
$group->expects($this->once())
->method('searchUsers')
->with('', null, 0)
->willReturn(array_values($users));

$this->groupManager
->method('get')
->with($gid)
->willReturn($group);
$this->groupManager->expects($this->any())
->method('getUserGroups')
->willReturn([$group]);

/** @var \PHPUnit_Framework_MockObject_MockObject */
$this->subAdminManager->expects($this->any())
->method('isSubAdminOfGroup')
->willReturn(false);
$this->subAdminManager->expects($this->any())
->method('getSubAdminsGroups')
->willReturn([]);


$this->api->getGroupUsersDetails(urlencode($gid));
}
}

0 comments on commit 8d3f2ed

Please sign in to comment.