forked from nextcloud/user_saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2021 Giuliano Mele <[email protected]> | ||
* | ||
* @author Giuliano Mele <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\User_SAML\Tests; | ||
|
||
use OC\BackgroundJob\JobList; | ||
use OCA\User_SAML\GroupBackend; | ||
use OCA\User_SAML\GroupDuplicateChecker; | ||
use OCA\User_SAML\GroupManager; | ||
use OCA\User_SAML\SAMLSettings; | ||
use OCP\IConfig; | ||
use OCP\IDBConnection; | ||
use OCP\IGroup; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use Test\TestCase; | ||
|
||
class GroupManagerTest extends TestCase { | ||
|
||
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $db; | ||
/** @var GroupDuplicateChecker|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $duplicateChecker; | ||
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $groupManager; | ||
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $userManager; | ||
/** @var GroupBackend|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $ownGroupBackend; | ||
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $config; | ||
/** @var JobList|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $jobList; | ||
/** @var SAMLSettings|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $settings; | ||
/** @var GroupManager|\PHPUnit_Framework_MockObject_MockObject */ | ||
private $ownGroupManager; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->db = $this->createMock(IDBConnection::class); | ||
$this->duplicateChecker = $this->createMock(GroupDuplicateChecker::class); | ||
$this->groupManager = $this->createMock(IGroupManager::class); | ||
$this->userManager = $this->createMock(IUserManager::class); | ||
$this->ownGroupBackend = $this->createMock(GroupBackend::class); | ||
$this->config = $this->createMock(IConfig::class); | ||
$this->jobList = $this->createMock(JobList::class); | ||
$this->settings = $this->createMock(SAMLSettings::class); | ||
$this->ownGroupManager = $this->createMock(GroupManager::class); | ||
} | ||
|
||
public function getGroupManager(array $mockedFunctions = []) { | ||
if (!empty($mockedFunctions)) { | ||
$this->ownGroupManager = $this->getMockBuilder(GroupManager::class) | ||
->setConstructorArgs([ | ||
$this->db, | ||
$this->duplicateChecker, | ||
$this->groupManager, | ||
$this->userManager, | ||
$this->ownGroupBackend, | ||
$this->config, | ||
$this->jobList, | ||
$this->settings | ||
]) | ||
->onlyMethods($mockedFunctions) | ||
->getMock(); | ||
} else { | ||
$this->ownGroupManager = new GroupManager( | ||
$this->db, | ||
$this->duplicateChecker, | ||
$this->groupManager, | ||
$this->userManager, | ||
$this->ownGroupBackend, | ||
$this->config, | ||
$this->jobList, | ||
$this->settings | ||
); | ||
} | ||
} | ||
|
||
public function testReplaceGroups() { | ||
$this->getGroupManager(['removeGroups', 'addGroups', 'translateGroupToIds']); | ||
$user = $this->createMock(IUser::class); | ||
$this->userManager | ||
->expects($this->once()) | ||
->method('get') | ||
->with('ExistingUser') | ||
->willReturn($user); | ||
$this->ownGroupManager | ||
->expects($this->once()) | ||
->method('translateGroupToIds') | ||
->with(['groupB', 'groupC']); | ||
// assert user is actually assigned to groupA and groupB | ||
$this->groupManager | ||
->expects($this->once()) | ||
->method('getUserGroupIds') | ||
->with($user) | ||
->willReturn(['groupA', 'groupB']); | ||
// assert removing membership to groupA | ||
$this->ownGroupManager | ||
->expects($this->once()) | ||
->method('removeGroups') | ||
->with($user, ['groupA']); | ||
// assert adding membership to groupC | ||
$this->ownGroupManager | ||
->expects($this->once()) | ||
->method('addGroups') | ||
->with($user, [1 => 'groupC']); | ||
|
||
// assert SAML provides user groups groupB and groupC | ||
$this->ownGroupManager->replaceGroups('ExistingUser', ['groupB', 'groupC']); | ||
} | ||
|
||
public function testRemoveGroups() { | ||
$this->getGroupManager(); | ||
$user = $this->createMock(IUser::class); | ||
$groupA = $this->createMock(IGroup::class); | ||
$groupB = $this->createMock(IGroup::class); | ||
$this->groupManager | ||
->expects($this->at(0)) | ||
->method('get') | ||
->with('groupA') | ||
->willReturn($groupA); | ||
// assert SAML group backend | ||
$groupA->expects($this->once()) | ||
->method('getBackendNames') | ||
->willReturn(['OCA\User_SAML\GroupBackend']); | ||
// assert membership gets removed | ||
$groupA->expects($this->once()) | ||
->method('removeUser') | ||
->with($user); | ||
$this->groupManager | ||
->expects($this->at(1)) | ||
->method('get') | ||
->with('groupB') | ||
->willReturn($groupB); | ||
// assert different group backend | ||
$groupB->expects($this->once()) | ||
->method('getBackendNames') | ||
->willReturn(['OC\Group\Database']); | ||
// assert user retains membership | ||
$groupB->expects($this->never()) | ||
->method('removeUser') | ||
->with($user); | ||
|
||
$this->ownGroupManager->removeGroups($user, ['groupA', 'groupB']); | ||
} | ||
|
||
public function testAddToExistingGroup() { | ||
$this->getGroupManager(); | ||
$user = $this->createMock(IUser::class); | ||
$groupA = $this->createMock(IGroup::class); | ||
|
||
// assert group already exists | ||
$this->groupManager | ||
->expects($this->once()) | ||
->method('get') | ||
->with('groupA') | ||
->willReturn($groupA); | ||
// assert SAML group backend | ||
$groupA->expects($this->once()) | ||
->method('getBackendNames') | ||
->willReturn(['OCA\User_SAML\GroupBackend']); | ||
// assert user gets added to group | ||
$groupA->expects($this->once()) | ||
->method('addUser') | ||
->with($user); | ||
|
||
$this->ownGroupManager->addGroups($user, ['groupA']); | ||
} | ||
|
||
public function testAddToNonExistingGroups() { | ||
$this->getGroupManager(); | ||
$user = $this->createMock(IUser::class); | ||
$groupB = $this->createMock(IGroup::class); | ||
|
||
// assert group does not exist | ||
$this->groupManager | ||
->expects($this->at(0)) | ||
->method('get') | ||
->with('groupB') | ||
->willReturn(null); | ||
// assert group is created | ||
$this->ownGroupBackend | ||
->expects($this->once()) | ||
->method('createGroup') | ||
->with('groupB', 'groupB') | ||
->willReturn(true); | ||
$this->groupManager | ||
->expects($this->at(1)) | ||
->method('get') | ||
->with('groupB') | ||
->willReturn($groupB); | ||
// assert user gets added to group | ||
$groupB->expects($this->once()) | ||
->method('addUser') | ||
->with($user); | ||
|
||
$this->ownGroupManager->addGroups($user, ['groupB']); | ||
} | ||
|
||
public function testAddGroupsWithCollision() { | ||
$this->getGroupManager(); | ||
$user = $this->createMock(IUser::class); | ||
$groupC = $this->createMock(IGroup::class); | ||
|
||
// assert group exists | ||
$this->groupManager | ||
->expects($this->at(0)) | ||
->method('get') | ||
->with('groupC') | ||
->willReturn($groupC); | ||
// assert differnt group backend | ||
$groupC->expects($this->once()) | ||
->method('getBackendNames') | ||
->willReturn(['OC\Groups\Database']); | ||
// assert there is only one idp config present | ||
$this->settings | ||
->expects($this->once()) | ||
->method('getPrefix') | ||
->willReturn(''); | ||
// assert the default group prefix is configured | ||
$this->config | ||
->expects($this->at(1)) | ||
->method('getAppValue') | ||
->with('user_saml', 'saml-attribute-mapping-group_mapping_prefix', 'SAML_') | ||
->willReturn('SAML_'); | ||
// assert group is created with prefix + gid | ||
$this->ownGroupBackend | ||
->expects($this->once()) | ||
->method('createGroup') | ||
->with('SAML_groupC', 'groupC') | ||
->willReturn(true); | ||
$this->groupManager | ||
->expects($this->at(1)) | ||
->method('get') | ||
->with('SAML_groupC') | ||
->willReturn($groupC); | ||
// assert user gets added to group | ||
$groupC->expects($this->once()) | ||
->method('addUser') | ||
->with($user); | ||
|
||
$this->ownGroupManager->addGroups($user, ['groupC']); | ||
} | ||
|
||
} |