-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
217 lines (172 loc) · 6.44 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
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
const nock = require('nock');
const Verisure = require('.');
nock.disableNetConnect();
const mockedCookies = [
'vid=myExampleToken',
'vs-access=foo',
'vs-refresh=bar',
];
const scope = nock(/https:\/\/automation0\d.verisure.com/, {
reqheaders: {
cookie: mockedCookies.join(';'),
},
});
describe('Verisure', () => {
const verisure = new Verisure('email', 'password');
beforeEach(() => {
verisure.cookies = mockedCookies;
});
it('should get token', async () => {
const authScope = nock(/https:\/\/automation0\d.verisure.com/);
// Verify retry on different host.
authScope.post('/auth/login').reply(500, 'Not this one');
authScope
.post('/auth/login')
.basicAuth({ user: 'email', pass: 'password' })
.replyWithFile(200, `${__dirname}/test/responses/login.json`, {
'Set-Cookie': 'vid=myExampleToken; Version=1; Path=/; Domain=verisure.com; Secure;',
});
const cookies = await verisure.getToken();
expect.assertions(3);
expect(cookies[0]).toEqual('vid=myExampleToken');
expect(verisure.cookies[0]).toEqual('vid=myExampleToken');
expect(verisure.host).toEqual('automation02.verisure.com');
});
it('should get step up token', async () => {
const authScope = nock(/https:\/\/automation0\d.verisure.com/);
authScope
.post('/auth/login')
.basicAuth({ user: 'email', pass: 'password' })
.replyWithFile(200, `${__dirname}/test/responses/login.json`, {
'Set-Cookie': 'vs-stepup=myStepUpToken; Version=1; Path=/; Domain=verisure.com; Secure;',
});
authScope
.post('/auth/mfa')
.reply(200, '');
const [stepUpCookie] = await verisure.getToken();
expect(stepUpCookie).toEqual('vs-stepup=myStepUpToken');
expect(verisure.getCookie('vs-stepup')).toEqual('vs-stepup=myStepUpToken');
authScope
.post('/auth/mfa/validate', { token: 'ASD123' })
.reply(200, '', {
'Set-Cookie': [
'vid=myToken; Version=1; Path=/; Domain=verisure.com; Secure;',
'vs-access=myAccessToken; Version=1; Path=/; Domain=verisure.com; Secure;',
'vs-refresh=myRefreshToken; Version=1; Path=/; Domain=verisure.com; Secure;',
],
});
const cookies = await verisure.getToken('ASD123');
const expectedCookies = [
'vid=myToken',
'vs-access=myAccessToken',
'vs-refresh=myRefreshToken',
];
expect(cookies).toEqual(expectedCookies);
expect(verisure.cookies).toEqual(expectedCookies);
});
it('should refresh cookies when expired', async () => {
scope
.get('/graphql').reply(500) // Time to switch hosts.
.post('/graphql').reply(401)
.get('/auth/token')
.reply(200, '', {
'Set-Cookie': [
'vid=myNewToken; Version=1; Path=/; Domain=verisure.com; Secure;',
'vs-access=myNewAccessToken; Version=1; Path=/; Domain=verisure.com; Secure;',
'vs-refresh=myNewRefreshToken; Version=1; Path=/; Domain=verisure.com; Secure;',
],
});
nock(/https:\/\/automation0\d.verisure.com/)
.matchHeader('cookie',
'vid=myNewToken;vs-access=myNewAccessToken;vs-refresh=myNewRefreshToken')
.post('/graphql')
.reply(200, { data: 'datadata' });
const response = await verisure.client({
operation: 'something',
});
const expectedCookies = [
'vid=myNewToken',
'vs-access=myNewAccessToken',
'vs-refresh=myNewRefreshToken',
];
expect.assertions(2);
expect(verisure.cookies).toEqual(expectedCookies);
expect(response).toEqual('datadata');
});
it('should throw if unable to refresh cookies', () => {
scope
.post('/graphql').reply(401) // Expired cookies?
.get('/auth/token').reply(401); // Failed to refresh.
return expect(verisure.client({}))
.rejects.toThrowError('Request failed with status code 401');
});
it('should throw if response contains errors', () => {
scope.post('/graphql').reply(200, {
errors: [{
message: 'Syntax Error: Expected Name, found ")".',
data: {
status: 123,
},
}],
});
return expect(verisure.client({}))
.rejects.toThrow('GraphQL response contains 1 errors');
});
it('should get installations', async () => {
scope.post('/graphql')
.replyWithFile(200, `${__dirname}/test/responses/fetch-all-installations.json`);
const installations = await verisure.getInstallations();
expect.assertions(5);
expect(installations.length).toBe(1);
const [installation] = installations;
expect(installation.giid).toBe('123456789');
expect(installation.locale).toBe('sv_SE');
expect(installation.config.locale).toBe('sv_SE');
scope.post('/graphql', { variables: { giid: '123456789' } })
.replyWithFile(200, `${__dirname}/test/responses/broadband.json`);
const broadband = await installation.client({});
expect(typeof broadband).toBe('object');
});
it('should retry once with different host', async () => {
expect.assertions(4);
verisure.host = 'automation01.verisure.com';
const url = '/graphql';
scope
.post(url).reply(200, {
errors: [{
message: 'Request Failed',
data: {
status: 503,
errorGroup: 'SERVICE_UNAVAILABLE',
errorCode: 'SYS_00004',
errorMessage: 'XBN Database is not activated',
},
}],
})
.post(url).reply(200, { data: 'Success' });
const firstResponse = await verisure.client({});
expect(firstResponse).toBe('Success');
expect(verisure.host).toEqual('automation02.verisure.com');
scope
.post(url).reply(500, 'Still not this one')
.post(url).reply(200, { data: 'Success again' });
const secondResponse = await verisure.client({});
expect(secondResponse).toBe('Success again');
expect(verisure.host).toEqual('automation01.verisure.com');
});
it('should reject on errors like timeouts etc', () => {
scope.post('/graphql').replyWithError('Oh no');
return expect(verisure.client({})).rejects.toThrowError('Oh no');
});
it('should reject on response code higher than 299', () => {
scope.post('/graphql').reply(300, 'Doh');
return expect(verisure.client({})).rejects.toThrowError('Request failed with status code 300');
});
it('should make one request when invoked in paralell', () => {
scope.post('/graphql').reply(200, 'Only once');
return Promise.all([
verisure.client({}),
verisure.client({}),
]);
});
});