Release v1.2.1 (#6)

* tests refactored

* moved test config files

* removed deprecated command

* added missing test case

* updated eslint config

* removed unused function / renamed helper

* added missing test cases

* code enhancement
This commit is contained in:
Martin M
2019-07-20 12:43:05 -03:00
committed by GitHub
parent d6406deeec
commit 513c2f660a
13 changed files with 261 additions and 230 deletions

View File

@@ -1,5 +1,8 @@
{ {
"extends": [ "extends": [
"wesbos" "wesbos"
] ],
"rules": {
"new-cap": "off"
}
} }

View File

@@ -1,27 +1,19 @@
# ewelink-api # ewelink-api
> eWeLink API for Node.js > eWeLink API for Node.js
## Installation ## Installation
``` sh ``` sh
npm install ewelink-api npm install ewelink-api
``` ```
## Usage ## Usage
``` Check `demo/` directory for examples.
const ewelink = require('ewelink-api');
(async () => {
const conn = new ewelink({ ## Testing
email: '<your ewelink email>', 1. open `test/_setup/credentials.json` and update parameters.
password: '<your ewelink password>', 2. in a terminal, `npm run test`
});
/* get all devices */ * Tests needs to be performed serially, so if run jest manually, add `--runInBand` parameter.
const devices = await conn.getDevices();
console.log(devices);
})();
```
Check `demo/` directory for more examples.

View File

@@ -146,10 +146,9 @@ const getDeviceChannelCountByType = deviceType => {
return DEVICE_CHANNEL_LENGTH[deviceType] || 0; return DEVICE_CHANNEL_LENGTH[deviceType] || 0;
}; };
const getDeviceTotalChannels = deviceUUID => { const getDeviceChannelCount = deviceUUID => {
const deviceType = getDeviceTypeByUiid(deviceUUID); const deviceType = getDeviceTypeByUiid(deviceUUID);
const channels = getDeviceChannelCountByType(deviceType); return getDeviceChannelCountByType(deviceType);
return channels;
}; };
module.exports = { module.exports = {
@@ -157,5 +156,5 @@ module.exports = {
loginPayload, loginPayload,
wssLoginPayload, wssLoginPayload,
wssUpdatePayload, wssUpdatePayload,
getDeviceTotalChannels, getDeviceChannelCount,
}; };

20
main.js
View File

@@ -10,7 +10,7 @@ const {
loginPayload, loginPayload,
wssLoginPayload, wssLoginPayload,
wssUpdatePayload, wssUpdatePayload,
getDeviceTotalChannels, getDeviceChannelCount,
} = require('./lib/ewelink-helper'); } = require('./lib/ewelink-helper');
class eWeLink { class eWeLink {
@@ -112,7 +112,7 @@ class eWeLink {
async getDevicePowerState(deviceId, channel = 1) { async getDevicePowerState(deviceId, channel = 1) {
const device = await this.getDevice(deviceId); const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false); const error = _get(device, 'error', false);
const switchesAmount = getDeviceTotalChannels(device.uiid); const switchesAmount = getDeviceChannelCount(device.uiid);
let state = _get(device, 'params.switch', false); let state = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false); const switches = _get(device, 'params.switches', false);
@@ -133,7 +133,7 @@ class eWeLink {
async setDevicePowerState(deviceId, state, channel = 1) { async setDevicePowerState(deviceId, state, channel = 1) {
const device = await this.getDevice(deviceId); const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false); const error = _get(device, 'error', false);
const switchesAmount = getDeviceTotalChannels(device.uiid); const switchesAmount = getDeviceChannelCount(device.uiid);
const status = _get(device, 'params.switch', false); const status = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false); const switches = _get(device, 'params.switches', false);
@@ -183,19 +183,7 @@ class eWeLink {
const newState = state === 'on' ? 'off' : 'on'; const newState = state === 'on' ? 'off' : 'on';
const newResponse = await this.setDevicePowerState( return this.setDevicePowerState(deviceId, newState, channel);
deviceId,
newState,
channel
);
return newResponse;
}
async getDeviceChannelCount(deviceId) {
const device = await this.getDevice(deviceId);
const switchesAmount = getDeviceTotalChannels(device.uiid);
return switchesAmount;
} }
} }

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "ewelink-api", "name": "ewelink-api",
"version": "1.2.0", "version": "1.2.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "ewelink-api", "name": "ewelink-api",
"version": "1.2.0", "version": "1.2.1",
"description": "eWeLink API for Node.js", "description": "eWeLink API for Node.js",
"author": "Martín M.", "author": "Martín M.",
"license": "MIT", "license": "MIT",
@@ -20,8 +20,7 @@
], ],
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"demo": "node ./demo/example.js", "test": "jest --runInBand --verbose --testPathIgnorePatterns '(node_modules|.cache)'"
"test": "jest --testPathIgnorePatterns '(node_modules|.cache)'"
}, },
"dependencies": { "dependencies": {
"delay": "^4.3.0", "delay": "^4.3.0",

View File

@@ -0,0 +1,72 @@
const ewelink = require('../main');
const { email, password, deviceId } = require('./_setup/credentials.json');
const {
loginExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_setup/expectations');
describe('env: node script', () => {
let conn;
beforeAll(() => {
conn = new ewelink({ email, password });
});
test('login into ewelink', async () => {
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
test('get all devices', async () => {
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(deviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get device power state', async () => {
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set device power state', async () => {
jest.setTimeout(30000);
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(deviceId, newState);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
test('toggle device power state', async () => {
jest.setTimeout(30000);
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(deviceId);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(deviceId);
const deviceVerifyAgain = await conn.getDevice(deviceId);
const currentStateVerifyAgain = deviceVerifyAgain.params.switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
});

View File

@@ -0,0 +1,77 @@
const ewelink = require('../main');
const { email, password, deviceId } = require('./_setup/credentials.json');
const {
loginExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_setup/expectations');
describe('env: serverless', () => {
let accessToken;
let apiKey;
test('login into ewelink', async () => {
const conn = new ewelink({ email, password });
const login = await conn.login();
accessToken = login.at;
apiKey = login.user.apikey;
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
test('get all devices', async () => {
const conn = new ewelink({ at: accessToken });
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const conn = new ewelink({ at: accessToken });
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(deviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get device power state', async () => {
const conn = new ewelink({ at: accessToken });
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set device power state', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ at: accessToken, apiKey });
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(deviceId, newState);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
test('toggle device power state', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ at: accessToken, apiKey });
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(deviceId);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(deviceId);
const deviceVerifyAgain = await conn.getDevice(deviceId);
const currentStateVerifyAgain = deviceVerifyAgain.params.switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
});

View File

@@ -0,0 +1,51 @@
const ewelink = require('../main');
const { deviceId } = require('./_setup/credentials.json');
describe('invalid credentials', () => {
test('no credentials given', async () => {
const conn = new ewelink({});
expect(typeof conn).toBe('object');
expect(conn.error).toBe('No credentials provided');
});
test('get error response on api login', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login.msg).toBe('Authentication error');
expect(login.error).toBe(400);
});
test('get error response on all devices', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const devices = await conn.getDevices();
expect(typeof devices).toBe('object');
expect(devices.msg).toBe('Authentication error');
expect(devices.error).toBe(401);
});
test('get error response on specific device', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.msg).toBe('Authentication error');
expect(device.error).toBe(401);
});
test('get device power state should fail', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Authentication error');
expect(powerState.error).toBe(401);
});
test('set device power state should fail', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const powerState = await conn.setDevicePowerState(deviceId, 'on');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Authentication error');
expect(powerState.error).toBe(401);
});
});

View File

@@ -0,0 +1,40 @@
const ewelink = require('../main');
const { email, password } = require('./_setup/credentials.json');
const { loginExpectations } = require('./_setup/expectations');
describe('valid credentials, invalid device', () => {
test('get device power state should fail', async () => {
const conn = new ewelink({ email, password });
const powerState = await conn.getDevicePowerState('invalid deviceid');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Device does not exist');
expect(powerState.error).toBe(500);
});
test('set device power state should fail', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ email, password });
const powerState = await conn.setDevicePowerState('invalid deviceid', 'on');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Device does not exist');
expect(powerState.error).toBe(500);
});
test('toggle device power state should fail', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ email, password });
const powerState = await conn.toggleDevice('invalid deviceid');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Device does not exist');
expect(powerState.error).toBe(500);
});
});
describe('valid credentials, wrong region', () => {
test('should login in the right region', async () => {
const conn = new ewelink({ email, password, region: 'eu' });
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
});

View File

@@ -1,190 +0,0 @@
const ewelink = require('../main');
const { email, password, deviceId } = require('./credentials.json');
const {
loginExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_expectations');
describe('env: node script', () => {
let conn;
beforeAll(() => {
conn = new ewelink({ email, password });
});
test('login into ewelink', async () => {
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
test('get all devices', async () => {
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(deviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get device power state', async () => {
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set device power state', async () => {
jest.setTimeout(30000);
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(deviceId, newState);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
});
describe('env: serverless', () => {
let accessToken;
let apiKey;
test('login into ewelink', async () => {
const conn = new ewelink({ email, password });
const login = await conn.login();
accessToken = login.at;
apiKey = login.user.apikey;
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
test('get all devices', async () => {
const conn = new ewelink({ at: accessToken });
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const conn = new ewelink({ at: accessToken });
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(deviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get device power state', async () => {
const conn = new ewelink({ at: accessToken });
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set device power state', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ at: accessToken, apiKey });
const device = await conn.getDevice(deviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(deviceId, newState);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(deviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
});
describe('valid credentials, invalid device', () => {
test('get device power state should fail', async () => {
const conn = new ewelink({ email, password });
const powerState = await conn.getDevicePowerState('invalid deviceid');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Device does not exist');
expect(powerState.error).toBe(500);
});
test('set device power state should fail', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ email, password });
const powerState = await conn.setDevicePowerState('invalid deviceid', 'on');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Device does not exist');
expect(powerState.error).toBe(500);
});
});
describe('valid credentials, wrong region', () => {
test('login into ewelink should fail', async () => {
const conn = new ewelink({ region: 'eu', email, password });
expect(conn.region).toBe('eu');
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
expect(conn.region).toBe('us');
});
});
describe('invalid credentials', () => {
test('no credentials given', async () => {
const conn = new ewelink({});
expect(typeof conn).toBe('object');
expect(conn.error).toBe('No credentials provided');
});
test('get error response on api login', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login.msg).toBe('Authentication error');
expect(login.error).toBe(400);
});
test('get error response on all devices', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const devices = await conn.getDevices();
expect(typeof devices).toBe('object');
expect(devices.msg).toBe('Authentication error');
expect(devices.error).toBe(401);
});
test('get error response on specific device', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const device = await conn.getDevice(deviceId);
expect(typeof device).toBe('object');
expect(device.msg).toBe('Authentication error');
expect(device.error).toBe(401);
});
test('get device power state should fail', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const powerState = await conn.getDevicePowerState(deviceId);
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Authentication error');
expect(powerState.error).toBe(401);
});
test('set device power state should fail', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const powerState = await conn.setDevicePowerState(deviceId, 'on');
expect(typeof powerState).toBe('object');
expect(powerState.msg).toBe('Authentication error');
expect(powerState.error).toBe(401);
});
});