forked from openhab/openhab-alexa
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openhab.test.js
455 lines (414 loc) · 13.8 KB
/
openhab.test.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import { expect } from 'chai';
import sinon from 'sinon';
import nock from 'nock';
import fs from 'node:fs';
import { AxiosError } from 'axios';
import { v4 as uuidv4 } from 'uuid';
import OpenHAB from '#openhab/index.js';
describe('OpenHAB Tests', () => {
// set default environment
const baseURL = 'https://foobar';
const token = 'token';
const timeout = 42;
let openhab;
beforeEach(() => {
// create new openhab instance
openhab = new OpenHAB({ baseURL }, token, timeout);
});
afterEach(() => {
// restore stub environment
sinon.restore();
// clean up nock environment
nock.cleanAll();
});
describe('authentication request', () => {
it('https oauth2 token', async () => {
// set environment
sinon.stub(fs, 'existsSync').returns(false);
nock(baseURL).get('/').matchHeader('Authorization', `Bearer ${token}`).reply(200);
// run test
await OpenHAB.createClient({ baseURL }, token, timeout).get('/');
expect(nock.isDone()).to.be.true;
});
it('https basic auth', async () => {
// set environment
const user = 'username';
const pass = 'password';
sinon.stub(fs, 'existsSync').returns(false);
nock(baseURL).get('/').basicAuth({ user, pass }).reply(200);
// run test
await OpenHAB.createClient({ baseURL, user, pass }, token, timeout).get('/');
expect(nock.isDone()).to.be.true;
});
it('https client cert', async () => {
// set environment
const certFile = 'cert.pfx';
const certData = 'data';
const certPass = 'passphrase';
sinon.stub(fs, 'existsSync').withArgs(certFile).returns(true);
sinon.stub(fs, 'readFileSync').withArgs(certFile).returns(certData);
nock(baseURL)
.get('/')
.reply(200)
.on('request', ({ headers, options, socket }) => {
expect(headers).to.not.have.property('authorization');
expect(options).to.nested.include({ 'agent.options.pfx': certData, 'agent.options.passphrase': certPass });
expect(socket).to.include({ timeout });
});
// run test
await OpenHAB.createClient({ baseURL, certFile, certPass }, token, timeout).get('/');
expect(nock.isDone()).to.be.true;
});
it('https no auth', async () => {
// set environment
sinon.stub(fs, 'existsSync').returns(false);
nock(baseURL)
.get('/')
.reply(200)
.on('request', ({ headers }) => {
expect(headers).to.not.have.property('authorization');
});
// run test
await OpenHAB.createClient({ baseURL }).get('/');
expect(nock.isDone()).to.be.true;
});
});
describe('get item state', () => {
it('defined state', async () => {
// set environment
nock(baseURL).get('/rest/items/foo').reply(200, { name: 'foo', state: '42', type: 'Dimmer' });
// run test
expect(await openhab.getItemState('foo')).to.equal('42');
expect(nock.isDone()).to.be.true;
});
it('defined state with state description but no pattern', async () => {
// set environment
nock(baseURL)
.get('/rest/items/foo')
.reply(200, { name: 'foo', state: '42', stateDescription: {}, type: 'Dimmer' });
// run test
expect(await openhab.getItemState('foo')).to.equal('42');
expect(nock.isDone()).to.be.true;
});
it('undefined state', async () => {
// set environment
nock(baseURL).get('/rest/items/foo').reply(200, { name: 'foo', state: 'NULL', type: 'Dimmer' });
// run test
expect(await openhab.getItemState('foo')).to.be.undefined;
expect(nock.isDone()).to.be.true;
});
it('dimmer state with pattern', async () => {
// set environment
nock(baseURL)
.get('/rest/items/foo')
.reply(200, {
name: 'foo',
state: '42.4242',
stateDescription: { pattern: '%d' },
type: 'Dimmer'
});
// run test
expect(await openhab.getItemState('foo')).to.equal('42');
expect(nock.isDone()).to.be.true;
});
it('number dimensionless group state with pattern', async () => {
// set environment
nock(baseURL)
.get('/rest/items/foo')
.reply(200, {
name: 'foo',
state: '42.4242 %',
stateDescription: { pattern: '%.2f %' },
type: 'Group',
groupType: 'Number:Dimensionless'
});
// run test
expect(await openhab.getItemState('foo')).to.equal('42.42');
expect(nock.isDone()).to.be.true;
});
it('rollershutter state with pattern', async () => {
// set environment
nock(baseURL)
.get('/rest/items/foo')
.reply(200, {
name: 'foo',
state: '42.4242',
stateDescription: { pattern: '%f' },
type: 'Rollershutter'
});
// run test
expect(await openhab.getItemState('foo')).to.equal('42.4242');
expect(nock.isDone()).to.be.true;
});
it('string state with no state description', async () => {
// set environment
nock(baseURL).get('/rest/items/foo').reply(200, { name: 'foo', state: 'bar', type: 'String' });
// run test
expect(await openhab.getItemState('foo')).to.equal('bar');
expect(nock.isDone()).to.be.true;
});
it('item not found error', async () => {
// set environment
nock(baseURL).get('/rest/items/foo').reply(404);
// run test
try {
await openhab.getItemState('foo');
} catch (error) {
expect(error).to.be.instanceof(AxiosError).and.nested.include({ 'response.status': 404 });
}
expect(nock.isDone()).to.be.true;
});
});
describe('get all items', () => {
// set default environment
const qs = {
fields: 'editable,groupNames,groupType,name,label,metadata,stateDescription,tags,type,unitSymbol',
metadata: 'alexa,autoupdate,channel,synonyms'
};
it('successful', async () => {
// set environment
const items = [
{ name: 'foo', type: 'Dimmer' },
{ name: 'bar', type: 'Switch' }
];
nock(baseURL).get('/rest/items').query(qs).reply(200, items);
// run test
expect(await openhab.getAllItems()).to.deep.equal(items);
expect(nock.isDone()).to.be.true;
});
it('type error', async () => {
// set environment
nock(baseURL)
.get('/rest/items')
.query(qs)
.reply(200, 'invalid 1')
.get('/rest/items')
.query(qs)
.reply(200, 'invalid 2')
.get('/rest/items')
.query(qs)
.reply(200, 'invalid 3');
// run test
try {
await openhab.getAllItems();
} catch (error) {
expect(error)
.to.be.instanceof(TypeError)
.and.include({ message: 'Failed to retrieve all items formatted array' });
}
expect(nock.isDone()).to.be.true;
});
it('unauthorized error', async () => {
// set environment
nock(baseURL).get('/rest/items').query(qs).reply(401);
// run test
try {
await openhab.getAllItems();
} catch (error) {
expect(error).to.be.instanceof(AxiosError).and.nested.include({ 'response.status': 401 });
}
expect(nock.isDone()).to.be.true;
});
});
describe('get server settings', () => {
// set default environment
const language = 'en';
const measurementSystem = 'US';
const region = 'US';
const locale = `${language}_${region}`;
const uuid = uuidv4();
it('oh2.4', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '2' })
// service i18n config
.get('/rest/services/org.eclipse.smarthome.core.i18nprovider/config')
.reply(200, { language, measurementSystem, region })
// uuid
.get('/rest/uuid')
.reply(200, uuid);
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { uuid, version: '2' }
});
expect(nock.isDone()).to.be.true;
});
it('oh2.5', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '3' })
// service i18n config
.get('/rest/services/org.eclipse.smarthome.i18n/config')
.reply(200, { language, measurementSystem, region })
// uuid
.get('/rest/uuid')
.reply(200, uuid);
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { uuid, version: '2' }
});
expect(nock.isDone()).to.be.true;
});
it('oh3.x', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '4', locale, measurementSystem, runtimeInfo: { version: '3.0.0' } })
// uuid
.get('/rest/uuid')
.reply(200, uuid);
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { uuid, version: '3.0.0' }
});
expect(nock.isDone()).to.be.true;
});
it('oh3.x with invalid uuid', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '4', locale, measurementSystem, runtimeInfo: { version: '3.0.0' } })
// uuid
.get('/rest/uuid')
.reply(200, 'invalid');
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { version: '3.0.0' }
});
expect(nock.isDone()).to.be.true;
});
it('oh3.x with unauthorized uuid', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '4', locale, measurementSystem, runtimeInfo: { version: '3.0.0' } })
// uuid
.get('/rest/uuid')
.reply(401);
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { version: '3.0.0' }
});
expect(nock.isDone()).to.be.true;
});
it('undefined root resource', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200)
// uuid
.get('/rest/uuid')
.reply(200, uuid);
// run test
expect(await openhab.getServerSettings()).to.deep.equal({ regional: {}, runtime: { uuid } });
expect(nock.isDone()).to.be.true;
});
it('request error', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.replyWithError('error');
// run test
try {
await openhab.getServerSettings();
} catch (error) {
expect(error).to.be.instanceof(AxiosError).and.include({ message: 'error' });
}
expect(nock.isDone()).to.be.true;
});
});
describe('send item command', () => {
it('successful', async () => {
// set environment
nock(baseURL).post('/rest/items/foo', '42').reply(200);
// run test
await openhab.sendCommand('foo', 42);
expect(nock.isDone()).to.be.true;
});
it('item not found error', async () => {
// set environment
nock(baseURL).post('/rest/items/foo', '42').reply(404);
// run test
try {
await openhab.sendCommand('foo', 42);
} catch (error) {
expect(error).to.be.instanceof(AxiosError).and.nested.include({ 'response.status': 404 });
}
expect(nock.isDone()).to.be.true;
});
});
describe('update item state', () => {
it('successful', async () => {
// set environment
nock(baseURL).put('/rest/items/foo/state', '42').reply(202);
// run test
await openhab.postUpdate('foo', 42);
expect(nock.isDone()).to.be.true;
});
it('item state null error', async () => {
// set environment
nock(baseURL).put('/rest/items/foo/state', 'invalid').reply(400);
// run test
try {
await openhab.postUpdate('foo', 'invalid');
} catch (error) {
expect(error).to.be.instanceof(AxiosError).and.nested.include({ 'response.status': 400 });
}
expect(nock.isDone()).to.be.true;
});
});
describe('get state presentation precision', () => {
it('integer', async () => {
expect(OpenHAB.getStatePresentationPrecision('%d %%')).to.equal(0);
});
it('float', async () => {
expect(OpenHAB.getStatePresentationPrecision('%.1f °F')).to.equal(1);
});
it('no precision', async () => {
expect(OpenHAB.getStatePresentationPrecision('foo')).to.be.NaN;
});
it('undefined', async () => {
expect(OpenHAB.getStatePresentationPrecision(undefined)).to.be.NaN;
});
});
describe('get state presentation unit symbol', () => {
it('percent', async () => {
expect(OpenHAB.getStatePresentationUnitSymbol('%d %%')).to.equal('%');
});
it('temperature', async () => {
expect(OpenHAB.getStatePresentationUnitSymbol('%.1f °F')).to.equal('°F');
});
it('no symbol', async () => {
expect(OpenHAB.getStatePresentationUnitSymbol('%.1f')).to.be.undefined;
});
it('undefined', async () => {
expect(OpenHAB.getStatePresentationUnitSymbol(undefined)).to.be.undefined;
});
});
});