]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/users/users.ts
Add reason when banning a user
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
CommitLineData
0e1dc3e7 1import * as request from 'supertest'
4bbfc6c6 2import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
0e1dc3e7 3
c5d31dba 4import { UserRole } from '../../../../shared/index'
0883b324 5import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type'
757f0da3
C
6
7function createUser (
8 url: string,
9 accessToken: string,
10 username: string,
11 password: string,
12 videoQuota = 1000000,
13 role: UserRole = UserRole.USER,
f05a1c30 14 specialStatus = 200
757f0da3 15) {
0e1dc3e7
C
16 const path = '/api/v1/users'
17 const body = {
18 username,
19 password,
757f0da3 20 role,
5c98d3bf
C
21 email: username + '@example.com',
22 videoQuota
0e1dc3e7
C
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
33function 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
26d21b78 48function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
0e1dc3e7
C
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)
26d21b78 55 .expect(specialStatus)
0e1dc3e7
C
56 .expect('Content-Type', /json/)
57}
58
92b9d60c
C
59function deleteMe (url: string, accessToken: string, specialStatus = 204) {
60 const path = '/api/v1/users/me'
61
62 return request(url)
63 .delete(path)
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
66 .expect(specialStatus)
67}
68
ce5496d6
C
69function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
70 const path = '/api/v1/users/me/video-quota-used'
71
72 return request(url)
73 .get(path)
74 .set('Accept', 'application/json')
75 .set('Authorization', 'Bearer ' + accessToken)
76 .expect(specialStatus)
77 .expect('Content-Type', /json/)
78}
79
5c98d3bf
C
80function getUserInformation (url: string, accessToken: string, userId: number) {
81 const path = '/api/v1/users/' + userId
82
83 return request(url)
84 .get(path)
85 .set('Accept', 'application/json')
86 .set('Authorization', 'Bearer ' + accessToken)
87 .expect(200)
88 .expect('Content-Type', /json/)
89}
90
26d21b78 91function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
0e1dc3e7
C
92 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
93
94 return request(url)
95 .get(path)
96 .set('Accept', 'application/json')
97 .set('Authorization', 'Bearer ' + accessToken)
26d21b78 98 .expect(specialStatus)
0e1dc3e7
C
99 .expect('Content-Type', /json/)
100}
101
86d13ec2 102function getUsersList (url: string, accessToken: string) {
0e1dc3e7
C
103 const path = '/api/v1/users'
104
105 return request(url)
106 .get(path)
107 .set('Accept', 'application/json')
86d13ec2 108 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
109 .expect(200)
110 .expect('Content-Type', /json/)
111}
112
86d13ec2 113function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
0e1dc3e7
C
114 const path = '/api/v1/users'
115
116 return request(url)
117 .get(path)
118 .query({ start })
119 .query({ count })
120 .query({ sort })
121 .set('Accept', 'application/json')
86d13ec2 122 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
123 .expect(200)
124 .expect('Content-Type', /json/)
125}
126
26d21b78 127function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
0e1dc3e7
C
128 const path = '/api/v1/users'
129
130 return request(url)
131 .delete(path + '/' + userId)
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
134 .expect(expectedStatus)
135}
136
eacb25c4 137function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
e6921918 138 const path = '/api/v1/users'
eacb25c4
C
139 let body: any
140 if (reason) body = { reason }
e6921918
C
141
142 return request(url)
143 .post(path + '/' + userId + '/block')
eacb25c4 144 .send(body)
e6921918
C
145 .set('Accept', 'application/json')
146 .set('Authorization', 'Bearer ' + accessToken)
147 .expect(expectedStatus)
148}
149
150function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
151 const path = '/api/v1/users'
152
153 return request(url)
154 .post(path + '/' + userId + '/unblock')
155 .set('Accept', 'application/json')
156 .set('Authorization', 'Bearer ' + accessToken)
157 .expect(expectedStatus)
158}
159
26d21b78
C
160function updateMyUser (options: {
161 url: string
162 accessToken: string,
163 newPassword?: string,
0883b324 164 nsfwPolicy?: NSFWPolicyType,
26d21b78
C
165 email?: string,
166 autoPlayVideo?: boolean
ed56ad11 167 displayName?: string,
2422c46b 168 description?: string
26d21b78 169}) {
5c98d3bf 170 const path = '/api/v1/users/me'
0e1dc3e7
C
171
172 const toSend = {}
26d21b78 173 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
0883b324 174 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
26d21b78
C
175 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
176 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
2422c46b 177 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
ed56ad11 178 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
26d21b78
C
179
180 return makePutBodyRequest({
181 url: options.url,
182 path,
183 token: options.accessToken,
184 fields: toSend,
185 statusCodeExpected: 204
186 })
5c98d3bf
C
187}
188
c5911fd3
C
189function updateMyAvatar (options: {
190 url: string,
191 accessToken: string,
192 fixture: string
193}) {
194 const path = '/api/v1/users/me/avatar/pick'
c5911fd3 195
4bbfc6c6 196 return updateAvatarRequest(Object.assign(options, { path }))
c5911fd3
C
197}
198
26d21b78
C
199function updateUser (options: {
200 url: string
201 userId: number,
202 accessToken: string,
203 email?: string,
204 videoQuota?: number,
205 role?: UserRole
206}) {
207 const path = '/api/v1/users/' + options.userId
5c98d3bf
C
208
209 const toSend = {}
26d21b78
C
210 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
211 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
212 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
213
214 return makePutBodyRequest({
215 url: options.url,
216 path,
217 token: options.accessToken,
218 fields: toSend,
219 statusCodeExpected: 204
220 })
0e1dc3e7
C
221}
222
f076daa7
C
223function askResetPassword (url: string, email: string) {
224 const path = '/api/v1/users/ask-reset-password'
225
226 return makePostBodyRequest({
227 url,
228 path,
229 fields: { email },
230 statusCodeExpected: 204
231 })
232}
233
234function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
235 const path = '/api/v1/users/' + userId + '/reset-password'
236
237 return makePostBodyRequest({
238 url,
239 path,
240 fields: { password, verificationString },
241 statusCodeExpected
242 })
243}
244
0e1dc3e7
C
245// ---------------------------------------------------------------------------
246
247export {
248 createUser,
249 registerUser,
5c98d3bf 250 getMyUserInformation,
26d21b78 251 getMyUserVideoRating,
92b9d60c 252 deleteMe,
ce5496d6 253 getMyUserVideoQuotaUsed,
0e1dc3e7
C
254 getUsersList,
255 getUsersListPaginationAndSort,
256 removeUser,
5c98d3bf
C
257 updateUser,
258 updateMyUser,
c5911fd3 259 getUserInformation,
e6921918
C
260 blockUser,
261 unblockUser,
f076daa7
C
262 askResetPassword,
263 resetPassword,
c5911fd3 264 updateMyAvatar
0e1dc3e7 265}