]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const request = require('supertest') | |
4 | ||
5 | const usersUtils = { | |
6 | createUser, | |
7 | getUserInformation, | |
8 | getUserVideoRating, | |
9 | getUsersList, | |
10 | getUsersListPaginationAndSort, | |
11 | removeUser, | |
12 | updateUser | |
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' | |
24 | const body = { | |
25 | username, | |
26 | password, | |
27 | email: username + '@example.com' | |
28 | } | |
29 | ||
30 | request(url) | |
31 | .post(path) | |
32 | .set('Accept', 'application/json') | |
33 | .set('Authorization', 'Bearer ' + accessToken) | |
34 | .send(body) | |
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 | ||
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 | ||
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 | ||
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 | ||
88 | function removeUser (url, userId, accessToken, expectedStatus, end) { | |
89 | if (!end) { | |
90 | end = expectedStatus | |
91 | expectedStatus = 204 | |
92 | } | |
93 | ||
94 | const path = '/api/v1/users' | |
95 | ||
96 | request(url) | |
97 | .delete(path + '/' + userId) | |
98 | .set('Accept', 'application/json') | |
99 | .set('Authorization', 'Bearer ' + accessToken) | |
100 | .expect(expectedStatus) | |
101 | .end(end) | |
102 | } | |
103 | ||
104 | function updateUser (url, userId, accessToken, newPassword, displayNSFW, end) { | |
105 | const path = '/api/v1/users/' + userId | |
106 | ||
107 | const toSend = {} | |
108 | if (newPassword !== undefined && newPassword !== null) toSend.password = newPassword | |
109 | if (displayNSFW !== undefined && displayNSFW !== null) toSend.displayNSFW = displayNSFW | |
110 | ||
111 | request(url) | |
112 | .put(path) | |
113 | .set('Accept', 'application/json') | |
114 | .set('Authorization', 'Bearer ' + accessToken) | |
115 | .send(toSend) | |
116 | .expect(204) | |
117 | .end(end) | |
118 | } | |
119 | ||
120 | // --------------------------------------------------------------------------- | |
121 | ||
122 | module.exports = usersUtils |