From 34426a481faa5ff76317295305d7c6fad2c8534f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 28 Jan 2020 20:48:18 +0100 Subject: Move can create token logic It makes sense to have this in the actual authtoken settings. As well. This is where it is used. Signed-off-by: Roeland Jago Douma --- apps/settings/lib/Settings/Personal/Security.php | 13 +- .../lib/Settings/Personal/Security/Authtokens.php | 12 ++ .../tests/Personal/Security/AuthtokensTest.php | 126 ------------------- .../Settings/Personal/Security/AuthtokensTest.php | 136 +++++++++++++++++++++ .../tests/Settings/Personal/SecurityTest.php | 6 - 5 files changed, 149 insertions(+), 144 deletions(-) delete mode 100644 apps/settings/tests/Personal/Security/AuthtokensTest.php create mode 100644 apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php (limited to 'apps/settings') diff --git a/apps/settings/lib/Settings/Personal/Security.php b/apps/settings/lib/Settings/Personal/Security.php index 5734eaa5ca3..192b082d6b1 100644 --- a/apps/settings/lib/Settings/Personal/Security.php +++ b/apps/settings/lib/Settings/Personal/Security.php @@ -50,9 +50,6 @@ use OCP\Settings\ISettings; class Security implements ISettings { - /** @var IInitialStateService */ - private $initialStateService; - /** @var IUserManager */ private $userManager; @@ -68,13 +65,11 @@ class Security implements ISettings { /** @var IConfig */ private $config; - public function __construct(IInitialStateService $initialStateService, - IUserManager $userManager, + public function __construct(IUserManager $userManager, ProviderLoader $providerLoader, IUserSession $userSession, IConfig $config, ?string $UserId) { - $this->initialStateService = $initialStateService; $this->userManager = $userManager; $this->providerLoader = $providerLoader; $this->userSession = $userSession; @@ -89,12 +84,6 @@ class Security implements ISettings { $passwordChangeSupported = $user->canChangePassword(); } - $this->initialStateService->provideInitialState( - 'settings', - 'can_create_app_token', - $this->userSession->getImpersonatingUserID() === null - ); - return new TemplateResponse('settings', 'settings/personal/security', [ 'passwordChangeSupported' => $passwordChangeSupported, 'twoFactorProviderData' => $this->getTwoFactorProviderData(), diff --git a/apps/settings/lib/Settings/Personal/Security/Authtokens.php b/apps/settings/lib/Settings/Personal/Security/Authtokens.php index 04825069920..1944670b2ea 100644 --- a/apps/settings/lib/Settings/Personal/Security/Authtokens.php +++ b/apps/settings/lib/Settings/Personal/Security/Authtokens.php @@ -27,6 +27,7 @@ declare(strict_types=1); namespace OCA\Settings\Personal\Security; +use OCP\IUserSession; use function array_map; use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Token\INamedToken; @@ -52,14 +53,19 @@ class Authtokens implements ISettings { /** @var string|null */ private $uid; + /** @var IUserSession */ + private $userSession; + public function __construct(IAuthTokenProvider $tokenProvider, ISession $session, + IUserSession $userSession, IInitialStateService $initialStateService, ?string $UserId) { $this->tokenProvider = $tokenProvider; $this->session = $session; $this->initialStateService = $initialStateService; $this->uid = $UserId; + $this->userSession = $userSession; } public function getForm(): TemplateResponse { @@ -69,6 +75,12 @@ class Authtokens implements ISettings { $this->getAppTokens() ); + $this->initialStateService->provideInitialState( + 'settings', + 'can_create_app_token', + $this->userSession->getImpersonatingUserID() === null + ); + return new TemplateResponse('settings', 'settings/personal/security/authtokens'); } diff --git a/apps/settings/tests/Personal/Security/AuthtokensTest.php b/apps/settings/tests/Personal/Security/AuthtokensTest.php deleted file mode 100644 index e8eadd38aba..00000000000 --- a/apps/settings/tests/Personal/Security/AuthtokensTest.php +++ /dev/null @@ -1,126 +0,0 @@ - - * - * @author Christoph Wurst - * @author Roeland Jago Douma - * - * @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 . - * - */ - -namespace Test\Settings\Personal\Security; - -use OC\Authentication\Token\DefaultToken; -use OC\Authentication\Token\IProvider as IAuthTokenProvider; -use OCA\Settings\Personal\Security; -use OCA\Settings\Personal\Security\Authtokens; -use OCP\AppFramework\Http\TemplateResponse; -use OCP\IInitialStateService; -use OCP\ISession; -use PHPUnit\Framework\MockObject\MockObject; -use Test\TestCase; - -class AuthtokensTest extends TestCase { - - /** @var IAuthTokenProvider|MockObject */ - private $authTokenProvider; - - /** @var ISession|MockObject */ - private $session; - - /** @var IInitialStateService|MockObject */ - private $initialStateService; - - /** @var string */ - private $uid; - - /** @var Security\Authtokens */ - private $section; - - protected function setUp(): void { - parent::setUp(); - - $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class); - $this->session = $this->createMock(ISession::class); - $this->initialStateService = $this->createMock(IInitialStateService::class); - $this->uid = 'test123'; - - $this->section = new Authtokens( - $this->authTokenProvider, - $this->session, - $this->initialStateService, - $this->uid - ); - } - - public function testGetForm() { - $token1 = new DefaultToken(); - $token1->setId(100); - $token2 = new DefaultToken(); - $token2->setId(200); - $tokens = [ - $token1, - $token2, - ]; - $sessionToken = new DefaultToken(); - $sessionToken->setId(100); - - $this->authTokenProvider->expects($this->once()) - ->method('getTokenByUser') - ->with($this->uid) - ->willReturn($tokens); - $this->session->expects($this->once()) - ->method('getId') - ->willReturn('session123'); - $this->authTokenProvider->expects($this->once()) - ->method('getToken') - ->with('session123') - ->willReturn($sessionToken); - $this->initialStateService->expects($this->once()) - ->method('provideInitialState') - ->with('settings', 'app_tokens', [ - [ - 'id' => 100, - 'name' => null, - 'lastActivity' => 0, - 'type' => 0, - 'canDelete' => false, - 'current' => true, - 'scope' => ['filesystem' => true], - 'canRename' => false, - ], - [ - 'id' => 200, - 'name' => null, - 'lastActivity' => 0, - 'type' => 0, - 'canDelete' => true, - 'scope' => ['filesystem' => true], - 'canRename' => true, - ], - ]); - - $form = $this->section->getForm(); - - $expected = new TemplateResponse('settings', 'settings/personal/security/authtokens'); - $this->assertEquals($expected, $form); - } - -} diff --git a/apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php b/apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php new file mode 100644 index 00000000000..6837fc101c4 --- /dev/null +++ b/apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php @@ -0,0 +1,136 @@ + + * + * @author Christoph Wurst + * @author Roeland Jago Douma + * + * @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 . + * + */ + +namespace OCA\Settings\Tests\Settings\Personal\Security; + +use OC\Authentication\Token\DefaultToken; +use OC\Authentication\Token\IProvider as IAuthTokenProvider; +use OCA\Settings\Personal\Security; +use OCA\Settings\Personal\Security\Authtokens; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IInitialStateService; +use OCP\ISession; +use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class AuthtokensTest extends TestCase { + + /** @var IAuthTokenProvider|MockObject */ + private $authTokenProvider; + + /** @var ISession|MockObject */ + private $session; + + /** @var IUserSession|MockObject */ + private $userSession; + + /** @var IInitialStateService|MockObject */ + private $initialStateService; + + /** @var string */ + private $uid; + + /** @var Security\Authtokens */ + private $section; + + protected function setUp(): void { + parent::setUp(); + + $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class); + $this->session = $this->createMock(ISession::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->initialStateService = $this->createMock(IInitialStateService::class); + $this->uid = 'test123'; + + $this->section = new Authtokens( + $this->authTokenProvider, + $this->session, + $this->userSession, + $this->initialStateService, + $this->uid + ); + } + + public function testGetForm() { + $token1 = new DefaultToken(); + $token1->setId(100); + $token2 = new DefaultToken(); + $token2->setId(200); + $tokens = [ + $token1, + $token2, + ]; + $sessionToken = new DefaultToken(); + $sessionToken->setId(100); + + $this->authTokenProvider->expects($this->once()) + ->method('getTokenByUser') + ->with($this->uid) + ->willReturn($tokens); + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('session123'); + $this->authTokenProvider->expects($this->once()) + ->method('getToken') + ->with('session123') + ->willReturn($sessionToken); + $this->initialStateService->expects($this->at(0)) + ->method('provideInitialState') + ->with('settings', 'app_tokens', [ + [ + 'id' => 100, + 'name' => null, + 'lastActivity' => 0, + 'type' => 0, + 'canDelete' => false, + 'current' => true, + 'scope' => ['filesystem' => true], + 'canRename' => false, + ], + [ + 'id' => 200, + 'name' => null, + 'lastActivity' => 0, + 'type' => 0, + 'canDelete' => true, + 'scope' => ['filesystem' => true], + 'canRename' => true, + ], + ]); + + $this->initialStateService->expects($this->at(1)) + ->method('provideInitialState') + ->with('settings', 'can_create_app_token', true); + + $form = $this->section->getForm(); + + $expected = new TemplateResponse('settings', 'settings/personal/security/authtokens'); + $this->assertEquals($expected, $form); + } + +} diff --git a/apps/settings/tests/Settings/Personal/SecurityTest.php b/apps/settings/tests/Settings/Personal/SecurityTest.php index 5dfb911716f..bd087afa466 100644 --- a/apps/settings/tests/Settings/Personal/SecurityTest.php +++ b/apps/settings/tests/Settings/Personal/SecurityTest.php @@ -32,7 +32,6 @@ use OC\Authentication\TwoFactorAuth\ProviderLoader; use OCA\Settings\Personal\Security; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; -use OCP\IInitialStateService; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; @@ -41,9 +40,6 @@ use Test\TestCase; class SecurityTest extends TestCase { - /** @var IInitialStateService|MockObject */ - private $initialStateService; - /** @var IUserManager|MockObject */ private $userManager; @@ -65,7 +61,6 @@ class SecurityTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->initialStateService = $this->createMock(IInitialStateService::class); $this->userManager = $this->createMock(IUserManager::class); $this->providerLoader = $this->createMock(ProviderLoader::class); $this->userSession = $this->createMock(IUserSession::class); @@ -73,7 +68,6 @@ class SecurityTest extends TestCase { $this->uid = 'test123'; $this->section = new Security( - $this->initialStateService, $this->userManager, $this->providerLoader, $this->userSession, -- cgit v1.2.3