Skip to content

Commit 50afc49

Browse files
committed
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.
1 parent 9efebf9 commit 50afc49

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

tests/Bootstrappers/BroadcastingConfigBootstrapperTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@
220220
expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers);
221221
});
222222

223-
test('tenant broadcasters receive the channel auth closures from the broadcaster bound in central context', function () {
223+
test('tenant broadcasters receive the auth state of the broadcaster bound in central context', function () {
224224
config([
225225
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
226226
'broadcasting.default' => 'testing',
@@ -233,34 +233,46 @@
233233
app(BroadcastManager::class)->extend('testing', fn () => new TestingBroadcaster('testing'));
234234
$getCurrentChannelsFromBoundBroadcaster = fn () => array_keys(invade(app(BroadcasterContract::class))->channels);
235235
$getCurrentChannelsThroughManager = fn () => array_keys(invade(app(BroadcastManager::class)->driver())->channels);
236+
$resolveUser = fn () => app(BroadcasterContract::class)->resolveAuthenticatedUser(request());
236237

237238
Broadcast::channel($channel = 'testing-channel', $callback = fn () => true, $options = ['guards' => ['web']]);
238239

240+
// resolveAuthenticatedUserUsing() sets the broadcaster's $authenticatedUserCallback, which should be
241+
// copied to tenant broadcasters along with the channels. User authentication (/broadcasting/user-auth)
242+
// only works when this callback is registered (403 otherwise), so an app that registers it in central
243+
// context would get 403s from user auth in tenant context if the callback wasn't copied.
244+
Broadcast::resolveAuthenticatedUserUsing(fn () => ['id' => 'central-user']);
245+
239246
expect($channel)
240247
->toBeIn($getCurrentChannelsThroughManager())
241248
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
249+
expect($resolveUser())->toBe(['id' => 'central-user']);
242250

243251
tenancy()->initialize($tenant1);
244252

245253
expect($channel)
246254
->toBeIn($getCurrentChannelsThroughManager())
247255
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
248256

249-
// The channel auth closure and the channel options are copied to the tenant broadcaster as-is
257+
// The channel auth closure, the channel options, and the authenticated user resolver
258+
// are copied to the tenant broadcaster as-is
250259
expect(invade(app(BroadcasterContract::class))->channels[$channel])->toBe($callback);
251260
expect(invade(app(BroadcasterContract::class))->retrieveChannelOptions($channel))->toBe($options);
261+
expect($resolveUser())->toBe(['id' => 'central-user']);
252262

253263
tenancy()->initialize($tenant2);
254264

255265
expect($channel)
256266
->toBeIn($getCurrentChannelsThroughManager())
257267
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
268+
expect($resolveUser())->toBe(['id' => 'central-user']);
258269

259270
tenancy()->end();
260271

261272
expect($channel)
262273
->toBeIn($getCurrentChannelsThroughManager())
263274
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
275+
expect($resolveUser())->toBe(['id' => 'central-user']);
264276
});
265277

266278
test('channels registered in tenant context persist within that context but do not leak into other contexts', function () {

0 commit comments

Comments
 (0)