]>
Commit | Line | Data |
---|---|---|
1 | import * as request from 'supertest' | |
2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../' | |
3 | ||
4 | import { UserRole } from '../../../../shared/index' | |
5 | import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type' | |
6 | ||
7 | function createUser ( | |
8 | url: string, | |
9 | accessToken: string, | |
10 | username: string, | |
11 | password: string, | |
12 | videoQuota = 1000000, | |
13 | role: UserRole = UserRole.USER, | |
14 | specialStatus = 200 | |
15 | ) { | |
16 | const path = '/api/v1/users' | |
17 | const body = { | |
18 | username, | |
19 | password, | |
20 | role, | |
21 | email: username + '@example.com', | |
22 | videoQuota | |
23 | } | |
24 | ||
25 | return request(url) | |
26 | .post(path) | |
27 | .set('Accept', 'application/json') | |
28 | .set('Authorization', 'Bearer ' + accessToken) | |
29 | .send(body) | |
30 | .expect(specialStatus) | |
31 | } | |
32 | ||
33 | function registerUser (url: string, username: string, password: string, specialStatus = 204) { | |
34 | const path = '/api/v1/users/register' | |
35 | const body = { | |
36 | username, | |
37 | password, | |
38 | email: username + '@example.com' | |
39 | } | |
40 | ||
41 | return request(url) | |
42 | .post(path) | |
43 | .set('Accept', 'application/json') | |
44 | .send(body) | |
45 | .expect(specialStatus) | |
46 | } | |
47 | ||
48 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { | |
49 | const path = '/api/v1/users/me' | |
50 | ||
51 | return request(url) | |
52 | .get(path) | |
53 | .set('Accept', 'application/json') | |
54 | .set('Authorization', 'Bearer ' + accessToken) | |
55 | .expect(specialStatus) | |
56 | .expect('Content-Type', /json/) | |
57 | } | |
58 | ||
59 | function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { | |
60 | const path = '/api/v1/users/me/video-quota-used' | |
61 | ||
62 | return request(url) | |
63 | .get(path) | |
64 | .set('Accept', 'application/json') | |
65 | .set('Authorization', 'Bearer ' + accessToken) | |
66 | .expect(specialStatus) | |
67 | .expect('Content-Type', /json/) | |
68 | } | |
69 | ||
70 | function getUserInformation (url: string, accessToken: string, userId: number) { | |
71 | const path = '/api/v1/users/' + userId | |
72 | ||
73 | return request(url) | |
74 | .get(path) | |
75 | .set('Accept', 'application/json') | |
76 | .set('Authorization', 'Bearer ' + accessToken) | |
77 | .expect(200) | |
78 | .expect('Content-Type', /json/) | |
79 | } | |
80 | ||
81 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { | |
82 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | |
83 | ||
84 | return request(url) | |
85 | .get(path) | |
86 | .set('Accept', 'application/json') | |
87 | .set('Authorization', 'Bearer ' + accessToken) | |
88 | .expect(specialStatus) | |
89 | .expect('Content-Type', /json/) | |
90 | } | |
91 | ||
92 | function getUsersList (url: string, accessToken: string) { | |
93 | const path = '/api/v1/users' | |
94 | ||
95 | return request(url) | |
96 | .get(path) | |
97 | .set('Accept', 'application/json') | |
98 | .set('Authorization', 'Bearer ' + accessToken) | |
99 | .expect(200) | |
100 | .expect('Content-Type', /json/) | |
101 | } | |
102 | ||
103 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) { | |
104 | const path = '/api/v1/users' | |
105 | ||
106 | return request(url) | |
107 | .get(path) | |
108 | .query({ start }) | |
109 | .query({ count }) | |
110 | .query({ sort }) | |
111 | .set('Accept', 'application/json') | |
112 | .set('Authorization', 'Bearer ' + accessToken) | |
113 | .expect(200) | |
114 | .expect('Content-Type', /json/) | |
115 | } | |
116 | ||
117 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
118 | const path = '/api/v1/users' | |
119 | ||
120 | return request(url) | |
121 | .delete(path + '/' + userId) | |
122 | .set('Accept', 'application/json') | |
123 | .set('Authorization', 'Bearer ' + accessToken) | |
124 | .expect(expectedStatus) | |
125 | } | |
126 | ||
127 | function updateMyUser (options: { | |
128 | url: string | |
129 | accessToken: string, | |
130 | newPassword?: string, | |
131 | nsfwPolicy?: NSFWPolicyType, | |
132 | email?: string, | |
133 | autoPlayVideo?: boolean | |
134 | displayName?: string, | |
135 | description?: string | |
136 | }) { | |
137 | const path = '/api/v1/users/me' | |
138 | ||
139 | const toSend = {} | |
140 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword | |
141 | if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy | |
142 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo | |
143 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
144 | if (options.description !== undefined && options.description !== null) toSend['description'] = options.description | |
145 | if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName | |
146 | ||
147 | return makePutBodyRequest({ | |
148 | url: options.url, | |
149 | path, | |
150 | token: options.accessToken, | |
151 | fields: toSend, | |
152 | statusCodeExpected: 204 | |
153 | }) | |
154 | } | |
155 | ||
156 | function updateMyAvatar (options: { | |
157 | url: string, | |
158 | accessToken: string, | |
159 | fixture: string | |
160 | }) { | |
161 | const path = '/api/v1/users/me/avatar/pick' | |
162 | ||
163 | return updateAvatarRequest(Object.assign(options, { path })) | |
164 | } | |
165 | ||
166 | function updateUser (options: { | |
167 | url: string | |
168 | userId: number, | |
169 | accessToken: string, | |
170 | email?: string, | |
171 | videoQuota?: number, | |
172 | role?: UserRole | |
173 | }) { | |
174 | const path = '/api/v1/users/' + options.userId | |
175 | ||
176 | const toSend = {} | |
177 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
178 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota | |
179 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role | |
180 | ||
181 | return makePutBodyRequest({ | |
182 | url: options.url, | |
183 | path, | |
184 | token: options.accessToken, | |
185 | fields: toSend, | |
186 | statusCodeExpected: 204 | |
187 | }) | |
188 | } | |
189 | ||
190 | function askResetPassword (url: string, email: string) { | |
191 | const path = '/api/v1/users/ask-reset-password' | |
192 | ||
193 | return makePostBodyRequest({ | |
194 | url, | |
195 | path, | |
196 | fields: { email }, | |
197 | statusCodeExpected: 204 | |
198 | }) | |
199 | } | |
200 | ||
201 | function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { | |
202 | const path = '/api/v1/users/' + userId + '/reset-password' | |
203 | ||
204 | return makePostBodyRequest({ | |
205 | url, | |
206 | path, | |
207 | fields: { password, verificationString }, | |
208 | statusCodeExpected | |
209 | }) | |
210 | } | |
211 | ||
212 | // --------------------------------------------------------------------------- | |
213 | ||
214 | export { | |
215 | createUser, | |
216 | registerUser, | |
217 | getMyUserInformation, | |
218 | getMyUserVideoRating, | |
219 | getMyUserVideoQuotaUsed, | |
220 | getUsersList, | |
221 | getUsersListPaginationAndSort, | |
222 | removeUser, | |
223 | updateUser, | |
224 | updateMyUser, | |
225 | getUserInformation, | |
226 | askResetPassword, | |
227 | resetPassword, | |
228 | updateMyAvatar | |
229 | } |