Debian Codegraph @

extensions/OATHAuth/tests/phpunit/integration/HTMLForm/TOTPDisableFormTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Extension\OATHAuth\Tests\Integration\HTMLForm;
22
23 use Base32\Base32;
24 use jakobo\HOTP\HOTP;
25 use MediaWiki\Context\RequestContext;
26 use MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm;
27 use MediaWiki\Extension\OATHAuth\Key\TOTPKey;
28 use MediaWiki\Extension\OATHAuth\OATHAuthServices;
29 use MediaWiki\MainConfigNames;
30 use MediaWikiIntegrationTestCase;
31 use Wikimedia\TestingAccessWrapper;
32
33 /**
34 * @author Taavi Väänänen <[email protected]tf>
35 * @group Database
36 * @coversDefaultClass \MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm
37 */
38 class TOTPDisableFormTest extends MediaWikiIntegrationTestCase {
39 protected function setUp(): void {
40 parent::setUp();
41 $this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' );
42 }
43
44 /**
45 * @return array
46 * @phan-return array{0:TOTPDisableForm,1:TOTPKey,2:MediaWiki\Extension\OATHAuth\OATHUser}
47 */
48 private function setupFormAndKey(): array {
49 $user = $this->getTestUser()->getUser();
50 $repository = OATHAuthServices::getInstance( $this->getServiceContainer() )
51 ->getUserRepository();
52 $oathUser = $repository->findByUser( $user );
53 $module = OATHAuthServices::getInstance( $this->getServiceContainer() )
54 ->getModuleRegistry()
55 ->getModuleByKey( 'totp' );
56
57 $key = $repository->createKey(
58 $oathUser,
59 $module,
60 TOTPKey::newFromRandom()->jsonSerialize(),
61 '127.0.0.1'
62 );
63
64 $form = new TOTPDisableForm(
65 $oathUser,
66 $repository,
67 $module,
68 RequestContext::getMain(),
69 );
70
71 return [ $form, $key, $oathUser ];
72 }
73
74 /**
75 * @covers ::onSubmit
76 */
77 public function testSubmitInvalidCode(): void {
78 [ $form ] = $this->setupFormAndKey();
79 $this->assertEquals(
80 [ 'oathauth-failedtovalidateoath' ],
81 $form->onSubmit( [ 'token' => 'wrong' ] ),
82 );
83 }
84
85 /**
86 * @covers ::onSubmit
87 */
88 public function testSubmitCorrectToken(): void {
89 [ $form, $key, $oathUser ] = $this->setupFormAndKey();
90
91 $secret = TestingAccessWrapper::newFromObject( $key )->secret;
92 $correctToken = HOTP::generateByTime(
93 Base32::decode( $secret['secret'] ),
94 $secret['period'],
95 )->toHOTP( 6 );
96
97 $this->assertTrue( $form->onSubmit( [ 'token' => $correctToken ] ) );
98 $this->assertEquals( [], $oathUser->getKeys() );
99 }
100
101 /**
102 * @covers ::onSubmit
103 */
104 public function testSubmitScratchToken(): void {
105 [ $form, $key, $oathUser ] = $this->setupFormAndKey();
106
107 $this->assertTrue( $form->onSubmit( [ 'token' => $key->getScratchTokens()[0] ] ) );
108 $this->assertEquals( [], $oathUser->getKeys() );
109 }
110 }