diff options
Diffstat (limited to 'apps/user_ldap/tests')
16 files changed, 1760 insertions, 128 deletions
diff --git a/apps/user_ldap/tests/GroupLDAPPluginTest.php b/apps/user_ldap/tests/GroupLDAPPluginTest.php new file mode 100644 index 00000000000..861806677e5 --- /dev/null +++ b/apps/user_ldap/tests/GroupLDAPPluginTest.php @@ -0,0 +1,247 @@ +<?php +/** + * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br) + * + * @author Vinicius Brand <vinicius@eita.org.br> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\User_LDAP\Tests; + + +use OCP\GroupInterface; +use OCA\User_LDAP\GroupPluginManager; + +class GroupLDAPPluginTest extends \Test\TestCase { + + /** + * @return GroupPluginManager + */ + private function getGroupPluginManager() { + return new GroupPluginManager(); + } + + public function testImplementsActions() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::CREATE_GROUP); + + $plugin2 = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions']) + ->getMock(); + + $plugin2->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::ADD_TO_GROUP); + + $pluginManager->register($plugin); + $pluginManager->register($plugin2); + + $this->assertEquals($pluginManager->getImplementedActions(), GroupInterface::CREATE_GROUP | GroupInterface::ADD_TO_GROUP); + $this->assertTrue($pluginManager->implementsActions(GroupInterface::CREATE_GROUP)); + $this->assertTrue($pluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)); + } + + public function testCreateGroup() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'createGroup']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::CREATE_GROUP); + + $plugin->expects($this->once()) + ->method('createGroup') + ->with( + $this->equalTo('group') + ); + + $pluginManager->register($plugin); + $pluginManager->createGroup('group'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements createGroup in this LDAP Backend. + */ + public function testCreateGroupNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->createGroup('foo'); + } + + public function testDeleteGroup() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'deleteGroup']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::DELETE_GROUP); + + $plugin->expects($this->once()) + ->method('deleteGroup') + ->with( + $this->equalTo('group') + ); + + $pluginManager->register($plugin); + $pluginManager->deleteGroup('group'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements deleteGroup in this LDAP Backend. + */ + public function testDeleteGroupNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->deleteGroup('foo'); + } + + public function testAddToGroup() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'addToGroup']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::ADD_TO_GROUP); + + $plugin->expects($this->once()) + ->method('addToGroup') + ->with( + $this->equalTo('uid'), + $this->equalTo('gid') + ); + + $pluginManager->register($plugin); + $pluginManager->addToGroup('uid', 'gid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements addToGroup in this LDAP Backend. + */ + public function testAddToGroupNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->addToGroup('foo', 'bar'); + } + + public function testRemoveFromGroup() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'removeFromGroup']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::REMOVE_FROM_GROUP); + + $plugin->expects($this->once()) + ->method('removeFromGroup') + ->with( + $this->equalTo('uid'), + $this->equalTo('gid') + ); + + $pluginManager->register($plugin); + $pluginManager->removeFromGroup('uid', 'gid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements removeFromGroup in this LDAP Backend. + */ + public function testRemoveFromGroupNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->removeFromGroup('foo', 'bar'); + } + + public function testCountUsersInGroup() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'countUsersInGroup']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::COUNT_USERS); + + $plugin->expects($this->once()) + ->method('countUsersInGroup') + ->with( + $this->equalTo('gid'), + $this->equalTo('search') + ); + + $pluginManager->register($plugin); + $pluginManager->countUsersInGroup('gid', 'search'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements countUsersInGroup in this LDAP Backend. + */ + public function testCountUsersInGroupNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->countUsersInGroup('foo', 'bar'); + } + + public function testgetGroupDetails() { + $pluginManager = $this->getGroupPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy') + ->setMethods(['respondToActions', 'getGroupDetails']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(GroupInterface::GROUP_DETAILS); + + $plugin->expects($this->once()) + ->method('getGroupDetails') + ->with( + $this->equalTo('gid') + ); + + $pluginManager->register($plugin); + $pluginManager->getGroupDetails('gid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements getGroupDetails in this LDAP Backend. + */ + public function testgetGroupDetailsNotRegistered() { + $pluginManager = $this->getGroupPluginManager(); + $pluginManager->getGroupDetails('foo'); + } +} 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'); + } + } diff --git a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php index 84e1e6b458a..8f9104999dd 100644 --- a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php +++ b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php @@ -68,6 +68,13 @@ abstract class AbstractIntegrationTest { * the LDAP backend. */ public function init() { + \OC::$server->registerService('LDAPUserPluginManager', function() { + return new \OCA\User_LDAP\UserPluginManager(); + }); + \OC::$server->registerService('LDAPGroupPluginManager', function() { + return new \OCA\User_LDAP\GroupPluginManager(); + }); + $this->initLDAPWrapper(); $this->initConnection(); $this->initUserManager(); diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php index bd8e4bdd7a2..bc616bfc772 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php @@ -49,12 +49,12 @@ class IntegrationTestAttributeDetection extends AbstractIntegrationTest { $groupMapper->clear(); $this->access->setGroupMapper($groupMapper); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); $userManager = \OC::$server->getUserManager(); $userManager->clearBackends(); $userManager->registerBackend($userBackend); - $groupBackend = new Group_LDAP($this->access); + $groupBackend = new Group_LDAP($this->access, \OC::$server->query('LDAPGroupPluginManager')); $groupManger = \OC::$server->getGroupManager(); $groupManger->clearBackends(); $groupManger->addBackend($groupBackend); diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php index 95bfb99b65b..e96e44a6858 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php @@ -47,7 +47,7 @@ class IntegrationTestFetchUsersByLoginName extends AbstractIntegrationTest { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); } /** diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php index 3c8cf22bb5b..c5f74863d9e 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php @@ -47,7 +47,7 @@ class IntegrationTestPaging extends AbstractIntegrationTest { require(__DIR__ . '/../setup-scripts/createExplicitUsers.php'); parent::init(); - $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); } public function initConnection() { diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php index 765cee00f91..c50baef49f3 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php @@ -51,7 +51,7 @@ class IntegrationTestUserHome extends AbstractIntegrationTest { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); } /** diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php index 8c9e215edaa..f8a317f0b10 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php @@ -50,7 +50,7 @@ class IntegrationTestUserAvatar extends AbstractIntegrationTest { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php index ce81b9a26d6..10f0968efb0 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php @@ -46,7 +46,7 @@ class IntegrationTestUserCleanUp extends AbstractIntegrationTest { $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php index 1f5d16567f7..187d13dfdba 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php @@ -43,7 +43,7 @@ class IntegrationTestUserDisplayName extends AbstractIntegrationTest { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query('LDAPUserPluginManager')); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/LDAPGroupPluginDummy.php b/apps/user_ldap/tests/LDAPGroupPluginDummy.php new file mode 100644 index 00000000000..c2ccf3338a7 --- /dev/null +++ b/apps/user_ldap/tests/LDAPGroupPluginDummy.php @@ -0,0 +1,59 @@ +<?php +/** + * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br) + * + * @author Vinicius Brand <vinicius@eita.org.br> + * @author Daniel Tygel <dtygel@eita.org.br> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\User_LDAP\Tests; + + +use OCA\User_LDAP\ILDAPGroupPlugin; + +class LDAPGroupPluginDummy implements ILDAPGroupPlugin { + + + public function respondToActions() { + return null; + } + + public function createGroup($gid) { + return null; + } + + public function deleteGroup($gid) { + return null; + } + + public function addToGroup($uid, $gid) { + return null; + } + + public function removeFromGroup($uid, $gid) { + return null; + } + + public function countUsersInGroup($gid, $search = '') { + return null; + } + + public function getGroupDetails($gid) { + return null; + } +} diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php index 585e0df662b..42a1b0a3c23 100644 --- a/apps/user_ldap/tests/LDAPProviderTest.php +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -3,6 +3,9 @@ * * @copyright Copyright (c) 2016, Roger Szabo (roger.szabo@web.de) * + * @author Roger Szabo <roger.szabo@web.de> + * @author Vinicius Brand <vinicius@eita.org.br> + * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -22,6 +25,8 @@ namespace OCA\User_LDAP\Tests; +use OCA\User_LDAP\IGroupLDAP; +use OCP\IConfig; use OCP\IServerContainer; use OCA\User_LDAP\IUserLDAP; @@ -38,21 +43,57 @@ class LDAPProviderTest extends \Test\TestCase { parent::setUp(); } - private function getServerMock(IUserLDAP $backend) { + private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) { $server = $this->getMockBuilder('OC\Server') - ->setMethods(['getUserManager', 'getBackends']) + ->setMethods(['getUserManager', 'getBackends', 'getGroupManager']) ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)]) ->getMock(); $server->expects($this->at(1)) ->method('getBackends') - ->willReturn([$backend]); + ->willReturn([$userBackend]); + $server->expects($this->any()) + ->method('getUserManager') + ->willReturn($this->getUserManagerMock($userBackend)); + $server->expects($this->any()) + ->method('getGroupManager') + ->willReturn($this->getGroupManagerMock($groupBackend)); $server->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); return $server; } + + private function getUserManagerMock(IUserLDAP $userBackend) { + $userManager = $this->getMockBuilder('OC\User\Manager') + ->setMethods(['getBackends']) + ->setConstructorArgs([$this->createMock(IConfig::class)]) + ->getMock(); + $userManager->expects($this->any()) + ->method('getBackends') + ->willReturn([$userBackend]); + return $userManager; + } + private function getGroupManagerMock(IGroupLDAP $groupBackend) { + $groupManager = $this->getMockBuilder('OC\Group\Manager') + ->setMethods(['getBackends']) + ->disableOriginalConstructor() + ->getMock(); + $groupManager->expects($this->any()) + ->method('getBackends') + ->willReturn([$groupBackend]); + return $groupManager; + } + + private function getDefaultGroupBackendMock() { + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + return $groupBackend; + } + private function getLDAPProvider(IServerContainer $serverContainer) { $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer); return $factory->getLDAPProvider(); @@ -63,50 +104,100 @@ class LDAPProviderTest extends \Test\TestCase { * @expectedExceptionMessage User id not found in LDAP */ public function testGetUserDNUserIDNotFound() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any())->method('userExists')->willReturn(false); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getUserDN('nonexisting_user'); } public function testGetUserDN() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'username2dn']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->at(0)) + $userBackend->expects($this->at(0)) ->method('userExists') ->willReturn(true); - $backend->expects($this->at(2)) + $userBackend->expects($this->at(2)) ->method('username2dn') ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org', $ldapProvider->getUserDN('existing_user')); } + /** + * @expectedException \Exception + * @expectedExceptionMessage Group id not found in LDAP + */ + public function testGetGroupDNGroupIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getGroupDN('nonexisting_group'); + } + + public function testGetGroupDN() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'username2dn']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists', 'getLDAPAccess', 'groupname2dn']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->at(0)) + ->method('groupExists') + ->willReturn(true); + $groupBackend->expects($this->at(2)) + ->method('groupname2dn') + ->willReturn('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org'); + $groupBackend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org', + $ldapProvider->getGroupDN('existing_group')); + } + public function testGetUserName() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['dn2UserName']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method('dn2UserName') ->willReturn('existing_user'); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('existing_user', @@ -114,12 +205,12 @@ class LDAPProviderTest extends \Test\TestCase { } public function testDNasBaseParameter() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig()); @@ -130,12 +221,12 @@ class LDAPProviderTest extends \Test\TestCase { } public function testSanitizeDN() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig()); @@ -150,69 +241,115 @@ class LDAPProviderTest extends \Test\TestCase { * @expectedExceptionMessage User id not found in LDAP */ public function testGetLDAPConnectionUserIDNotFound() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any())->method('userExists')->willReturn(false); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPConnection('nonexisting_user'); } public function testGetLDAPConnection() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getNewLDAPConnection']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method('userExists') ->willReturn(true); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method('getNewLDAPConnection') ->willReturn(true); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertTrue($ldapProvider->getLDAPConnection('existing_user')); } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Group id not found in LDAP + */ + public function testGetGroupLDAPConnectionGroupIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getGroupLDAPConnection('nonexisting_group'); + } + + public function testGetGroupLDAPConnection() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists','getNewLDAPConnection']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->any()) + ->method('groupExists') + ->willReturn(true); + + $groupBackend->expects($this->any()) + ->method('getNewLDAPConnection') + ->willReturn(true); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group')); + } /** * @expectedException \Exception * @expectedExceptionMessage User id not found in LDAP */ public function testGetLDAPBaseUsersUserIDNotFound() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any())->method('userExists')->willReturn(false); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPBaseUsers('nonexisting_user'); } public function testGetLDAPBaseUsers() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->at(0)) + $userBackend->expects($this->at(0)) ->method('userExists') ->willReturn(true); - $backend->expects($this->at(3)) + $userBackend->expects($this->at(3)) ->method('getConfiguration') ->willReturn(array('ldap_base_users'=>'ou=users,dc=example,dc=org')); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('ou=users,dc=example,dc=org', $ldapProvider->getLDAPBaseUsers('existing_user')); @@ -223,34 +360,34 @@ class LDAPProviderTest extends \Test\TestCase { * @expectedExceptionMessage User id not found in LDAP */ public function testGetLDAPBaseGroupsUserIDNotFound() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any())->method('userExists')->willReturn(false); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->getLDAPBaseGroups('nonexisting_user'); } public function testGetLDAPBaseGroups() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->at(0)) + $userBackend->expects($this->at(0)) ->method('userExists') ->willReturn(true); - $backend->expects($this->at(3)) + $userBackend->expects($this->at(3)) ->method('getConfiguration') ->willReturn(array('ldap_base_groups'=>'ou=groups,dc=example,dc=org')); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertEquals('ou=groups,dc=example,dc=org', $ldapProvider->getLDAPBaseGroups('existing_user')); @@ -261,62 +398,107 @@ class LDAPProviderTest extends \Test\TestCase { * @expectedExceptionMessage User id not found in LDAP */ public function testClearCacheUserIDNotFound() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any())->method('userExists')->willReturn(false); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->clearCache('nonexisting_user'); } public function testClearCache() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->at(0)) + $userBackend->expects($this->at(0)) ->method('userExists') ->willReturn(true); - $backend->expects($this->at(3)) + $userBackend->expects($this->at(3)) ->method('clearCache') ->willReturn(true); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method($this->anything()) ->willReturnSelf(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->clearCache('existing_user'); $this->assertTrue(TRUE); } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Group id not found in LDAP + */ + public function testClearGroupCacheGroupIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists']) + ->disableOriginalConstructor() + ->getMock(); + $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->clearGroupCache('nonexisting_group'); + } + + public function testClearGroupCache() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'clearCache']) + ->disableOriginalConstructor() + ->getMock(); + $groupBackend->expects($this->at(0)) + ->method('groupExists') + ->willReturn(true); + $groupBackend->expects($this->at(3)) + ->method('clearCache') + ->willReturn(true); + $groupBackend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->clearGroupCache('existing_group'); + $this->assertTrue(TRUE); + } public function testDnExists() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods(['dn2UserName']) ->disableOriginalConstructor() ->getMock(); - $backend->expects($this->any()) + $userBackend->expects($this->any()) ->method('dn2UserName') ->willReturn('existing_user'); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } public function testFlagRecord() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->flagRecord('existing_user'); @@ -324,15 +506,140 @@ class LDAPProviderTest extends \Test\TestCase { } public function testUnflagRecord() { - $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') ->setMethods([]) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($backend); + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider = $this->getLDAPProvider($server); $ldapProvider->unflagRecord('existing_user'); $this->assertTrue(TRUE); } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetLDAPDisplayNameFieldUserIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPDisplayNameField('nonexisting_user'); + } + + public function testGetLDAPDisplayNameField() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) + ->disableOriginalConstructor() + ->getMock(); + $userBackend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $userBackend->expects($this->at(3)) + ->method('getConfiguration') + ->willReturn(array('ldap_display_name'=>'displayName')); + $userBackend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetLDAPEmailFieldUserIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $userBackend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPEmailField('nonexisting_user'); + } + + public function testGetLDAPEmailField() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) + ->disableOriginalConstructor() + ->getMock(); + $userBackend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $userBackend->expects($this->at(3)) + ->method('getConfiguration') + ->willReturn(array('ldap_email_attr'=>'mail')); + $userBackend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Group id not found in LDAP + */ + public function testGetLDAPGroupMemberAssocUserIDNotFound() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPGroupMemberAssoc('nonexisting_group'); + } + + public function testgetLDAPGroupMemberAssoc() { + $userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP') + ->setMethods(['groupExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) + ->disableOriginalConstructor() + ->getMock(); + + $groupBackend->expects($this->at(0)) + ->method('groupExists') + ->willReturn(true); + $groupBackend->expects($this->any()) + ->method('getConfiguration') + ->willReturn(array('ldap_group_member_assoc_attribute'=>'assoc_type')); + $groupBackend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($userBackend, $groupBackend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group')); + } + } diff --git a/apps/user_ldap/tests/LDAPUserPluginDummy.php b/apps/user_ldap/tests/LDAPUserPluginDummy.php new file mode 100644 index 00000000000..8115141b5e2 --- /dev/null +++ b/apps/user_ldap/tests/LDAPUserPluginDummy.php @@ -0,0 +1,63 @@ +<?php +/** + * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br) + * + * @author Vinicius Brand <vinicius@eita.org.br> + * @author Daniel Tygel <dtygel@eita.org.br> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\User_LDAP\Tests; + + +use OCA\User_LDAP\ILDAPUserPlugin; + +class LDAPUserPluginDummy implements ILDAPUserPlugin { + + public function respondToActions() { + return null; + } + + public function createUser($username, $password) { + return null; + } + + public function setPassword($uid, $password) { + return null; + } + + public function getHome($uid) { + return null; + } + + public function getDisplayName($uid) { + return null; + } + + public function setDisplayName($uid, $displayName) { + return null; + } + + public function canChangeAvatar($uid) { + return null; + } + + public function countUsers() { + return null; + } + +} diff --git a/apps/user_ldap/tests/UserLDAPPluginTest.php b/apps/user_ldap/tests/UserLDAPPluginTest.php new file mode 100644 index 00000000000..2fe9d6f7352 --- /dev/null +++ b/apps/user_ldap/tests/UserLDAPPluginTest.php @@ -0,0 +1,310 @@ +<?php +/** + * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br) + * + * @author Vinicius Brand <vinicius@eita.org.br> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\User_LDAP\Tests; + + +use OC\User\Backend; +use OCA\User_LDAP\UserPluginManager; + +class UserLDAPPluginTest extends \Test\TestCase { + + /** + * @return UserPluginManager + */ + private function getUserPluginManager() { + return new UserPluginManager(); + } + + public function testImplementsActions() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::CREATE_USER); + + $plugin2 = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions']) + ->getMock(); + + $plugin2->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::PROVIDE_AVATAR); + + $pluginManager->register($plugin); + $pluginManager->register($plugin2); + + $this->assertEquals($pluginManager->getImplementedActions(), Backend::CREATE_USER | Backend::PROVIDE_AVATAR); + $this->assertTrue($pluginManager->implementsActions(Backend::CREATE_USER)); + $this->assertTrue($pluginManager->implementsActions(Backend::PROVIDE_AVATAR)); + } + + public function testCreateUser() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'createUser']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::CREATE_USER); + + $plugin->expects($this->once()) + ->method('createUser') + ->with( + $this->equalTo('user'), + $this->equalTo('password') + ); + + $pluginManager->register($plugin); + $pluginManager->createUser('user', 'password'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements createUser in this LDAP Backend. + */ + public function testCreateUserNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->createUser('foo','bar'); + } + + public function testSetPassword() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'setPassword']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::SET_PASSWORD); + + $plugin->expects($this->once()) + ->method('setPassword') + ->with( + $this->equalTo('user'), + $this->equalTo('password') + ); + + $pluginManager->register($plugin); + $pluginManager->setPassword('user', 'password'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements setPassword in this LDAP Backend. + */ + public function testSetPasswordNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->setPassword('foo','bar'); + } + + public function testGetHome() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'getHome']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::GET_HOME); + + $plugin->expects($this->once()) + ->method('getHome') + ->with( + $this->equalTo('uid') + ); + + $pluginManager->register($plugin); + $pluginManager->getHome('uid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements getHome in this LDAP Backend. + */ + public function testGetHomeNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->getHome('foo'); + } + + public function testGetDisplayName() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'getDisplayName']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::GET_DISPLAYNAME); + + $plugin->expects($this->once()) + ->method('getDisplayName') + ->with( + $this->equalTo('uid') + ); + + $pluginManager->register($plugin); + $pluginManager->getDisplayName('uid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements getDisplayName in this LDAP Backend. + */ + public function testGetDisplayNameNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->getDisplayName('foo'); + } + + public function testSetDisplayName() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'setDisplayName']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::SET_DISPLAYNAME); + + $plugin->expects($this->once()) + ->method('setDisplayName') + ->with( + $this->equalTo('user'), + $this->equalTo('password') + ); + + $pluginManager->register($plugin); + $pluginManager->setDisplayName('user', 'password'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements setDisplayName in this LDAP Backend. + */ + public function testSetDisplayNameNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->setDisplayName('foo', 'bar'); + } + + public function testCanChangeAvatar() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'canChangeAvatar']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::PROVIDE_AVATAR); + + $plugin->expects($this->once()) + ->method('canChangeAvatar') + ->with( + $this->equalTo('uid') + ); + + $pluginManager->register($plugin); + $pluginManager->canChangeAvatar('uid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements canChangeAvatar in this LDAP Backend. + */ + public function testCanChangeAvatarNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->canChangeAvatar('foo'); + } + + public function testCountUsers() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'countUsers']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(Backend::COUNT_USERS); + + $plugin->expects($this->once()) + ->method('countUsers'); + + $pluginManager->register($plugin); + $pluginManager->countUsers(); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements countUsers in this LDAP Backend. + */ + public function testCountUsersNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->countUsers(); + } + + public function testDeleteUser() { + $pluginManager = $this->getUserPluginManager(); + + $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy') + ->setMethods(['respondToActions', 'canDeleteUser','deleteUser']) + ->getMock(); + + $plugin->expects($this->any()) + ->method('respondToActions') + ->willReturn(0); + + $plugin->expects($this->any()) + ->method('canDeleteUser') + ->willReturn(true); + + $plugin->expects($this->once()) + ->method('deleteUser') + ->with( + $this->equalTo('uid') + ); + + $this->assertFalse($pluginManager->canDeleteUser()); + $pluginManager->register($plugin); + $this->assertTrue($pluginManager->canDeleteUser()); + $pluginManager->deleteUser('uid'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage No plugin implements deleteUser in this LDAP Backend. + */ + public function testDeleteUserNotRegistered() { + $pluginManager = $this->getUserPluginManager(); + $pluginManager->deleteUser('foo'); + } +} diff --git a/apps/user_ldap/tests/User_LDAPTest.php b/apps/user_ldap/tests/User_LDAPTest.php index 44bc55b4148..b6582c816ef 100644 --- a/apps/user_ldap/tests/User_LDAPTest.php +++ b/apps/user_ldap/tests/User_LDAPTest.php @@ -10,6 +10,7 @@ * @author Robin McCorkell <robin@mccorkell.me.uk> * @author Thomas Müller <thomas.mueller@tmit.eu> * @author Roger Szabo <roger.szabo@web.de> + * @author Vinicius Brand <vinicius@eita.org.br> * * @license AGPL-3.0 * @@ -29,6 +30,7 @@ namespace OCA\User_LDAP\Tests; +use OC\User\Backend; use OC\User\Session; use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; @@ -119,6 +121,10 @@ class User_LDAPTest extends TestCase { return $access; } + private function getDefaultPluginManagerMock() { + return $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager')->getMock(); + } + private function prepareMockForUserExists(&$access) { $access->expects($this->any()) ->method('username2dn') @@ -207,7 +213,8 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = $backend->checkPassword('roland', 'dt19'); @@ -218,7 +225,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = $backend->checkPassword('roland', 'wrong'); @@ -229,7 +236,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = $backend->checkPassword('mallory', 'evil'); @@ -244,7 +251,7 @@ class User_LDAPTest extends TestCase { ->method('username2dn') ->will($this->returnValue(false)); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = $backend->checkPassword('roland', 'dt19'); @@ -254,7 +261,7 @@ class User_LDAPTest extends TestCase { public function testCheckPasswordPublicAPI() { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::checkPassword('roland', 'dt19'); @@ -264,7 +271,7 @@ class User_LDAPTest extends TestCase { public function testCheckPasswordPublicAPIWrongPassword() { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::checkPassword('roland', 'wrong'); @@ -274,7 +281,7 @@ class User_LDAPTest extends TestCase { public function testCheckPasswordPublicAPIWrongUser() { $access = $this->getAccessMock(); $this->prepareAccessForCheckPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::checkPassword('mallory', 'evil'); @@ -283,7 +290,7 @@ class User_LDAPTest extends TestCase { public function testDeleteUserCancel() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->deleteUser('notme'); $this->assertFalse($result); } @@ -313,7 +320,7 @@ class User_LDAPTest extends TestCase { ->method('getOCName') ->willReturn($uid); - $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $user = $this->createMock(IUser::class); $user->expects($this->once()) @@ -326,6 +333,36 @@ class User_LDAPTest extends TestCase { $this->assertSame($backend->getHome($uid), $home); } + public function testDeleteUserWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['canDeleteUser','deleteUser']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('canDeleteUser') + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('deleteUser') + ->with('uid') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->deleteUser('uid'),'result'); + } + /** * Prepares the Access mock for getUsers tests * @param Access $access mock @@ -381,7 +418,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersNoParam() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->getUsers(); $this->assertEquals(3, count($result)); @@ -390,7 +427,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersLimitOffset() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->getUsers('', 1, 2); $this->assertEquals(1, count($result)); @@ -399,7 +436,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersLimitOffset2() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->getUsers('', 2, 1); $this->assertEquals(2, count($result)); @@ -408,7 +445,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersSearchWithResult() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->getUsers('yo'); $this->assertEquals(2, count($result)); @@ -417,7 +454,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersSearchEmptyResult() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->getUsers('nix'); $this->assertEquals(0, count($result)); @@ -426,7 +463,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersViaAPINoParam() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::getUsers(); @@ -436,7 +473,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersViaAPILimitOffset() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::getUsers('', 1, 2); @@ -446,7 +483,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersViaAPILimitOffset2() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::getUsers('', 2, 1); @@ -456,7 +493,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersViaAPISearchWithResult() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::getUsers('yo'); @@ -466,7 +503,7 @@ class User_LDAPTest extends TestCase { public function testGetUsersViaAPISearchEmptyResult() { $access = $this->getAccessMock(); $this->prepareAccessForGetUsers($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $result = \OCP\User::getUsers('nix'); @@ -475,7 +512,7 @@ class User_LDAPTest extends TestCase { public function testUserExists() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->expects($this->any()) @@ -497,7 +534,7 @@ class User_LDAPTest extends TestCase { */ public function testUserExistsForDeleted() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->expects($this->any()) @@ -515,7 +552,7 @@ class User_LDAPTest extends TestCase { public function testUserExistsForNeverExisting() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->expects($this->any()) @@ -534,7 +571,7 @@ class User_LDAPTest extends TestCase { public function testUserExistsPublicAPI() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); \OC_User::useBackend($backend); @@ -557,7 +594,7 @@ class User_LDAPTest extends TestCase { */ public function testUserExistsPublicAPIForDeleted() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); \OC_User::useBackend($backend); @@ -576,7 +613,7 @@ class User_LDAPTest extends TestCase { public function testUserExistsPublicAPIForNeverExisting() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); \OC_User::useBackend($backend); @@ -596,7 +633,7 @@ class User_LDAPTest extends TestCase { public function testDeleteUserExisting() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); //we do not support deleting existing users at all $result = $backend->deleteUser('gunslinger'); @@ -607,7 +644,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $config = $this->createMock(IConfig::class); $noti = $this->createMock(INotificationManager::class); - $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class)); + $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->connection->expects($this->any()) @@ -643,7 +680,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $config = $this->createMock(IConfig::class); $noti = $this->createMock(INotificationManager::class); - $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class)); + $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $dataDir = \OC::$server->getConfig()->getSystemValue( @@ -686,7 +723,7 @@ class User_LDAPTest extends TestCase { */ public function testGetHomeNoPath() { $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->connection->expects($this->any()) @@ -719,7 +756,7 @@ class User_LDAPTest extends TestCase { $uid = 'newyorker'; $access = $this->getAccessMock(); - $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->connection->expects($this->any()) @@ -751,6 +788,43 @@ class User_LDAPTest extends TestCase { $backend->getHome($uid); } + public function testGetHomeWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','getHome']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::GET_HOME) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('getHome') + ->with('uid') + ->willReturn('result'); + + $access = $this->getAccessMock(); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $access->connection->expects($this->any()) + ->method('getFromCache') + ->will($this->returnCallback(function($uid) { + return true; + })); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->getHome('uid'),'result'); + } + private function prepareAccessForGetDisplayName(&$access) { $access->connection->expects($this->any()) ->method('__get') @@ -792,7 +866,7 @@ class User_LDAPTest extends TestCase { public function testGetDisplayName() { $access = $this->getAccessMock(); $this->prepareAccessForGetDisplayName($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->connection->expects($this->any()) @@ -833,7 +907,7 @@ class User_LDAPTest extends TestCase { } })); $this->prepareAccessForGetDisplayName($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $this->prepareMockForUserExists($access); $access->connection->expects($this->any()) @@ -853,6 +927,37 @@ class User_LDAPTest extends TestCase { $this->assertEquals('newyorker', $result); } + public function testGetDisplayNameWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','getDisplayName']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::GET_DISPLAYNAME) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('getDisplayName') + ->with('uid') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->getDisplayName('uid'),'result'); + } + //no test for getDisplayNames, because it just invokes getUsers and //getDisplayName @@ -863,7 +968,7 @@ class User_LDAPTest extends TestCase { ->method('countUsers') ->will($this->returnValue(5)); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->countUsers(); $this->assertEquals(5, $result); @@ -876,12 +981,42 @@ class User_LDAPTest extends TestCase { ->method('countUsers') ->will($this->returnValue(false)); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $result = $backend->countUsers(); $this->assertFalse($result); } + public function testCountUsersWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','countUsers']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::COUNT_USERS) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('countUsers') + ->willReturn(42); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->countUsers(),42); + } + public function testLoginName2UserNameSuccess() { $loginName = 'Alice'; $username = 'alice'; @@ -909,7 +1044,7 @@ class User_LDAPTest extends TestCase { ->method('writeToCache') ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $name = $backend->loginName2UserName($loginName); $this->assertSame($username, $name); @@ -938,7 +1073,7 @@ class User_LDAPTest extends TestCase { ->method('writeToCache') ->with($this->equalTo('loginName2UserName-'.$loginName), false); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $name = $backend->loginName2UserName($loginName); $this->assertSame(false, $name); @@ -985,7 +1120,7 @@ class User_LDAPTest extends TestCase { ->method('getUserValue') ->willReturn(1); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $name = $backend->loginName2UserName($loginName); $this->assertSame(false, $name); @@ -1010,7 +1145,7 @@ class User_LDAPTest extends TestCase { } return null; })); - + $access->connection->expects($this->any()) ->method('getFromCache') ->will($this->returnCallback(function($uid) { @@ -1066,7 +1201,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForSetPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $this->assertTrue(\OC_User::setPassword('roland', 'dt')); @@ -1076,7 +1211,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForSetPassword($access); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); @@ -1086,7 +1221,7 @@ class User_LDAPTest extends TestCase { $access = $this->getAccessMock(); $this->prepareAccessForSetPassword($access, false); - $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class)); + $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); \OC_User::useBackend($backend); $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); @@ -1111,7 +1246,8 @@ class User_LDAPTest extends TestCase { $access, $config, $noti, - $userSession + $userSession, + $this->getDefaultPluginManagerMock() ); $ldap->setPassword('NotExistingUser', 'Password'); } @@ -1136,8 +1272,185 @@ class User_LDAPTest extends TestCase { $access, $config, $noti, - $userSession + $userSession, + $this->getDefaultPluginManagerMock() ); $this->assertFalse($ldap->setPassword('NotExistingUser', 'Password')); } + + public function testSetPasswordWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','setPassword']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::SET_PASSWORD) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('setPassword') + ->with('uid','password') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->setPassword('uid', 'password'),'result'); + } + + public function testCanChangeAvatarWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','canChangeAvatar']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::PROVIDE_AVATAR) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('canChangeAvatar') + ->with('uid') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->canChangeAvatar('uid'),'result'); + } + + public function testSetDisplayNameWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','setDisplayName']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::SET_DISPLAYNAME) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('setDisplayName') + ->with('uid','displayName') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->setDisplayName('uid', 'displayName'),'result'); + } + + public function testSetDisplayNameFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','setDisplayName']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::SET_DISPLAYNAME) + ->willReturn(false); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertFalse($ldap->setDisplayName('uid', 'displayName')); + } + + public function testCreateUserWithPlugin() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions','createUser']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::CREATE_USER) + ->willReturn(true); + + $pluginManager->expects($this->once()) + ->method('createUser') + ->with('uid','password') + ->willReturn('result'); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertEquals($ldap->createUser('uid', 'password'),'result'); + } + + public function testCreateUserFailing() { + $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\UserPluginManager') + ->setMethods(['implementsActions', 'createUser']) + ->getMock(); + + $pluginManager->expects($this->once()) + ->method('implementsActions') + ->with(Backend::CREATE_USER) + ->willReturn(false); + + $access = $this->createMock(Access::class); + $config = $this->createMock(IConfig::class); + $noti = $this->createMock(INotificationManager::class); + $session = $this->createMock(Session::class); + + $ldap = new User_LDAP( + $access, + $config, + $noti, + $session, + $pluginManager + ); + + $this->assertFalse($ldap->createUser('uid', 'password')); + } } diff --git a/apps/user_ldap/tests/User_ProxyTest.php b/apps/user_ldap/tests/User_ProxyTest.php index 68b1e4428ca..f6aaa01cb8d 100644 --- a/apps/user_ldap/tests/User_ProxyTest.php +++ b/apps/user_ldap/tests/User_ProxyTest.php @@ -2,6 +2,9 @@ /** * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch> * + * @author Lukas Reschke <lukas@statuscode.ch> + * @author Vinicius Brand <vinicius@eita.org.br> + * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -21,8 +24,10 @@ namespace OCA\User_LDAP\Tests; +use OCA\User_LDAP\ILDAPUserPlugin; use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\User_Proxy; +use OCA\User_LDAP\UserPluginManager; use OCP\IConfig; use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; @@ -39,6 +44,8 @@ class User_ProxyTest extends TestCase { private $userSession; /** @var User_Proxy|\PHPUnit_Framework_MockObject_MockObject */ private $proxy; + /** @var UserPluginManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userPluginManager; public function setUp() { parent::setUp(); @@ -47,6 +54,7 @@ class User_ProxyTest extends TestCase { $this->config = $this->createMock(IConfig::class); $this->notificationManager = $this->createMock(INotificationManager::class); $this->userSession = $this->createMock(IUserSession::class); + $this->userPluginManager = $this->createMock(UserPluginManager::class); $this->proxy = $this->getMockBuilder(User_Proxy::class) ->setConstructorArgs([ [], @@ -54,6 +62,7 @@ class User_ProxyTest extends TestCase { $this->config, $this->notificationManager, $this->userSession, + $this->userPluginManager ]) ->setMethods(['handleRequest']) ->getMock(); @@ -68,4 +77,23 @@ class User_ProxyTest extends TestCase { $this->assertTrue($this->proxy->setPassword('MyUid', 'MyPassword')); } + + public function testSetDisplayName() { + $this->proxy + ->expects($this->once()) + ->method('handleRequest') + ->with('MyUid', 'setDisplayName', ['MyUid', 'MyPassword']) + ->willReturn(true); + + $this->assertTrue($this->proxy->setDisplayName('MyUid', 'MyPassword')); } + + public function testCreateUser() { + $this->proxy + ->expects($this->once()) + ->method('handleRequest') + ->with('MyUid', 'createUser', ['MyUid', 'MyPassword']) + ->willReturn(true); + + $this->assertTrue($this->proxy->createUser('MyUid', 'MyPassword')); + } } |