1 import { omit } from 'lodash'
2 import * as request from 'supertest'
3 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 import { UserUpdateMe } from '../../models/users'
5 import { UserAdminFlag } from '../../models/users/user-flag.model'
6 import { UserRegister } from '../../models/users/user-register.model'
7 import { UserRole } from '../../models/users/user-role'
8 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateImageRequest } from '../requests/requests'
9 import { ServerInfo } from '../server/servers'
10 import { userLogin } from './login'
12 function createUser (parameters: {
18 videoQuotaDaily?: number
20 adminFlags?: UserAdminFlag
21 specialStatus?: number
28 password = 'password',
32 specialStatus = HttpStatusCode.OK_200
35 const path = '/api/v1/users'
41 email: username + '@example.com',
48 .set('Accept', 'application/json')
49 .set('Authorization', 'Bearer ' + accessToken)
51 .expect(specialStatus)
54 async function generateUser (server: ServerInfo, username: string) {
55 const password = 'my super password'
56 const resCreate = await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
58 const token = await userLogin(server, { username, password })
60 const resMe = await getMyUserInformation(server.url, token)
64 userId: resCreate.body.user.id,
65 userChannelId: resMe.body.videoChannels[0].id
69 async function generateUserAccessToken (server: ServerInfo, username: string) {
70 const password = 'my super password'
71 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
73 return userLogin(server, { username, password })
76 function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
77 const path = '/api/v1/users/register'
81 email: username + '@example.com'
86 .set('Accept', 'application/json')
88 .expect(specialStatus)
91 function registerUserWithChannel (options: {
93 user: { username: string, password: string, displayName?: string }
94 channel: { name: string, displayName: string }
96 const path = '/api/v1/users/register'
97 const body: UserRegister = {
98 username: options.user.username,
99 password: options.user.password,
100 email: options.user.username + '@example.com',
101 channel: options.channel
104 if (options.user.displayName) {
105 Object.assign(body, { displayName: options.user.displayName })
108 return makePostBodyRequest({
112 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
116 function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
117 const path = '/api/v1/users/me'
121 .set('Accept', 'application/json')
122 .set('Authorization', 'Bearer ' + accessToken)
123 .expect(specialStatus)
124 .expect('Content-Type', /json/)
127 function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
128 const path = '/api/v1/users/scoped-tokens'
130 return makeGetRequest({
138 function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
139 const path = '/api/v1/users/scoped-tokens'
141 return makePostBodyRequest({
149 function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
150 const path = '/api/v1/users/me'
154 .set('Accept', 'application/json')
155 .set('Authorization', 'Bearer ' + accessToken)
156 .expect(specialStatus)
159 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
160 const path = '/api/v1/users/me/video-quota-used'
164 .set('Accept', 'application/json')
165 .set('Authorization', 'Bearer ' + accessToken)
166 .expect(specialStatus)
167 .expect('Content-Type', /json/)
170 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
171 const path = '/api/v1/users/' + userId
175 .query({ withStats })
176 .set('Accept', 'application/json')
177 .set('Authorization', 'Bearer ' + accessToken)
178 .expect(HttpStatusCode.OK_200)
179 .expect('Content-Type', /json/)
182 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
183 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
187 .set('Accept', 'application/json')
188 .set('Authorization', 'Bearer ' + accessToken)
189 .expect(specialStatus)
190 .expect('Content-Type', /json/)
193 function getUsersList (url: string, accessToken: string) {
194 const path = '/api/v1/users'
198 .set('Accept', 'application/json')
199 .set('Authorization', 'Bearer ' + accessToken)
200 .expect(HttpStatusCode.OK_200)
201 .expect('Content-Type', /json/)
204 function getUsersListPaginationAndSort (
213 const path = '/api/v1/users'
226 .set('Accept', 'application/json')
227 .set('Authorization', 'Bearer ' + accessToken)
228 .expect(HttpStatusCode.OK_200)
229 .expect('Content-Type', /json/)
232 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
233 const path = '/api/v1/users'
236 .delete(path + '/' + userId)
237 .set('Accept', 'application/json')
238 .set('Authorization', 'Bearer ' + accessToken)
239 .expect(expectedStatus)
244 userId: number | string,
246 expectedStatus = HttpStatusCode.NO_CONTENT_204,
249 const path = '/api/v1/users'
251 if (reason) body = { reason }
254 .post(path + '/' + userId + '/block')
256 .set('Accept', 'application/json')
257 .set('Authorization', 'Bearer ' + accessToken)
258 .expect(expectedStatus)
261 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
262 const path = '/api/v1/users'
265 .post(path + '/' + userId + '/unblock')
266 .set('Accept', 'application/json')
267 .set('Authorization', 'Bearer ' + accessToken)
268 .expect(expectedStatus)
271 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
272 const path = '/api/v1/users/me'
274 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
276 return makePutBodyRequest({
279 token: options.accessToken,
281 statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
285 function updateMyAvatar (options: {
290 const path = '/api/v1/users/me/avatar/pick'
292 return updateImageRequest({ ...options, path, fieldname: 'avatarfile' })
295 function updateUser (options: {
300 emailVerified?: boolean
302 videoQuotaDaily?: number
304 adminFlags?: UserAdminFlag
308 const path = '/api/v1/users/' + options.userId
311 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
312 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
313 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
314 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
315 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
316 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
317 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
318 if (options.pluginAuth !== undefined) toSend['pluginAuth'] = options.pluginAuth
320 return makePutBodyRequest({
323 token: options.accessToken,
325 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
329 function askResetPassword (url: string, email: string) {
330 const path = '/api/v1/users/ask-reset-password'
332 return makePostBodyRequest({
336 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
340 function resetPassword (
343 verificationString: string,
345 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
347 const path = '/api/v1/users/' + userId + '/reset-password'
349 return makePostBodyRequest({
352 fields: { password, verificationString },
357 function askSendVerifyEmail (url: string, email: string) {
358 const path = '/api/v1/users/ask-send-verify-email'
360 return makePostBodyRequest({
364 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
368 function verifyEmail (
371 verificationString: string,
372 isPendingEmail = false,
373 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
375 const path = '/api/v1/users/' + userId + '/verify-email'
377 return makePostBodyRequest({
388 // ---------------------------------------------------------------------------
393 getMyUserInformation,
394 getMyUserVideoRating,
396 registerUserWithChannel,
397 getMyUserVideoQuotaUsed,
399 getUsersListPaginationAndSort,
408 renewUserScopedTokens,
412 generateUserAccessToken,