1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:04:02 +00:00
tenancy/tests/SubdomainTest.php
Abrar Ahmad ff46bcfe20
Early identification support (#1)
* wip

* Improve tests

* rename class

* wip

* improve tests

* introduce early identification middlewares

* Update PreventAccessFromCentralDomains.php

* method rename

* method rename

* Update UniversalRouteTest.php

* Update UniversalRouteTest.php

* Update EarlyIdentificationTest.php

* remove early classes and add check in existing classes

* MWs improvements

* Update UniversalRouteTest.php

* Fix code style (php-cs-fixer)

* trigger ci

* fix failing test after merge

* add test for universal route in early identification

* Update InitializeTenancyByDomain.php

* Update UniversalRouteTest.php

* remove `routeHasMiddleware` method from MW and use the UniversalRoutes class method

* helper dockblock

* add test

* handle universal routes in early identification

* remove UniversalRoute class because we are not using it anymore

* rename class from PreventAccessFromCentralDomains to PreventAccessFromUnwantedDomains

* improvements after self review

* Update PreventAccessFromUnwantedDomains.php

* remove inline class namespaces

* remove DomainTenant class and use the Tenant class

* update comment

* removed custom expection and add method

* Update tests/EarlyIdentificationTest.php

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>

* use ltrim and simplify the comment

* remove comments and typhint

* dataset and keys rename

* rename $route parameter

* removed helper functions

* fix style

* Update InitializeTenancyByPath.php

* Update tests/EarlyIdentificationTest.php

* code style

* improve subdomain test

* use TenancyInitialized event and remove DomainTenant alias

* remove comment and move expectException below

* code style

* use TenancyInitialized event

* improve test

* improve datasets

* Initialized -> Initializing

* Update InitializeTenancyByPath.php

* remove todo

* Fix code style (php-cs-fixer)

* refactor helper method

* Update UniversalRouteTest.php

* add note above test

* remove after each hook

* renamed universal_middleware to global_middleware

* remove helper and improve url names

* change check position

* Revert "change check position"

This reverts commit e4371d2f3aa8ad7ae5e5b7d15781b72a5f1be03c.

* repositioned central check

* add comment

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
2022-11-20 02:31:37 +01:00

113 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Database\Concerns\HasDomains;
use Stancl\Tenancy\Exceptions\NotASubdomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Database\Models;
beforeEach(function () {
// Global state cleanup after some tests
InitializeTenancyBySubdomain::$onFail = null;
Route::group([
'middleware' => InitializeTenancyBySubdomain::class,
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
config(['tenancy.models.tenant' => SubdomainTenant::class]);
});
test('tenant can be identified by subdomain', function () {
$tenant = SubdomainTenant::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');
});
test('onfail logic can be customized', function () {
InitializeTenancyBySubdomain::$onFail = function () {
return response('foo');
};
pest()
->get('http://foo.localhost/foo/abc/xyz')
->assertSee('foo');
});
test('archte.ch is not a valid subdomain', function () {
pest()->expectException(NotASubdomainException::class);
// This gets routed to the app, but with a request domain of 'archte.ch'
$this
->withoutExceptionHandling()
->get('http://archte.ch/foo/abc/xyz');
});
test('ip address is not a valid subdomain', function () {
pest()->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://127.0.0.2/foo/abc/xyz');
});
test('oninvalidsubdomain logic can be customized', function () {
// in this case, we need to return a response instance
// since a string would be treated as the subdomain
InitializeTenancyBySubdomain::$onFail = function ($e) {
if ($e instanceof NotASubdomainException) {
return response('foo custom invalid subdomain handler');
}
throw $e;
};
$this
->withoutExceptionHandling()
->get('http://127.0.0.2/foo/abc/xyz')
->assertSee('foo custom invalid subdomain handler');
});
test('we cant use a subdomain that doesnt belong to our central domains', function () {
config(['tenancy.central_domains' => [
'127.0.0.1',
// not 'localhost'
]]);
$tenant = SubdomainTenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'foo',
]);
pest()->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://foo.localhost/foo/abc/xyz');
});
class SubdomainTenant extends Models\Tenant
{
use HasDomains;
}