diff options
Diffstat (limited to 'apps/user_ldap/tests/Group_LDAPTest.php')
-rw-r--r-- | apps/user_ldap/tests/Group_LDAPTest.php | 340 |
1 files changed, 319 insertions, 21 deletions
diff --git a/apps/user_ldap/tests/Group_LDAPTest.php b/apps/user_ldap/tests/Group_LDAPTest.php index 9b5216742fe..8ca7556e09f 100644 --- a/apps/user_ldap/tests/Group_LDAPTest.php +++ b/apps/user_ldap/tests/Group_LDAPTest.php @@ -29,6 +29,7 @@ namespace OCA\User_LDAP\Tests; +use OCP\GroupInterface; use OCA\User_LDAP\Group_LDAP as GroupLDAP; use OCA\User_LDAP\ILDAPWrapper; @@ -69,6 +70,10 @@ class Group_LDAPTest extends \Test\TestCase { return $access; } + private function getPluginManagerMock() { + return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock(); + } + private function enableGroups($access) { $access->connection->expects($this->any()) ->method('__get') @@ -82,6 +87,7 @@ class Group_LDAPTest extends \Test\TestCase { public function testCountEmptySearchString() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); $this->enableGroups($access); @@ -98,7 +104,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('countUsers') ->will($this->returnValue(2)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $users = $groupBackend->countUsersInGroup('group'); $this->assertSame(6, $users); @@ -106,6 +112,7 @@ class Group_LDAPTest extends \Test\TestCase { public function testCountWithSearchString() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); $this->enableGroups($access); @@ -137,14 +144,39 @@ class Group_LDAPTest extends \Test\TestCase { return 'foobar' . \OCP\Util::generateRandomBytes(7); })); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access,$pluginManager); $users = $groupBackend->countUsersInGroup('group', '3'); $this->assertSame(2, $users); } + public function testCountUsersWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','countUsersInGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::COUNT_USERS) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('countUsersInGroup') + ->with('gid', 'search') + ->willReturn(42); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $this->assertEquals($ldap->countUsersInGroup('gid', 'search'),42); + } + public function testGidNumber2NameSuccess() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -158,7 +190,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with('cn=foo,dc=barfoo,dc=bar') ->will($this->returnValue('MyGroup')); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->gidNumber2Name('3117', $userDN); @@ -167,6 +199,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testGidNumberID2NameNoGroup() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -178,7 +212,7 @@ class Group_LDAPTest extends \Test\TestCase { $access->expects($this->never()) ->method('dn2groupname'); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->gidNumber2Name('3117', $userDN); @@ -187,6 +221,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testGidNumberID2NameNoName() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -199,7 +235,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('dn2groupname') ->will($this->returnValue(false)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->gidNumber2Name('3117', $userDN); @@ -208,6 +244,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testGetEntryGidNumberValue() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -218,7 +256,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with($dn, $attr) ->will($this->returnValue(array('3117'))); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $gid = $groupBackend->getGroupGidNumber($dn); @@ -227,6 +265,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testGetEntryGidNumberNoValue() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -237,7 +277,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with($dn, $attr) ->will($this->returnValue(false)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $gid = $groupBackend->getGroupGidNumber($dn); @@ -246,6 +286,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testPrimaryGroupID2NameSuccess() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -264,7 +306,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with('cn=foo,dc=barfoo,dc=bar') ->will($this->returnValue('MyGroup')); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->primaryGroupID2Name('3117', $userDN); @@ -273,6 +315,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testPrimaryGroupID2NameNoSID() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -288,7 +332,7 @@ class Group_LDAPTest extends \Test\TestCase { $access->expects($this->never()) ->method('dn2groupname'); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->primaryGroupID2Name('3117', $userDN); @@ -297,6 +341,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testPrimaryGroupID2NameNoGroup() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -313,7 +359,7 @@ class Group_LDAPTest extends \Test\TestCase { $access->expects($this->never()) ->method('dn2groupname'); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->primaryGroupID2Name('3117', $userDN); @@ -322,6 +368,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testPrimaryGroupID2NameNoName() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -339,7 +387,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('dn2groupname') ->will($this->returnValue(false)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $group = $groupBackend->primaryGroupID2Name('3117', $userDN); @@ -350,6 +398,8 @@ class Group_LDAPTest extends \Test\TestCase { //tests getEntryGroupID via getGroupPrimaryGroupID //which is basically identical to getUserPrimaryGroupIDs $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -360,7 +410,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with($dn, $attr) ->will($this->returnValue(array('3117'))); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $gid = $groupBackend->getGroupPrimaryGroupID($dn); @@ -371,6 +421,8 @@ class Group_LDAPTest extends \Test\TestCase { //tests getEntryGroupID via getGroupPrimaryGroupID //which is basically identical to getUserPrimaryGroupIDs $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -381,7 +433,7 @@ class Group_LDAPTest extends \Test\TestCase { ->with($dn, $attr) ->will($this->returnValue(false)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $gid = $groupBackend->getGroupPrimaryGroupID($dn); @@ -394,6 +446,8 @@ class Group_LDAPTest extends \Test\TestCase { */ public function testInGroupHitsUidGidCache() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $uid = 'someUser'; @@ -408,19 +462,21 @@ class Group_LDAPTest extends \Test\TestCase { $access->expects($this->never()) ->method('username2dn'); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $groupBackend->inGroup($uid, $gid); } public function testGetGroupsWithOffset() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $access->expects($this->once()) ->method('nextcloudGroupNames') ->will($this->returnValue(array('group1', 'group2'))); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $groups = $groupBackend->getGroups('', 2, 2); $this->assertSame(2, count($groups)); @@ -432,6 +488,8 @@ class Group_LDAPTest extends \Test\TestCase { */ public function testUsersInGroupPrimaryMembersOnly() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $access->connection->expects($this->any()) @@ -457,7 +515,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('nextcloudUserNames') ->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $users = $groupBackend->usersInGroup('foobar'); $this->assertSame(7, count($users)); @@ -469,6 +527,8 @@ class Group_LDAPTest extends \Test\TestCase { */ public function testUsersInGroupPrimaryAndUnixMembers() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $access->connection->expects($this->any()) @@ -492,7 +552,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('nextcloudUserNames') ->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad'))); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $users = $groupBackend->usersInGroup('foobar'); $this->assertSame(4, count($users)); @@ -504,6 +564,8 @@ class Group_LDAPTest extends \Test\TestCase { */ public function testCountUsersInGroupPrimaryMembersOnly() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $access->connection->expects($this->any()) @@ -527,7 +589,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('countUsers') ->will($this->returnValue(4)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $users = $groupBackend->countUsersInGroup('foobar'); $this->assertSame(4, $users); @@ -535,6 +597,8 @@ class Group_LDAPTest extends \Test\TestCase { public function testGetUserGroupsMemberOf() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); + $this->enableGroups($access); $dn = 'cn=userX,dc=foobar'; @@ -558,7 +622,7 @@ class Group_LDAPTest extends \Test\TestCase { ->method('groupsMatchFilter') ->will($this->returnArgument(0)); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $groups = $groupBackend->getUserGroups('userX'); $this->assertSame(2, count($groups)); @@ -566,6 +630,7 @@ class Group_LDAPTest extends \Test\TestCase { public function testGetUserGroupsMemberOfDisabled() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); $access->connection->expects($this->any()) ->method('__get') @@ -595,12 +660,13 @@ class Group_LDAPTest extends \Test\TestCase { ->method('nextcloudGroupNames') ->will($this->returnValue([])); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $groupBackend->getUserGroups('userX'); } public function testGetGroupsByMember() { $access = $this->getAccessMock(); + $pluginManager = $this->getPluginManagerMock(); $access->connection->expects($this->any()) ->method('__get') @@ -646,11 +712,243 @@ class Group_LDAPTest extends \Test\TestCase { ->method('fetchListOfGroups') ->will($this->returnValue([$group1, $group2])); - $groupBackend = new GroupLDAP($access); + $groupBackend = new GroupLDAP($access, $pluginManager); $groups = $groupBackend->getUserGroups('userX'); $this->assertEquals(['group1', 'group2'], $groups); $groupsAgain = $groupBackend->getUserGroups('userX'); $this->assertEquals(['group1', 'group2'], $groupsAgain); } + + public function testCreateGroupWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','createGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::CREATE_GROUP) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('createGroup') + ->with('gid') + ->willReturn('result'); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $this->assertEquals($ldap->createGroup('gid'),true); + } + + /** + * @expectedException \Exception + */ + public function testCreateGroupFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions', 'createGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::CREATE_GROUP) + ->willReturn(false); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $ldap->createGroup('gid'); + } + + public function testDeleteGroupWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','deleteGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::DELETE_GROUP) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('deleteGroup') + ->with('gid') + ->willReturn('result'); + + $access = $this->getAccessMock(); + + $mapper = $this->getMockBuilder('\OCA\User_LDAP\Mapping\GroupMapping') + ->setMethods(['unmap']) + ->disableOriginalConstructor() + ->getMock(); + + $access->expects($this->any()) + ->method('getGroupMapper') + ->will($this->returnValue($mapper)); + + $ldap = new GroupLDAP( + $access, + $pluginManager + ); + + $this->assertEquals($ldap->deleteGroup('gid'),'result'); + } + + /** + * @expectedException \Exception + */ + public function testDeleteGroupFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions', 'deleteGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::DELETE_GROUP) + ->willReturn(false); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $ldap->deleteGroup('gid'); + } + + public function testAddToGroupWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','addToGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::ADD_TO_GROUP) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('addToGroup') + ->with('uid', 'gid') + ->willReturn('result'); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $this->assertEquals($ldap->addToGroup('uid', 'gid'),'result'); + } + + /** + * @expectedException \Exception + */ + public function testAddToGroupFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions', 'addToGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::ADD_TO_GROUP) + ->willReturn(false); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $ldap->addToGroup('uid', 'gid'); + } + + public function testRemoveFromGroupWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','removeFromGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::REMOVE_FROM_GROUP) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('removeFromGroup') + ->with('uid', 'gid') + ->willReturn('result'); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $this->assertEquals($ldap->removeFromGroup('uid', 'gid'),'result'); + } + + /** + * @expectedException \Exception + */ + public function testRemoveFromGroupFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions', 'removeFromGroup']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::REMOVE_FROM_GROUP) + ->willReturn(false); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $ldap->removeFromGroup('uid', 'gid'); + } + + public function testGetGroupDetailsWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions','getGroupDetails']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::GROUP_DETAILS) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('getGroupDetails') + ->with('gid') + ->willReturn('result'); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $this->assertEquals($ldap->getGroupDetails('gid'),'result'); + } + + /** + * @expectedException \Exception + */ + public function testGetGroupDetailsFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager') + ->setMethods(['implementsActions', 'getGroupDetails']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(GroupInterface::GROUP_DETAILS) + ->willReturn(false); + + $ldap = new GroupLDAP( + $this->getAccessMock(), + $pluginManager + ); + + $ldap->getGroupDetails('gid'); + } + } |