123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- declare(strict_types=1);
- /**
- * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\Core\Controller;
- use OC\Core\Controller\ClientFlowLoginV2Controller;
- use OC\Core\Data\LoginFlowV2Credentials;
- use OC\Core\Db\LoginFlowV2;
- use OC\Core\Exception\LoginFlowV2NotFoundException;
- use OC\Core\Service\LoginFlowV2Service;
- use OCP\AppFramework\Http;
- use OCP\Defaults;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\ISession;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Security\ISecureRandom;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class ClientFlowLoginV2ControllerTest extends TestCase {
- /** @var IRequest|MockObject */
- private $request;
- /** @var LoginFlowV2Service|MockObject */
- private $loginFlowV2Service;
- /** @var IURLGenerator|MockObject */
- private $urlGenerator;
- /** @var ISession|MockObject */
- private $session;
- /** @var IUserSession|MockObject */
- private $userSession;
- /** @var ISecureRandom|MockObject */
- private $random;
- /** @var Defaults|MockObject */
- private $defaults;
- /** @var IL10N|MockObject */
- private $l;
- /** @var ClientFlowLoginV2Controller */
- private $controller;
- protected function setUp(): void {
- parent::setUp();
- $this->request = $this->createMock(IRequest::class);
- $this->loginFlowV2Service = $this->createMock(LoginFlowV2Service::class);
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->session = $this->createMock(ISession::class);
- $this->userSession = $this->createMock(IUserSession::class);
- $this->random = $this->createMock(ISecureRandom::class);
- $this->defaults = $this->createMock(Defaults::class);
- $this->l = $this->createMock(IL10N::class);
- $this->controller = new ClientFlowLoginV2Controller(
- 'core',
- $this->request,
- $this->loginFlowV2Service,
- $this->urlGenerator,
- $this->session,
- $this->userSession,
- $this->random,
- $this->defaults,
- 'user',
- $this->l
- );
- }
- public function testPollInvalid(): void {
- $this->loginFlowV2Service->method('poll')
- ->with('token')
- ->willThrowException(new LoginFlowV2NotFoundException());
- $result = $this->controller->poll('token');
- $this->assertSame([], $result->getData());
- $this->assertSame(Http::STATUS_NOT_FOUND, $result->getStatus());
- }
- public function testPollValid(): void {
- $creds = new LoginFlowV2Credentials('server', 'login', 'pass');
- $this->loginFlowV2Service->method('poll')
- ->with('token')
- ->willReturn($creds);
- $result = $this->controller->poll('token');
- $this->assertSame($creds->jsonSerialize(), $result->getData());
- $this->assertSame(Http::STATUS_OK, $result->getStatus());
- }
- public function testLandingInvalid(): void {
- $this->session->expects($this->never())
- ->method($this->anything());
- $this->loginFlowV2Service->method('startLoginFlow')
- ->with('token')
- ->willReturn(false);
- $result = $this->controller->landing('token');
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- $this->assertInstanceOf(Http\StandaloneTemplateResponse::class, $result);
- }
- public function testLandingValid(): void {
- $this->session->expects($this->once())
- ->method('set')
- ->with('client.flow.v2.login.token', 'token');
- $this->loginFlowV2Service->method('startLoginFlow')
- ->with('token')
- ->willReturn(true);
- $this->urlGenerator->method('linkToRouteAbsolute')
- ->with('core.ClientFlowLoginV2.showAuthPickerPage')
- ->willReturn('https://server/path');
- $result = $this->controller->landing('token');
- $this->assertInstanceOf(Http\RedirectResponse::class, $result);
- $this->assertSame(Http::STATUS_SEE_OTHER, $result->getStatus());
- $this->assertSame('https://server/path', $result->getRedirectURL());
- }
- public function testShowAuthPickerNoLoginToken(): void {
- $this->session->method('get')
- ->willReturn(null);
- $result = $this->controller->showAuthPickerPage();
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testShowAuthPickerInvalidLoginToken(): void {
- $this->session->method('get')
- ->with('client.flow.v2.login.token')
- ->willReturn('loginToken');
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willThrowException(new LoginFlowV2NotFoundException());
- $result = $this->controller->showAuthPickerPage();
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testShowAuthPickerValidLoginToken(): void {
- $this->session->method('get')
- ->with('client.flow.v2.login.token')
- ->willReturn('loginToken');
- $flow = new LoginFlowV2();
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willReturn($flow);
- $this->random->method('generate')
- ->with(64, ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS)
- ->willReturn('random');
- $this->session->expects($this->once())
- ->method('set')
- ->with('client.flow.v2.state.token', 'random');
- $this->controller->showAuthPickerPage();
- }
- public function testGrantPageNoStateToken(): void {
- $result = $this->controller->grantPage(null);
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testGrantPageInvalidStateToken(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- return null;
- });
- $result = $this->controller->grantPage('stateToken');
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testGrantPageInvalidLoginToken(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- if ($name === 'client.flow.v2.state.token') {
- return 'stateToken';
- }
- if ($name === 'client.flow.v2.login.token') {
- return 'loginToken';
- }
- return null;
- });
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willThrowException(new LoginFlowV2NotFoundException());
- $result = $this->controller->grantPage('stateToken');
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testGrantPageValid(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- if ($name === 'client.flow.v2.state.token') {
- return 'stateToken';
- }
- if ($name === 'client.flow.v2.login.token') {
- return 'loginToken';
- }
- return null;
- });
- $user = $this->createMock(IUser::class);
- $user->method('getUID')
- ->willReturn('uid');
- $user->method('getDisplayName')
- ->willReturn('display name');
- $this->userSession->method('getUser')
- ->willReturn($user);
- $flow = new LoginFlowV2();
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willReturn($flow);
- $result = $this->controller->grantPage('stateToken');
- $this->assertSame(Http::STATUS_OK, $result->getStatus());
- }
- public function testGenerateAppPasswordInvalidStateToken(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- return null;
- });
- $result = $this->controller->generateAppPassword('stateToken');
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testGenerateAppPassworInvalidLoginToken(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- if ($name === 'client.flow.v2.state.token') {
- return 'stateToken';
- }
- if ($name === 'client.flow.v2.login.token') {
- return 'loginToken';
- }
- return null;
- });
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willThrowException(new LoginFlowV2NotFoundException());
- $result = $this->controller->generateAppPassword('stateToken');
- $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
- }
- public function testGenerateAppPassworValid(): void {
- $this->session->method('get')
- ->willReturnCallback(function ($name) {
- if ($name === 'client.flow.v2.state.token') {
- return 'stateToken';
- }
- if ($name === 'client.flow.v2.login.token') {
- return 'loginToken';
- }
- return null;
- });
- $flow = new LoginFlowV2();
- $this->loginFlowV2Service->method('getByLoginToken')
- ->with('loginToken')
- ->willReturn($flow);
- $clearedState = false;
- $clearedLogin = false;
- $this->session->method('remove')
- ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState) {
- if ($name === 'client.flow.v2.state.token') {
- $clearedState = true;
- }
- if ($name === 'client.flow.v2.login.token') {
- $clearedLogin = true;
- }
- });
- $this->session->method('getId')
- ->willReturn('sessionId');
- $this->loginFlowV2Service->expects($this->once())
- ->method('flowDone')
- ->with(
- 'loginToken',
- 'sessionId',
- 'https://server',
- 'user'
- )->willReturn(true);
- $this->request->method('getServerProtocol')
- ->willReturn('https');
- $this->request->method('getRequestUri')
- ->willReturn('/login/v2');
- $this->request->method('getServerHost')
- ->willReturn('server');
- $result = $this->controller->generateAppPassword('stateToken');
- $this->assertSame(Http::STATUS_OK, $result->getStatus());
- $this->assertTrue($clearedLogin);
- $this->assertTrue($clearedState);
- }
- }
|