mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 02:54:03 +00:00
Fix encrypted cookie identification test
This commit is contained in:
parent
da30e1ee5c
commit
dec9350643
1 changed files with 7 additions and 22 deletions
|
|
@ -9,6 +9,7 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
|
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
use Illuminate\Cookie\CookieValuePrefix;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
|
|
@ -97,30 +98,14 @@ test('cookie identification works', function (string|null $tenantModelColumn) {
|
||||||
expect(fn () => $this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
expect(fn () => $this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
||||||
})->with([null, 'slug']);
|
})->with([null, 'slug']);
|
||||||
|
|
||||||
test('encrypted cookie identification works', function (string|null $tenantModelColumn) {
|
test('cookie identification works with encrypted cookies', function () {
|
||||||
if ($tenantModelColumn) {
|
$tenant = Tenant::create(['id' => 'acme']);
|
||||||
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]);
|
// Mimic MakesHttpRequests cookie encryption
|
||||||
|
$encryptedCookie = encrypt(CookieValuePrefix::create('tenant', app('encrypter')->getKey()) . 'acme', false);
|
||||||
|
|
||||||
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
|
$this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $encryptedCookie)->get('test')->assertSee($tenant->id);
|
||||||
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
|
});
|
||||||
|
|
||||||
// Default encrypted cookie name
|
|
||||||
$this->withoutExceptionHandling()->withCookie('tenant', $payload)->get('test')->assertSee($tenant->id);
|
|
||||||
|
|
||||||
// Custom encrypted cookie name
|
|
||||||
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => 'custom_tenant_id']);
|
|
||||||
$this->withoutExceptionHandling()->withCookie('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()->withCookie('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 () {
|
test('an exception is thrown when no tenant data is provided in the request', function () {
|
||||||
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue