Files
ewelink-api/test/zeroconf.spec.js
Martin M 098011b285 Release v2.0.0 (#44)
* Added arpTableSolver (#18)

* Added arpTableSolver

* fix package import

* linting class

* changed arp library

* refactor arp class

* using arpping fork

* refactor arpTableSolver class

* Added Zero Conf functionality (LAN mode) (#46)

* added crypto-js

* zeroconf helper functions

* zeroconf update payload

* new method to save devices cache file

* class renamed

* refactor Zeroconf class

* return cached device if exists

* moved method to get local ip address

* fix mac addresses without leading zeroes

* refactor Zeroconf class

* using new zeroconf functionality

* zeroconf working with single and multichannel devices

* save device mixin enhancement

* working on zeroconf test cases

* catch errors on filesystem methods

* zeroconf: added extra test cases

* better error handling

* zeroconf: 100% code coverage

* removed deprecated login method

* updates on credentials file

* version bump

* Docs for v2.0 (#52)

* added v1 docs

* added zeroconf docs

* updated readme

* docs updated

* removed zeroconf article warning

* updated vscode config

Co-authored-by: Luis Llamas <luisllamas@hotmail.com>
2020-01-11 01:39:29 -03:00

121 lines
4.0 KiB
JavaScript

const ewelink = require('../main');
const Zeroconf = require('../classes/Zeroconf');
const {
email,
password,
region,
localIp,
localIpInvalid,
} = require('./_setup/credentials.js');
const { allDevicesExpectations } = require('./_setup/expectations');
describe('zeroconf: save devices to cache file', () => {
test('can save cached devices file', async () => {
jest.setTimeout(30000);
const file = './test/_setup/devices-cache.json';
const conn = new ewelink({ region, email, password });
const result = await conn.saveDevicesCache(file);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.file).toBe(file);
});
test('error saving cached devices file', async () => {
jest.setTimeout(30000);
const file = '/tmp/non-existent-folder/devices-cache.json';
const conn = new ewelink({ region, email, password });
const result = await conn.saveDevicesCache(file);
expect(typeof result).toBe('object');
expect(result.error).toContain('ENOENT: no such file or directory');
});
test('invalid credentials trying to create cached devices file', async () => {
const file = '/tmp/non-existent-folder/devices-cache.json';
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const result = await conn.saveDevicesCache(file);
expect(typeof result).toBe('object');
expect(result.msg).toBe('Authentication error');
expect(result.error).toBe(401);
});
});
describe('zeroconf: save arp table to file', () => {
test('can save arp table file', async () => {
jest.setTimeout(30000);
const file = './test/_setup/arp-table.json';
const arpTable = await Zeroconf.saveArpTable({
ip: localIp,
file,
});
expect(typeof arpTable).toBe('object');
expect(arpTable.status).toBe('ok');
expect(arpTable.file).toBe(file);
});
test('error saving arp table file', async () => {
jest.setTimeout(30000);
const file = '/tmp/non-existent-folder/arp-table.json';
const arpTable = await Zeroconf.saveArpTable({
ip: localIp,
file,
});
expect(typeof arpTable).toBe('object');
expect(arpTable.error).toContain('ENOENT: no such file or directory');
});
test('error saving arp table file with invalid local network', async () => {
jest.setTimeout(30000);
const file = './test/_setup/arp-table.json';
const arpTable = await Zeroconf.saveArpTable({
ip: localIpInvalid,
file,
});
expect(typeof arpTable).toBe('object');
expect(arpTable.error).toBe('Error: range must not be empty');
});
});
describe('zeroconf: load devices to cache file', () => {
test('can load cached devices file', async () => {
jest.setTimeout(30000);
const conn = new ewelink({ region, email, password });
const devices = await conn.getDevices();
const devicesCache = await Zeroconf.loadCachedDevices(
'./test/_setup/devices-cache.json'
);
expect(typeof devicesCache).toBe('object');
expect(devicesCache.length).toBe(devices.length);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('error trying to load invalidcached devices file', async () => {
jest.setTimeout(30000);
const devicesCache = await Zeroconf.loadCachedDevices('file-not-found');
expect(typeof devicesCache).toBe('object');
expect(devicesCache.error).toContain('ENOENT: no such file or directory');
});
});
describe('zeroconf: load arp table file', () => {
test('can load arp table file', async () => {
const arpTable = await Zeroconf.loadArpTable(
'./test/_setup/arp-table.json'
);
expect(typeof arpTable).toBe('object');
expect(arpTable[0]).toMatchObject({
ip: expect.any(String),
mac: expect.any(String),
});
});
test('error trying to load invalidcached devices file', async () => {
const arpTable = await Zeroconf.loadArpTable(
'/tmp/non-existent-folder/arp-table.json'
);
expect(typeof arpTable).toBe('object');
expect(arpTable.error).toContain('ENOENT: no such file or directory');
});
});