Users.test.js
5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const expect = require('chai').expect
, v3Api = require('../v3').api
, discovery = require('../v3').discovery
, ApiError = require('../../index').ApiError
, testValues = require('../../test/support/testValues.js') //TODO move these
;
describe('Hue API #users', () => {
let unauthenticatedHue
, authenticatedHue
;
before(async function () {
this.timeout(5000);
const searchResults = await discovery.nupnpSearch();
const ipAddress = searchResults[0].ipaddress;
unauthenticatedHue = await v3Api.createLocal(ipAddress).connect();
authenticatedHue = await v3Api.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) {
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 = 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 = [];
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[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.null;
});
});
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 () => {
const appName = 'node-hue-api'
, deviceName = 'example-code' //TODO hardcoded value, pull this out into test data
, users = await authenticatedHue.users.getUserByName(appName, deviceName)
;
expect(users).to.be.instanceof(Array);
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;
});
});
});
});