-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
239 lines (222 loc) · 7.37 KB
/
index.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
const assert = require('assert');
const axios = require('axios');
const nock = require('nock');
const Eurecia = require('./index');
describe('Eurecia', () => {
const baseUrl = 'https://api.eurecia.com';
let eurecia;
axios.defaults.adapter = 'http';
before(() => {
eurecia = new Eurecia();
});
after(() => {});
beforeEach(() => {});
afterEach(() => {});
describe('authenticate', () => {
it('should throw an error when axios throws', async () => {
const scope = nock(baseUrl)
.get('/eurecia/rest/v1/Auth')
.matchHeader('Accept', 'application/json')
.matchHeader('authToken', 'invalid-token')
.reply(500, null);
try {
await eurecia.authenticate('invalid-token');
} catch (error) {
assert.strictEqual(error.message, 'Invalid token (#1)');
scope.done();
}
});
it('should throw an error for a non 200 http code', async () => {
const scope = nock(baseUrl)
.get('/eurecia/rest/v1/Auth')
.matchHeader('Accept', 'application/json')
.matchHeader('authToken', 'invalid-token')
.reply(204, null);
try {
await eurecia.authenticate('invalid-token');
} catch (error) {
assert.strictEqual(error.message, 'Invalid token (#2)');
scope.done();
}
});
it('should throw an error for an invalid returned object', async () => {
const response = {
eureciaResponse: {
temporaryToken: {
token: {},
},
},
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v1/Auth')
.matchHeader('Accept', 'application/json')
.matchHeader('authToken', 'valid-token')
.reply(200, response);
try {
await eurecia.authenticate('valid-token');
} catch (error) {
assert.strictEqual(error.message, 'Invalid token (#3)');
scope.done();
}
});
it('should set the instance with the correct token', async () => {
const response = {
eureciaResponse: {
temporaryToken: {
token: {
$: 'temporary-token',
},
},
},
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v1/Auth')
.matchHeader('Accept', 'application/json')
.matchHeader('authToken', 'valid-token')
.reply(200, response);
await eurecia.authenticate('valid-token');
assert.strictEqual(eurecia.instance.defaults.headers.token, 'temporary-token');
scope.done();
});
});
describe('getCompanies', () => {
it('should return the companies', async () => {
const response = {
data: ['company1', 'company2'],
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/companies?page=0')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const companies = await eurecia.getCompanies();
assert.deepStrictEqual(companies, response);
scope.done();
});
});
describe('getAllCompanies', () => {
it('should return all the companies', async () => {
const response1 = {
content: ['company1', 'company2'],
totalPages: 2,
};
const response2 = {
content: ['company3', 'company4'],
};
const scope1 = nock(baseUrl)
.get('/eurecia/rest/v4/companies?page=0')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response1);
const scope2 = nock(baseUrl)
.get('/eurecia/rest/v4/companies?page=1')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response2);
const companies = await eurecia.getAllCompanies();
assert.deepStrictEqual(companies, ['company1', 'company2', 'company3', 'company4']);
scope1.done();
scope2.done();
});
});
describe('getEmployees', () => {
it('should return the employees', async () => {
const response = {
data: ['employee1', 'employee2'],
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/users/active?page=0')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const employees = await eurecia.getEmployees();
assert.deepStrictEqual(employees, response);
scope.done();
});
});
describe('getAllEmployees', () => {
it('should return all the employees', async () => {
const response1 = {
content: ['employee1', 'employee2'],
totalPages: 2,
};
const response2 = {
content: ['employee3', 'employee4'],
};
const scope1 = nock(baseUrl)
.get('/eurecia/rest/v4/users/active?page=0')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response1);
const scope2 = nock(baseUrl)
.get('/eurecia/rest/v4/users/active?page=1')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response2);
const employees = await eurecia.getAllEmployees();
assert.deepStrictEqual(employees, ['employee1', 'employee2', 'employee3', 'employee4']);
scope1.done();
scope2.done();
});
});
describe('getEmployeeById', () => {
it('should return the employee', async () => {
const response = {
data: 'employee',
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/users/1')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const employee = await eurecia.getEmployeeById(1);
assert.deepStrictEqual(employee, response);
scope.done();
});
});
describe('getDepartments', () => {
it('should return the departments', async () => {
const response = {
nodes: ['department1', 'department2'],
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/departments')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const departments = await eurecia.getDepartments();
assert.deepStrictEqual(departments, response.nodes);
scope.done();
});
});
describe('getStructures', () => {
it('should return the structures', async () => {
const response = {
nodes: ['structure1', 'structure2'],
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/structures')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const structures = await eurecia.getStructures();
assert.deepStrictEqual(structures, response.nodes);
scope.done();
});
});
describe('getPayrollGrids', () => {
it('should return the payroll grids', async () => {
const response = {
data: ['payrollGrid1', 'payrollGrid2'],
};
const scope = nock(baseUrl)
.get('/eurecia/rest/v4/payrollGrids')
.matchHeader('Accept', 'application/json')
.matchHeader('token', 'temporary-token')
.reply(200, response);
const payrollGrids = await eurecia.getPayrollGrids();
assert.deepStrictEqual(payrollGrids, response);
scope.done();
});
});
});