mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:54:03 +00:00
Fix origin id w/ empty header & using full-hostname subdomain records
This makes it possible to have Domain records in both `foo` and
`foo.{centralDomain}` format when using the combined domain/subdomain
identification middleware, or the origin header id mw which extends it.
This commit also refactors some related logic.
This commit is contained in:
parent
c199a6e0c8
commit
56dd4117ab
7 changed files with 131 additions and 63 deletions
|
|
@ -6,62 +6,56 @@ use Illuminate\Support\Facades\Route;
|
|||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
||||
use Stancl\Tenancy\Database\Models;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
beforeEach(function () {
|
||||
Route::group([
|
||||
'middleware' => InitializeTenancyByDomainOrSubdomain::class,
|
||||
], function () {
|
||||
Route::get('/foo/{a}/{b}', function ($a, $b) {
|
||||
return "$a + $b";
|
||||
Route::get('/test', function () {
|
||||
return tenant('id');
|
||||
});
|
||||
});
|
||||
|
||||
config(['tenancy.models.tenant' => CombinedTenant::class]);
|
||||
});
|
||||
|
||||
test('tenant can be identified by subdomain', function () {
|
||||
config(['tenancy.identification.central_domains' => ['localhost']]);
|
||||
|
||||
$tenant = CombinedTenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'foo',
|
||||
]);
|
||||
$tenant = Tenant::create(['id' => 'acme']);
|
||||
$tenant->domains()->create(['domain' => 'foo']);
|
||||
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
|
||||
pest()
|
||||
->get('http://foo.localhost/foo/abc/xyz')
|
||||
->assertSee('abc + xyz');
|
||||
|
||||
expect(tenancy()->initialized)->toBeTrue();
|
||||
expect(tenant('id'))->toBe('acme');
|
||||
pest()->get('http://foo.localhost/test')->assertSee('acme');
|
||||
});
|
||||
|
||||
test('tenant can be identified by domain', function () {
|
||||
config(['tenancy.identification.central_domains' => []]);
|
||||
|
||||
$tenant = CombinedTenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'foobar.localhost',
|
||||
]);
|
||||
$tenant = Tenant::create(['id' => 'acme']);
|
||||
$tenant->domains()->create(['domain' => 'foobar.localhost']);
|
||||
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
|
||||
pest()
|
||||
->get('http://foobar.localhost/foo/abc/xyz')
|
||||
->assertSee('abc + xyz');
|
||||
|
||||
expect(tenancy()->initialized)->toBeTrue();
|
||||
expect(tenant('id'))->toBe('acme');
|
||||
pest()->get('http://foobar.localhost/test')->assertSee('acme');
|
||||
});
|
||||
|
||||
class CombinedTenant extends Models\Tenant
|
||||
{
|
||||
use HasDomains;
|
||||
}
|
||||
test('domain records can be either in domain syntax or subdomain syntax', function () {
|
||||
config(['tenancy.identification.central_domains' => ['localhost']]);
|
||||
|
||||
$foo = Tenant::create(['id' => 'foo']);
|
||||
$foo->domains()->create(['domain' => 'foo']);
|
||||
|
||||
$bar = Tenant::create(['id' => 'bar']);
|
||||
$bar->domains()->create(['domain' => 'bar.localhost']);
|
||||
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
|
||||
// Subdomain format
|
||||
pest()->get('http://foo.localhost/test')->assertSee('foo');
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Domain format
|
||||
pest()->get('http://bar.localhost/test')->assertSee('bar');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -38,6 +38,12 @@ test('origin identification works', function () {
|
|||
});
|
||||
|
||||
test('tenant routes are not accessible on central domains while using origin identification', function () {
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'foo',
|
||||
]);
|
||||
|
||||
pest()
|
||||
->withHeader('Origin', 'localhost')
|
||||
->post('home')
|
||||
|
|
@ -54,3 +60,50 @@ test('onfail logic can be customized', function() {
|
|||
->post('home')
|
||||
->assertSee('onFail message');
|
||||
});
|
||||
|
||||
test('origin identification can be used with universal routes', function () {
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'foo',
|
||||
]);
|
||||
|
||||
Route::post('/universal', function () {
|
||||
return response(tenant('id') ?? 'central');
|
||||
})->middleware([InitializeTenancyByOriginHeader::class, 'universal'])->name('universal');
|
||||
|
||||
pest()
|
||||
->withHeader('Origin', 'foo.localhost')
|
||||
->post('universal')
|
||||
->assertSee($tenant->id);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
pest()
|
||||
->withHeader('Origin', 'localhost')
|
||||
->post('universal')
|
||||
->assertSee('central');
|
||||
|
||||
pest()
|
||||
// no header
|
||||
->post('universal')
|
||||
->assertSee('central');
|
||||
});
|
||||
|
||||
test('origin identification can be used with both domains and subdomains', function () {
|
||||
$foo = Tenant::create();
|
||||
$foo->domains()->create(['domain' => 'foo']);
|
||||
|
||||
$bar = Tenant::create();
|
||||
$bar->domains()->create(['domain' => 'bar.localhost']);
|
||||
|
||||
pest()
|
||||
->withHeader('Origin', 'foo.localhost')
|
||||
->post('home')
|
||||
->assertSee($foo->id);
|
||||
|
||||
pest()
|
||||
->withHeader('Origin', 'bar.localhost')
|
||||
->post('home')
|
||||
->assertSee($bar->id);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue