* @group Database * @coversDefaultClass \MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm */ class TOTPDisableFormTest extends MediaWikiIntegrationTestCase { /** * @return array * @phan-return array{0:TOTPDisableForm,1:TOTPKey,2:MediaWiki\Extension\OATHAuth\OATHUser} */ private function setupFormAndKey(): array { $user = $this->getTestUser()->getUser(); $repository = OATHAuthServices::getInstance( $this->getServiceContainer() ) ->getUserRepository(); $oathUser = $repository->findByUser( $user ); $module = OATHAuthServices::getInstance( $this->getServiceContainer() ) ->getModuleRegistry() ->getModuleByKey( 'totp' ); $key = $repository->createKey( $oathUser, $module, TOTPKey::newFromRandom()->jsonSerialize(), '127.0.0.1' ); $form = new TOTPDisableForm( $oathUser, $repository, $module, RequestContext::getMain(), ); return [ $form, $key, $oathUser ]; } /** * @covers ::onSubmit */ public function testSubmitInvalidCode(): void { [ $form ] = $this->setupFormAndKey(); $this->assertEquals( [ 'oathauth-failedtovalidateoath' ], $form->onSubmit( [ 'token' => 'wrong' ] ), ); } /** * @covers ::onSubmit */ public function testSubmitCorrectToken(): void { [ $form, $key, $oathUser ] = $this->setupFormAndKey(); $secret = TestingAccessWrapper::newFromObject( $key )->secret; $correctToken = HOTP::generateByTime( Base32::decode( $secret['secret'] ), $secret['period'], )->toHOTP( 6 ); $this->assertTrue( $form->onSubmit( [ 'token' => $correctToken ] ) ); $this->assertEquals( [], $oathUser->getKeys() ); } /** * @covers ::onSubmit */ public function testSubmitScratchToken(): void { [ $form, $key, $oathUser ] = $this->setupFormAndKey(); $this->assertTrue( $form->onSubmit( [ 'token' => $key->getScratchTokens()[0] ] ) ); $this->assertEquals( [], $oathUser->getKeys() ); } }