alert = m::mock(AlertsMessageBag::class); $this->config = m::mock(Repository::class); $this->repository = m::mock(SessionRepositoryInterface::class); $this->toggleTwoFactorService = m::mock(ToggleTwoFactorService::class); $this->twoFactorSetupService = m::mock(TwoFactorSetupService::class); } /** * Test the index controller when using a database driver. */ public function testIndexControllerWithDatabaseDriver() { $model = $this->generateRequestUserModel(); $this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('database'); $this->repository->shouldReceive('getUserSessions')->with($model->id)->once()->andReturn(collect(['sessions'])); $response = $this->getController()->index($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals('base.security', $response); $this->assertViewHasKey('sessions', $response); $this->assertViewKeyEquals('sessions', collect(['sessions']), $response); } /** * Test the index controller when not using the database driver. */ public function testIndexControllerWithoutDatabaseDriver() { $this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('redis'); $response = $this->getController()->index($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals('base.security', $response); $this->assertViewHasKey('sessions', $response); $this->assertViewKeyEquals('sessions', null, $response); } /** * Test TOTP generation controller. */ public function testGenerateTotpController() { $model = $this->generateRequestUserModel(); $this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn('qrCodeImage'); $response = $this->getController()->generateTotp($this->request); $this->assertIsJsonResponse($response); $this->assertResponseJsonEquals(['qrImage' => 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=qrCodeImage'], $response); } /** * Test TOTP setting controller when no exception is thrown by the service. */ public function testSetTotpControllerSuccess() { $model = $this->generateRequestUserModel(); $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once(); $response = $this->getController()->setTotp($this->request); $this->assertIsResponse($response); $this->assertSame('true', $response->getContent()); } /** * Test TOTP setting controller when an exception is thrown by the service. */ public function testSetTotpControllerWhenExceptionIsThrown() { $model = $this->generateRequestUserModel(); $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once()->andThrow(new TwoFactorAuthenticationTokenInvalid()); $response = $this->getController()->setTotp($this->request); $this->assertIsResponse($response); $this->assertSame('false', $response->getContent()); } /** * Test the disable totp controller when no exception is thrown by the service. */ public function testDisableTotpControllerSuccess() { $model = $this->generateRequestUserModel(); $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken', false)->once()->andReturn(true); $response = $this->getController()->disableTotp($this->request); $this->assertIsRedirectResponse($response); $this->assertRedirectRouteEquals('account.security', $response); } /** * Test the disable totp controller when an exception is thrown by the service. */ public function testDisableTotpControllerWhenExceptionIsThrown() { $model = $this->generateRequestUserModel(); $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken', false)->once()->andThrow(new TwoFactorAuthenticationTokenInvalid); $this->alert->shouldReceive('danger')->with(trans('base.security.2fa_disable_error'))->once()->andReturnSelf(); $this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); $response = $this->getController()->disableTotp($this->request); $this->assertIsRedirectResponse($response); $this->assertRedirectRouteEquals('account.security', $response); } /** * Test the revoke controller. */ public function testRevokeController() { $model = $this->generateRequestUserModel(); $this->repository->shouldReceive('deleteUserSession')->with($model->id, 123)->once()->andReturnNull(); $response = $this->getController()->revoke($this->request, 123); $this->assertIsRedirectResponse($response); $this->assertRedirectRouteEquals('account.security', $response); } /** * Return an instance of the controller for testing with mocked dependencies. * * @return \Pterodactyl\Http\Controllers\Base\SecurityController */ private function getController(): SecurityController { return new SecurityController( $this->alert, $this->config, $this->repository, $this->toggleTwoFactorService, $this->twoFactorSetupService ); } }