mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
* wip * Improve tests * rename class * wip * improve tests * introduce early identification middlewares * Update PreventAccessFromCentralDomains.php * method rename * method rename * Update UniversalRouteTest.php * Update UniversalRouteTest.php * Update EarlyIdentificationTest.php * remove early classes and add check in existing classes * MWs improvements * Update UniversalRouteTest.php * Fix code style (php-cs-fixer) * trigger ci * fix failing test after merge * add test for universal route in early identification * Update InitializeTenancyByDomain.php * Update UniversalRouteTest.php * remove `routeHasMiddleware` method from MW and use the UniversalRoutes class method * helper dockblock * add test * handle universal routes in early identification * remove UniversalRoute class because we are not using it anymore * rename class from PreventAccessFromCentralDomains to PreventAccessFromUnwantedDomains * improvements after self review * Update PreventAccessFromUnwantedDomains.php * remove inline class namespaces * remove DomainTenant class and use the Tenant class * update comment * removed custom expection and add method * Update tests/EarlyIdentificationTest.php Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * use ltrim and simplify the comment * remove comments and typhint * dataset and keys rename * rename $route parameter * removed helper functions * fix style * Update InitializeTenancyByPath.php * Update tests/EarlyIdentificationTest.php * code style * improve subdomain test * use TenancyInitialized event and remove DomainTenant alias * remove comment and move expectException below * code style * use TenancyInitialized event * improve test * improve datasets * Initialized -> Initializing * Update InitializeTenancyByPath.php * remove todo * Fix code style (php-cs-fixer) * refactor helper method * Update UniversalRouteTest.php * add note above test * remove after each hook * renamed universal_middleware to global_middleware * remove helper and improve url names * change check position * Revert "change check position" This reverts commit e4371d2f3aa8ad7ae5e5b7d15781b72a5f1be03c. * repositioned central check * add comment Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
120 lines
3.9 KiB
PHP
120 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
|
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
|
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
use Illuminate\Contracts\Http\Kernel;
|
|
|
|
test('a route can work in both central and tenant context', function (array $routeMiddleware, string|null $globalMiddleware) {
|
|
if ($globalMiddleware) {
|
|
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
|
}
|
|
|
|
Route::middlewareGroup('universal', []);
|
|
|
|
Route::get('/foo', function () {
|
|
return tenancy()->initialized
|
|
? 'Tenancy is initialized.'
|
|
: 'Tenancy is not initialized.';
|
|
})->middleware($routeMiddleware);
|
|
|
|
$tenant = Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
$tenant->domains()->create([
|
|
'domain' => 'acme.localhost',
|
|
]);
|
|
|
|
pest()->get("http://localhost/foo")
|
|
->assertSuccessful()
|
|
->assertSee('Tenancy is not initialized.');
|
|
|
|
pest()->get("http://acme.localhost/foo")
|
|
->assertSuccessful()
|
|
->assertSee('Tenancy is initialized.');
|
|
})->with('identification types');
|
|
|
|
test('making one route universal doesnt make all routes universal', function (array $routeMiddleware, string|null $globalMiddleware) {
|
|
if ($globalMiddleware) {
|
|
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
|
}
|
|
|
|
Route::middlewareGroup('universal', []);
|
|
|
|
Route::middleware($routeMiddleware)->group(function () {
|
|
Route::get('/nonuniversal', function () {
|
|
return tenant('id');
|
|
});
|
|
|
|
Route::get('/universal', function () {
|
|
return tenancy()->initialized
|
|
? 'Tenancy is initialized.'
|
|
: 'Tenancy is not initialized.';
|
|
})->middleware('universal');
|
|
});
|
|
|
|
$tenant = Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
$tenant->domains()->create([
|
|
'domain' => 'acme.localhost',
|
|
]);
|
|
|
|
pest()->get("http://localhost/universal")
|
|
->assertSuccessful()
|
|
->assertSee('Tenancy is not initialized.');
|
|
|
|
pest()->get("http://acme.localhost/universal")
|
|
->assertSuccessful()
|
|
->assertSee('Tenancy is initialized.');
|
|
|
|
tenancy()->end();
|
|
|
|
pest()->get('http://localhost/nonuniversal')
|
|
->assertStatus(404);
|
|
|
|
pest()->get('http://acme.localhost/nonuniversal')
|
|
->assertSuccessful()
|
|
->assertSee('acme');
|
|
})->with([
|
|
'early identification' => [
|
|
'route_middleware' => [PreventAccessFromUnwantedDomains::class],
|
|
'global_middleware' => InitializeTenancyByDomain::class,
|
|
],
|
|
'route-level identification' => [
|
|
'route_middleware' => [PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
|
|
'global_middleware' => null,
|
|
]
|
|
]);
|
|
|
|
test('it throws correct exception when route is universal and tenant does not exist', function (array $routeMiddleware, string|null $globalMiddleware) {
|
|
if ($globalMiddleware) {
|
|
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
|
}
|
|
|
|
Route::middlewareGroup('universal', []);
|
|
|
|
Route::get('/foo', function () {
|
|
return tenancy()->initialized
|
|
? 'Tenancy is initialized.'
|
|
: 'Tenancy is not initialized.';
|
|
})->middleware($routeMiddleware);
|
|
|
|
pest()->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
|
|
$this->withoutExceptionHandling()->get('http://acme.localhost/foo');
|
|
})->with('identification types');
|
|
|
|
dataset('identification types', [
|
|
'early identification' => [
|
|
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class],
|
|
'global_middleware' => InitializeTenancyByDomain::class,
|
|
],
|
|
'route-level identification' => [
|
|
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
|
|
'global_middleware' => null,
|
|
]
|
|
]);
|