1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:34:03 +00:00
tenancy/tests/SubdomainTest.php
Samuel Štancl eecf6f21c8
Cache prefixing logic rewrite, session scoping improvements, tests refactor (#43)
* Run cache tests on all supported drivers

* update ci healthcheck for memcached

* remove memcached healthcheck

* fix typos in test comments, expand internal.md [ci skip]

* add empty line [ci skip]

* switch to using $store->setPrefix()

* add dynamodb

* refactor try-finally to try-catch

* remove unnecessary clearResolvedInstances() call

* add dual Cache:: and cache() assertions

* add apc

* Flush APCu cache in test setup

* Revert "add dual Cache:: and cache() assertions"

This reverts commit a0bab162fbe2dd0d25e7056ceca4fb7ce54efc77.

* phpstan fix

* Add logic for scoping 'file' disks to FilesystemTenancyBootstrapper

* minor changes, add todos

* refactor how the session.connection is used in the DB session bootstrapper

* add session forgery prevention logic to the db session bootstrapper

* only use the fs bootstrapper for file disk in 'cache data is separated' dataset

* minor session scoping test changes

* Add session scoping logic to FilesystemTenancyBootstrapper, correctly update disk roots even with storage_path_tenancy disabled

* Fix code style (php-cs-fixer)

* update docblock

* make not-null check more explicit

* separate bootstrapper tests, fix swapped test names for two tests

* refactor cache bootstrapper tests

* resolve global cache todo

* expand tests: session separation tests, more filesystem separation assertions; change prefix_base-type config keys to templates/formats

* add apc session scoping test, various session separation bugfixes

* phpstan + minor logic fixes

* prefix_format -> prefix

* fix database session separation test

* revert composer.json changes, update laravel dependencies to expected next release

* only run session scoping logic in cache bootstrapper for redis, memcached, dynamodb, apc; update gitattributes

* tenancy.central_domains -> tenancy.identification.central_domains

* db session separation test: add datasets

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
2024-04-09 20:40:27 +02: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.identification.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;
}