mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
* Test encrypted cookie identification * Add Fortify bootstrapper custom query param passing test * Correct Fortify route bootstrapper test (todo refactor, convoluted) * Clarify Fortify bootstrapper test * Fix encrypted cookie identification test * Move encrypted cookie assertion to "cookie identification works" * Cover configured tenant model columns in cached resolver tests * Refactor testing resolver with default vs custom tenant model name config * Delete resolved todo * Make code more concise * Keep initial formatting (minimize diff noise) * Make dataset/helper method parameter clearer * Clarify fortify test * Clarify assertions, improve comments * Delete excessive comments, make existing comments consistent and clearer * Make cached resolver test file clearer, update outdated comments * Use the tenant model column term consistently * FIx inconsistencies * Provide more info in comment * make comment more clear * static property reset --------- Co-authored-by: Samuel Štancl <samuel@archte.ch>
109 lines
5.4 KiB
PHP
109 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
|
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
|
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
use function Stancl\Tenancy\Tests\pest;
|
|
use Illuminate\Cookie\CookieValuePrefix;
|
|
|
|
beforeEach(function () {
|
|
config([
|
|
'tenancy.identification.central_domains' => [
|
|
'localhost',
|
|
],
|
|
]);
|
|
|
|
Route::middleware([InitializeTenancyByRequestData::class])->get('/test', function () {
|
|
return 'Tenant id: ' . tenant('id');
|
|
});
|
|
});
|
|
|
|
test('header identification works', function (string|null $tenantModelColumn) {
|
|
if ($tenantModelColumn) {
|
|
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
|
|
$table->string($tenantModelColumn)->unique();
|
|
});
|
|
Tenant::$extraCustomColumns = [$tenantModelColumn];
|
|
}
|
|
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
|
|
|
|
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
|
|
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
|
|
|
|
// Default header name
|
|
$this->withoutExceptionHandling()->withHeader('X-Tenant', $payload)->get('test')->assertSee($tenant->id);
|
|
|
|
// Custom header name
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => 'X-Custom-Tenant']);
|
|
$this->withoutExceptionHandling()->withHeader('X-Custom-Tenant', $payload)->get('test')->assertSee($tenant->id);
|
|
|
|
// Setting the header to null disables header identification
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => null]);
|
|
expect(fn () => $this->withoutExceptionHandling()->withHeader('X-Tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
|
})->with([null, 'slug']);
|
|
|
|
test('query parameter identification works', function (string|null $tenantModelColumn) {
|
|
if ($tenantModelColumn) {
|
|
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
|
|
$table->string($tenantModelColumn)->unique();
|
|
});
|
|
Tenant::$extraCustomColumns = [$tenantModelColumn];
|
|
}
|
|
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
|
|
|
|
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
|
|
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
|
|
|
|
// Default query parameter name
|
|
$this->withoutExceptionHandling()->get('test?tenant=' . $payload)->assertSee($tenant->id);
|
|
|
|
// Custom query parameter name
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => 'custom_tenant']);
|
|
$this->withoutExceptionHandling()->get('test?custom_tenant=' . $payload)->assertSee($tenant->id);
|
|
|
|
// Setting the query parameter to null disables query parameter identification
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => null]);
|
|
expect(fn () => $this->withoutExceptionHandling()->get('test?tenant=' . $payload))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
|
})->with([null, 'slug']);
|
|
|
|
test('cookie identification works', function (string|null $tenantModelColumn) {
|
|
if ($tenantModelColumn) {
|
|
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
|
|
$table->string($tenantModelColumn)->unique();
|
|
});
|
|
Tenant::$extraCustomColumns = [$tenantModelColumn];
|
|
}
|
|
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
|
|
|
|
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
|
|
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
|
|
|
|
// Default cookie name
|
|
$this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test')->assertSee($tenant->id);
|
|
|
|
// Manually encrypted cookie (encrypt the cookie exactly like MakesHttpRequests does in prepareCookiesForRequest())
|
|
$encryptedPayload = encrypt(CookieValuePrefix::create('tenant', app('encrypter')->getKey()) . $payload, false);
|
|
|
|
$this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $encryptedPayload)->get('test')->assertSee($tenant->id);
|
|
|
|
// Custom cookie name
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => 'custom_tenant_id']);
|
|
$this->withoutExceptionHandling()->withUnencryptedCookie('custom_tenant_id', $payload)->get('test')->assertSee($tenant->id);
|
|
|
|
// Setting the cookie to null disables cookie identification
|
|
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => null]);
|
|
expect(fn () => $this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
|
})->with([null, 'slug']);
|
|
|
|
test('an exception is thrown when no tenant data is provided in the request', function () {
|
|
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
|
$this->withoutExceptionHandling()->get('test');
|
|
});
|