adjust tests

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2020-04-29 17:43:47 +02:00
parent a7aabdd71f
commit c06679fa74
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
4 changed files with 272 additions and 356 deletions

View File

@ -25,6 +25,7 @@ use Test\TestCase;
class Test extends TestCase {
public function testFile() {
$dir =__DIR__;
$routes = require_once __DIR__ . '/../../../appinfo/routes.php';
$expected = [

View File

@ -25,6 +25,7 @@ use OCA\User_SAML\Controller\SAMLController;
use OCA\User_SAML\Exceptions\NoUserFoundException;
use OCA\User_SAML\SAMLSettings;
use OCA\User_SAML\UserBackend;
use OCA\User_SAML\UserData;
use OCA\User_SAML\UserResolver;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
@ -36,6 +37,7 @@ use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
@ -43,6 +45,8 @@ use Test\TestCase;
class SAMLControllerTest extends TestCase {
/** @var UserResolver|\PHPUnit\Framework\MockObject\MockObject */
protected $userResolver;
/** @var UserData|\PHPUnit\Framework\MockObject\MockObject */
private $userData;
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
private $request;
/** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
@ -74,14 +78,12 @@ class SAMLControllerTest extends TestCase {
$this->userSession = $this->createMock(IUserSession::class);
$this->samlSettings = $this->createMock(SAMLSettings::class);
$this->userBackend = $this->createMock(UserBackend::class);
$this->userBackend->expects($this->any())
->method('testEncodedObjectGUID')
->willReturnArgument(0);
$this->config = $this->createMock(IConfig::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->logger = $this->createMock(ILogger::class);
$this->l = $this->createMock(IL10N::class);
$this->userResolver = $this->createMock(UserResolver::class);
$this->userData = $this->createMock(UserData::class);
$this->crypto = $this->createMock(ICrypto::class);
$this->l->expects($this->any())->method('t')->willReturnCallback(
@ -107,6 +109,7 @@ class SAMLControllerTest extends TestCase {
$this->logger,
$this->l,
$this->userResolver,
$this->userData,
$this->crypto
);
@ -125,349 +128,202 @@ class SAMLControllerTest extends TestCase {
$this->samlController->login(1);
}
public function testLoginWithEnvVariableAndNotExistingUidInSettingsArray() {
$this->config
->expects($this->at(0))
public function samlUserDataProvider() {
$userNotExisting = 0;
$userExisting = 1;
$userLazyExisting = 2;
$apDisabled = 0;
$apEnabled = 1;
$apEnabledUnsuccessful = 2;
return [
[ # 0 - Not existing uid in settings array
[
'foo' => 'bar',
'bar' => 'foo',
],
'https://nextcloud.com/notProvisioned/',
$userNotExisting,
$apDisabled
],
[ # 1 - existing user
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => 'MyUid',
],
'https://nextcloud.com/absolute/',
$userExisting,
$apDisabled
],
[ # 2 - existing user and uid attribute in array
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => ['MyUid'],
],
'https://nextcloud.com/absolute/',
$userExisting,
$apDisabled
],
[ # 3 - Not existing user with provisioning
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => 'MyUid',
],
'https://nextcloud.com/absolute/',
$userNotExisting,
$apEnabled
],
[ # 4 - Not existing user with malfunctioning backend
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => 'MyUid',
],
'https://nextcloud.com/notProvisioned/',
$userNotExisting,
$apEnabledUnsuccessful
],
[ # 5 - Not existing user without provisioning
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => 'MyUid',
],
'https://nextcloud.com/notProvisioned/',
$userNotExisting,
$apDisabled
],
[ # 6 - Not yet mapped user without provisioning
[
'foo' => 'bar',
'bar' => 'foo',
'uid' => 'MyUid',
],
'https://nextcloud.com/absolute/',
$userLazyExisting,
$apDisabled
],
];
}
/**
* @dataProvider samlUserDataProvider
*/
public function testLoginWithEnvVariable(array $samlUserData, string $redirect, int $userState, int $autoProvision) {
$this->config->expects($this->any())
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
->willReturnCallback(function (string $app, string $key) {
if($app === 'user_saml') {
if($key === 'type') {
return 'environment-variable';
}
if($key === 'general-uid_mapping') {
return 'uid';
}
}
return null;
});
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with('user_saml.SAML.notProvisioned')
->willReturn('https://nextcloud.com/notProvisioned/');
->willReturn($samlUserData);
$expected = new RedirectResponse('https://nextcloud.com/notProvisioned/');
$this->userData
->expects($this->once())
->method('setAttributes')
->with($samlUserData);
$this->userData
->expects($this->any())
->method('getAttributes')
->willReturn($samlUserData);
$this->userData
->expects($this->any())
->method('hasUidMappingAttribute')
->willReturn(isset($samlUserData['uid']));
$this->userData
->expects(isset($samlUserData['uid']) ? $this->any() : $this->never())
->method('getOriginalUid')
->willReturn('MyUid');
$this->userData
->expects($this->any())
->method('getEffectiveUid')
->willReturn($userState > 0 ? 'MyUid' : '');
if(strpos($redirect, 'notProvisioned') !== false) {
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with('user_saml.SAML.notProvisioned')
->willReturn($redirect);
} else {
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->willReturn($redirect);
}
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn($userState === 1);
if(isset($samlUserData['uid']) && !($userState === 0 && $autoProvision === 0)) {
/** @var IUser|MockObject $user */
$user = $this->createMock(IUser::class);
$im = $this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid');
if($autoProvision < 2) {
$im->willReturn($user);
} else {
$im->willThrowException(new NoUserFoundException());
}
$user
->expects($this->exactly((int)($autoProvision < 2)))
->method('updateLastLoginTimestamp');
if($userState === 0) {
$this->userResolver
->expects($this->any())
->method('findExistingUserId')
->with('MyUid', true)
->willThrowException(new NoUserFoundException());
} else if($userState === 2) {
$this->userResolver
->expects($this->any())
->method('findExistingUserId')
->with('MyUid', true)
->willReturn('MyUid');
}
}
$this->userBackend
->expects($this->any())
->method('getCurrentUserId')
->willReturn(isset($samlUserData['uid']) ? 'MyUid' : '');
$this->userBackend
->expects($autoProvision > 0 ? $this->once() : $this->any())
->method('autoprovisionAllowed')
->willReturn($autoProvision > 0);
$this->userBackend
->expects($this->exactly(min(1, $autoProvision)))
->method('createUserIfNotExists')
->with('MyUid');
$expected = new RedirectResponse($redirect);
$result = $this->samlController->login(1);
$this->assertEquals($expected, $result);
}
public function testLoginWithEnvVariableAndExistingUser() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => 'MyUid',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn(true);
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn('https://nextcloud.com/absolute/');
$this->userBackend
->expects($this->any())
->method('getCurrentUserId')
->willReturn('MyUid');
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid')
->willReturn($user);
$user
->expects($this->once())
->method('updateLastLoginTimestamp');
$expected = new RedirectResponse('https://nextcloud.com/absolute/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testLoginWithEnvVariableAndExistingUserAndArray() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => ['MyUid'],
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn(true);
$this->userBackend
->expects($this->once())
->method('getCurrentUserId')
->willReturn('MyUid');
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid')
->willReturn($user);
$user
->expects($this->once())
->method('updateLastLoginTimestamp');
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn('https://nextcloud.com/absolute/');
$expected = new RedirectResponse('https://nextcloud.com/absolute/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testLoginWithEnvVariableAndNotExistingUserWithProvisioning() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => 'MyUid',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn(false);
$this->userResolver
->expects($this->any())
->method('findExistingUserId')
->with('MyUid', true)
->willThrowException(new NoUserFoundException());
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn('https://nextcloud.com/absolute/');
$this->userBackend
->expects($this->once())
->method('autoprovisionAllowed')
->willReturn(true);
$this->userBackend
->expects($this->once())
->method('createUserIfNotExists')
->with('MyUid');
$this->userBackend
->expects($this->once())
->method('getCurrentUserId')
->willReturn('MyUid');
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid')
->willReturn($user);
$user
->expects($this->once())
->method('updateLastLoginTimestamp');
$expected = new RedirectResponse('https://nextcloud.com/absolute/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testLoginWithEnvVariableAndNotExistingUserWithMalfunctioningBackend() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => 'MyUid',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn(false);
$this->userResolver
->expects($this->any())
->method('findExistingUserId')
->with('MyUid', true)
->willThrowException(new NoUserFoundException());
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with('user_saml.SAML.notProvisioned')
->willReturn('https://nextcloud.com/notprovisioned/');
$this->userBackend
->expects($this->once())
->method('autoprovisionAllowed')
->willReturn(true);
$this->userBackend
->expects($this->once())
->method('createUserIfNotExists')
->with('MyUid');
$this->userBackend
->expects($this->atLeastOnce())
->method('getCurrentUserId')
->willReturn('MyUid');
$this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid')
->willThrowException(new NoUserFoundException());
$expected = new RedirectResponse('https://nextcloud.com/notprovisioned/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testLoginWithEnvVariableAndNotExistingUserWithoutProvisioning() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => 'MyUid',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->any())
->method('userExists')
->with('MyUid')
->willReturn(false);
$this->userResolver
->expects($this->any())
->method('findExistingUserId')
->with('MyUid', $this->anything())
->willThrowException(new NoUserFoundException());
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with('user_saml.SAML.notProvisioned')
->willReturn('https://nextcloud.com/notprovisioned/');
$this->userBackend
->expects($this->once())
->method('autoprovisionAllowed')
->willReturn(false);
$expected = new RedirectResponse('https://nextcloud.com/notprovisioned/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testLoginWithEnvVariableAndNotYetMappedUserWithoutProvisioning() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('user_saml', 'type')
->willReturn('environment-variable');
$this->session
->expects($this->once())
->method('get')
->with('user_saml.samlUserData')
->willReturn([
'foo' => 'bar',
'uid' => 'MyUid',
'bar' => 'foo',
]);
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('user_saml', 'general-uid_mapping')
->willReturn('uid');
$this->userResolver
->expects($this->once())
->method('findExistingUserId')
->with('MyUid', true)
->willReturn('MyUid');
$this->userResolver
->expects($this->once())
->method('findExistingUser')
->with('MyUid')
->willReturn($this->createMock(IUser::class));
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteUrl')
->with('/')
->willReturn('https://nextcloud.com/absolute/');
$this->urlGenerator
->expects($this->never())
->method('linkToRouteAbsolute');
$this->userBackend
->expects($this->once())
->method('autoprovisionAllowed')
->willReturn(false);
$this->userBackend
->expects($this->once())
->method('getCurrentUserId')
->willReturn('MyUid');
$expected = new RedirectResponse('https://nextcloud.com/absolute/');
$this->assertEquals($expected, $this->samlController->login(1));
}
public function testNotProvisioned() {
$expected = new TemplateResponse('user_saml', 'notProvisioned', [], 'guest');
$this->assertEquals($expected, $this->samlController->notProvisioned());

View File

@ -23,6 +23,7 @@ namespace OCA\User_SAML\Tests\Settings;
use OCA\User_SAML\SAMLSettings;
use OCA\User_SAML\UserBackend;
use OCA\User_SAML\UserData;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
@ -35,6 +36,8 @@ use OCP\IUserManager;
use Test\TestCase;
class UserBackendTest extends TestCase {
/** @var UserData|\PHPUnit\Framework\MockObject\MockObject */
private $userData;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
@ -65,6 +68,7 @@ class UserBackendTest extends TestCase {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->SAMLSettings = $this->getMockBuilder(SAMLSettings::class)->disableOriginalConstructor()->getMock();
$this->logger = $this->createMock(ILogger::class);
$this->userData = $this->createMock(UserData::class);
}
public function getMockedBuilder(array $mockedFunctions = []) {
@ -78,7 +82,8 @@ class UserBackendTest extends TestCase {
$this->userManager,
$this->groupManager,
$this->SAMLSettings,
$this->logger
$this->logger,
$this->userData,
])
->setMethods($mockedFunctions)
->getMock();
@ -91,7 +96,8 @@ class UserBackendTest extends TestCase {
$this->userManager,
$this->groupManager,
$this->SAMLSettings,
$this->logger
$this->logger,
$this->userData
);
}
}
@ -281,27 +287,9 @@ class UserBackendTest extends TestCase {
$this->userBackend->updateAttributes('ExistingUser', ['email' => 'new@example.com', 'displayname' => 'New Displayname', 'quota' => '']);
}
public function objectGuidProvider() {
return [
['Joey No Conversion', 'Joey No Conversion'],
['no@convers.ion', 'no@convers.ion'],
['a0aa9ed8-6b48-1034-8ad7-8fb78330d80a', 'a0aa9ed8-6b48-1034-8ad7-8fb78330d80a'],
['EDE70D16-B9D5-4E9A-ABD7-614D17246E3F', 'EDE70D16-B9D5-4E9A-ABD7-614D17246E3F'],
['Tm8gY29udmVyc2lvbgo=', 'Tm8gY29udmVyc2lvbgo='],
['ASfjU2OYEd69ZgAVF4pePA==', '53E32701-9863-DE11-BD66-0015178A5E3C'],
['aaabbbcc@aa.bbbccdd.eee.ff', 'aaabbbcc@aa.bbbccdd.eee.ff'],
['aaabbbcccaa.bbbccdddeee', 'aaabbbcccaa.bbbccdddeee']
];
}
/**
* @dataProvider objectGuidProvider
*/
public function testTestEncodedObjectGUID(string $input, string $expectation) {
$this->getMockedBuilder(['getDisplayName', 'setDisplayName']);
$uid = $this->userBackend->testEncodedObjectGUID($input);
$this->assertSame($expectation, $uid);
}
}

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\User_SAML\Tests;
use OCA\User_SAML\SAMLSettings;
use OCA\User_SAML\UserData;
use OCA\User_SAML\UserResolver;
use OCP\IConfig;
use Test\TestCase;
class UserDataTest extends TestCase {
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
/** @var UserResolver|\PHPUnit\Framework\MockObject\MockObject */
protected $resolver;
/** @var SAMLSettings|\PHPUnit\Framework\MockObject\MockObject */
protected $samlSettings;
/** @var UserData */
protected $userData;
public function setUp(): void {
parent::setUp();
$this->resolver = $this->createMock(UserResolver::class);
$this->samlSettings = $this->createMock(SAMLSettings::class);
$this->config = $this->createMock(IConfig::class);
$this->userData = new UserData($this->resolver, $this->samlSettings, $this->config);
}
public function objectGuidProvider() {
return [
['Joey No Conversion', 'Joey No Conversion'],
['no@convers.ion', 'no@convers.ion'],
['a0aa9ed8-6b48-1034-8ad7-8fb78330d80a', 'a0aa9ed8-6b48-1034-8ad7-8fb78330d80a'],
['EDE70D16-B9D5-4E9A-ABD7-614D17246E3F', 'EDE70D16-B9D5-4E9A-ABD7-614D17246E3F'],
['Tm8gY29udmVyc2lvbgo=', 'Tm8gY29udmVyc2lvbgo='],
['ASfjU2OYEd69ZgAVF4pePA==', '53E32701-9863-DE11-BD66-0015178A5E3C'],
];
}
/**
* @dataProvider objectGuidProvider
*/
public function testTestEncodedObjectGUID(string $input, string $expectation) {
$uid = $this->invokePrivate($this->userData, 'testEncodedObjectGUID', [$input]);
$this->assertSame($expectation, $uid);
}
}