mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:04:03 +00:00
* Add Laravel 12 support, drop Laravel 11 support * Fix RLS tree generation (specify schema name in generateTrees()) * ci fixes, use stable virtualcolumn version --------- Co-authored-by: lukinovec <lukinovec@gmail.com>
57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Session\Middleware\StartSession;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
|
use Stancl\Tenancy\Middleware\ScopeSessions;
|
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
use function Stancl\Tenancy\Tests\pest;
|
|
|
|
beforeEach(function () {
|
|
Route::group([
|
|
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
|
], function () {
|
|
Route::get('/foo', function () {
|
|
return 'true';
|
|
});
|
|
});
|
|
});
|
|
|
|
test('tenant id is auto added to session if its missing', function () {
|
|
Tenant::create([
|
|
'id' => 'acme',
|
|
])->createDomain('acme');
|
|
|
|
pest()->get('http://acme.localhost/foo')
|
|
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
|
|
});
|
|
|
|
test('changing tenant id in session will abort the request', function () {
|
|
Tenant::create([
|
|
'id' => 'acme',
|
|
])->createDomain('acme');
|
|
|
|
pest()->get('http://acme.localhost/foo')
|
|
->assertSuccessful();
|
|
|
|
session()->put(ScopeSessions::$tenantIdKey, 'foobar');
|
|
|
|
pest()->get('http://acme.localhost/foo')
|
|
->assertStatus(403);
|
|
});
|
|
|
|
test('an exception is thrown when the middleware is executed before tenancy is initialized', function () {
|
|
Route::get('/bar', function () {
|
|
return true;
|
|
})->middleware([StartSession::class, ScopeSessions::class]);
|
|
|
|
Tenant::create([
|
|
'id' => 'acme',
|
|
])->createDomain('acme');
|
|
|
|
pest()->expectException(TenancyNotInitializedException::class);
|
|
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
|
|
});
|