UNPKG

node-hue-api

Version:
173 lines (128 loc) 5.96 kB
'use strict'; import {expect } from 'chai'; import {v3} from '../v3'; import {discovery, ApiError} from '../index'; import {UserRecord} from "./Users"; const testValues = require('../../test/support/testValues'); //TODO move these describe('Hue API #users', () => { let unauthenticatedHue: any , authenticatedHue: any ; before(async function () { this.timeout(5000); const searchResults = await discovery.mdnsSearch(); const ipAddress = searchResults[0].ipaddress; expect(ipAddress).to.not.equal(undefined); // @ts-ignore unauthenticatedHue = await v3.api.createLocal(ipAddress).connect(); // @ts-ignore authenticatedHue = await v3.api.createLocal(ipAddress).connect(testValues.username); }); describe('unauthenticated access', () => { let createdUser = null; describe.skip('#createUser()', () => { it('should create a user when the link button has been pressed', async () => { const user = await unauthenticatedHue.users.createUser('node-hue-api', 'delete-tests'); expect(user).to.have.property('username').to.have.length.greaterThan(39); expect(user).to.have.property('clientkey'); createdUser = user; }); it('should not create a new user when link button not pressed', async () => { try { await unauthenticatedHue.users.createUser('node-hue-api', 'node-hue-api-tests'); expect.fail('should not get here unless the link button was pressed'); } catch (err: any) { expect(err).to.be.instanceof(ApiError); expect(err.getHueErrorType()).to.equal(101); } }); }); //TODO would need to mock this now as it is based of the modelid of the bridge // describe('#createUser() no clientkey', () => { // // it('should create a user when the link button has been pressed', async () => { // const user = await unauthenticatedHue.users.createUser('node-hue-api', 'delete-tests', true); // expect(user).to.have.property('username').to.have.length.greaterThan(39); // expect(user).to.not.have.property('clientkey'); // }); // }); describe.skip('#deleteUser()', () => { // it('should delete a user that we created', async () => { // if (!createdUser) { // expect.fail('A user needs to have been created above before we can delete it'); // } // // const deleted = await authenticatedHue.users.deleteUser(createdUser); // console.log(JSON.stringify(deleted)); // }); it('should remove test accounts', async () => { const users: UserRecord[] = await authenticatedHue.users.getUserByName('node-hue-api', 'node-hue-api-tests'); expect(users).to.be.instanceof(Array); expect(users).to.have.length.greaterThan(0); const promises: Promise<any>[] = []; users.forEach(user => { promises.push(authenticatedHue.users.deleteUser(user.username)); }); const deletionResults = await Promise.all(promises); console.log(JSON.stringify(deletionResults)); }); }); }); describe('authenticated access', () => { describe('#getAll()', () => { it('should get all the users', async () => { const allUsers = await authenticatedHue.users.getAll(); // console.log(JSON.stringify(allUsers, null, 2)); expect(allUsers).to.be.instanceof(Array); expect(allUsers).to.have.length.greaterThan(0); expect(allUsers[0]).to.have.property('username'); expect(allUsers[0]).to.have.property('name'); expect(allUsers[0]).to.have.property('last use date'); expect(allUsers[0]).to.have.property('create date'); }); }); describe('#get()', () => { it('should get a user by username', async () => { const user = await authenticatedHue.users.getUser(testValues.username); expect(user).to.have.property('username').to.equal(testValues.username); expect(user).to.have.property('name'); expect(user).to.have.property('create date'); expect(user).to.have.property('last use date'); }); it('should not find an invalid username', async () => { const user = await authenticatedHue.users.getUser(testValues.username + '0000'); expect(user).to.be.undefined; }); }); describe('#getByName()', () => { it('should get a list of user accounts for valid name', async () => { const username = 'Indigo Hue Lights' //TODO hardcoded value, pull this out into test data , users = await authenticatedHue.users.getUserByName(username) ; expect(users).to.be.instanceof(Array).to.have.length.greaterThan(0); expect(users[0]).to.have.property('name').to.equal(username); expect(users[0]).to.have.property('username'); expect(users[0]).to.have.property('create date'); expect(users[0]).to.have.property('last use date'); }); it('should get a list of user accounts for appName, deviceName', async () => { //TODO hardcoded value, pull this out into test data const appName = 'Hue 3' , deviceName = 'iPhone' , users = await authenticatedHue.users.getUserByName(appName, deviceName) ; expect(users).to.be.instanceof(Array); expect(users).to.have.length.greaterThan(0); expect(users[0]).to.have.property('name').to.equal(`${appName}#${deviceName}`); expect(users[0]).to.have.property('username'); expect(users[0]).to.have.property('create date'); expect(users[0]).to.have.property('last use date'); }); it('should not find a account that does not exist', async () => { const users = await authenticatedHue.users.getUserByName('0000000001'); expect(users).to.be.instanceOf(Array); expect(users).to.be.empty; }); }); }); });