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

Merge branch 'master' into cache-prefix

This commit is contained in:
lukinovec 2023-01-31 08:17:22 +01:00 committed by GitHub
commit ba8cfdda85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 631 additions and 362 deletions

View file

@ -180,17 +180,17 @@ test('rollback command works', function () {
expect(Schema::hasTable('users'))->toBeFalse();
});
test('seed command works', function (){
test('seed command works', function () {
$tenant = Tenant::create();
Artisan::call('tenants:migrate');
$tenant->run(function (){
$tenant->run(function () {
expect(DB::table('users')->count())->toBe(0);
});
Artisan::call('tenants:seed', ['--class' => TestSeeder::class]);
$tenant->run(function (){
$tenant->run(function () {
$user = DB::table('users');
expect($user->count())->toBe(1)
->and($user->first()->email)->toBe('seeded@user');

View file

@ -1,68 +0,0 @@
<?php
use Stancl\Tenancy\Enums\LogMode;
use Stancl\Tenancy\Events\EndingTenancy;
use Stancl\Tenancy\Events\InitializingTenancy;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Tests\Etc\Tenant;
test('tenancy can log events silently', function () {
tenancy()->log(LogMode::SILENT);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
tenancy()->end();
assertTenancyInitializedAndEnded(tenancy()->getLog(), $tenant);
});
test('tenancy logs event silently by default', function () {
tenancy()->log();
expect(tenancy()->logMode())->toBe(LogMode::SILENT);
});
test('the log can be dumped', function (string $method) {
tenancy()->log();
$tenant = Tenant::create();
tenancy()->initialize($tenant);
tenancy()->end();
$output = [];
tenancy()->$method(function ($data) use (&$output) {
$output = $data;
});
assertTenancyInitializedAndEnded($output, $tenant);
})->with([
'dump',
'dd',
]);
test('tenancy can log events immediately', function () {
// todo implement
pest()->markTestIncomplete();
});
// todo test the different behavior of the methods in different contexts, or get rid of the logic and simplify it
function assertTenancyInitializedAndEnded(array $log, Tenant $tenant): void
{
expect($log)->toHaveCount(4);
expect($log[0]['event'])->toBe(InitializingTenancy::class);
expect($log[0]['tenant'])->toBe($tenant);
expect($log[1]['event'])->toBe(TenancyInitialized::class);
expect($log[1]['tenant'])->toBe($tenant);
expect($log[2]['event'])->toBe(EndingTenancy::class);
expect($log[2]['tenant'])->toBe($tenant);
expect($log[3]['event'])->toBe(TenancyEnded::class);
expect($log[3]['tenant'])->toBe($tenant);
}

View file

@ -9,7 +9,7 @@ beforeEach(function () {
config(['tenancy.models.tenant' => DatabaseAndDomainTenant::class]);
});
test('job delete domains successfully', function (){
test('job delete domains successfully', function () {
$tenant = DatabaseAndDomainTenant::create();
$tenant->domains()->create([

View file

@ -8,7 +8,6 @@ use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Database\Models\Domain;
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Features\UniversalRoutes;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
@ -95,7 +94,6 @@ test('throw correct exception when onFail is null and universal routes are enabl
// Enable UniversalRoute feature
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);
$this->withoutExceptionHandling()->get('http://foo.localhost/foo/abc/xyz');
})->throws(TenantCouldNotBeIdentifiedOnDomainException::class);;

View file

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
use Stancl\Tenancy\Tests\Etc\EarlyIdentification\Controller;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
config()->set([
'tenancy.token' => 'central-abc123',
]);
Event::listen(TenancyInitialized::class, function (TenancyInitialized $event) {
config()->set([
'tenancy.token' => $event->tenancy->tenant->getTenantKey() . '-abc123',
]);
});
});
test('early identification works with path identification', function () {
app(Kernel::class)->pushMiddleware(InitializeTenancyByPath::class);
Route::group([
'prefix' => '/{tenant}',
], function () {
Route::get('/foo', [Controller::class, 'index'])->name('foo');
});
Tenant::create([
'id' => 'acme',
]);
$response = pest()->get('/acme/foo')->assertOk();
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
// check if default parameter feature is working fine by asserting that the route WITHOUT the tenant parameter
// matches the route WITH the tenant parameter
expect(route('foo'))->toBe(route('foo', ['tenant' => 'acme']));
});
test('early identification works with request data identification', function (string $type) {
app(Kernel::class)->pushMiddleware(InitializeTenancyByRequestData::class);
Route::get('/foo', [Controller::class, 'index'])->name('foo');
$tenant = Tenant::create([
'id' => 'acme',
]);
if ($type === 'header') {
$response = pest()->get('/foo', ['X-Tenant' => $tenant->id])->assertOk();
} elseif ($type === 'queryParameter') {
$response = pest()->get("/foo?tenant=$tenant->id")->assertOk();
}
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
})->with([
'using request header parameter' => 'header',
'using request query parameter' => 'queryParameter'
]);
// The name of this test is suffixed by the dataset — domain / subdomain / domainOrSubdomain identification
test('early identification works', function (string $middleware, string $domain, string $url) {
app(Kernel::class)->pushMiddleware($middleware);
config(['tenancy.tenant_model' => Tenant::class]);
Route::get('/foo', [Controller::class, 'index'])
->middleware(PreventAccessFromUnwantedDomains::class)
->name('foo');
$tenant = Tenant::create();
$tenant->domains()->create([
'domain' => $domain,
]);
$response = pest()->get($url)->assertOk();
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
})->with([
'domain identification' => ['middleware' => InitializeTenancyByDomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
'subdomain identification' => ['middleware' => InitializeTenancyBySubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
'domainOrSubdomain identification using domain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
'domainOrSubdomain identification using subdomain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
]);
function assertTenancyInitializedInEarlyIdentificationRequest(string|false $string): void
{
expect($string)->toBe(tenant()->getTenantKey() . '-abc123'); // Assert that the service class returns tenant value
expect(app()->make('additionalMiddlewareRunsInTenantContext'))->toBeTrue(); // Assert that middleware added in the controller constructor runs in tenant context
expect(app()->make('controllerRunsInTenantContext'))->toBeTrue(); // Assert that tenancy is initialized in the controller constructor
}

View file

@ -0,0 +1,16 @@
<?php
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
use Closure;
use Illuminate\Http\Request;
class AdditionalMiddleware
{
public function handle(Request $request, Closure $next): mixed
{
app()->instance('additionalMiddlewareRunsInTenantContext', tenancy()->initialized);
return $next($request);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
public function __construct(public Service $service)
{
app()->instance('controllerRunsInTenantContext', tenancy()->initialized);
$this->middleware(AdditionalMiddleware::class);
}
public function index(): string
{
return $this->service->token;
}
}

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
class Service
{
public string $token;
public function __construct()
{
$this->token = config('tenancy.token');
}
}

View file

@ -30,7 +30,7 @@ class HttpKernel extends Kernel
*/
protected $middlewareGroups = [
'web' => [
\Orchestra\Testbench\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity')->index();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sessions');
}
}

View file

@ -0,0 +1,145 @@
<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Listeners;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Tests\Etc\Tenant;
/**
* This collection of regression tests verifies that SessionTenancyBootstrapper
* fully fixes the issue described here https://github.com/archtechx/tenancy/issues/547
*
* This means: using the DB session driver and:
* 1) switching to the central context from tenant requests, OR
* 2) switching to the tenant context from central requests
*/
beforeEach(function () {
config(['session.driver' => 'database']);
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
Event::listen(
TenantCreated::class,
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(Events\TenancyInitialized::class, Listeners\BootstrapTenancy::class);
Event::listen(Events\TenancyEnded::class, Listeners\RevertToCentralContext::class);
// Sessions table for central database
pest()->artisan('migrate', [
'--path' => __DIR__ . '/Etc/session_migrations',
'--realpath' => true,
])->assertExitCode(0);
});
test('central helper can be used in tenant requests', function (bool $enabled, bool $shouldThrow) {
if ($enabled) {
config()->set(
'tenancy.bootstrappers',
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
);
}
$tenant = Tenant::create();
$tenant->domains()->create(['domain' => 'foo.localhost']);
// run for tenants
pest()->artisan('tenants:migrate', [
'--path' => __DIR__ . '/Etc/session_migrations',
'--realpath' => true,
])->assertExitCode(0);
Route::middleware(['web', InitializeTenancyByDomain::class])->get('/bar', function () {
session(['message' => 'tenant session']);
tenancy()->central(function () {
return 'central results';
});
return session('message');
});
// We initialize tenancy before making the request, since sessions work a bit differently in tests
// and we need the DB session handler to use the tenant connection (as it does in a real app on tenant requests).
tenancy()->initialize($tenant);
try {
$this->withoutExceptionHandling()
->get('http://foo.localhost/bar')
->assertOk()
->assertSee('tenant session');
if ($shouldThrow) {
pest()->fail('Exception not thrown');
}
} catch (Throwable $e) {
if ($shouldThrow) {
pest()->assertTrue(true); // empty assertion to make the test pass
} else {
pest()->fail('Exception thrown: ' . $e->getMessage());
}
}
})->with([
['enabled' => false, 'shouldThrow' => true],
['enabled' => true, 'shouldThrow' => false],
]);
test('tenant run helper can be used on central requests', function (bool $enabled, bool $shouldThrow) {
if ($enabled) {
config()->set(
'tenancy.bootstrappers',
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
);
}
Tenant::create();
// run for tenants
pest()->artisan('tenants:migrate', [
'--path' => __DIR__ . '/Etc/session_migrations',
'--realpath' => true,
])->assertExitCode(0);
Route::middleware(['web'])->get('/bar', function () {
session(['message' => 'central session']);
Tenant::first()->run(function () {
return 'tenant results';
});
return session('message');
});
try {
$this->withoutExceptionHandling()
->get('http://localhost/bar')
->assertOk()
->assertSee('central session');
if ($shouldThrow) {
pest()->fail('Exception not thrown');
}
} catch (Throwable $e) {
if ($shouldThrow) {
pest()->assertTrue(true); // empty assertion to make the test pass
} else {
pest()->fail('Exception thrown: ' . $e->getMessage());
}
}
})->with([
['enabled' => false, 'shouldThrow' => true],
['enabled' => true, 'shouldThrow' => false],
]);

View file

@ -52,12 +52,13 @@ test('onfail logic can be customized', function () {
->assertSee('foo');
});
test('localhost is not a valid subdomain', function () {
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://localhost/foo/abc/xyz');
->get('http://archte.ch/foo/abc/xyz');
});
test('ip address is not a valid subdomain', function () {
@ -65,7 +66,7 @@ test('ip address is not a valid subdomain', function () {
$this
->withoutExceptionHandling()
->get('http://127.0.0.1/foo/abc/xyz');
->get('http://127.0.0.2/foo/abc/xyz');
});
test('oninvalidsubdomain logic can be customized', function () {
@ -81,7 +82,7 @@ test('oninvalidsubdomain logic can be customized', function () {
$this
->withoutExceptionHandling()
->get('http://127.0.0.1/foo/abc/xyz')
->get('http://127.0.0.2/foo/abc/xyz')
->assertSee('foo custom invalid subdomain handler');
});
@ -106,26 +107,6 @@ test('we cant use a subdomain that doesnt belong to our central domains', functi
->get('http://foo.localhost/foo/abc/xyz');
});
test('central domain is not a subdomain', function () {
config(['tenancy.central_domains' => [
'localhost',
]]);
$tenant = SubdomainTenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'acme',
]);
pest()->expectException(NotASubdomainException::class);
$this
->withoutExceptionHandling()
->get('http://localhost/foo/abc/xyz');
});
class SubdomainTenant extends Models\Tenant
{
use HasDomains;

View file

@ -3,27 +3,24 @@
declare(strict_types=1);
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Features\UniversalRoutes;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Contracts\Http\Kernel;
afterEach(function () {
InitializeTenancyByDomain::$onFail = null;
});
test('a route can work in both central and tenant context', function (array $routeMiddleware, string|null $globalMiddleware) {
if ($globalMiddleware) {
app(Kernel::class)->pushMiddleware($globalMiddleware);
}
test('a route can work in both central and tenant context', function () {
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);
Route::get('/foo', function () {
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
})->middleware(['universal', InitializeTenancyByDomain::class]);
pest()->get('http://localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is not initialized.');
})->middleware($routeMiddleware);
$tenant = Tenant::create([
'id' => 'acme',
@ -32,28 +29,33 @@ test('a route can work in both central and tenant context', function () {
'domain' => 'acme.localhost',
]);
pest()->get('http://acme.localhost/foo')
pest()->get("http://localhost/foo")
->assertSuccessful()
->assertSee('Tenancy is not initialized.');
pest()->get("http://acme.localhost/foo")
->assertSuccessful()
->assertSee('Tenancy is initialized.');
});
})->with('identification types');
test('making one route universal doesnt make all routes universal', function () {
Route::get('/bar', function () {
return tenant('id');
})->middleware(InitializeTenancyByDomain::class);
test('making one route universal doesnt make all routes universal', function (array $routeMiddleware, string|null $globalMiddleware) {
if ($globalMiddleware) {
app(Kernel::class)->pushMiddleware($globalMiddleware);
}
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);
Route::get('/foo', function () {
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
})->middleware(['universal', InitializeTenancyByDomain::class]);
Route::middleware($routeMiddleware)->group(function () {
Route::get('/nonuniversal', function () {
return tenant('id');
});
pest()->get('http://localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is not initialized.');
Route::get('/universal', function () {
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
})->middleware('universal');
});
$tenant = Tenant::create([
'id' => 'acme',
@ -62,16 +64,57 @@ test('making one route universal doesnt make all routes universal', function ()
'domain' => 'acme.localhost',
]);
pest()->get('http://acme.localhost/foo')
pest()->get("http://localhost/universal")
->assertSuccessful()
->assertSee('Tenancy is not initialized.');
pest()->get("http://acme.localhost/universal")
->assertSuccessful()
->assertSee('Tenancy is initialized.');
tenancy()->end();
pest()->get('http://localhost/bar')
->assertStatus(500);
pest()->get('http://localhost/nonuniversal')
->assertStatus(404);
pest()->get('http://acme.localhost/bar')
pest()->get('http://acme.localhost/nonuniversal')
->assertSuccessful()
->assertSee('acme');
});
})->with([
'early identification' => [
'route_middleware' => [PreventAccessFromUnwantedDomains::class],
'global_middleware' => InitializeTenancyByDomain::class,
],
'route-level identification' => [
'route_middleware' => [PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
'global_middleware' => null,
]
]);
test('it throws correct exception when route is universal and tenant does not exist', function (array $routeMiddleware, string|null $globalMiddleware) {
if ($globalMiddleware) {
app(Kernel::class)->pushMiddleware($globalMiddleware);
}
Route::middlewareGroup('universal', []);
Route::get('/foo', function () {
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
})->middleware($routeMiddleware);
pest()->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
$this->withoutExceptionHandling()->get('http://acme.localhost/foo');
})->with('identification types');
dataset('identification types', [
'early identification' => [
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class],
'global_middleware' => InitializeTenancyByDomain::class,
],
'route-level identification' => [
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
'global_middleware' => null,
]
]);