|
220 | 220 | expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers); |
221 | 221 | }); |
222 | 222 |
|
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 () { |
224 | 224 | config([ |
225 | 225 | 'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class], |
226 | 226 | 'broadcasting.default' => 'testing', |
|
233 | 233 | app(BroadcastManager::class)->extend('testing', fn () => new TestingBroadcaster('testing')); |
234 | 234 | $getCurrentChannelsFromBoundBroadcaster = fn () => array_keys(invade(app(BroadcasterContract::class))->channels); |
235 | 235 | $getCurrentChannelsThroughManager = fn () => array_keys(invade(app(BroadcastManager::class)->driver())->channels); |
| 236 | + $resolveUser = fn () => app(BroadcasterContract::class)->resolveAuthenticatedUser(request()); |
236 | 237 |
|
237 | 238 | Broadcast::channel($channel = 'testing-channel', $callback = fn () => true, $options = ['guards' => ['web']]); |
238 | 239 |
|
| 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 | + |
239 | 246 | expect($channel) |
240 | 247 | ->toBeIn($getCurrentChannelsThroughManager()) |
241 | 248 | ->toBeIn($getCurrentChannelsFromBoundBroadcaster()); |
| 249 | + expect($resolveUser())->toBe(['id' => 'central-user']); |
242 | 250 |
|
243 | 251 | tenancy()->initialize($tenant1); |
244 | 252 |
|
245 | 253 | expect($channel) |
246 | 254 | ->toBeIn($getCurrentChannelsThroughManager()) |
247 | 255 | ->toBeIn($getCurrentChannelsFromBoundBroadcaster()); |
248 | 256 |
|
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 |
250 | 259 | expect(invade(app(BroadcasterContract::class))->channels[$channel])->toBe($callback); |
251 | 260 | expect(invade(app(BroadcasterContract::class))->retrieveChannelOptions($channel))->toBe($options); |
| 261 | + expect($resolveUser())->toBe(['id' => 'central-user']); |
252 | 262 |
|
253 | 263 | tenancy()->initialize($tenant2); |
254 | 264 |
|
255 | 265 | expect($channel) |
256 | 266 | ->toBeIn($getCurrentChannelsThroughManager()) |
257 | 267 | ->toBeIn($getCurrentChannelsFromBoundBroadcaster()); |
| 268 | + expect($resolveUser())->toBe(['id' => 'central-user']); |
258 | 269 |
|
259 | 270 | tenancy()->end(); |
260 | 271 |
|
261 | 272 | expect($channel) |
262 | 273 | ->toBeIn($getCurrentChannelsThroughManager()) |
263 | 274 | ->toBeIn($getCurrentChannelsFromBoundBroadcaster()); |
| 275 | + expect($resolveUser())->toBe(['id' => 'central-user']); |
264 | 276 | }); |
265 | 277 |
|
266 | 278 | test('channels registered in tenant context persist within that context but do not leak into other contexts', function () { |
|
0 commit comments