1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
3 import { UserAdminFlag } from '../../models/users/user-flag.model'
4 import { UserRegister } from '../../models/users/user-register.model'
5 import { UserRole } from '../../models/users/user-role'
6 import { ServerInfo } from '../server/servers'
7 import { userLogin } from './login'
8 import { UserUpdateMe } from '../../models/users'
9 import { omit } from 'lodash'
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 deleteMe (url: string, accessToken: string, specialStatus = 204) {
113 const path = '/api/v1/users/me'
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + accessToken)
119 .expect(specialStatus)
122 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
123 const path = '/api/v1/users/me/video-quota-used'
127 .set('Accept', 'application/json')
128 .set('Authorization', 'Bearer ' + accessToken)
129 .expect(specialStatus)
130 .expect('Content-Type', /json/)
133 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
134 const path = '/api/v1/users/' + userId
138 .query({ withStats })
139 .set('Accept', 'application/json')
140 .set('Authorization', 'Bearer ' + accessToken)
142 .expect('Content-Type', /json/)
145 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
146 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
150 .set('Accept', 'application/json')
151 .set('Authorization', 'Bearer ' + accessToken)
152 .expect(specialStatus)
153 .expect('Content-Type', /json/)
156 function getUsersList (url: string, accessToken: string) {
157 const path = '/api/v1/users'
161 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken)
164 .expect('Content-Type', /json/)
167 function getUsersListPaginationAndSort (
176 const path = '/api/v1/users'
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + accessToken)
192 .expect('Content-Type', /json/)
195 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
196 const path = '/api/v1/users'
199 .delete(path + '/' + userId)
200 .set('Accept', 'application/json')
201 .set('Authorization', 'Bearer ' + accessToken)
202 .expect(expectedStatus)
205 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
206 const path = '/api/v1/users'
208 if (reason) body = { reason }
211 .post(path + '/' + userId + '/block')
213 .set('Accept', 'application/json')
214 .set('Authorization', 'Bearer ' + accessToken)
215 .expect(expectedStatus)
218 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
219 const path = '/api/v1/users'
222 .post(path + '/' + userId + '/unblock')
223 .set('Accept', 'application/json')
224 .set('Authorization', 'Bearer ' + accessToken)
225 .expect(expectedStatus)
228 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
229 const path = '/api/v1/users/me'
231 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
233 return makePutBodyRequest({
236 token: options.accessToken,
238 statusCodeExpected: options.statusCodeExpected || 204
242 function updateMyAvatar (options: {
247 const path = '/api/v1/users/me/avatar/pick'
249 return updateAvatarRequest(Object.assign(options, { path }))
252 function updateUser (options: {
257 emailVerified?: boolean
259 videoQuotaDaily?: number
261 adminFlags?: UserAdminFlag
264 const path = '/api/v1/users/' + options.userId
267 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
268 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
269 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
270 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
271 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
272 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
273 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
275 return makePutBodyRequest({
278 token: options.accessToken,
280 statusCodeExpected: 204
284 function askResetPassword (url: string, email: string) {
285 const path = '/api/v1/users/ask-reset-password'
287 return makePostBodyRequest({
291 statusCodeExpected: 204
295 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
296 const path = '/api/v1/users/' + userId + '/reset-password'
298 return makePostBodyRequest({
301 fields: { password, verificationString },
306 function askSendVerifyEmail (url: string, email: string) {
307 const path = '/api/v1/users/ask-send-verify-email'
309 return makePostBodyRequest({
313 statusCodeExpected: 204
317 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
318 const path = '/api/v1/users/' + userId + '/verify-email'
320 return makePostBodyRequest({
331 // ---------------------------------------------------------------------------
336 getMyUserInformation,
337 getMyUserVideoRating,
339 registerUserWithChannel,
340 getMyUserVideoQuotaUsed,
342 getUsersListPaginationAndSort,
353 generateUserAccessToken,