Skip to content

Commit

Permalink
Refactored mock-auth-server, included method to broadcast to others
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 20, 2024
1 parent fbb8b63 commit 50fdae4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tests/ably/ably-user-login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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>= []

Expand Down
27 changes: 24 additions & 3 deletions tests/ably/setup/mock-auth-server.ts
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) => {
Expand Down
5 changes: 2 additions & 3 deletions tests/ably/setup/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { httpRequestAsync } from '../../../src/channel/ably/utils';
import { httpRequestAsync} from '../../../src/channel/ably/utils';
export { toBase64 } from '../../../src/channel/ably/utils';

const safeAssert = (assertions: Function, done: Function, finalAssertion = false) => {
try {
Expand All @@ -17,8 +18,6 @@ export const execute = (fn: Function, times: number) => {
}
};

export const toBase64 = (text: string) => Buffer.from(text, 'binary').toString('base64');

export const httpPostAsync = async (url: string, postData: any) => {
postData = JSON.stringify(postData);
let postOptions = {
Expand Down

0 comments on commit 50fdae4

Please sign in to comment.