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) {
134 const path = '/api/v1/users/' + userId
138 .set('Accept', 'application/json')
139 .set('Authorization', 'Bearer ' + accessToken)
141 .expect('Content-Type', /json/)
144 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
145 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
149 .set('Accept', 'application/json')
150 .set('Authorization', 'Bearer ' + accessToken)
151 .expect(specialStatus)
152 .expect('Content-Type', /json/)
155 function getUsersList (url: string, accessToken: string) {
156 const path = '/api/v1/users'
160 .set('Accept', 'application/json')
161 .set('Authorization', 'Bearer ' + accessToken)
163 .expect('Content-Type', /json/)
166 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
167 const path = '/api/v1/users'
179 .set('Accept', 'application/json')
180 .set('Authorization', 'Bearer ' + accessToken)
182 .expect('Content-Type', /json/)
185 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
186 const path = '/api/v1/users'
189 .delete(path + '/' + userId)
190 .set('Accept', 'application/json')
191 .set('Authorization', 'Bearer ' + accessToken)
192 .expect(expectedStatus)
195 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
196 const path = '/api/v1/users'
198 if (reason) body = { reason }
201 .post(path + '/' + userId + '/block')
203 .set('Accept', 'application/json')
204 .set('Authorization', 'Bearer ' + accessToken)
205 .expect(expectedStatus)
208 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
209 const path = '/api/v1/users'
212 .post(path + '/' + userId + '/unblock')
213 .set('Accept', 'application/json')
214 .set('Authorization', 'Bearer ' + accessToken)
215 .expect(expectedStatus)
218 function updateMyUser (options: { url: string, accessToken: string } & UserUpdateMe) {
219 const path = '/api/v1/users/me'
221 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
223 return makePutBodyRequest({
226 token: options.accessToken,
228 statusCodeExpected: 204
232 function updateMyAvatar (options: {
237 const path = '/api/v1/users/me/avatar/pick'
239 return updateAvatarRequest(Object.assign(options, { path }))
242 function updateUser (options: {
247 emailVerified?: boolean
249 videoQuotaDaily?: number
251 adminFlags?: UserAdminFlag
254 const path = '/api/v1/users/' + options.userId
257 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
258 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
259 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
260 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
261 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
262 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
263 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
265 return makePutBodyRequest({
268 token: options.accessToken,
270 statusCodeExpected: 204
274 function askResetPassword (url: string, email: string) {
275 const path = '/api/v1/users/ask-reset-password'
277 return makePostBodyRequest({
281 statusCodeExpected: 204
285 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
286 const path = '/api/v1/users/' + userId + '/reset-password'
288 return makePostBodyRequest({
291 fields: { password, verificationString },
296 function askSendVerifyEmail (url: string, email: string) {
297 const path = '/api/v1/users/ask-send-verify-email'
299 return makePostBodyRequest({
303 statusCodeExpected: 204
307 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
308 const path = '/api/v1/users/' + userId + '/verify-email'
310 return makePostBodyRequest({
321 // ---------------------------------------------------------------------------
326 getMyUserInformation,
327 getMyUserVideoRating,
329 registerUserWithChannel,
330 getMyUserVideoQuotaUsed,
332 getUsersListPaginationAndSort,
343 generateUserAccessToken,