1 import { omit } from 'lodash'
2 import * as request from 'supertest'
3 import { UserUpdateMe } from '../../models/users'
4 import { UserAdminFlag } from '../../models/users/user-flag.model'
5 import { UserRegister } from '../../models/users/user-register.model'
6 import { UserRole } from '../../models/users/user-role'
7 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
8 import { ServerInfo } from '../server/servers'
9 import { userLogin } from './login'
10 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
12 type CreateUserArgs = {
18 videoQuotaDaily?: number
20 adminFlags?: UserAdminFlag
21 specialStatus?: number
23 function createUser (parameters: CreateUserArgs) {
29 password = 'password',
33 specialStatus = HttpStatusCode.OK_200
36 const path = '/api/v1/users'
42 email: username + '@example.com',
49 .set('Accept', 'application/json')
50 .set('Authorization', 'Bearer ' + accessToken)
52 .expect(specialStatus)
55 async function generateUserAccessToken (server: ServerInfo, username: string) {
56 const password = 'my super password'
57 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
59 return userLogin(server, { username, password })
62 function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
63 const path = '/api/v1/users/register'
67 email: username + '@example.com'
72 .set('Accept', 'application/json')
74 .expect(specialStatus)
77 function registerUserWithChannel (options: {
79 user: { username: string, password: string, displayName?: string }
80 channel: { name: string, displayName: string }
82 const path = '/api/v1/users/register'
83 const body: UserRegister = {
84 username: options.user.username,
85 password: options.user.password,
86 email: options.user.username + '@example.com',
87 channel: options.channel
90 if (options.user.displayName) {
91 Object.assign(body, { displayName: options.user.displayName })
94 return makePostBodyRequest({
98 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
102 function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
103 const path = '/api/v1/users/me'
107 .set('Accept', 'application/json')
108 .set('Authorization', 'Bearer ' + accessToken)
109 .expect(specialStatus)
110 .expect('Content-Type', /json/)
113 function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
114 const path = '/api/v1/users/scoped-tokens'
116 return makeGetRequest({
124 function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
125 const path = '/api/v1/users/scoped-tokens'
127 return makePostBodyRequest({
135 function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
136 const path = '/api/v1/users/me'
140 .set('Accept', 'application/json')
141 .set('Authorization', 'Bearer ' + accessToken)
142 .expect(specialStatus)
145 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
146 const path = '/api/v1/users/me/video-quota-used'
150 .set('Accept', 'application/json')
151 .set('Authorization', 'Bearer ' + accessToken)
152 .expect(specialStatus)
153 .expect('Content-Type', /json/)
156 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
157 const path = '/api/v1/users/' + userId
161 .query({ withStats })
162 .set('Accept', 'application/json')
163 .set('Authorization', 'Bearer ' + accessToken)
164 .expect(HttpStatusCode.OK_200)
165 .expect('Content-Type', /json/)
168 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
169 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
173 .set('Accept', 'application/json')
174 .set('Authorization', 'Bearer ' + accessToken)
175 .expect(specialStatus)
176 .expect('Content-Type', /json/)
179 function getUsersList (url: string, accessToken: string) {
180 const path = '/api/v1/users'
184 .set('Accept', 'application/json')
185 .set('Authorization', 'Bearer ' + accessToken)
186 .expect(HttpStatusCode.OK_200)
187 .expect('Content-Type', /json/)
190 function getUsersListPaginationAndSort (
199 const path = '/api/v1/users'
212 .set('Accept', 'application/json')
213 .set('Authorization', 'Bearer ' + accessToken)
214 .expect(HttpStatusCode.OK_200)
215 .expect('Content-Type', /json/)
218 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
219 const path = '/api/v1/users'
222 .delete(path + '/' + userId)
223 .set('Accept', 'application/json')
224 .set('Authorization', 'Bearer ' + accessToken)
225 .expect(expectedStatus)
230 userId: number | string,
232 expectedStatus = HttpStatusCode.NO_CONTENT_204,
235 const path = '/api/v1/users'
237 if (reason) body = { reason }
240 .post(path + '/' + userId + '/block')
242 .set('Accept', 'application/json')
243 .set('Authorization', 'Bearer ' + accessToken)
244 .expect(expectedStatus)
247 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
248 const path = '/api/v1/users'
251 .post(path + '/' + userId + '/unblock')
252 .set('Accept', 'application/json')
253 .set('Authorization', 'Bearer ' + accessToken)
254 .expect(expectedStatus)
257 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
258 const path = '/api/v1/users/me'
260 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
262 return makePutBodyRequest({
265 token: options.accessToken,
267 statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
271 function updateMyAvatar (options: {
276 const path = '/api/v1/users/me/avatar/pick'
278 return updateAvatarRequest(Object.assign(options, { path }))
281 function updateUser (options: {
286 emailVerified?: boolean
288 videoQuotaDaily?: number
290 adminFlags?: UserAdminFlag
294 const path = '/api/v1/users/' + options.userId
297 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
298 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
299 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
300 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
301 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
302 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
303 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
304 if (options.pluginAuth !== undefined) toSend['pluginAuth'] = options.pluginAuth
306 return makePutBodyRequest({
309 token: options.accessToken,
311 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
315 function askResetPassword (url: string, email: string) {
316 const path = '/api/v1/users/ask-reset-password'
318 return makePostBodyRequest({
322 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
326 function resetPassword (
329 verificationString: string,
331 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
333 const path = '/api/v1/users/' + userId + '/reset-password'
335 return makePostBodyRequest({
338 fields: { password, verificationString },
343 function askSendVerifyEmail (url: string, email: string) {
344 const path = '/api/v1/users/ask-send-verify-email'
346 return makePostBodyRequest({
350 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
354 function verifyEmail (
357 verificationString: string,
358 isPendingEmail = false,
359 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
361 const path = '/api/v1/users/' + userId + '/verify-email'
363 return makePostBodyRequest({
374 // ---------------------------------------------------------------------------
379 getMyUserInformation,
380 getMyUserVideoRating,
382 registerUserWithChannel,
383 getMyUserVideoQuotaUsed,
385 getUsersListPaginationAndSort,
394 renewUserScopedTokens,
397 generateUserAccessToken,