‪TYPO3CMS  11.5
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 Psr\Http\Message\ResponseInterface;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
28 class ‪MfaAjaxControllerTest extends FunctionalTestCase
29 {
32 
33  protected ‪$backendUserFixture = 'EXT:core/Tests/Functional/Authentication/Fixtures/be_users.xml';
34 
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  $this->setUpBackendUserFromFixture(1);
40 
41  $this->subject = new ‪MfaAjaxController($this->get(MfaProviderRegistry::class));
42 
43  $this->request = (new ‪ServerRequest())
44  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
45  }
46 
51  public function ‪handleRequestHandlesInvalidRequestTest(array $parsedBody): void
52  {
53  $response = $this->‪parseResponse($this->subject->handleRequest($this->request->withParsedBody($parsedBody)));
54 
55  self::assertFalse($response['success']);
56  self::assertEquals('Invalid request could not be processed', $response['message']);
57  }
58 
60  {
61  yield 'No parameters' => [[]];
62  yield 'Invalid action' => [['action' => 'unknown']];
63  yield 'Missing user' => [['action' => 'deactivate']];
64  yield 'Missing table' => [['action' => 'deactivate', 'userId' => 5]];
65  yield 'Invalid table' => [['action' => 'deactivate', 'userId' => 5, 'tableName' => 'some_table']];
66  }
67 
72  {
73  // Make the target user a system maintainer. Since the current user (1)
74  // is only admin, he is not allowed to deactivate the providers, nor MFA.
75  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] = ['5'];
76 
77  $response = $this->‪parseResponse(
78  $this->subject->handleRequest(
79  $this->request->withParsedBody([
80  'action' => 'deactivate',
81  'userId' => 5,
82  'tableName' => 'be_users',
83  ])
84  )
85  );
86 
87  self::assertFalse($response['success']);
88  self::assertEquals('Your are not allowed to perform this action', $response['message']);
89  }
90 
96  array $parsedBody,
97  bool $success,
98  string $message,
99  int $remaining
100  ): void {
101  $response = $this->‪parseResponse(
102  $this->subject->handleRequest(
103  $this->request->withParsedBody(
104  array_replace_recursive([
105  'action' => 'deactivate',
106  'tableName' => 'be_users',
107  ], $parsedBody)
108  )
109  )
110  );
111 
112  self::assertEquals($success, $response['success']);
113  self::assertEquals($message, $response['message']);
114  self::assertEquals($remaining, $response['remaining']);
115  }
116 
118  {
119  yield 'No deactivation because no active providers' => [
120  ['userId' => 3],
121  false,
122  'No provider has been deactivated',
123  0,
124  ];
125  yield 'Requested provider can not be found' => [
126  ['userId' => 3, 'provider' => 'unknown'],
127  false,
128  'Provider unknown could not be found',
129  0,
130  ];
131  yield 'Does not deactivate an inactive provider' => [
132  ['userId' => 3, 'provider' => 'recovery-codes'],
133  false,
134  'Could not deactivate provider Recovery codes',
135  0,
136  ];
137  yield 'Deactivates all providers on missing provider parameter' => [
138  ['userId' => 5],
139  true,
140  'Successfully deactivated all active providers for user mfa_admin_locked',
141  0,
142  ];
143  yield 'Deactivates requested provider' => [
144  ['userId' => 5, 'provider' => 'recovery-codes'],
145  true,
146  'Successfully deactivated provider Recovery codes for user mfa_admin_locked',
147  1,
148  ];
149  yield 'Deactivation of last main provider does also deactivate recovery codes' => [
150  ['userId' => 5, 'provider' => 'totp'],
151  true,
152  'Successfully deactivated provider Time-based one-time password for user mfa_admin_locked',
153  0,
154  ];
155  }
156 
157  protected function ‪parseResponse(ResponseInterface $response): array
158  {
159  $response = json_decode($response->getBody()->getContents(), true);
160 
161  return [
162  'success' => (bool)($response['success'] ?? false),
163  'message' => (string)(array_shift($response['status'])['message'] ?? ''),
164  'remaining' => (int)($response['remaining'] ?? 0),
165  ];
166  }
167 }
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\$subject
‪MfaAjaxController $subject
Definition: MfaAjaxControllerTest.php:30
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\$request
‪ServerRequest $request
Definition: MfaAjaxControllerTest.php:31
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestReturnsInvalidRequestOnInsufficientPermissionsTest
‪handleRequestReturnsInvalidRequestOnInsufficientPermissionsTest()
Definition: MfaAjaxControllerTest.php:71
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\setUp
‪setUp()
Definition: MfaAjaxControllerTest.php:35
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest
Definition: MfaAjaxControllerTest.php:29
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Controller\MfaAjaxController
Definition: MfaAjaxController.php:39
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:598
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesDeactivationRequestTest
‪handleRequestHandlesDeactivationRequestTest(array $parsedBody, bool $success, string $message, int $remaining)
Definition: MfaAjaxControllerTest.php:95
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesDeactivationRequestTestDataProvider
‪handleRequestHandlesDeactivationRequestTestDataProvider()
Definition: MfaAjaxControllerTest.php:117
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesInvalidRequestTestDataProvider
‪handleRequestHandlesInvalidRequestTestDataProvider()
Definition: MfaAjaxControllerTest.php:59
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\handleRequestHandlesInvalidRequestTest
‪handleRequestHandlesInvalidRequestTest(array $parsedBody)
Definition: MfaAjaxControllerTest.php:51
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\parseResponse
‪parseResponse(ResponseInterface $response)
Definition: MfaAjaxControllerTest.php:157
‪TYPO3\CMS\Backend\Tests\Functional\Controller
Definition: EditDocumentControllerTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Controller\MfaAjaxControllerTest\$backendUserFixture
‪$backendUserFixture
Definition: MfaAjaxControllerTest.php:33
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry
Definition: MfaProviderRegistry.php:28