Merge pull request #14834 from bryanlopezinc/AddAppUrlTest

Added test for app url config
This commit is contained in:
snipe 2024-06-12 11:14:17 +01:00 committed by GitHub
commit c641b733e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 29 deletions

View file

@ -67,28 +67,9 @@ class SettingsController extends Controller
$start_settings['db_error'] = $e->getMessage();
}
if (array_key_exists("HTTP_X_FORWARDED_PROTO", $_SERVER)) {
$protocol = $_SERVER["HTTP_X_FORWARDED_PROTO"] . "://";
} elseif (array_key_exists('HTTPS', $_SERVER) && ('on' == $_SERVER['HTTPS'])) {
$protocol = "https://";
} else {
$protocol = "http://";
}
if (array_key_exists("HTTP_X_FORWARDED_HOST", $_SERVER)) {
$host = $_SERVER["HTTP_X_FORWARDED_HOST"];
} else {
$host = array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER['SERVER_NAME'] : null;
$port = array_key_exists('SERVER_PORT', $_SERVER) ? $_SERVER['SERVER_PORT'] : null;
if (('http://' === $protocol && '80' != $port) || ('https://' === $protocol && '443' != $port)) {
$host .= ':'.$port;
}
}
$pageURL = $protocol.$host.$_SERVER['REQUEST_URI'];
$start_settings['url_config'] = config('app.url').'/setup';
$start_settings['url_valid'] = ($start_settings['url_config'] === $pageURL);
$start_settings['real_url'] = $pageURL;
$start_settings['url_config'] = trim(config('app.url'), '/'). '/setup';
$start_settings['real_url'] = request()->url();
$start_settings['url_valid'] = $start_settings['url_config'] === $start_settings['real_url'];
$start_settings['php_version_min'] = true;
// Curl the .env file to make sure it's not accessible via a browser

View file

@ -25,13 +25,6 @@ class ShowSetUpPageTest extends TestCase
*/
protected bool $preventStrayRequest = true;
protected function setUp(): void
{
parent::setUp();
$_SERVER['REQUEST_URI'] = '/setup';
}
protected function getSetUpPageResponse(): TestResponse
{
if ($this->preventStrayRequest) {
@ -234,4 +227,44 @@ class ShowSetUpPageTest extends TestCase
return true;
});
}
public function testWillShowErrorMessageWhenAppUrlIsNotSameWithPageUrl(): void
{
config(['app.url' => 'http://www.github.com']);
$this->getSetUpPageResponse()->assertOk();
$this->assertSeeAppUrlMisconfigurationErrorMessage();
}
protected function assertSeeAppUrlMisconfigurationErrorMessage(bool $shouldSee = true): void
{
$url = URL::to('setup');
$errorMessage = "Uh oh! Snipe-IT thinks your URL is http://www.github.com/setup, but your real URL is {$url}";
$successMessage = 'That URL looks right! Good job!';
if ($shouldSee) {
self::$latestResponse->assertSee($errorMessage)->assertDontSee($successMessage);
return;
}
self::$latestResponse->assertSee($successMessage)->assertDontSee($errorMessage);
}
public function testWillNotShowErrorMessageWhenAppUrlIsSameWithPageUrl(): void
{
$this->getSetUpPageResponse()->assertOk();
$this->assertSeeAppUrlMisconfigurationErrorMessage(false);
}
public function testWhenAppUrlContainsTrailingSlash(): void
{
config(['app.url' => 'http://www.github.com/']);
$this->getSetUpPageResponse()->assertOk();
$this->assertSeeAppUrlMisconfigurationErrorMessage();
}
}