Issues (83)

tests/router/status-api-router.test.js (26 issues)

1
/** global: jest */
2
/* global jest describe test expect beforeAll afterAll jasmine */
3
4
// Load local environments
5
require('dotenv').config();
0 ignored issues
show
Expected 1 empty line after require statement not followed by another require.
Loading history...
6
process.env.PORT = 9001;
7
8
const Server = require('../../src/server');
9
const Status = require('../../src/repository/Status');
10
const config = require('../../src/config/server.config.js');
11
const moment = require('moment');
12
13
jest.dontMock('console');
14
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;  // 10s
15
16
const serverPromise = Server.start();
17
18
const statusDataForAllVersion = {
19
  type: 'routineInspection',
20
  deviceTypes: ['android', 'ios'],
21
  contents: 'routineInspection-test1',
22
  isActivated: true,
23
  deviceSemVersion: '*',
24
  appSemVersion: '*',
25
  startTime: new Date(),
26
  endTime: new Date(new Date().getTime() + 3600000),
27
};
28
29
const statusDataForRangedVersion = {
30
  type: 'routineInspection',
31
  deviceTypes: ['android'],
32
  contents: 'routineInspection-test2',
33
  isActivated: true,
34
  deviceSemVersion: '>=1.2.3 < 3.2.1',
35
  appSemVersion: '>=1.2.3 < 3.2.1',
36
  startTime: new Date(),
37
  endTime: new Date(new Date().getTime() + 3600000),
38
};
39
40
const check1 = { deviceType: 'android', deviceVersion: '1.2.2', appVersion: '3.3.3' };
41
const check2 = { deviceType: '*', deviceVersion: '*', appVersion: '*' };
42
const check3 = { deviceType: 'paper', deviceVersion: '1.2.3', appVersion: '3.4.5' };
43
const check4 = { deviceType: '*', deviceVersion: 'invalid-version', appVersion: 'invalid-version' };
44
45
const statusDataToBeAdded = {
46
  type: 'routineInspection',
47
  device_types: ['android', 'ios'],
48
  contents: 'routineInspection-added1',
49
  is_activated: true,
50
  device_sem_version: '*',
51
  app_sem_version: '*',
52
  start_time: moment().format(),
53
  end_time: moment().add(2, 'hours').format(),
54
  url: 'http://localhost:8080/added1',
55
  title: 'title-added1',
56
};
57
58
const statusDataToBeUpdated = {
59
  type: 'routineInspection',
60
  device_types: ['paper', 'ios'],
61
  contents: 'routineInspection-added1-updated1',
62
  is_activated: true,
63
  device_sem_version: '>=1.2.3',
64
  app_sem_version: '>=4.5.6 || =1.2.3',
65
  start_time: moment().format(),
66
  end_time: moment().add(3, 'hours').format(),
67
  url: 'http://localhost:8080/added1-updated1',
68
  title: 'title-added1-updated1',
69
};
70
71
const statusDataToBeUpdatedForAllDay = {
72
  type: 'routineInspection',
73
  device_types: ['paper', 'ios'],
74
  contents: 'routineInspection-added1-updated1',
75
  is_activated: true,
76
  device_sem_version: '>=1.2.3',
77
  app_sem_version: '>=4.5.6 || =1.2.3',
78
  url: 'http://localhost:8080/added1-updated1',
79
  title: 'title-added1-updated1',
80
};
81
82
const makeUrl = check => `${config.url.statusApiPrefix}/check?device_type=${check.deviceType}&device_version=${check.deviceVersion}&app_version=${check.appVersion}`;
0 ignored issues
show
This line exceeds the maximum configured line length of 120.
Loading history...
83
84
describe('status', () => {
85
  describe('checkStatus', () => {
86
    let ids = [];
87
88
    beforeAll(() => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
89
      return Promise.all([
90
        Status.add(statusDataForAllVersion),
91
        Status.add(statusDataForRangedVersion),
92
      ]).then((results) => {
93
        ids = results.map(result => result.data[0]._id);
94
      });
95
    });
96
97
    test('matches one', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
98
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
99
        return server.inject({ url: makeUrl(check1) }).then((response) => {
100
          expect(response.statusCode).toBe(200);
101
          const payload = JSON.parse(response.payload);
102
          expect(payload.success).toBe(true);
103
          expect(payload.data).toBeDefined();
104
          expect(payload.data.length).toBeGreaterThan(0);
105
          expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeTruthy();
106
          expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeFalsy();
107
        });
108
      });
109
    });
110
111
    test('matches all', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
112
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
113
        return server.inject({ url: makeUrl(check2) }).then((response) => {
114
          expect(response.statusCode).toBe(200);
115
          const payload = JSON.parse(response.payload);
116
          expect(payload.success).toBe(true);
117
          expect(payload.data).toBeDefined();
118
          expect(payload.data.length).toBeGreaterThanOrEqual(2);
119
          expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeTruthy();
120
          expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeTruthy();
121
        });
122
      });
123
    });
124
125
    test('matches nothing', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
126
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
127
        return server.inject({ url: makeUrl(check3) }).then((response) => {
128
          expect(response.statusCode).toBe(200);
129
          const payload = JSON.parse(response.payload);
130
          expect(payload.success).toBe(true);
131
          expect(payload.data).toBeDefined();
132
          expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeFalsy();
133
          expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeFalsy();
134
        });
135
      });
136
    });
137
138
    test('invalid version', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
139
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
140
        return server.inject({ url: makeUrl(check4) }).then((response) => {
141
          expect(response.statusCode).toBe(400);
142
          const payload = JSON.parse(response.payload);
143
          expect(payload.code).toBe(400100);
144
          expect(payload.success).toBe(false);
145
        });
146
      });
147
    });
148
149
    afterAll(() => Promise.all(ids.map(id => Status.remove(id))));
150
  });
151
152
  describe('ordering by startTime ASC', () => {
153
    let added;
154
    const statusData = {
155
      type: 'routineInspection',
156
      deviceTypes: ['android', 'ios'],
157
      contents: 'routineInspection-test1',
158
      isActivated: true,
159
      deviceSemVersion: '*',
160
      appSemVersion: '*',
161
      startTime: new Date(),
162
      endTime: new Date(new Date().getTime() + 3600000),
163
    };
164
    beforeAll(() => {
165
      const items = [];
166
      for (let i = 0; i < 10; i++) {
0 ignored issues
show
As per coding-style, please do not use ++, but use += 1 instead.
Loading history...
167
        items.push(Object.assign({}, statusData, {
168
          startTime: new Date(new Date().getTime() - Math.floor((Math.random() * 3600000))),
169
        }));
170
      }
171
172
      return Promise.all(items.map(item => Status.add(item)))
173
        .then((results) => {
174
          added = results.map(result => result.data[0]._id);
175
        });
176
    });
177
178
    test('ordering by startTime ASC', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
179
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
180
        return server.inject({ url: makeUrl(check2) }).then((response) => {
181
          expect(response.statusCode).toBe(200);
182
          const payload = JSON.parse(response.payload);
183
          expect(payload.data).toBeDefined();
184
          expect(payload.data.length).toBeGreaterThanOrEqual(10);
185
186
          let previous;
187
          payload.data.forEach((d) => {
188
            if (previous && previous.start_time && d.start_time) {
189
              expect(new Date(d.start_time).getTime()).toBeGreaterThanOrEqual(new Date(previous.start_time).getTime());
190
            }
191
            if (d.start_time) {
192
              previous = d;
193
            }
194
          });
195
        });
196
      });
197
    });
198
199
    afterAll(() => Promise.all(added.map(id => Status.remove(id))));
200
  });
201
202
  describe('status CRUD', () => {
203
    const url = config.url.statusApiPrefix;
204
205
    test('add status', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
206
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
207
        return server.inject({
208
          url,
209
          method: 'POST',
210
          payload: statusDataToBeAdded,
211
          credentials: { username: 'admin' },
212
        }).then((response) => {
213
          expect(response.statusCode).toBe(200);
214
          const payload = JSON.parse(response.payload);
215
          expect(payload.success).toBe(true);
216
          expect(payload.data[0].contents).toBe(statusDataToBeAdded.contents);
217
          expect(payload.data[0].url).toBe(statusDataToBeAdded.url);
218
          expect(payload.data[0].title).toBe(statusDataToBeAdded.title);
219
          statusDataToBeAdded.id = payload.data[0].id;
220
        });
221
      });
222
    });
223
224
    test('update status', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
225
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
226
        return server.inject({
227
          url: `${url}/${statusDataToBeAdded.id}`,
228
          method: 'PUT',
229
          payload: statusDataToBeUpdated,
230
          credentials: { username: 'admin' },
231
        }).then((response) => {
232
          expect(response.statusCode).toBe(200);
233
          const payload = JSON.parse(response.payload);
234
          expect(payload.success).toBe(true);
235
          expect(payload.data[0].id).toBe(statusDataToBeAdded.id);
236
          return payload;
237
        }).then(payload => Status.find({ _id: payload.data[0].id }))
238
          .then((result) => {
239
            expect(result.length).toBe(1);
240
            expect(result[0].contents).toBe(statusDataToBeUpdated.contents);
241
            expect(result[0].deviceTypes).toEqual(statusDataToBeUpdated.device_types);
242
            expect(result[0].deviceSemVersion).toBe(statusDataToBeUpdated.device_sem_version);
243
            expect(result[0].appSemVersion).toBe(statusDataToBeUpdated.app_sem_version);
244
            expect(result[0].url).toBe(statusDataToBeUpdated.url);
245
            expect(moment(result[0].startTime).format()).toBe(statusDataToBeUpdated.start_time);
246
            expect(moment(result[0].endTime).format()).toBe(statusDataToBeUpdated.end_time);
247
            expect(result[0].title).toBe(statusDataToBeUpdated.title);
248
          });
249
      });
250
    });
251
252
    test('update status for all day', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
253
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
254
        return server.inject({
255
          url: `${url}/${statusDataToBeAdded.id}`,
256
          method: 'PUT',
257
          payload: statusDataToBeUpdatedForAllDay,
258
          credentials: { username: 'admin' },
259
        }).then((response) => {
260
          expect(response.statusCode).toBe(200);
261
          const payload = JSON.parse(response.payload);
262
          expect(payload.success).toBe(true);
263
          expect(payload.data[0].id).toBe(statusDataToBeAdded.id);
264
          return payload;
265
        }).then(payload => Status.find({ _id: payload.data[0].id }))
266
          .then((result) => {
267
            expect(result.length).toBe(1);
268
            expect(result[0].contents).toBe(statusDataToBeUpdatedForAllDay.contents);
269
            expect(result[0].deviceTypes).toEqual(statusDataToBeUpdatedForAllDay.device_types);
270
            expect(result[0].deviceSemVersion).toBe(statusDataToBeUpdatedForAllDay.device_sem_version);
271
            expect(result[0].appSemVersion).toBe(statusDataToBeUpdatedForAllDay.app_sem_version);
272
            expect(result[0].startTime).toBe(undefined);
273
            expect(result[0].endTime).toBe(undefined);
274
            expect(result[0].title).toBe(statusDataToBeUpdatedForAllDay.title);
275
          });
276
      });
277
    });
278
279
    test('deactivate status', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
280
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
281
        return server.inject({
282
          url: `${url}/${statusDataToBeAdded.id}/deactivate`,
283
          method: 'PUT',
284
          credentials: { username: 'admin' },
285
        }).then((response) => {
286
          expect(response.statusCode).toBe(200);
287
          const payload = JSON.parse(response.payload);
288
          expect(payload.success).toBe(true);
289
          expect(payload.data[0].id).toBe(statusDataToBeAdded.id);
290
          return payload;
291
        }).then(payload => Status.find({ _id: payload.data[0].id }))
292
          .then((result) => {
293
            expect(result.length).toBe(1);
294
            expect(result[0].isActivated).toBe(false);
295
          });
296
      });
297
    });
298
299
    test('activate status', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
300
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
301
        return server.inject({
302
          url: `${url}/${statusDataToBeAdded.id}/activate`,
303
          method: 'PUT',
304
          credentials: { username: 'admin' },
305
        }).then((response) => {
306
          expect(response.statusCode).toBe(200);
307
          const payload = JSON.parse(response.payload);
308
          expect(payload.success).toBe(true);
309
          expect(payload.data[0].id).toBe(statusDataToBeAdded.id);
310
          return payload;
311
        }).then(payload => Status.find({ _id: payload.data[0].id }))
312
          .then((result) => {
313
            expect(result.length).toBe(1);
314
            expect(result[0].isActivated).toBe(true);
315
          });
316
      });
317
    });
318
319
    test('remove status', () => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
320
      return serverPromise.then((server) => {
0 ignored issues
show
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
321
        return server.inject({
322
          url: `${url}/${statusDataToBeAdded.id}`,
323
          method: 'DELETE',
324
          credentials: { username: 'admin' },
325
        }).then((response) => {
326
          expect(response.statusCode).toBe(200);
327
          const payload = JSON.parse(response.payload);
328
          expect(payload.success).toBe(true);
329
          expect(payload.data[0].id).toBe(statusDataToBeAdded.id);
330
        });
331
      });
332
    });
333
  });
334
});
335