diff options
Diffstat (limited to 'apps/settings/tests/Controller/AuthSettingsControllerTest.php')
-rw-r--r-- | apps/settings/tests/Controller/AuthSettingsControllerTest.php | 142 |
1 files changed, 50 insertions, 92 deletions
diff --git a/apps/settings/tests/Controller/AuthSettingsControllerTest.php b/apps/settings/tests/Controller/AuthSettingsControllerTest.php index b642b1c06c5..d195dbf83d3 100644 --- a/apps/settings/tests/Controller/AuthSettingsControllerTest.php +++ b/apps/settings/tests/Controller/AuthSettingsControllerTest.php @@ -1,75 +1,43 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Fabrizio Steiner <fabrizio.steiner@gmail.com> - * @author Greta Doci <gretadoci@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Sergej Nikolaev <kinolaev@gmail.com> - * - * @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/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace Test\Settings\Controller; use OC\AppFramework\Http; use OC\Authentication\Exceptions\ExpiredTokenException; use OC\Authentication\Exceptions\InvalidTokenException; -use OC\Authentication\Token\DefaultToken; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OC\Authentication\Token\IWipeableToken; +use OC\Authentication\Token\PublicKeyToken; use OC\Authentication\Token\RemoteWipe; use OCA\Settings\Controller\AuthSettingsController; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\AppFramework\Http\JSONResponse; -use OCP\ILogger; use OCP\IRequest; use OCP\ISession; use OCP\IUserSession; use OCP\Security\ISecureRandom; use OCP\Session\Exceptions\SessionNotAvailableException; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class AuthSettingsControllerTest extends TestCase { - - /** @var AuthSettingsController */ - private $controller; - /** @var IRequest|MockObject */ - private $request; - /** @var IProvider|MockObject */ - private $tokenProvider; - /** @var ISession|MockObject */ - private $session; - /**@var IUserSession|MockObject */ - private $userSession; - /** @var ISecureRandom|MockObject */ - private $secureRandom; - /** @var IManager|MockObject */ - private $activityManager; - /** @var RemoteWipe|MockObject */ - private $remoteWipe; - private $uid = 'jane'; + private IRequest&MockObject $request; + private IProvider&MockObject $tokenProvider; + private ISession&MockObject $session; + private IUserSession&MockObject $userSession; + private ISecureRandom&MockObject $secureRandom; + private IManager&MockObject $activityManager; + private RemoteWipe&MockObject $remoteWipe; + private string $uid = 'jane'; + private AuthSettingsController $controller; protected function setUp(): void { parent::setUp(); @@ -81,8 +49,8 @@ class AuthSettingsControllerTest extends TestCase { $this->secureRandom = $this->createMock(ISecureRandom::class); $this->activityManager = $this->createMock(IManager::class); $this->remoteWipe = $this->createMock(RemoteWipe::class); - /** @var ILogger|MockObject $logger */ - $logger = $this->createMock(ILogger::class); + /** @var LoggerInterface&MockObject $logger */ + $logger = $this->createMock(LoggerInterface::class); $this->controller = new AuthSettingsController( 'core', @@ -98,7 +66,7 @@ class AuthSettingsControllerTest extends TestCase { ); } - public function testCreate() { + public function testCreate(): void { $name = 'Nexus 4'; $sessionToken = $this->createMock(IToken::class); $deviceToken = $this->createMock(IToken::class); @@ -147,12 +115,12 @@ class AuthSettingsControllerTest extends TestCase { $this->assertEquals($expected, $response->getData()); } - public function testCreateSessionNotAvailable() { + public function testCreateSessionNotAvailable(): void { $name = 'personal phone'; $this->session->expects($this->once()) ->method('getId') - ->will($this->throwException(new SessionNotAvailableException())); + ->willThrowException(new SessionNotAvailableException()); $expected = new JSONResponse(); $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); @@ -160,7 +128,7 @@ class AuthSettingsControllerTest extends TestCase { $this->assertEquals($expected, $this->controller->create($name)); } - public function testCreateInvalidToken() { + public function testCreateInvalidToken(): void { $name = 'Company IPhone'; $this->session->expects($this->once()) @@ -169,7 +137,7 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sessionid') - ->will($this->throwException(new InvalidTokenException())); + ->willThrowException(new InvalidTokenException()); $expected = new JSONResponse(); $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); @@ -177,9 +145,9 @@ class AuthSettingsControllerTest extends TestCase { $this->assertEquals($expected, $this->controller->create($name)); } - public function testDestroy() { + public function testDestroy(): void { $tokenId = 124; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); @@ -199,9 +167,9 @@ class AuthSettingsControllerTest extends TestCase { $this->assertEquals([], $this->controller->destroy($tokenId)); } - public function testDestroyExpired() { + public function testDestroyExpired(): void { $tokenId = 124; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $token->expects($this->exactly(2)) ->method('getId') @@ -223,9 +191,9 @@ class AuthSettingsControllerTest extends TestCase { $this->assertSame([], $this->controller->destroy($tokenId)); } - public function testDestroyWrongUser() { + public function testDestroyWrongUser(): void { $tokenId = 124; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); @@ -238,22 +206,17 @@ class AuthSettingsControllerTest extends TestCase { $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } - public function dataRenameToken(): array { + public static function dataRenameToken(): array { return [ 'App password => Other token name' => ['App password', 'Other token name'], 'Other token name => App password' => ['Other token name', 'App password'], ]; } - /** - * @dataProvider dataRenameToken - * - * @param string $name - * @param string $newName - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataRenameToken')] public function testUpdateRename(string $name, string $newName): void { $tokenId = 42; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); @@ -268,7 +231,7 @@ class AuthSettingsControllerTest extends TestCase { $token->expects($this->once()) ->method('getScopeAsArray') - ->willReturn(['filesystem' => true]); + ->willReturn([IToken::SCOPE_FILESYSTEM => true]); $token->expects($this->once()) ->method('setName') @@ -278,25 +241,20 @@ class AuthSettingsControllerTest extends TestCase { ->method('updateToken') ->with($this->equalTo($token)); - $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName)); + $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], $newName)); } - public function dataUpdateFilesystemScope(): array { + public static function dataUpdateFilesystemScope(): array { return [ 'Grant filesystem access' => [false, true], 'Revoke filesystem access' => [true, false], ]; } - /** - * @dataProvider dataUpdateFilesystemScope - * - * @param bool $filesystem - * @param bool $newFilesystem - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataUpdateFilesystemScope')] public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem): void { $tokenId = 42; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); $this->mockActivityManager(); @@ -311,22 +269,22 @@ class AuthSettingsControllerTest extends TestCase { $token->expects($this->once()) ->method('getScopeAsArray') - ->willReturn(['filesystem' => $filesystem]); + ->willReturn([IToken::SCOPE_FILESYSTEM => $filesystem]); $token->expects($this->once()) ->method('setScope') - ->with($this->equalTo(['filesystem' => $newFilesystem])); + ->with($this->equalTo([IToken::SCOPE_FILESYSTEM => $newFilesystem])); $this->tokenProvider->expects($this->once()) ->method('updateToken') ->with($this->equalTo($token)); - $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password')); + $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => $newFilesystem], 'App password')); } public function testUpdateNoChange(): void { $tokenId = 42; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); @@ -340,7 +298,7 @@ class AuthSettingsControllerTest extends TestCase { $token->expects($this->once()) ->method('getScopeAsArray') - ->willReturn(['filesystem' => true]); + ->willReturn([IToken::SCOPE_FILESYSTEM => true]); $token->expects($this->never()) ->method('setName'); @@ -352,12 +310,12 @@ class AuthSettingsControllerTest extends TestCase { ->method('updateToken') ->with($this->equalTo($token)); - $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password')); + $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password')); } - public function testUpdateExpired() { + public function testUpdateExpired(): void { $tokenId = 42; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $token->expects($this->once()) ->method('getUID') @@ -372,12 +330,12 @@ class AuthSettingsControllerTest extends TestCase { ->method('updateToken') ->with($this->equalTo($token)); - $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password')); + $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password')); } - public function testUpdateTokenWrongUser() { + public function testUpdateTokenWrongUser(): void { $tokenId = 42; - $token = $this->createMock(DefaultToken::class); + $token = $this->createMock(PublicKeyToken::class); $this->mockGetTokenById($tokenId, $token); @@ -390,12 +348,12 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->never()) ->method('updateToken'); - $response = $this->controller->update($tokenId, ['filesystem' => true], 'App password'); + $response = $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } - public function testUpdateTokenNonExisting() { + public function testUpdateTokenNonExisting(): void { $this->tokenProvider->expects($this->once()) ->method('getTokenById') ->with($this->equalTo(42)) @@ -404,7 +362,7 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->never()) ->method('updateToken'); - $response = $this->controller->update(42, ['filesystem' => true], 'App password'); + $response = $this->controller->update(42, [IToken::SCOPE_FILESYSTEM => true], 'App password'); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } |