-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
102 lines (74 loc) · 3.26 KB
/
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
const nock = require('nock');
const tk = require('timekeeper');
const SecuritasDirect = require('.');
nock.disableNetConnect();
const scope = nock('https://mob2217.securitasdirect.es:12010/WebService/').log(console.log);
describe('ES', () => {
tk.freeze(new Date('2020-02-06T21:02:54.868Z'));
const client = new SecuritasDirect('john', 'topsecret', 'es');
const loginParams = {
...client.params,
request: 'LOGIN',
};
const transactionParams = {
...client.params,
callby: 'AND_61',
numinst: '2423443',
panel: 'SDVFAST',
hash: '11111111111',
};
afterEach(() => {
expect(nock.isDone()).toBe(true); // nock.pendingMocks()
});
test('login should get hash for user', async () => {
scope.post('/ws.do').query(loginParams)
.replyWithFile(200, `${__dirname}/test/responses/LOGIN.xml`);
const hash = await client.login();
expect(hash).toBe('11111111111');
expect(client.params.hash).toBe('11111111111');
});
test('should renew hash when session has expired', async () => {
client.params.hash = 'expired';
scope.get('/ws.do').query({ ...transactionParams, request: 'EST1', hash: 'expired' })
.replyWithFile(200, `${__dirname}/test/responses/ERROR-60022.xml`);
scope.post('/ws.do').query(loginParams)
.replyWithFile(200, `${__dirname}/test/responses/LOGIN.xml`);
scope.get('/ws.do').query({ ...transactionParams, request: 'EST1' })
.replyWithFile(200, `${__dirname}/test/responses/EST1.xml`);
scope.get('/ws.do').query({ ...transactionParams, request: 'EST2' })
.replyWithFile(200, `${__dirname}/test/responses/EST2-WAIT.xml`);
scope.get('/ws.do').query({ ...transactionParams, request: 'EST2' })
.replyWithFile(200, `${__dirname}/test/responses/EST2.xml`);
const { STATUS } = await client.transaction('EST', '2423443', 'SDVFAST');
expect(client.params.hash).toBe('11111111111');
expect(STATUS[0]).toBe('0');
}, 20000);
test('should get installation', async () => {
scope.post('/ws.do').query({
...client.params,
numinst: '2423443',
request: 'MYINSTALLATION',
}).replyWithFile(200, `${__dirname}/test/responses/MYINSTALLATION.xml`);
const installation = await client.getInstallation('2423443');
expect(installation.DEVICES.length).toBe(1);
});
test('should get panel status', async () => {
scope.get('/ws.do').query({ ...transactionParams, request: 'EST1' })
.replyWithFile(200, `${__dirname}/test/responses/EST1.xml`);
scope.get('/ws.do').query({ ...transactionParams, request: 'EST2' })
.times(3)
.replyWithFile(200, `${__dirname}/test/responses/EST2-WAIT.xml`);
scope.get('/ws.do').query({ ...transactionParams, request: 'EST2' })
.replyWithFile(200, `${__dirname}/test/responses/EST2.xml`);
const { NUMINST, STATUS } = await client.transaction('EST', '2423443', 'SDVFAST');
expect(NUMINST[0]).toBe('2423443');
expect(STATUS[0]).toBe('0');
});
test('should throw with too many retries', () => {
scope.get('/ws.do').query({ ...transactionParams, request: 'ASD2' })
.times(2)
.replyWithFile(200, `${__dirname}/test/responses/EST2-WAIT.xml`);
return expect(client.transaction('ASD', '2423443', 'SDVFAST', 9)).rejects
.toThrow('Too many retries');
});
});