mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:44:03 +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>
104 lines
4.2 KiB
PHP
104 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Contracts\Http\Kernel;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
|
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
|
|
use Stancl\Tenancy\Tests\Etc\EarlyIdentification\Controller;
|
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
|
|
beforeEach(function () {
|
|
config()->set([
|
|
'tenancy.token' => 'central-abc123',
|
|
]);
|
|
|
|
Event::listen(TenancyInitialized::class, function (TenancyInitialized $event) {
|
|
config()->set([
|
|
'tenancy.token' => $event->tenancy->tenant->getTenantKey() . '-abc123',
|
|
]);
|
|
});
|
|
});
|
|
|
|
test('early identification works with path identification', function () {
|
|
app(Kernel::class)->pushMiddleware(InitializeTenancyByPath::class);
|
|
|
|
Route::group([
|
|
'prefix' => '/{tenant}',
|
|
], function () {
|
|
Route::get('/foo', [Controller::class, 'index'])->name('foo');
|
|
});
|
|
|
|
Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
|
|
$response = pest()->get('/acme/foo')->assertOk();
|
|
|
|
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
|
|
|
// check if default parameter feature is working fine by asserting that the route WITHOUT the tenant parameter
|
|
// matches the route WITH the tenant parameter
|
|
expect(route('foo'))->toBe(route('foo', ['tenant' => 'acme']));
|
|
});
|
|
|
|
test('early identification works with request data identification', function (string $type) {
|
|
app(Kernel::class)->pushMiddleware(InitializeTenancyByRequestData::class);
|
|
|
|
Route::get('/foo', [Controller::class, 'index'])->name('foo');
|
|
|
|
$tenant = Tenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
|
|
if ($type === 'header') {
|
|
$response = pest()->get('/foo', ['X-Tenant' => $tenant->id])->assertOk();
|
|
} elseif ($type === 'queryParameter') {
|
|
$response = pest()->get("/foo?tenant=$tenant->id")->assertOk();
|
|
}
|
|
|
|
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
|
})->with([
|
|
'using request header parameter' => 'header',
|
|
'using request query parameter' => 'queryParameter'
|
|
]);
|
|
|
|
// The name of this test is suffixed by the dataset — domain / subdomain / domainOrSubdomain identification
|
|
test('early identification works', function (string $middleware, string $domain, string $url) {
|
|
app(Kernel::class)->pushMiddleware($middleware);
|
|
|
|
config(['tenancy.tenant_model' => Tenant::class]);
|
|
|
|
Route::get('/foo', [Controller::class, 'index'])
|
|
->middleware(PreventAccessFromUnwantedDomains::class)
|
|
->name('foo');
|
|
|
|
$tenant = Tenant::create();
|
|
|
|
$tenant->domains()->create([
|
|
'domain' => $domain,
|
|
]);
|
|
|
|
$response = pest()->get($url)->assertOk();
|
|
|
|
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
|
})->with([
|
|
'domain identification' => ['middleware' => InitializeTenancyByDomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
|
|
'subdomain identification' => ['middleware' => InitializeTenancyBySubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
|
|
'domainOrSubdomain identification using domain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
|
|
'domainOrSubdomain identification using subdomain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
|
|
]);
|
|
|
|
function assertTenancyInitializedInEarlyIdentificationRequest(string|false $string): void
|
|
{
|
|
expect($string)->toBe(tenant()->getTenantKey() . '-abc123'); // Assert that the service class returns tenant value
|
|
expect(app()->make('additionalMiddlewareRunsInTenantContext'))->toBeTrue(); // Assert that middleware added in the controller constructor runs in tenant context
|
|
expect(app()->make('controllerRunsInTenantContext'))->toBeTrue(); // Assert that tenancy is initialized in the controller constructor
|
|
}
|