Skip to content

Commit

Permalink
Merge branch 'main' into MFOS_FedData
Browse files Browse the repository at this point in the history
  • Loading branch information
kdivya153 authored May 21, 2024
2 parents e5325fc + d8123ea commit 368b2db
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 54 deletions.
2 changes: 1 addition & 1 deletion functional/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "NODE_ENV=test npx jest --config=jest.config.js --silent -i",
"test:openRpcCall": "NODE_ENV=test npx jest --config=jest.config.js --forceExit -- ./tests/openRpcCall.test.mjs",
"test:healthCheck": "NODE_ENV=test npx jest --config=jest.config.js --forceExit -- ./tests/health-check.test.mjs",
"test:scope": "NODE_ENV=test npx jest --config=jest.config.js --forceExit -- ./tests/scope.test.mjs"
"test:scope": "NODE_ENV=test npx jest --config=jest.config.js --forceExit -- ./tests/scope.test.mjs"
},
"repository": {
"type": "git",
Expand Down
52 changes: 31 additions & 21 deletions functional/tests/openRpcCall.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,41 @@ test(`MF Startup/Health Check`, async () => {

// JsonRpc for CORE SDK
test(`Validate OPENRPC Response for CORE SDK`, async () => {
const response = await utilities.fireboltCommand(
const commander = new utilities.FireboltCommander();
const response = await commander.sendCommand(
JSON.stringify({
method: "Accessibility.closedCaptionsSettings",
params: {},
id: 0,
})
);
}));
commander.closeConnection();

expect(response.includes('"enabled":true')).toEqual(true);
});

test(`Validate OPENRPC Response for manage SDK`, async () => {
const response = await utilities.fireboltCommand(
const commander = new utilities.FireboltCommander();
const response = await commander.sendCommand(
JSON.stringify({
method: "UserGrants.device",
params: {},
id: 0,
})
);
}));
commander.closeConnection();

expect(response.includes(`"state":"granted"`)).toEqual(true);
});

test(`Validate firebolt response for Discovery SDK`, async () => {
const response = await utilities.fireboltCommand(
const commander = new utilities.FireboltCommander();
const response = await commander.sendCommand(
JSON.stringify({
method: "content.providers",
params: {},
id: 0,
})
);
}));
commander.closeConnection();

expect(response.includes(`"id":"NetflixApp"`)).toEqual(true);
});

Expand Down Expand Up @@ -166,41 +172,45 @@ test(`Validate start and stop session`, async () => {

// Send event for default user
test(`Validate send event for default user`, async () => {
await utilities.fireboltCommand(
const commander = new utilities.FireboltCommander();
await commander.sendCommand(
JSON.stringify({
method: "Accessibility.onVoiceGuidanceSettingsChanged",
params: { listen: true },
params: { listen: true},
id: 4,
})
);
}));

const result = await utilities.callMfCli(
`cd ../cli/src/ && node cli.mjs --event ../examples/accessibility-onVoiceGuidanceSettingsChanged1.event.json && cd ../../functional`,
true
);
expect(result.includes(`{ status: 'SUCCESS' }`)).toBe(true);
commander.closeConnection();
});

// Broadcast event for a particular user
test(`Validate broadcast event for a user in a group and Validate that other user in that group getting that`, async () => {
await utilities.fireboltCommand(
const commander = new utilities.FireboltCommander(9998, "567~B");
await commander.sendCommand(
JSON.stringify({
method: "Device.onNameChanged",
params: { listen: true },
id: 11,
}),
9998,
"567~B"
);
method: "Device.onNameChanged",
params: { listen: true },
id: 11,
}));

const result = await utilities.callMfCli(
`cd ../cli/src/ && node cli.mjs --broadcastEvent ../examples/device-onNameChanged1.event.json --user 567~B && cd ../../functional`,
true
);

expect(result.includes(`{ status: 'SUCCESS' }`)).toBe(true);
const resultTwo = await utilities.callMfCli(
`cd ../cli/src/ && node cli.mjs --broadcastEvent ../examples/device-onNameChanged1.event.json --user 978~B && cd ../../functional`,
true
);
expect(resultTwo.includes(`{ status: 'SUCCESS' }`)).toBe(true);

commander.closeConnection();
});

// Send event without any active listener
Expand Down
28 changes: 19 additions & 9 deletions functional/tests/scope.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import * as utilities from "./utilities.mjs";

jest.setTimeout(20000);

let commander;

beforeAll(async () => {
const response = await utilities.mfState(
true,
` -- --mock`
);
console.log(response)
expect(response).toBe("MF started successfully");

commander = new utilities.FireboltCommander(9998, "123~A");
});

afterAll(async () => {
Expand All @@ -41,18 +45,18 @@ afterAll(async () => {

// Updating method for a particular user
test(`Validate group scope updates`, async () => {

const userId = "123~A"
const group = "~A"
const fbCommand = "accessibility.closedCaptionsSettings"

//Validate the OpenRPC response
let result = await utilities.fireboltCommand(
let result = await commander.sendCommand(
JSON.stringify({
method: fbCommand,
params: {},
id: 0,
}), null, userId)
})
);

console.log(JSON.stringify(result))
expect(result.includes('\"fontFamily\":\"Monospace sans-serif\"')).toBe(true)

Expand All @@ -63,12 +67,14 @@ test(`Validate group scope updates`, async () => {
);

//Validate the override took effect
result = await utilities.fireboltCommand(
result = await commander.sendCommand(
JSON.stringify({
method: fbCommand,
params: {},
id: 0,
}), null, userId)
})
);

console.log(JSON.stringify(result))
expect(result.includes('\"fontFamily\":\"testValue1\"')).toBe(true)

Expand All @@ -79,12 +85,16 @@ test(`Validate group scope updates`, async () => {
);

//Validate the second override took effect
result = await utilities.fireboltCommand(
result = await commander.sendCommand(
JSON.stringify({
method: fbCommand,
params: {},
id: 0,
}), null, userId)
})
);

console.log(JSON.stringify(result))
expect(result.includes('\"fontFamily\":\"testValue2\"')).toBe(true)
});

commander.closeConnection();
});
67 changes: 44 additions & 23 deletions functional/tests/utilities.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,53 @@ function url(host, port, path) {
}

/**
* To run MF ws commands
*
* @param {String} command the command which we need to execute
* @param {Number} port ws port to connect
* @param {String} user to pass the userID
* @returns Promise yielding the response on resolve()
* Class representing a FireboltCommander.
*/
async function fireboltCommand(command, port, user) {
//TODO - websocket need not to be init all the time, it needs to be handled to init once and close the connection once testing completed.
return new Promise((res) => {
//Establish a WS connection to MF living at port 9998
//Send the firebolt command given by "command"
//Return the response from MF
//TODO - wsClient is added for testing, it needs to be moved to differnt method to init WS client
const wsClientURL = `${wsClient}${port || 9998}${user ? `/${user}` : ""}`;
const ws = new WebSocket(wsClientURL);
ws.on("open", function open() {
ws.send(command);
class FireboltCommander {
/**
* Create a FireboltCommander.
* @param {number} [port=9998] - The port to connect to.
* @param {string} user - The user ID to pass.
*/
constructor(port = 9998, user) {
this.wsClientURL = `${wsClient}${port}${user ? `/${user}` : ""}`;
this.ws = new WebSocket(this.wsClientURL);
this.ws.on("message", function message(data) {
console.log("received: %s", data);
});
}

/**
* Send a command to the WebSocket.
* @param {string} command - The command to send.
* @return {Promise} A promise that resolves with the response data.
*/
sendCommand(command) {
return new Promise((res, rej) => {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.send(command);
} else {
this.ws.on("open", function open() {
this.ws.send(command);
}.bind(this));
}

ws.on("message", function message(data) {
console.log("received: %s", data);
res(data);
ws.close();
this.ws.on("message", function message(data) {
res(data);
});

this.ws.on("error", function error(err) {
rej(err);
});
});
});
}

/**
* Close the WebSocket connection.
*/
closeConnection() {
this.ws.close();
}
}

/**
Expand Down Expand Up @@ -223,4 +244,4 @@ async function mfState(on, extraConfig = "") {
});
}

export { fireboltCommand, callApi, callMfCli, mfState, killPort };
export { FireboltCommander, callApi, callMfCli, mfState, killPort };
6 changes: 6 additions & 0 deletions server/src/userManagement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ function addUser(userId) {
ws.on('pong', async hb => {
heartbeat(ws)
});

// Remove ws connection of user
ws.on('close', function close() {
closeConnection(userId, ws);
});

// If multiUserConnections configuration is set as deny and there is a ws object associated with userId, deny and log second ws connection and drop the attempt
if (/deny/i.test(config.multiUserConnections) == true && getWsForUser(userId) !== undefined) {
logger.info(`Denying second websocket connection of user ${userId}`)
Expand Down

0 comments on commit 368b2db

Please sign in to comment.