]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users/users.ts
Implement user blocking on server side
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
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 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
69 function 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
80 function 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
91 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
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)
98 .expect(specialStatus)
99 .expect('Content-Type', /json/)
100 }
101
102 function getUsersList (url: string, accessToken: string) {
103 const path = '/api/v1/users'
104
105 return request(url)
106 .get(path)
107 .set('Accept', 'application/json')
108 .set('Authorization', 'Bearer ' + accessToken)
109 .expect(200)
110 .expect('Content-Type', /json/)
111 }
112
113 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
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')
122 .set('Authorization', 'Bearer ' + accessToken)
123 .expect(200)
124 .expect('Content-Type', /json/)
125 }
126
127 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
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
137 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
138 const path = '/api/v1/users'
139
140 return request(url)
141 .post(path + '/' + userId + '/block')
142 .set('Accept', 'application/json')
143 .set('Authorization', 'Bearer ' + accessToken)
144 .expect(expectedStatus)
145 }
146
147 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
148 const path = '/api/v1/users'
149
150 return request(url)
151 .post(path + '/' + userId + '/unblock')
152 .set('Accept', 'application/json')
153 .set('Authorization', 'Bearer ' + accessToken)
154 .expect(expectedStatus)
155 }
156
157 function updateMyUser (options: {
158 url: string
159 accessToken: string,
160 newPassword?: string,
161 nsfwPolicy?: NSFWPolicyType,
162 email?: string,
163 autoPlayVideo?: boolean
164 displayName?: string,
165 description?: string
166 }) {
167 const path = '/api/v1/users/me'
168
169 const toSend = {}
170 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
171 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
172 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
173 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
174 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
175 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
176
177 return makePutBodyRequest({
178 url: options.url,
179 path,
180 token: options.accessToken,
181 fields: toSend,
182 statusCodeExpected: 204
183 })
184 }
185
186 function updateMyAvatar (options: {
187 url: string,
188 accessToken: string,
189 fixture: string
190 }) {
191 const path = '/api/v1/users/me/avatar/pick'
192
193 return updateAvatarRequest(Object.assign(options, { path }))
194 }
195
196 function updateUser (options: {
197 url: string
198 userId: number,
199 accessToken: string,
200 email?: string,
201 videoQuota?: number,
202 role?: UserRole
203 }) {
204 const path = '/api/v1/users/' + options.userId
205
206 const toSend = {}
207 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
208 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
209 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
210
211 return makePutBodyRequest({
212 url: options.url,
213 path,
214 token: options.accessToken,
215 fields: toSend,
216 statusCodeExpected: 204
217 })
218 }
219
220 function askResetPassword (url: string, email: string) {
221 const path = '/api/v1/users/ask-reset-password'
222
223 return makePostBodyRequest({
224 url,
225 path,
226 fields: { email },
227 statusCodeExpected: 204
228 })
229 }
230
231 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
232 const path = '/api/v1/users/' + userId + '/reset-password'
233
234 return makePostBodyRequest({
235 url,
236 path,
237 fields: { password, verificationString },
238 statusCodeExpected
239 })
240 }
241
242 // ---------------------------------------------------------------------------
243
244 export {
245 createUser,
246 registerUser,
247 getMyUserInformation,
248 getMyUserVideoRating,
249 deleteMe,
250 getMyUserVideoQuotaUsed,
251 getUsersList,
252 getUsersListPaginationAndSort,
253 removeUser,
254 updateUser,
255 updateMyUser,
256 getUserInformation,
257 blockUser,
258 unblockUser,
259 askResetPassword,
260 resetPassword,
261 updateMyAvatar
262 }