]>
Commit | Line | Data |
---|---|---|
8d309058 C |
1 | 'use strict' |
2 | ||
3 | const request = require('supertest') | |
4 | ||
5 | const usersUtils = { | |
c4403b29 C |
6 | createUser, |
7 | getUserInformation, | |
8 | getUsersList, | |
9 | getUsersListPaginationAndSort, | |
10 | removeUser, | |
11 | updateUser | |
8d309058 C |
12 | } |
13 | ||
14 | // ---------------------- Export functions -------------------- | |
15 | ||
16 | function createUser (url, accessToken, username, password, specialStatus, end) { | |
17 | if (!end) { | |
18 | end = specialStatus | |
19 | specialStatus = 204 | |
20 | } | |
21 | ||
22 | const path = '/api/v1/users' | |
23 | ||
24 | request(url) | |
25 | .post(path) | |
26 | .set('Accept', 'application/json') | |
27 | .set('Authorization', 'Bearer ' + accessToken) | |
28 | .send({ username: username, password: password }) | |
29 | .expect(specialStatus) | |
30 | .end(end) | |
31 | } | |
32 | ||
33 | function getUserInformation (url, accessToken, end) { | |
34 | const path = '/api/v1/users/me' | |
35 | ||
36 | request(url) | |
37 | .get(path) | |
38 | .set('Accept', 'application/json') | |
39 | .set('Authorization', 'Bearer ' + accessToken) | |
40 | .expect(200) | |
41 | .expect('Content-Type', /json/) | |
42 | .end(end) | |
43 | } | |
44 | ||
45 | function getUsersList (url, end) { | |
46 | const path = '/api/v1/users' | |
47 | ||
48 | request(url) | |
49 | .get(path) | |
50 | .set('Accept', 'application/json') | |
51 | .expect(200) | |
52 | .expect('Content-Type', /json/) | |
53 | .end(end) | |
54 | } | |
55 | ||
5c39adb7 C |
56 | function getUsersListPaginationAndSort (url, start, count, sort, end) { |
57 | const path = '/api/v1/users' | |
58 | ||
59 | request(url) | |
60 | .get(path) | |
61 | .query({ start: start }) | |
62 | .query({ count: count }) | |
63 | .query({ sort: sort }) | |
64 | .set('Accept', 'application/json') | |
65 | .expect(200) | |
66 | .expect('Content-Type', /json/) | |
67 | .end(end) | |
68 | } | |
69 | ||
68a3b9f2 | 70 | function removeUser (url, userId, accessToken, expectedStatus, end) { |
8d309058 C |
71 | if (!end) { |
72 | end = expectedStatus | |
73 | expectedStatus = 204 | |
74 | } | |
75 | ||
76 | const path = '/api/v1/users' | |
77 | ||
78 | request(url) | |
68a3b9f2 | 79 | .delete(path + '/' + userId) |
8d309058 | 80 | .set('Accept', 'application/json') |
68a3b9f2 | 81 | .set('Authorization', 'Bearer ' + accessToken) |
8d309058 C |
82 | .expect(expectedStatus) |
83 | .end(end) | |
84 | } | |
85 | ||
86 | function updateUser (url, userId, accessToken, newPassword, end) { | |
87 | const path = '/api/v1/users/' + userId | |
88 | ||
89 | request(url) | |
90 | .put(path) | |
91 | .set('Accept', 'application/json') | |
92 | .set('Authorization', 'Bearer ' + accessToken) | |
93 | .send({ password: newPassword }) | |
94 | .expect(204) | |
95 | .end(end) | |
96 | } | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | module.exports = usersUtils |