1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 05:54:03 +00:00

Test that the authenticated user resolvers get copied to tenant broadcasters

Currently, the test will fail because the resolvers don't get copied over to tenant broadcasters.
This commit is contained in:
lukinovec 2026-07-16 12:47:02 +02:00
parent 9efebf964c
commit 50afc49d2b

View file

@ -220,7 +220,7 @@ test('tenant broadcast manager receives the custom driver creators of the centra
expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers); expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers);
}); });
test('tenant broadcasters receive the channel auth closures from the broadcaster bound in central context', function () { test('tenant broadcasters receive the auth state of the broadcaster bound in central context', function () {
config([ config([
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class], 'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
'broadcasting.default' => 'testing', 'broadcasting.default' => 'testing',
@ -233,12 +233,20 @@ test('tenant broadcasters receive the channel auth closures from the broadcaster
app(BroadcastManager::class)->extend('testing', fn () => new TestingBroadcaster('testing')); app(BroadcastManager::class)->extend('testing', fn () => new TestingBroadcaster('testing'));
$getCurrentChannelsFromBoundBroadcaster = fn () => array_keys(invade(app(BroadcasterContract::class))->channels); $getCurrentChannelsFromBoundBroadcaster = fn () => array_keys(invade(app(BroadcasterContract::class))->channels);
$getCurrentChannelsThroughManager = fn () => array_keys(invade(app(BroadcastManager::class)->driver())->channels); $getCurrentChannelsThroughManager = fn () => array_keys(invade(app(BroadcastManager::class)->driver())->channels);
$resolveUser = fn () => app(BroadcasterContract::class)->resolveAuthenticatedUser(request());
Broadcast::channel($channel = 'testing-channel', $callback = fn () => true, $options = ['guards' => ['web']]); Broadcast::channel($channel = 'testing-channel', $callback = fn () => true, $options = ['guards' => ['web']]);
// resolveAuthenticatedUserUsing() sets the broadcaster's $authenticatedUserCallback, which should be
// copied to tenant broadcasters along with the channels. User authentication (/broadcasting/user-auth)
// only works when this callback is registered (403 otherwise), so an app that registers it in central
// context would get 403s from user auth in tenant context if the callback wasn't copied.
Broadcast::resolveAuthenticatedUserUsing(fn () => ['id' => 'central-user']);
expect($channel) expect($channel)
->toBeIn($getCurrentChannelsThroughManager()) ->toBeIn($getCurrentChannelsThroughManager())
->toBeIn($getCurrentChannelsFromBoundBroadcaster()); ->toBeIn($getCurrentChannelsFromBoundBroadcaster());
expect($resolveUser())->toBe(['id' => 'central-user']);
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
@ -246,21 +254,25 @@ test('tenant broadcasters receive the channel auth closures from the broadcaster
->toBeIn($getCurrentChannelsThroughManager()) ->toBeIn($getCurrentChannelsThroughManager())
->toBeIn($getCurrentChannelsFromBoundBroadcaster()); ->toBeIn($getCurrentChannelsFromBoundBroadcaster());
// The channel auth closure and the channel options are copied to the tenant broadcaster as-is // The channel auth closure, the channel options, and the authenticated user resolver
// are copied to the tenant broadcaster as-is
expect(invade(app(BroadcasterContract::class))->channels[$channel])->toBe($callback); expect(invade(app(BroadcasterContract::class))->channels[$channel])->toBe($callback);
expect(invade(app(BroadcasterContract::class))->retrieveChannelOptions($channel))->toBe($options); expect(invade(app(BroadcasterContract::class))->retrieveChannelOptions($channel))->toBe($options);
expect($resolveUser())->toBe(['id' => 'central-user']);
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
expect($channel) expect($channel)
->toBeIn($getCurrentChannelsThroughManager()) ->toBeIn($getCurrentChannelsThroughManager())
->toBeIn($getCurrentChannelsFromBoundBroadcaster()); ->toBeIn($getCurrentChannelsFromBoundBroadcaster());
expect($resolveUser())->toBe(['id' => 'central-user']);
tenancy()->end(); tenancy()->end();
expect($channel) expect($channel)
->toBeIn($getCurrentChannelsThroughManager()) ->toBeIn($getCurrentChannelsThroughManager())
->toBeIn($getCurrentChannelsFromBoundBroadcaster()); ->toBeIn($getCurrentChannelsFromBoundBroadcaster());
expect($resolveUser())->toBe(['id' => 'central-user']);
}); });
test('channels registered in tenant context persist within that context but do not leak into other contexts', function () { test('channels registered in tenant context persist within that context but do not leak into other contexts', function () {