Skip to content

Commit

Permalink
Rollback un-necessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Dec 10, 2022
1 parent 9c6d6cf commit 3dd3b47
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 41 deletions.
6 changes: 3 additions & 3 deletions server/lib/gateway/gateway.handleAlexaMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ async function handleAlexaMessage(data, rawMessage, cb) {
logger.debug(`gateway.handleAlexaMessage: New message : ${directiveNamespace}`);
let response;
if (directiveNamespace === 'Alexa.Discovery') {
response = await service.alexaHandler.onDiscovery();
response = service.alexaHandler.onDiscovery();
} else if (DIRECTIVE_NAMESPACES_LIST.indexOf(directiveNamespace) !== -1) {
response = await service.alexaHandler.onExecute(body);
response = service.alexaHandler.onExecute(body);
} else if (directiveNamespace === 'Alexa' && directiveName === 'ReportState') {
response = await service.alexaHandler.onReportState(body);
response = service.alexaHandler.onReportState(body);
} else {
response = {
status: 400,
Expand Down
7 changes: 7 additions & 0 deletions server/lib/gateway/gateway.login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const get = require('get-value');
const { webcrypto } = require('crypto');
const logger = require('../../utils/logger');
const { ERROR_MESSAGES } = require('../../utils/constants');
const { Error403, Error500 } = require('../../utils/httpErrors');
Expand All @@ -16,6 +17,12 @@ async function login(email, password) {
if (this.gladysGatewayClient) {
this.gladysGatewayClient.disconnect();
}
// create a new instance of the client
this.gladysGatewayClient = new this.GladysGatewayClient({
cryptoLib: webcrypto,
serverUrl: this.config.gladysGatewayServerUrl,
logger,
});
// We login with email/password to get two factor token
const loginResults = await this.gladysGatewayClient.login(email, password);
return loginResults;
Expand Down
1 change: 1 addition & 0 deletions server/lib/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const Gateway = function Gateway(
this.alexaForwardStateTimeout = 5 * 1000;
this.backupRandomInterval = 2 * 60 * 60 * 1000; // 2 hours
this.getLatestGladysVersionInitTimeout = 5 * 60 * 1000; // 5 minutes
this.GladysGatewayClient = GladysGatewayClient;
this.gladysGatewayClient = new GladysGatewayClient({
cryptoLib: webcrypto,
serverUrl: config.gladysGatewayServerUrl,
Expand Down
20 changes: 17 additions & 3 deletions server/test/lib/gateway/GladysGatewayClientMock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ const { fake } = require('sinon');

const GladysGatewayClientMock = function GladysGatewayClientMock() {
return {
login: fake.resolves({
two_factor_token: 'token',
}),
login: async (email, password) => {
return new Promise((resolve, reject) => {
if (password === 'pass403') {
const error = new Error();
error.response = { status: 403 };
reject(error);
return;
}
if (password === 'pass500') {
reject(new Error());
return;
}
resolve({
two_factor_token: 'token',
});
});
},
loginInstance: fake.resolves({}),
createInstance: fake.resolves({
instance: {
Expand Down
4 changes: 2 additions & 2 deletions server/test/lib/gateway/gateway.backup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.backup', function Describe() {
describe('gateway.backup', async function describe() {
this.timeout(20000);

const variable = {};
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('gateway.backup', function Describe() {
assert.calledOnce(gateway.gladysGatewayClient.abortMultiPartBackup);
});

it('should backup gladys with lots of insert at the same time', async function Test() {
it('should backup gladys with lots of insert at the same time', async () => {
const promisesDevices = [];
const promises = [];
// create 100 state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.checkIfBackupNeeded', function Describe() {
describe('gateway.checkIfBackupNeeded', () => {
const event = {};

let gateway;
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/gateway/gateway.disconnect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.disconnect', function Describe() {
describe('gateway.disconnect', () => {
const variable = {};

let gateway;
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/gateway/gateway.downloadBackup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.downloadBackup', function Describe() {
describe('gateway.downloadBackup', () => {
const variable = {};
const event = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
const stateManager = {};
const event = {};

const newEvent = {
const oneEvent = {
type: EVENTS.DEVICE.NEW_STATE,
device_feature: 'my-device',
};
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
});

it('should forward an event to google home', async () => {
stateManager.get = (key) => {
stateManager.get = (type) => {
const feature = {
id: 'f2e2d5ea-bcea-4092-b597-7b8fb723e070',
device_id: '31ad9f11-4ec7-495e-8238-2e576092ac0c',
Expand All @@ -95,7 +95,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
last_value: 97,
last_value_string: null,
};
if (key === 'deviceById') {
if (type === 'deviceById') {
return {
id: '31ad9f11-4ec7-495e-8238-2e576092ac0c',
service_id: '54a4c447-0caa-4ed5-aa6f-5019e4b27754',
Expand All @@ -110,11 +110,10 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
params: [],
};
}

return feature;
};

await gateway.forwardDeviceStateToGoogleHome(newEvent);
await gateway.forwardDeviceStateToGoogleHome(oneEvent);

// Force wait
clock.tick(200);
Expand All @@ -125,7 +124,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
});

it('should not forward event to google home, event empty', async () => {
stateManager.get = (key) => {
stateManager.get = (type) => {
const feature = {
id: 'f2e2d5ea-bcea-4092-b597-7b8fb723e070',
device_id: '31ad9f11-4ec7-495e-8238-2e576092ac0c',
Expand All @@ -143,7 +142,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
last_value: 97,
last_value_string: null,
};
if (key === 'deviceById') {
if (type === 'deviceById') {
return {
id: '31ad9f11-4ec7-495e-8238-2e576092ac0c',
service_id: '54a4c447-0caa-4ed5-aa6f-5019e4b27754',
Expand All @@ -161,7 +160,7 @@ describe('gateway.forwardDeviceStateToGoogleHome', () => {
return feature;
};

await gateway.forwardDeviceStateToGoogleHome(newEvent);
await gateway.forwardDeviceStateToGoogleHome(oneEvent);

// Force wait
clock.tick(200);
Expand Down
6 changes: 3 additions & 3 deletions server/test/lib/gateway/gateway.forwardWebsockets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.forwardWebsockets', function Describe() {
describe('gateway.forwardWebsockets', () => {
const variable = {};

let gateway;
Expand Down Expand Up @@ -52,15 +52,15 @@ describe('gateway.forwardWebsockets', function Describe() {
gateway.connected = true;

const websocketMessage = {
type: 'zwave.new-node',
type: 'device.new',
payload: {},
};
gateway.forwardWebsockets(websocketMessage);
assert.calledWith(gateway.gladysGatewayClient.newEventInstance, websocketMessage.type, websocketMessage.payload);
});
it('should prevent forwarding a websocket message when not connected', () => {
const websocketMessage = {
type: 'zwave.new-node',
type: 'device.new',
payload: {},
};
gateway.forwardWebsockets(websocketMessage);
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/gateway/gateway.getBackups.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.getBackups', function Describe() {
describe('gateway.getBackups', () => {
const event = {};

let gateway;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.getLatestGladysVersion', function Describe() {
describe('gateway.getLatestGladysVersion', () => {
const variable = {};
const event = {};
const system = {};
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/gateway/gateway.getUsersKeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.getUsersKeys', function Describe() {
describe('gateway.getUsersKeys', () => {
const variable = {};

let gateway;
Expand Down
6 changes: 3 additions & 3 deletions server/test/lib/gateway/gateway.handleNewMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ describe('gateway.handleNewMessage', () => {
it('should handle a new gateway open api message: alexa-request', async () => {
serviceManager.getService = fake.returns({
alexaHandler: {
onDiscovery: fake.resolves({ onDiscovery: true }),
onReportState: fake.resolves({ onReportState: true }),
onExecute: fake.resolves({ onExecute: true }),
onDiscovery: fake.returns({ onDiscovery: true }),
onReportState: fake.returns({ onReportState: true }),
onExecute: fake.returns({ onExecute: true }),
},
});

Expand Down
19 changes: 7 additions & 12 deletions server/test/lib/gateway/gateway.login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,25 @@ describe('gateway.login', () => {

it('should login to gladys gateway', async () => {
const loginResults = await gateway.login('tony.stark@gladysassistant.com', 'warmachine123');
expect(loginResults).to.have.property('two_factor_token');
assert.calledWith(gateway.gladysGatewayClient.login, 'tony.stark@gladysassistant.com', 'warmachine123');
expect(loginResults).deep.eq({
two_factor_token: 'token',
});
});

it('should throw 403 error on error with gateway', async () => {
// force error on gateway client
const error = new Error();
error.response = { status: 403 };
gateway.gladysGatewayClient.login = fake.rejects(error);

try {
await gateway.login('tony.stark@gladysassistant.com', 'warmachine123');
// force error on gateway client
await gateway.login('tony.stark@gladysassistant.com', 'pass403');
assert.fail();
} catch (e) {
expect(e).instanceOf(Error403);
}
});

it('should throw 500 error on invalid gateway', async () => {
// force error on gateway client
gateway.gladysGatewayClient.login = fake.rejects(null);

try {
await gateway.login('tony.stark@gladysassistant.com', 'warmachine123');
// force error on gateway client
await gateway.login('tony.stark@gladysassistant.com', 'pass500');
assert.fail();
} catch (e) {
expect(e).instanceOf(Error500);
Expand Down
2 changes: 1 addition & 1 deletion server/test/lib/gateway/gateway.restoreBackup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Gateway = proxyquire('../../../lib/gateway', {
'@gladysassistant/gladys-gateway-js': GladysGatewayClientMock,
});

describe('gateway.restoreBackup', function Describe() {
describe('gateway.restoreBackup', () => {
const variable = {};
const event = {};
const sequelize = {};
Expand Down

0 comments on commit 3dd3b47

Please sign in to comment.