1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:34:04 +00:00

Combined subdomain/domain identification

This commit is contained in:
Samuel Štancl 2020-05-10 19:30:01 +02:00
parent 8ea4940f34
commit f328fc9c08
4 changed files with 141 additions and 4 deletions

View file

@ -0,0 +1,78 @@
<?php
namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Database\Models\Concerns\HasDomains;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
use Stancl\Tenancy\Tests\TestCase;
class CombinedDomainAndSubdomainIdentificationTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Route::group([
'middleware' => InitializeTenancyByDomainOrSubdomain::class,
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
config(['tenancy.tenant_model' => Tenant::class]);
}
/** @test */
public function tenant_can_be_identified_by_subdomain()
{
config(['tenancy.central_domains' => ['localhost']]);
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'foo',
]);
$this->assertFalse(tenancy()->initialized);
$this
->get('http://foo.localhost/foo/abc/xyz')
->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized);
$this->assertSame('acme', tenant('id'));
}
/** @test */
public function tenant_can_be_identified_by_domain()
{
config(['tenancy.central_domains' => []]);
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'foobar.localhost',
]);
$this->assertFalse(tenancy()->initialized);
$this
->get('http://foobar.localhost/foo/abc/xyz')
->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized);
$this->assertSame('acme', tenant('id'));
}
}
class Tenant extends Models\Tenant
{
use HasDomains;
}

View file

@ -15,6 +15,9 @@ class SubdomainTest extends TestCase
{
parent::setUp();
// Global state cleanup after some tests
InitializeTenancyBySubdomain::$onInvalidSubdomain = null;
Route::group([
'middleware' => InitializeTenancyBySubdomain::class,
], function () {
@ -93,6 +96,29 @@ class SubdomainTest extends TestCase
->get('http://127.0.0.1/foo/abc/xyz')
->assertSee('foo custom invalid subdomain handler');
}
/** @test */
public function we_cant_use_a_subdomain_that_doesnt_belong_to_our_central_domains()
{
config(['tenancy.central_domains' => [
'127.0.0.1',
// not 'localhost'
]]);
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'foo',
]);
$this->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://foo.localhost/foo/abc/xyz');
}
}
class Tenant extends Models\Tenant