Skip to content

Commit

Permalink
Merge pull request #18028 from nextcloud/enhancement/user-create-events
Browse files Browse the repository at this point in the history
Add typed create user events
  • Loading branch information
rullzer authored Dec 3, 2019
2 parents bcc32f6 + cc80339 commit 1d707cc
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 137 deletions.
139 changes: 72 additions & 67 deletions apps/user_ldap/tests/LDAPProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\User_LDAP\Connection;
use OCA\User_LDAP\IGroupLDAP;
use OCA\User_LDAP\IUserLDAP;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IServerContainer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -46,7 +47,7 @@ class LDAPProviderTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
}

private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) {
$server = $this->getMockBuilder('OC\Server')
->setMethods(['getUserManager', 'getBackends', 'getGroupManager'])
Expand All @@ -71,7 +72,11 @@ private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend)
private function getUserManagerMock(IUserLDAP $userBackend) {
$userManager = $this->getMockBuilder(Manager::class)
->setMethods(['getBackends'])
->setConstructorArgs([$this->createMock(IConfig::class), $this->createMock(EventDispatcherInterface::class)])
->setConstructorArgs([
$this->createMock(IConfig::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(IEventDispatcher::class)
])
->getMock();
$userManager->expects($this->any())
->method('getBackends')
Expand All @@ -92,18 +97,18 @@ private function getGroupManagerMock(IGroupLDAP $groupBackend) {

private function getDefaultGroupBackendMock() {
$groupBackend = $this->getMockBuilder('OCA\User_LDAP\Group_LDAP')
->disableOriginalConstructor()
->disableOriginalConstructor()
->getMock();

return $groupBackend;
}

private function getLDAPProvider(IServerContainer $serverContainer) {
$factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer);
return $factory->getLDAPProvider();
}


public function testGetUserDNUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand All @@ -113,13 +118,13 @@ public function testGetUserDNUserIDNotFound() {
->disableOriginalConstructor()
->getMock();
$userBackend->expects($this->any())->method('userExists')->willReturn(false);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->getUserDN('nonexisting_user');
}

public function testGetUserDN() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods(['userExists', 'getLDAPAccess', 'username2dn'])
Expand All @@ -134,15 +139,15 @@ public function testGetUserDN() {
$userBackend->expects($this->any())
->method($this->anything())
->willReturnSelf();

$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',
$this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org',
$ldapProvider->getUserDN('existing_user'));
}


public function testGetGroupDNGroupIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Group id not found in LDAP');
Expand Down Expand Up @@ -190,7 +195,7 @@ public function testGetGroupDN() {
$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() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
Expand All @@ -200,27 +205,27 @@ public function testGetUserName() {
$userBackend->expects($this->any())
->method('dn2UserName')
->willReturn('existing_user');

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals('existing_user',
$this->assertEquals('existing_user',
$ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
}

public function testDNasBaseParameter() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods([])
->disableOriginalConstructor()
->getMock();

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(
$helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
$helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
$ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
}

Expand All @@ -229,18 +234,18 @@ public function testSanitizeDN() {
->setMethods([])
->disableOriginalConstructor()
->getMock();

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(
$helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
$helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'),
$ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'));
}


public function testGetLDAPConnectionUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand All @@ -250,13 +255,13 @@ public function testGetLDAPConnectionUserIDNotFound() {
->disableOriginalConstructor()
->getMock();
$userBackend->expects($this->any())->method('userExists')->willReturn(false);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->getLDAPConnection('nonexisting_user');
}

public function testGetLDAPConnection() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods(['userExists', 'getNewLDAPConnection'])
Expand All @@ -268,14 +273,14 @@ public function testGetLDAPConnection() {
$userBackend->expects($this->any())
->method('getNewLDAPConnection')
->willReturn(true);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
}


public function testGetGroupLDAPConnectionGroupIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Group id not found in LDAP');
Expand Down Expand Up @@ -320,8 +325,8 @@ public function testGetGroupLDAPConnection() {
$ldapProvider = $this->getLDAPProvider($server);
$this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group'));
}


public function testGetLDAPBaseUsersUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand All @@ -331,13 +336,13 @@ public function testGetLDAPBaseUsersUserIDNotFound() {
->disableOriginalConstructor()
->getMock();
$userBackend->expects($this->any())->method('userExists')->willReturn(false);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->getLDAPBaseUsers('nonexisting_user');
}

public function testGetLDAPBaseUsers() {
$bases = [
'ou=users,ou=foobar,dc=example,dc=org',
Expand Down Expand Up @@ -379,12 +384,12 @@ public function testGetLDAPBaseUsers() {
->willReturn($access);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user'));
}


public function testGetLDAPBaseGroupsUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand All @@ -394,13 +399,13 @@ public function testGetLDAPBaseGroupsUserIDNotFound() {
->disableOriginalConstructor()
->getMock();
$userBackend->expects($this->any())->method('userExists')->willReturn(false);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->getLDAPBaseGroups('nonexisting_user');
}

public function testGetLDAPBaseGroups() {
$bases = [
'ou=groupd,ou=foobar,dc=example,dc=org',
Expand Down Expand Up @@ -435,12 +440,12 @@ public function testGetLDAPBaseGroups() {
->willReturn($access);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user'));
}


public function testClearCacheUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand All @@ -450,13 +455,13 @@ public function testClearCacheUserIDNotFound() {
->disableOriginalConstructor()
->getMock();
$userBackend->expects($this->any())->method('userExists')->willReturn(false);

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->clearCache('nonexisting_user');
}

public function testClearCache() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache'])
Expand All @@ -471,15 +476,15 @@ public function testClearCache() {
$userBackend->expects($this->any())
->method($this->anything())
->willReturnSelf();

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->clearCache('existing_user');
$this->addToAssertionCount(1);
}


public function testClearGroupCacheGroupIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Group id not found in LDAP');
Expand Down Expand Up @@ -523,7 +528,7 @@ public function testClearGroupCache() {
$ldapProvider->clearGroupCache('existing_group');
$this->addToAssertionCount(1);
}

public function testDnExists() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods(['dn2UserName'])
Expand All @@ -532,40 +537,40 @@ public function testDnExists() {
$userBackend->expects($this->any())
->method('dn2UserName')
->willReturn('existing_user');

$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() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods([])
->disableOriginalConstructor()
->getMock();

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->flagRecord('existing_user');
$this->addToAssertionCount(1);
}

public function testUnflagRecord() {
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
->setMethods([])
->disableOriginalConstructor()
->getMock();

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$ldapProvider = $this->getLDAPProvider($server);
$ldapProvider->unflagRecord('existing_user');
$this->addToAssertionCount(1);
}


public function testGetLDAPDisplayNameFieldUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand Down Expand Up @@ -603,7 +608,7 @@ public function testGetLDAPDisplayNameField() {
$this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user'));
}


public function testGetLDAPEmailFieldUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('User id not found in LDAP');
Expand Down Expand Up @@ -641,7 +646,7 @@ public function testGetLDAPEmailField() {
$this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user'));
}


public function testGetLDAPGroupMemberAssocUserIDNotFound() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Group id not found in LDAP');
Expand Down Expand Up @@ -687,6 +692,6 @@ public function testgetLDAPGroupMemberAssoc() {

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group'));
}
}

}
Loading

0 comments on commit 1d707cc

Please sign in to comment.