adjust tests

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-11-03 17:40:05 +01:00
parent 59c05d5447
commit fc6b3902af
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 413 additions and 403 deletions

View File

@ -353,8 +353,9 @@ class AccessTest extends TestCase {
$userMock->expects($this->never())
->method('processAttributes');
$this->userManager->expects($this->never())
->method('get');
$this->userManager->expects($this->any())
->method('get')
->willReturn($this->createMock(User::class));
$this->c->expects($this->any())
->method('getConfig')
@ -402,10 +403,6 @@ class AccessTest extends TestCase {
->will($this->returnValue($userMock));
$configMock = $this->createMock(IConfig::class);
$configMock->expects($this->once())
->method('getAppValue')
->with('core', 'backgroundjobs_mode', $this->anything())
->willReturn('ajax');
$this->c->expects($this->any())
->method('getConfig')

View File

@ -33,8 +33,11 @@
namespace OCA\User_LDAP\Tests;
use OCP\GroupInterface;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Connection;
use OCA\User_LDAP\Group_LDAP as GroupLDAP;
use OCA\User_LDAP\ILDAPWrapper;
use OCA\User_LDAP\User\Manager;
/**
* Class GroupLDAPTest
@ -44,6 +47,9 @@ use OCA\User_LDAP\ILDAPWrapper;
* @package OCA\User_LDAP\Tests
*/
class Group_LDAPTest extends \Test\TestCase {
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Access
*/
private function getAccessMock() {
static $conMethods;
static $accMethods;
@ -57,14 +63,8 @@ class Group_LDAPTest extends \Test\TestCase {
->setMethods($conMethods)
->setConstructorArgs([$lw, null, null])
->getMock();
$um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager')
->disableOriginalConstructor()
->getMock();
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$access = $this->getMockBuilder('\OCA\User_LDAP\Access')
->setMethods($accMethods)
->setConstructorArgs([$connector, $lw, $um, $helper])
->getMock();
$access = $this->createMock(Access::class);
$access->expects($this->any())
->method('getConnection')
@ -77,7 +77,12 @@ class Group_LDAPTest extends \Test\TestCase {
return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock();
}
/**
* @param Access|\PHPUnit_Framework_MockObject_MockObject $access
*/
private function enableGroups($access) {
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any())
->method('__get')
->will($this->returnCallback(function($name) {
@ -498,7 +503,6 @@ class Group_LDAPTest extends \Test\TestCase {
$access->connection->expects($this->any())
->method('getFromCache')
->will($this->returnValue(null));
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) {
@ -509,14 +513,13 @@ class Group_LDAPTest extends \Test\TestCase {
}
return array();
}));
$access->expects($this->any())
->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->exactly(2))
->method('nextcloudUserNames')
->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']);
$access->userManager = $this->createMock(Manager::class);
$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar');
@ -537,7 +540,6 @@ class Group_LDAPTest extends \Test\TestCase {
$access->connection->expects($this->any())
->method('getFromCache')
->will($this->returnValue(null));
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) {
@ -546,14 +548,13 @@ class Group_LDAPTest extends \Test\TestCase {
}
return array();
}));
$access->expects($this->any())
->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->once())
->method('nextcloudUserNames')
->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
$access->userManager = $this->createMock(Manager::class);
$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar');
@ -635,6 +636,7 @@ class Group_LDAPTest extends \Test\TestCase {
$access = $this->getAccessMock();
$pluginManager = $this->getPluginManagerMock();
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any())
->method('__get')
->will($this->returnCallback(function($name) {
@ -671,6 +673,7 @@ class Group_LDAPTest extends \Test\TestCase {
$access = $this->getAccessMock();
$pluginManager = $this->getPluginManagerMock();
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any())
->method('__get')
->will($this->returnCallback(function($name) {

File diff suppressed because it is too large Load Diff

View File

@ -81,12 +81,6 @@ class User_LDAPTest extends TestCase {
* @return \PHPUnit_Framework_MockObject_MockObject|Access
*/
private function getAccessMock() {
$lw = $this->createMock(ILDAPWrapper::class);
$connector = $this->getMockBuilder(Connection::class)
->setMethodsExcept(['getConnection'])
->setConstructorArgs([$lw, null, null])
->getMock();
$this->configMock = $this->createMock(IConfig::class);
$this->offlineUser = $this->createMock(OfflineUser::class);
@ -106,18 +100,16 @@ class User_LDAPTest extends TestCase {
])
->getMock();
$um->expects($this->any())
->method('getDeletedUser')
->will($this->returnValue($this->offlineUser));
/** @var Connection|\PHPUnit_Framework_MockObject_MockObject $connection */
$connection = $this->createMock(Connection::class);
$helper = new Helper(\OC::$server->getConfig());
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject $userManager */
$userManager = $this->createMock(Manager::class);
$access = $this->getMockBuilder(Access::class)
->setMethodsExcept(['getConnection'])
->setConstructorArgs([$connector, $lw, $um, $helper])
->getMock();
$um->setLdapAccess($access);
/** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
$access = $this->createMock(Access::class);
$access->connection = $connection;
$access->userManager = $userManager;
return $access;
}
@ -211,11 +203,17 @@ class User_LDAPTest extends TestCase {
}
public function testCheckPasswordUidReturn() {
$access = $this->getAccessMock();
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
->willReturn($user);
\OC_User::useBackend($backend);
$result = $backend->checkPassword('roland', 'dt19');
@ -246,11 +244,12 @@ class User_LDAPTest extends TestCase {
public function testCheckPasswordNoDisplayName() {
$access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access, true);
$access->expects($this->once())
->method('username2dn')
->will($this->returnValue(false));
$this->prepareAccessForCheckPassword($access);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn(null);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
\OC_User::useBackend($backend);
@ -260,9 +259,17 @@ class User_LDAPTest extends TestCase {
}
public function testCheckPasswordPublicAPI() {
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
->method('get')
->willReturn($user);
\OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'dt19');
@ -308,6 +315,9 @@ class User_LDAPTest extends TestCase {
$access->expects($this->once())
->method('getUserMapper')
->will($this->returnValue($mapping));
$access->connection->expects($this->any())
->method('getConnectionResource')
->willReturn('this is an ldap link');
$this->configMock->expects($this->any())
->method('getUserValue')
@ -320,6 +330,9 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->once())
->method('getOCName')
->willReturn($uid);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->offlineUser);
$backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
@ -516,6 +529,11 @@ class User_LDAPTest extends TestCase {
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$this->prepareMockForUserExists($access);
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn) {
@ -524,6 +542,12 @@ class User_LDAPTest extends TestCase {
}
return false;
}));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
//test for existing user
$result = $backend->userExists('gunslinger');
@ -547,8 +571,13 @@ class User_LDAPTest extends TestCase {
return false;
}));
$access->userManager = $this->createMock(Manager::class);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
//test for deleted user
$result = $backend->userExists('formerUser');
$backend->userExists('formerUser');
}
public function testUserExistsForNeverExisting() {
@ -576,6 +605,11 @@ class User_LDAPTest extends TestCase {
$this->prepareMockForUserExists($access);
\OC_User::useBackend($backend);
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn) {
@ -584,6 +618,12 @@ class User_LDAPTest extends TestCase {
}
return false;
}));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
//test for existing user
$result = \OCP\User::userExists('gunslinger');
@ -607,9 +647,13 @@ class User_LDAPTest extends TestCase {
}
return false;
}));
$access->userManager = $this->createMock(Manager::class);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
//test for deleted user
$result = \OCP\User::userExists('formerUser');
\OCP\User::userExists('formerUser');
}
public function testUserExistsPublicAPIForNeverExisting() {
@ -672,7 +716,22 @@ class User_LDAPTest extends TestCase {
}
}));
//absolut path
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user->expects($this->any())
->method('getHomePath')
->willReturn('/tmp/rolandshome/');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
//absolute path
$result = $backend->getHome('gunslinger');
$this->assertEquals('/tmp/rolandshome/', $result);
}
@ -687,10 +746,6 @@ class User_LDAPTest extends TestCase {
$dataDir = \OC::$server->getConfig()->getSystemValue(
'datadirectory', \OC::$SERVERROOT.'/data');
$this->configMock->expects($this->once())
->method('getSystemValue')
->will($this->returnValue($dataDir));
$access->connection->expects($this->any())
->method('__get')
->will($this->returnCallback(function($name) {
@ -715,6 +770,21 @@ class User_LDAPTest extends TestCase {
}
}));
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('ladyofshadows');
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfLadyOfShadows,dc=test');
$user->expects($this->any())
->method('getHomePath')
->willReturn($dataDir.'/susannah/');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$result = $backend->getHome('ladyofshadows');
$this->assertEquals($dataDir.'/susannah/', $result);
}
@ -745,6 +815,18 @@ class User_LDAPTest extends TestCase {
}
}));
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('newyorker');
$user->expects($this->any())
->method('getHomePath')
->willThrowException(new \Exception());
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
//no path at all triggers OC default behaviour
$result = $backend->getHome('newyorker');
$this->assertFalse($result);
@ -786,6 +868,10 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->never())
->method('getHomePath');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->offlineUser);
$backend->getHome($uid);
}
@ -852,14 +938,6 @@ class User_LDAPTest extends TestCase {
}
}));
$userMapper = $this->getMockBuilder('\OCA\User_LDAP\Mapping\UserMapping')
->disableOriginalConstructor()
->getMock();
$access->expects($this->any())
->method('getUserMapper')
->will($this->returnValue($userMapper));
$access->method('fetchUsersByLoginName')
->willReturn([]);
}
@ -876,6 +954,42 @@ class User_LDAPTest extends TestCase {
return true;
}));
$user1 = $this->createMock(User::class);
$user1->expects($this->once())
->method('composeAndStoreDisplayName')
->willReturn('Roland Deschain');
$user1->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user2 = $this->createMock(User::class);
$user2->expects($this->never())
->method('composeAndStoreDisplayName');
$user2->expects($this->any())
->method('getDN')
->willReturn('another DN');
$mapper = $this->createMock(UserMapping::class);
$mapper->expects($this->any())
->method('getUUIDByDN')
->willReturnCallback(function($dn) { return $dn; });
$access->userManager->expects($this->any())
->method('get')
->willReturnCallback(function($uid) use ($user1, $user2) {
if($uid === 'gunslinger') {
return $user1;
} else if($uid === 'newyorker') {
return $user2;
}
});
$access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
$access->expects($this->any())
->method('getUserDnByUuid')
->willReturnCallback(function($uuid) { return $uuid . '1'; });
//with displayName
$result = $backend->getDisplayName('gunslinger');
$this->assertEquals('Roland Deschain', $result);
@ -919,6 +1033,42 @@ class User_LDAPTest extends TestCase {
\OC_User::useBackend($backend);
$user1 = $this->createMock(User::class);
$user1->expects($this->once())
->method('composeAndStoreDisplayName')
->willReturn('Roland Deschain');
$user1->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user2 = $this->createMock(User::class);
$user2->expects($this->never())
->method('composeAndStoreDisplayName');
$user2->expects($this->any())
->method('getDN')
->willReturn('another DN');
$mapper = $this->createMock(UserMapping::class);
$mapper->expects($this->any())
->method('getUUIDByDN')
->willReturnCallback(function($dn) { return $dn; });
$access->userManager->expects($this->any())
->method('get')
->willReturnCallback(function($uid) use ($user1, $user2) {
if($uid === 'gunslinger') {
return $user1;
} else if($uid === 'newyorker') {
return $user2;
}
});
$access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
$access->expects($this->any())
->method('getUserDnByUuid')
->willReturnCallback(function($uuid) { return $uuid . '1'; });
//with displayName
$result = \OCP\User::getDisplayName('gunslinger');
$this->assertEquals('Roland Deschain', $result);
@ -1028,11 +1178,11 @@ class User_LDAPTest extends TestCase {
->method('fetchUsersByLoginName')
->with($this->equalTo($loginName))
->willReturn([['dn' => [$dn]]]);
$access->expects($this->once())
$access->expects($this->any())
->method('stringResemblesDN')
->with($this->equalTo($dn))
->willReturn(true);
$access->expects($this->once())
$access->expects($this->any())
->method('dn2username')
->with($this->equalTo($dn))
->willReturn($username);
@ -1046,6 +1196,15 @@ class User_LDAPTest extends TestCase {
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$user->expects($this->any())
->method('getUsername')
->willReturn('alice');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->with($dn)
->willReturn($user);
$name = $backend->loginName2UserName($loginName);
$this->assertSame($username, $name);
@ -1084,7 +1243,6 @@ class User_LDAPTest extends TestCase {
public function testLoginName2UserNameOfflineUser() {
$loginName = 'Alice';
$username = 'alice';
$dn = 'uid=alice,dc=what,dc=ever';
$offlineUser = $this->getMockBuilder(OfflineUser::class)
@ -1096,13 +1254,6 @@ class User_LDAPTest extends TestCase {
->method('fetchUsersByLoginName')
->with($this->equalTo($loginName))
->willReturn([['dn' => [$dn]]]);
$access->expects($this->once())
->method('stringResemblesDN')
->with($this->equalTo($dn))
->willReturn(true);
$access->expects($this->once())
->method('dn2username')
->willReturn(false); // this is fake, but allows us to force-enter the OfflineUser path
$access->connection->expects($this->exactly(2))
->method('getFromCache')
@ -1112,14 +1263,10 @@ class User_LDAPTest extends TestCase {
->method('writeToCache')
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false));
$access->userManager->expects($this->once())
->method('getDeletedUser')
->will($this->returnValue($offlineUser));
//$config = $this->createMock(IConfig::class);
$this->configMock->expects($this->once())
->method('getUserValue')
->willReturn(1);
$access->userManager->expects($this->any())
->method('get')
->with($dn)
->willReturn($offlineUser);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$name = $backend->loginName2UserName($loginName);
@ -1192,6 +1339,10 @@ class User_LDAPTest extends TestCase {
}
return true;
}));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
}
/**
@ -1213,6 +1364,10 @@ class User_LDAPTest extends TestCase {
$this->prepareAccessForSetPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$access->userManager->expects($this->any())
->method('get')
->willReturn($this->createMock(User::class));
\OC_User::useBackend($backend);
$this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
@ -1220,6 +1375,9 @@ class User_LDAPTest extends TestCase {
public function testSetPasswordValidDisabled() {
$access = $this->getAccessMock();
$access->userManager->expects($this->any())
->method('get')
->willReturn($this->createMock(User::class));
$this->prepareAccessForSetPassword($access, false);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());

View File

@ -28,8 +28,11 @@
namespace OCA\User_LDAP\Tests;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ILDAPWrapper;
use \OCA\User_LDAP\Wizard;
use Test\TestCase;
/**
* Class Test_Wizard
@ -38,7 +41,7 @@ use \OCA\User_LDAP\Wizard;
*
* @package OCA\User_LDAP\Tests
*/
class WizardTest extends \Test\TestCase {
class WizardTest extends TestCase {
protected function setUp() {
parent::setUp();
//we need to make sure the consts are defined, otherwise tests will fail
@ -62,30 +65,22 @@ class WizardTest extends \Test\TestCase {
$connMethods = get_class_methods('\OCA\User_LDAP\Connection');
$accMethods = get_class_methods('\OCA\User_LDAP\Access');
}
/** @var ILDAPWrapper|\PHPUnit_Framework_MockObject_MockObject $lw */
$lw = $this->createMock(ILDAPWrapper::class);
$conf = $this->getMockBuilder('\OCA\User_LDAP\Configuration')
/** @var Configuration|\PHPUnit_Framework_MockObject_MockObject $conf */
$conf = $this->getMockBuilder(Configuration::class)
->setMethods($confMethods)
->setConstructorArgs([$lw, null, null])
->getMock();
$connector = $this->getMockBuilder('\OCA\User_LDAP\Connection')
->setMethods($connMethods)
->setConstructorArgs([$lw, null, null])
->getMock();
$um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager')
->disableOriginalConstructor()
->getMock();
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$access = $this->getMockBuilder('\OCA\User_LDAP\Access')
->setMethods($accMethods)
->setConstructorArgs([$connector, $lw, $um, $helper])
->getMock();
/** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
$access = $this->createMock(Access::class);
return array(new Wizard($conf, $lw, $access), $conf, $lw, $access);
}
private function prepareLdapWrapperForConnections(&$ldap) {
private function prepareLdapWrapperForConnections(\PHPUnit_Framework_MockObject_MockObject &$ldap) {
$ldap->expects($this->once())
->method('connect')
//dummy value, usually invalid