forked from laravel/echo
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored mock-auth-server, included method to broadcast to others
- Loading branch information
Showing
3 changed files
with
28 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ describe('AblyUserLogin', () => { | |
}); | ||
}); | ||
|
||
test('user logs in without previous (guest) channels', async () => { | ||
test('user logs in without previous (public) channels', async () => { | ||
let connectionStates : Array<any>= [] | ||
// Initial clientId is null | ||
expect(mockAuthServer.clientId).toBeNull(); | ||
|
@@ -70,7 +70,7 @@ describe('AblyUserLogin', () => { | |
expect(echo.connector.ablyAuth.existingToken().clientId).toBe('[email protected]'); | ||
}); | ||
|
||
test('user logs in with previous (guest) channels', async () => { | ||
test('user logs in with previous (public) channels', async () => { | ||
let connectionStates : Array<any>= [] | ||
let publicChannelStates : Array<any>= [] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,49 @@ | ||
import { isNullOrUndefinedOrEmpty, parseJwt } from '../../../src/channel/ably/utils'; | ||
import { isNullOrUndefinedOrEmpty, parseJwt, toBase64, toText } from '../../../src/channel/ably/utils'; | ||
import * as Ably from 'ably/promises'; | ||
import * as jwt from 'jsonwebtoken'; | ||
|
||
type channels = Array<string>; | ||
|
||
/** | ||
* MockAuthServer mimicks {@link https://github.com/ably/laravel-broadcaster/blob/main/src/AblyBroadcaster.php AblyBroadcaster.php}. | ||
* Aim is to keep implementation and behaviour in sync with AblyBroadcaster.php, so that it can be tested | ||
* without running actual PHP server. | ||
*/ | ||
export class MockAuthServer { | ||
keyName: string; | ||
keySecret: string; | ||
ablyClient: Ably.Rest; | ||
clientId: string | null = '[email protected]'; | ||
userInfo = { id: '[email protected]', name: 'sacOO7' }; | ||
|
||
shortLived: channels; | ||
banned: channels; | ||
|
||
userInfo = { id: '[email protected]', name: 'sacOO7' }; // Used for presence | ||
|
||
constructor(apiKey: string, environment = 'sandbox') { | ||
const keys = apiKey.split(':'); | ||
this.keyName = keys[0]; | ||
this.keySecret = keys[1]; | ||
this.ablyClient = new Ably.Rest({ key: apiKey, environment }); | ||
} | ||
|
||
/** | ||
* Broadcast to all clients subscribed to given channel. | ||
*/ | ||
broadcast = async (channelName: string, eventName: string, message: string) => { | ||
await this.ablyClient.channels.get(channelName).publish(eventName, message); | ||
await this.broadcastToOthers("", {channelName, eventName, payload: message}); | ||
}; | ||
|
||
/** | ||
* Broadcast on behalf of a given realtime client. | ||
*/ | ||
broadcastToOthers = async (socketId: string, {channelName, eventName, payload}) => { | ||
let protoMsg = {name: eventName, data: payload}; | ||
if (!isNullOrUndefinedOrEmpty(socketId)) { | ||
const socketIdObj = JSON.parse(toText(socketId)); | ||
protoMsg = {...protoMsg, ...socketIdObj} | ||
} | ||
await this.ablyClient.channels.get(channelName).publish(protoMsg); | ||
}; | ||
|
||
tokenInvalidOrExpired = (serverTime, token) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters