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

Identification middleware & tests

This commit is contained in:
Samuel Štancl 2020-05-10 05:47:27 +02:00
parent a17727b437
commit 8ea4940f34
18 changed files with 362 additions and 174 deletions

View file

@ -2,10 +2,12 @@
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\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use Stancl\Tenancy\Tests\TestCase;
@ -15,6 +17,14 @@ class DomainTest extends TestCase
{
parent::setUp();
Route::group([
'middleware' => InitializeTenancyByDomain::class,
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
config(['tenancy.tenant_model' => Tenant::class]);
}
@ -46,7 +56,7 @@ class DomainTest extends TestCase
$tenant2 = Tenant::create();
$this->expectException(DomainsOccupiedByOtherTenantException::class);
$this->expectException(DomainOccupiedByOtherTenantException::class);
$tenant2->domains()->create([
'domain' => 'foo.localhost',
]);
@ -61,29 +71,40 @@ class DomainTest extends TestCase
}
/** @test */
public function tenancy_is_initialized_prior_to_controller_constructors()
public function tenant_can_be_identified_by_domain()
{
// todo
$this->assertTrue(app('tenancy_was_initialized_in_constructor'));
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'foo.localhost',
]);
$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 onfail_logic_can_be_customized()
{
InitializeTenancyByDomain::$onFail = function () {
return 'foo';
};
$this
->get('http://foo.localhost/foo/abc/xyz')
->assertSee('foo');
}
}
class Tenant extends Models\Tenant
{
use HasDomains;
}
class TestController
{
public function __construct()
{
app()->instance('tenancy_was_initialized_in_constructor', tenancy()->initialized);
}
public function index()
{
return 'foo';
}
}

View file

@ -10,13 +10,6 @@ use Stancl\Tenancy\Tests\TestCase;
class PathIdentificationTest extends TestCase
{
public function getEnvironmentSetup($app)
{
parent::getEnvironmentSetUp($app);
config(['tenancy.global_middleware' => []]);
}
public function setUp(): void
{
parent::setUp();
@ -28,8 +21,6 @@ class PathIdentificationTest extends TestCase
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
Route::get('/bar', [TestController::class, 'index']);
});
}
@ -42,8 +33,7 @@ class PathIdentificationTest extends TestCase
$this->assertFalse(tenancy()->initialized);
$this
->get('/acme/foo/abc/xyz');
$this->get('/acme/foo/abc/xyz');
$this->assertTrue(tenancy()->initialized);
$this->assertSame('acme', tenant('id'));
@ -77,4 +67,16 @@ class PathIdentificationTest extends TestCase
$this->assertFalse(tenancy()->initialized);
}
/** @test */
public function onfail_logic_can_be_customized()
{
InitializeTenancyByPath::$onFail = function () {
return 'foo';
};
$this
->get('/acme/foo/abc/xyz')
->assertSee('foo');
}
}

101
tests/v3/SubdomainTest.php Normal file
View file

@ -0,0 +1,101 @@
<?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\Exceptions\NotASubdomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Tests\TestCase;
class SubdomainTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Route::group([
'middleware' => InitializeTenancyBySubdomain::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()
{
$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 onfail_logic_can_be_customized()
{
InitializeTenancyBySubdomain::$onFail = function () {
return 'foo';
};
$this
->get('http://foo.localhost/foo/abc/xyz')
->assertSee('foo');
}
/** @test */
public function localhost_is_not_a_valid_subdomain()
{
$this->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://localhost/foo/abc/xyz');
}
/** @test */
public function ip_address_is_not_a_valid_subdomain()
{
$this->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://127.0.0.1/foo/abc/xyz');
}
/** @test */
public function oninvalidsubdomain_logic_can_be_customized()
{
// in this case, we need to return a response instance
// since a string would be treated as the subdomain
InitializeTenancyBySubdomain::$onInvalidSubdomain = function () {
return response('foo custom invalid subdomain handler');
};
$this
->withoutExceptionHandling()
->get('http://127.0.0.1/foo/abc/xyz')
->assertSee('foo custom invalid subdomain handler');
}
}
class Tenant extends Models\Tenant
{
use HasDomains;
}