diff options
Diffstat (limited to 'server/tests/utils/users.js')
-rw-r--r-- | server/tests/utils/users.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js new file mode 100644 index 000000000..ed7a9d672 --- /dev/null +++ b/server/tests/utils/users.js | |||
@@ -0,0 +1,85 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const request = require('supertest') | ||
4 | |||
5 | const usersUtils = { | ||
6 | createUser: createUser, | ||
7 | getUserInformation: getUserInformation, | ||
8 | getUsersList: getUsersList, | ||
9 | removeUser: removeUser, | ||
10 | updateUser: updateUser | ||
11 | } | ||
12 | |||
13 | // ---------------------- Export functions -------------------- | ||
14 | |||
15 | function createUser (url, accessToken, username, password, specialStatus, end) { | ||
16 | if (!end) { | ||
17 | end = specialStatus | ||
18 | specialStatus = 204 | ||
19 | } | ||
20 | |||
21 | const path = '/api/v1/users' | ||
22 | |||
23 | request(url) | ||
24 | .post(path) | ||
25 | .set('Accept', 'application/json') | ||
26 | .set('Authorization', 'Bearer ' + accessToken) | ||
27 | .send({ username: username, password: password }) | ||
28 | .expect(specialStatus) | ||
29 | .end(end) | ||
30 | } | ||
31 | |||
32 | function getUserInformation (url, accessToken, end) { | ||
33 | const path = '/api/v1/users/me' | ||
34 | |||
35 | request(url) | ||
36 | .get(path) | ||
37 | .set('Accept', 'application/json') | ||
38 | .set('Authorization', 'Bearer ' + accessToken) | ||
39 | .expect(200) | ||
40 | .expect('Content-Type', /json/) | ||
41 | .end(end) | ||
42 | } | ||
43 | |||
44 | function getUsersList (url, end) { | ||
45 | const path = '/api/v1/users' | ||
46 | |||
47 | request(url) | ||
48 | .get(path) | ||
49 | .set('Accept', 'application/json') | ||
50 | .expect(200) | ||
51 | .expect('Content-Type', /json/) | ||
52 | .end(end) | ||
53 | } | ||
54 | |||
55 | function removeUser (url, token, username, expectedStatus, end) { | ||
56 | if (!end) { | ||
57 | end = expectedStatus | ||
58 | expectedStatus = 204 | ||
59 | } | ||
60 | |||
61 | const path = '/api/v1/users' | ||
62 | |||
63 | request(url) | ||
64 | .delete(path + '/' + username) | ||
65 | .set('Accept', 'application/json') | ||
66 | .set('Authorization', 'Bearer ' + token) | ||
67 | .expect(expectedStatus) | ||
68 | .end(end) | ||
69 | } | ||
70 | |||
71 | function updateUser (url, userId, accessToken, newPassword, end) { | ||
72 | const path = '/api/v1/users/' + userId | ||
73 | |||
74 | request(url) | ||
75 | .put(path) | ||
76 | .set('Accept', 'application/json') | ||
77 | .set('Authorization', 'Bearer ' + accessToken) | ||
78 | .send({ password: newPassword }) | ||
79 | .expect(204) | ||
80 | .end(end) | ||
81 | } | ||
82 | |||
83 | // --------------------------------------------------------------------------- | ||
84 | |||
85 | module.exports = usersUtils | ||