CluPilotCloud/tests/Feature/PortalHostTest.php

86 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
/**
* The portal's hostname shows the product, never the shop window.
*
* The landing page was registered host-agnostically, so app.clupilot.com served
* marketing copy, a pricing table and a "sign up" call to action — at the
* address where a customer's servers live. Reported exactly that way.
*
* Routes are built once at boot, so changing config alone changes nothing.
* These re-register them against a fresh router, which is the idiom
* WelcomeTest already uses for the status host.
*/
function routerWithSiteHost(?string $host): Router
{
config()->set('admin_access.site_host', $host ?? '');
$router = new Router(app('events'), app());
Route::swap($router);
require base_path('routes/web.php');
$router->getRoutes()->refreshNameLookups();
return $router;
}
it('does not serve the website on the portals hostname', function () {
$router = routerWithSiteHost('www.example.test');
$action = fn (string $host) => $router->getRoutes()
->match(Request::create("http://{$host}/", 'GET'))
->getActionName();
expect($action('www.example.test'))->toContain('LandingController')
->and($action('app.example.test'))->not->toContain('LandingController');
});
it('sends a signed-out visitor on the portal host to the sign-in page', function () {
$router = routerWithSiteHost('www.example.test');
$response = $router->dispatch(Request::create('http://app.example.test/', 'GET'));
expect($response->getStatusCode())->toBe(302)
->and($response->headers->get('Location'))->toContain('/login');
});
it('sends a signed-in customer on the portal host to their dashboard', function () {
$this->actingAs(User::factory()->create());
$router = routerWithSiteHost('www.example.test');
$response = $router->dispatch(Request::create('http://app.example.test/', 'GET'));
expect($response->getStatusCode())->toBe(302)
->and($response->headers->get('Location'))->toContain('/dashboard');
});
it('leaves the imprint reachable from wherever the reader is standing', function () {
// Deliberately not bound to the website's host: an imprint is a legal
// requirement, and a mail footer links to it from whichever host sent the
// mail.
$router = routerWithSiteHost('www.example.test');
$action = fn (string $host) => $router->getRoutes()
->match(Request::create("http://{$host}/legal/impressum", 'GET'))
->getActionName();
expect($action('app.example.test'))->toBe($action('www.example.test'));
});
it('keeps the landing page on every host while no website host is configured', function () {
// The installed base, and every development machine reached by IP.
$router = routerWithSiteHost(null);
$action = fn (string $host) => $router->getRoutes()
->match(Request::create("http://{$host}/", 'GET'))
->getActionName();
expect($action('app.example.test'))->toContain('LandingController')
->and($action('anything.example.test'))->toContain('LandingController');
});