mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 16:24:03 +00:00
BroadcastingConfigBootstrapper: test mapping credentials
Test that BroadcastingConfigBootstrapper correctly maps tenant properties to broadcasting config/credentials, and that the credentials don't leak when switching contexts. Also add the `$config` property to `TestingBroadcaster` so that we can access the credentials used by the broadcaster.
This commit is contained in:
parent
fb654e7a6b
commit
7d749eb592
2 changed files with 44 additions and 1 deletions
|
|
@ -52,6 +52,48 @@ test('bound broadcaster instance is the same before initializing tenancy and aft
|
||||||
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('broadcasting config bootstrapper maps the config to broadcaster credentials correctly', function() {
|
||||||
|
config([
|
||||||
|
'broadcasting.default' => $driver = 'testing',
|
||||||
|
'broadcasting.connections.testing.driver' => $driver,
|
||||||
|
'broadcasting.connections.testing.key' => 'central_key',
|
||||||
|
'tenancy.bootstrappers' => [
|
||||||
|
BroadcastingConfigBootstrapper::class,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
BroadcastingConfigBootstrapper::$credentialsMap['broadcasting.connections.testing.key'] = 'testing_key';
|
||||||
|
|
||||||
|
// Register the testing broadcaster
|
||||||
|
app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing', $config));
|
||||||
|
|
||||||
|
$tenant1 = Tenant::create(['testing_key' => 'tenant1_key']);
|
||||||
|
$tenant2 = Tenant::create(['testing_key' => 'tenant2_key']);
|
||||||
|
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('central_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('central_key');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
// Switching to tenant context makes the currently bound Broadcaster instance use the tenant's config
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('tenant1_key');
|
||||||
|
// The Broadcast facade (used in BroadcastController::authenticate) uses the broadcaster with tenant config
|
||||||
|
// instead of the stale broadcaster instance resolved before tenancy was initialized
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('tenant1_key');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
// Switching to another tenant context makes the current broadcaster use the new tenant's config
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('tenant2_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('tenant2_key');
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// Ending tenancy reverts the broadcaster changes
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('central_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('central_key');
|
||||||
|
});
|
||||||
|
|
||||||
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
||||||
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
||||||
config([
|
config([
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||||
|
|
||||||
class TestingBroadcaster extends Broadcaster {
|
class TestingBroadcaster extends Broadcaster {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $message = 'nothing'
|
public string $message = 'nothing',
|
||||||
|
public array $config = [],
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function auth($request)
|
public function auth($request)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue