‪TYPO3CMS  ‪main
MfaAjaxControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Psr\Http\Message\ResponseInterface;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪MfaAjaxControllerTest extends FunctionalTestCase
31 {
34 
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users_mfa.csv');
39  $backendUser = $this->setUpBackendUser(1);
40  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
41 
42  $this->subject = new ‪MfaAjaxController($this->get(MfaProviderRegistry::class));
43 
44  $this->request = (new ‪ServerRequest())
45  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
46  }
47 
48  #[DataProvider('handleRequestHandlesInvalidRequestTestDataProvider')]
49  #[Test]
50  public function ‪handleRequestHandlesInvalidRequestTest(array $parsedBody): void
51  {
52  $response = $this->‪parseResponse($this->subject->handleRequest($this->request->withParsedBody($parsedBody)));
53 
54  self::assertFalse($response['success']);
55  self::assertEquals('Invalid request could not be processed', $response['message']);
56  }
57 
58  public static function ‪handleRequestHandlesInvalidRequestTestDataProvider(): \Generator
59  {
60  yield 'No parameters' => [[]];
61  yield 'Invalid action' => [['action' => 'unknown']];
62  yield 'Missing user' => [['action' => 'deactivate']];
63  yield 'Missing table' => [['action' => 'deactivate', 'userId' => 5]];
64  yield 'Invalid table' => [['action' => 'deactivate', 'userId' => 5, 'tableName' => 'some_table']];
65  }
66 
67  #[Test]
69  {
70  // Make the target user a system maintainer. Since the current user (1)
71  // is only admin, he is not allowed to deactivate the providers, nor MFA.
72  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] = ['5'];
73 
74  $response = $this->‪parseResponse(
75  $this->subject->handleRequest(
76  $this->request->withParsedBody([
77  'action' => 'deactivate',
78  'userId' => 5,
79  'tableName' => 'be_users',
80  ])
81  )
82  );
83 
84  self::assertFalse($response['success']);
85  self::assertEquals('Your are not allowed to perform this action', $response['message']);
86  }
87 
88  #[DataProvider('handleRequestHandlesDeactivationRequestTestDataProvider')]
89  #[Test]
91  array $parsedBody,
92  bool $success,
93  string $message,
94  int $remaining
95  ): void {
96  $response = $this->‪parseResponse(
97  $this->subject->handleRequest(
98  $this->request->withParsedBody(
99  array_replace_recursive([
100  'action' => 'deactivate',
101  'tableName' => 'be_users',
102  ], $parsedBody)
103  )
104  )
105  );
106 
107  self::assertEquals($success, $response['success']);
108  self::assertEquals($message, $response['message']);
109  self::assertEquals($remaining, $response['remaining']);
110  }
111 
113  {
114  yield 'No deactivation because no active providers' => [
115  ['userId' => 3],
116  false,
117  'No provider has been deactivated',
118  0,
119  ];
120  yield 'Requested provider can not be found' => [
121  ['userId' => 3, 'provider' => 'unknown'],
122  false,
123  'Provider unknown could not be found',
124  0,
125  ];
126  yield 'Does not deactivate an inactive provider' => [
127  ['userId' => 3, 'provider' => 'recovery-codes'],
128  false,
129  'Could not deactivate provider Recovery codes',
130  0,
131  ];
132  yield 'Deactivates all providers on missing provider parameter' => [
133  ['userId' => 5],
134  true,
135  'Successfully deactivated all active providers for user mfa_admin_locked',
136  0,
137  ];
138  yield 'Deactivates requested provider' => [
139  ['userId' => 5, 'provider' => 'recovery-codes'],
140  true,
141  'Successfully deactivated provider Recovery codes for user mfa_admin_locked',
142  1,
143  ];
144  yield 'Deactivation of last main provider does also deactivate recovery codes' => [
145  ['userId' => 5, 'provider' => 'totp'],
146  true,
147  'Successfully deactivated provider Time-based one-time password for user mfa_admin_locked',
148  0,
149  ];
150  }
151 
152  protected function ‪parseResponse(ResponseInterface $response): array
153  {
154  $response = json_decode($response->getBody()->getContents(), true);
155 
156  return [
157  'success' => (bool)($response['success'] ?? false),
158  'message' => (string)(array_shift($response['status'])['message'] ?? ''),
159  'remaining' => (int)($response['remaining'] ?? 0),
160  ];
161  }
162 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\$subject
‪MfaAjaxController $subject
Definition: MfaAjaxControllerTest.php:32
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\$request
‪ServerRequest $request
Definition: MfaAjaxControllerTest.php:33
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestReturnsInvalidRequestOnInsufficientPermissionsTest
‪handleRequestReturnsInvalidRequestOnInsufficientPermissionsTest()
Definition: MfaAjaxControllerTest.php:68
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\setUp
‪setUp()
Definition: MfaAjaxControllerTest.php:35
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest
Definition: MfaAjaxControllerTest.php:31
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Controller\MfaAjaxController
Definition: MfaAjaxController.php:41
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesDeactivationRequestTest
‪handleRequestHandlesDeactivationRequestTest(array $parsedBody, bool $success, string $message, int $remaining)
Definition: MfaAjaxControllerTest.php:90
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesInvalidRequestTestDataProvider
‪static handleRequestHandlesInvalidRequestTestDataProvider()
Definition: MfaAjaxControllerTest.php:58
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesInvalidRequestTest
‪handleRequestHandlesInvalidRequestTest(array $parsedBody)
Definition: MfaAjaxControllerTest.php:50
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesDeactivationRequestTestDataProvider
‪static handleRequestHandlesDeactivationRequestTestDataProvider()
Definition: MfaAjaxControllerTest.php:112
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\parseResponse
‪parseResponse(ResponseInterface $response)
Definition: MfaAjaxControllerTest.php:152
‪TYPO3\CMS\Backend\Tests\Functional\Controller
Definition: BackendControllerTest.php:18
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry
Definition: MfaProviderRegistry.php:28