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