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 (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
168 const path = '/api/v1/users'
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + accessToken)
183 .expect('Content-Type', /json/)
186 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
187 const path = '/api/v1/users'
190 .delete(path + '/' + userId)
191 .set('Accept', 'application/json')
192 .set('Authorization', 'Bearer ' + accessToken)
193 .expect(expectedStatus)
196 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
197 const path = '/api/v1/users'
199 if (reason) body = { reason }
202 .post(path + '/' + userId + '/block')
204 .set('Accept', 'application/json')
205 .set('Authorization', 'Bearer ' + accessToken)
206 .expect(expectedStatus)
209 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
210 const path = '/api/v1/users'
213 .post(path + '/' + userId + '/unblock')
214 .set('Accept', 'application/json')
215 .set('Authorization', 'Bearer ' + accessToken)
216 .expect(expectedStatus)
219 function updateMyUser (options: { url: string, accessToken: string } & UserUpdateMe) {
220 const path = '/api/v1/users/me'
222 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
224 return makePutBodyRequest({
227 token: options.accessToken,
229 statusCodeExpected: 204
233 function updateMyAvatar (options: {
238 const path = '/api/v1/users/me/avatar/pick'
240 return updateAvatarRequest(Object.assign(options, { path }))
243 function updateUser (options: {
248 emailVerified?: boolean
250 videoQuotaDaily?: number
252 adminFlags?: UserAdminFlag
255 const path = '/api/v1/users/' + options.userId
258 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
259 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
260 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
261 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
262 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
263 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
264 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
266 return makePutBodyRequest({
269 token: options.accessToken,
271 statusCodeExpected: 204
275 function askResetPassword (url: string, email: string) {
276 const path = '/api/v1/users/ask-reset-password'
278 return makePostBodyRequest({
282 statusCodeExpected: 204
286 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
287 const path = '/api/v1/users/' + userId + '/reset-password'
289 return makePostBodyRequest({
292 fields: { password, verificationString },
297 function askSendVerifyEmail (url: string, email: string) {
298 const path = '/api/v1/users/ask-send-verify-email'
300 return makePostBodyRequest({
304 statusCodeExpected: 204
308 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
309 const path = '/api/v1/users/' + userId + '/verify-email'
311 return makePostBodyRequest({
322 // ---------------------------------------------------------------------------
327 getMyUserInformation,
328 getMyUserVideoRating,
330 registerUserWithChannel,
331 getMyUserVideoQuotaUsed,
333 getUsersListPaginationAndSort,
344 generateUserAccessToken,