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'
11 type CreateUserArgs = {
17 videoQuotaDaily?: number
19 adminFlags?: UserAdminFlag
20 specialStatus?: number
22 function createUser (parameters: CreateUserArgs) {
28 password = 'password',
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 generateUserAccessToken (server: ServerInfo, username: string) {
55 const password = 'my super password'
56 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
58 return userLogin(server, { username, password })
61 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
62 const path = '/api/v1/users/register'
66 email: username + '@example.com'
71 .set('Accept', 'application/json')
73 .expect(specialStatus)
76 function registerUserWithChannel (options: {
78 user: { username: string, password: string, displayName?: string }
79 channel: { name: string, displayName: string }
81 const path = '/api/v1/users/register'
82 const body: UserRegister = {
83 username: options.user.username,
84 password: options.user.password,
85 email: options.user.username + '@example.com',
86 channel: options.channel
89 if (options.user.displayName) {
90 Object.assign(body, { displayName: options.user.displayName })
93 return makePostBodyRequest({
97 statusCodeExpected: 204
101 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
102 const path = '/api/v1/users/me'
106 .set('Accept', 'application/json')
107 .set('Authorization', 'Bearer ' + accessToken)
108 .expect(specialStatus)
109 .expect('Content-Type', /json/)
112 function getUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
113 const path = '/api/v1/users/scoped-tokens'
115 return makeGetRequest({
123 function renewUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
124 const path = '/api/v1/users/scoped-tokens'
126 return makePostBodyRequest({
134 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
135 const path = '/api/v1/users/me'
139 .set('Accept', 'application/json')
140 .set('Authorization', 'Bearer ' + accessToken)
141 .expect(specialStatus)
144 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
145 const path = '/api/v1/users/me/video-quota-used'
149 .set('Accept', 'application/json')
150 .set('Authorization', 'Bearer ' + accessToken)
151 .expect(specialStatus)
152 .expect('Content-Type', /json/)
155 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
156 const path = '/api/v1/users/' + userId
160 .query({ withStats })
161 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken)
164 .expect('Content-Type', /json/)
167 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
168 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
172 .set('Accept', 'application/json')
173 .set('Authorization', 'Bearer ' + accessToken)
174 .expect(specialStatus)
175 .expect('Content-Type', /json/)
178 function getUsersList (url: string, accessToken: string) {
179 const path = '/api/v1/users'
183 .set('Accept', 'application/json')
184 .set('Authorization', 'Bearer ' + accessToken)
186 .expect('Content-Type', /json/)
189 function getUsersListPaginationAndSort (
198 const path = '/api/v1/users'
211 .set('Accept', 'application/json')
212 .set('Authorization', 'Bearer ' + accessToken)
214 .expect('Content-Type', /json/)
217 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
218 const path = '/api/v1/users'
221 .delete(path + '/' + userId)
222 .set('Accept', 'application/json')
223 .set('Authorization', 'Bearer ' + accessToken)
224 .expect(expectedStatus)
227 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
228 const path = '/api/v1/users'
230 if (reason) body = { reason }
233 .post(path + '/' + userId + '/block')
235 .set('Accept', 'application/json')
236 .set('Authorization', 'Bearer ' + accessToken)
237 .expect(expectedStatus)
240 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
241 const path = '/api/v1/users'
244 .post(path + '/' + userId + '/unblock')
245 .set('Accept', 'application/json')
246 .set('Authorization', 'Bearer ' + accessToken)
247 .expect(expectedStatus)
250 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
251 const path = '/api/v1/users/me'
253 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
255 return makePutBodyRequest({
258 token: options.accessToken,
260 statusCodeExpected: options.statusCodeExpected || 204
264 function updateMyAvatar (options: {
269 const path = '/api/v1/users/me/avatar/pick'
271 return updateAvatarRequest(Object.assign(options, { path }))
274 function updateUser (options: {
279 emailVerified?: boolean
281 videoQuotaDaily?: number
283 adminFlags?: UserAdminFlag
286 const path = '/api/v1/users/' + options.userId
289 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
290 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
291 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
292 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
293 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
294 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
295 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
297 return makePutBodyRequest({
300 token: options.accessToken,
302 statusCodeExpected: 204
306 function askResetPassword (url: string, email: string) {
307 const path = '/api/v1/users/ask-reset-password'
309 return makePostBodyRequest({
313 statusCodeExpected: 204
317 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
318 const path = '/api/v1/users/' + userId + '/reset-password'
320 return makePostBodyRequest({
323 fields: { password, verificationString },
328 function askSendVerifyEmail (url: string, email: string) {
329 const path = '/api/v1/users/ask-send-verify-email'
331 return makePostBodyRequest({
335 statusCodeExpected: 204
339 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
340 const path = '/api/v1/users/' + userId + '/verify-email'
342 return makePostBodyRequest({
353 // ---------------------------------------------------------------------------
358 getMyUserInformation,
359 getMyUserVideoRating,
361 registerUserWithChannel,
362 getMyUserVideoQuotaUsed,
364 getUsersListPaginationAndSort,
373 renewUserScopedTokens,
376 generateUserAccessToken,