1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 21:54:03 +00:00
tenancy/tests/OriginHeaderIdentificationTest.php
lukinovec 9e4f33e5c5
Identify tenants by the "Origin" header (#21)
* Add origin ID MW

* Test origin ID MW

* Test origin ID MW with early identification

* Fix code style (php-cs-fixer)

* Fix PHPStan errors

* Add getDomain() to domain ID MW, simplify origin ID MW

* Fix code style (php-cs-fixer)

* Rename InitializeTenancyByOrigin to InitializeTenancyByOriginHeader

* Add onFail test

* Stop throwing the exception in getDomain()

* FIx merge

* Improve origin identification test file

* Clean up test

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
2024-01-08 00:29:01 +01:00

56 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByOriginHeader;
beforeEach(function () {
InitializeTenancyByOriginHeader::$onFail = null;
config([
'tenancy.central_domains' => [
'localhost',
],
]);
Route::post('/home', function () {
return response(tenant('id'));
})->middleware([InitializeTenancyByOriginHeader::class])->name('home');
});
afterEach(function () {
InitializeTenancyByOriginHeader::$onFail = null;
});
test('origin identification works', function () {
$tenant = Tenant::create();
$tenant->domains()->create([
'domain' => 'foo',
]);
pest()
->withHeader('Origin', 'foo.localhost')
->post('home')
->assertSee($tenant->id);
});
test('tenant routes are not accessible on central domains while using origin identification', function () {
pest()
->withHeader('Origin', 'localhost')
->post('home')
->assertStatus(500);
});
test('onfail logic can be customized', function() {
InitializeTenancyByOriginHeader::$onFail = function () {
return response('onFail message');
};
pest()
->withHeader('Origin', 'bar.localhost') // 'bar'/'bar.localhost' is not an existing tenant domain
->post('home')
->assertSee('onFail message');
});