Skip to content

Commit

Permalink
Remove callExit() from AuthenticationHttp::authForm()
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 9, 2024
1 parent 02044f2 commit e435bb7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/Plugins/Auth/AuthenticationHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public function showLoginForm(): Response
return $responseRenderer->response();
}

$this->authForm();
return $this->authForm();
}

/**
* Displays authentication form
*/
public function authForm(): never
public function authForm(): Response
{
$config = Config::getInstance();
if (empty($config->selectedServer['auth_http_realm'])) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public function authForm(): never

$response->addHTML(Config::renderFooter());

$response->callExit();
return $response->response();
}

/**
Expand Down Expand Up @@ -198,7 +198,7 @@ public function showFailure(AuthenticationFailure $failure): Response
return $responseRenderer->response();
}

$this->authForm();
return $this->authForm();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Plugins/Auth/AuthenticationCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public function testAuthError(): void
Current::$table = 'testTable';
$config->settings['Servers'] = [1, 2];

(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);

$response = $this->object->showLoginForm();

$result = (string) $response->getBody();
Expand Down
53 changes: 18 additions & 35 deletions tests/unit/Plugins/Auth/AuthenticationHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpMyAdmin\Current;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Exceptions\AuthenticationFailure;
use PhpMyAdmin\Exceptions\ExitException;
use PhpMyAdmin\Plugins\Auth\AuthenticationHttp;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Tests\AbstractTestCase;
Expand All @@ -17,7 +16,6 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Medium;
use ReflectionProperty;
use Throwable;

use function base64_encode;
use function json_decode;
Expand Down Expand Up @@ -82,13 +80,8 @@ public function testAuthVerbose(): void
$responseStub = new ResponseRendererStub();
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showLoginForm();
} catch (Throwable $throwable) {
}
$response = $this->object->showLoginForm();

self::assertInstanceOf(ExitException::class, $throwable ?? null);
$response = $responseStub->getResponse();
self::assertSame(['Basic realm="phpMyAdmin verboseMessag"'], $response->getHeader('WWW-Authenticate'));
self::assertSame(401, $response->getStatusCode());
}
Expand All @@ -103,13 +96,8 @@ public function testAuthHost(): void
$responseStub = new ResponseRendererStub();
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showLoginForm();
} catch (Throwable $throwable) {
}
$response = $this->object->showLoginForm();

self::assertInstanceOf(ExitException::class, $throwable ?? null);
$response = $responseStub->getResponse();
self::assertSame(['Basic realm="phpMyAdmin hst"'], $response->getHeader('WWW-Authenticate'));
self::assertSame(401, $response->getStatusCode());
}
Expand All @@ -124,13 +112,8 @@ public function testAuthRealm(): void
$responseStub = new ResponseRendererStub();
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showLoginForm();
} catch (Throwable $throwable) {
}
$response = $this->object->showLoginForm();

self::assertInstanceOf(ExitException::class, $throwable ?? null);
$response = $responseStub->getResponse();
self::assertSame(['Basic realm="realmmessage"'], $response->getHeader('WWW-Authenticate'));
self::assertSame(401, $response->getStatusCode());
}
Expand Down Expand Up @@ -286,33 +269,33 @@ public function testAuthFails(): void
DatabaseInterface::$instance = $dbi;
$GLOBALS['errno'] = 31;

(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
ResponseRenderer::getInstance()->setAjax(false);

$response = $this->object->showFailure(AuthenticationFailure::serverDenied());

$result = (string) $response->getBody();

self::assertStringContainsString('<p>error 123</p>', $result);

$this->object = $this->getMockBuilder(AuthenticationHttp::class)
->disableOriginalConstructor()
->onlyMethods(['authForm'])
->getMock();

$this->object->expects(self::exactly(2))
->method('authForm')
->willThrowException(new ExitException());
// case 2
$config->selectedServer['host'] = 'host';
$GLOBALS['errno'] = 1045;

try {
$this->object->showFailure(AuthenticationFailure::serverDenied());
} catch (ExitException) {
}
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
ResponseRenderer::getInstance()->setAjax(false);

$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$result = (string) $response->getBody();
self::assertStringContainsString('Wrong username/password. Access denied.', $result);

(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
ResponseRenderer::getInstance()->setAjax(false);

// case 3
$GLOBALS['errno'] = 1043;
$this->expectException(ExitException::class);
$this->object->showFailure(AuthenticationFailure::serverDenied());
$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$result = (string) $response->getBody();
self::assertStringContainsString('Wrong username/password. Access denied.', $result);
}

public function testShowLoginFormWithAjax(): void
Expand Down

0 comments on commit e435bb7

Please sign in to comment.