diff options
Diffstat (limited to 'apps/files_external/tests/Controller/AjaxControllerTest.php')
-rw-r--r-- | apps/files_external/tests/Controller/AjaxControllerTest.php | 91 |
1 files changed, 42 insertions, 49 deletions
diff --git a/apps/files_external/tests/Controller/AjaxControllerTest.php b/apps/files_external/tests/Controller/AjaxControllerTest.php index 304a1807931..b1ea7a2b1b1 100644 --- a/apps/files_external/tests/Controller/AjaxControllerTest.php +++ b/apps/files_external/tests/Controller/AjaxControllerTest.php @@ -1,26 +1,9 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_External\Tests\Controller; @@ -29,35 +12,29 @@ use OCA\Files_External\Lib\Auth\Password\GlobalAuth; use OCA\Files_External\Lib\Auth\PublicKey\RSA; use OCP\AppFramework\Http\JSONResponse; use OCP\IGroupManager; +use OCP\IL10N; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class AjaxControllerTest extends TestCase { - /** @var IRequest */ - private $request; - /** @var RSA */ - private $rsa; - /** @var GlobalAuth */ - private $globalAuth; - /** @var IUserSession */ - private $userSession; - /** @var IGroupManager */ - private $groupManager; - /** @var AjaxController */ - private $ajaxController; + private IRequest&MockObject $request; + private RSA&MockObject $rsa; + private GlobalAuth&MockObject $globalAuth; + private IUserSession&MockObject $userSession; + private IGroupManager&MockObject $groupManager; + private IL10N&MockObject $l10n; + private AjaxController $ajaxController; protected function setUp(): void { $this->request = $this->createMock(IRequest::class); - $this->rsa = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSA') - ->disableOriginalConstructor() - ->getMock(); - $this->globalAuth = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\Password\GlobalAuth') - ->disableOriginalConstructor() - ->getMock(); + $this->rsa = $this->createMock(RSA::class); + $this->globalAuth = $this->createMock(GlobalAuth::class); $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->l10n = $this->createMock(IL10N::class); $this->ajaxController = new AjaxController( 'files_external', @@ -65,13 +42,23 @@ class AjaxControllerTest extends TestCase { $this->rsa, $this->globalAuth, $this->userSession, - $this->groupManager + $this->groupManager, + $this->l10n, ); + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($string, $args) { + if (!is_array($args)) { + $args = [$args]; + } + return vsprintf($string, $args); + }); + parent::setUp(); } - public function testGetSshKeys() { + public function testGetSshKeys(): void { $this->rsa ->expects($this->once()) ->method('createKey') @@ -92,7 +79,7 @@ class AjaxControllerTest extends TestCase { $this->assertEquals($expected, $this->ajaxController->getSshKeys()); } - public function testSaveGlobalCredentialsAsAdminForAnotherUser() { + public function testSaveGlobalCredentialsAsAdminForAnotherUser(): void { $user = $this->createMock(IUser::class); $user ->expects($this->once()) @@ -106,10 +93,12 @@ class AjaxControllerTest extends TestCase { ->expects($this->never()) ->method('saveAuth'); - $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'); + $this->assertSame($response->getStatus(), 403); + $this->assertSame('Permission denied', $response->getData()['message']); } - public function testSaveGlobalCredentialsAsAdminForSelf() { + public function testSaveGlobalCredentialsAsAdminForSelf(): void { $user = $this->createMock(IUser::class); $user ->expects($this->once()) @@ -124,10 +113,11 @@ class AjaxControllerTest extends TestCase { ->method('saveAuth') ->with('MyAdminUid', 'test', 'password'); - $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 200); } - public function testSaveGlobalCredentialsAsNormalUserForSelf() { + public function testSaveGlobalCredentialsAsNormalUserForSelf(): void { $user = $this->createMock(IUser::class); $user ->method('getUID') @@ -139,10 +129,11 @@ class AjaxControllerTest extends TestCase { ->method('saveAuth') ->with('MyUserUid', 'test', 'password'); - $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 200); } - public function testSaveGlobalCredentialsAsNormalUserForAnotherUser() { + public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void { $user = $this->createMock(IUser::class); $user ->method('getUID') @@ -154,6 +145,8 @@ class AjaxControllerTest extends TestCase { ->expects($this->never()) ->method('saveAuth'); - $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 403); + $this->assertSame('Permission denied', $response->getData()['message']); } } |