1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
3 import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
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 { ServerInfo } from '../server/servers'
8 import { userLogin } from './login'
9 import { UserUpdateMe } from '../../models/users'
11 type CreateUserArgs = { url: string,
16 videoQuotaDaily?: number,
18 adminFlags?: UserAdminFlag,
19 specialStatus?: number
21 function createUser (parameters: CreateUserArgs) {
27 password = 'password',
34 const path = '/api/v1/users'
40 email: username + '@example.com',
47 .set('Accept', 'application/json')
48 .set('Authorization', 'Bearer ' + accessToken)
50 .expect(specialStatus)
53 async function generateUserAccessToken (server: ServerInfo, username: string) {
54 const password = 'my super password'
55 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
57 return userLogin(server, { username, password })
60 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
61 const path = '/api/v1/users/register'
65 email: username + '@example.com'
70 .set('Accept', 'application/json')
72 .expect(specialStatus)
75 function registerUserWithChannel (options: {
77 user: { username: string, password: string, displayName?: string },
78 channel: { name: string, displayName: string }
80 const path = '/api/v1/users/register'
81 const body: UserRegister = {
82 username: options.user.username,
83 password: options.user.password,
84 email: options.user.username + '@example.com',
85 channel: options.channel
88 if (options.user.displayName) {
89 Object.assign(body, { displayName: options.user.displayName })
92 return makePostBodyRequest({
96 statusCodeExpected: 204
100 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
101 const path = '/api/v1/users/me'
105 .set('Accept', 'application/json')
106 .set('Authorization', 'Bearer ' + accessToken)
107 .expect(specialStatus)
108 .expect('Content-Type', /json/)
111 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
112 const path = '/api/v1/users/me'
116 .set('Accept', 'application/json')
117 .set('Authorization', 'Bearer ' + accessToken)
118 .expect(specialStatus)
121 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
122 const path = '/api/v1/users/me/video-quota-used'
126 .set('Accept', 'application/json')
127 .set('Authorization', 'Bearer ' + accessToken)
128 .expect(specialStatus)
129 .expect('Content-Type', /json/)
132 function getUserInformation (url: string, accessToken: string, userId: number) {
133 const path = '/api/v1/users/' + userId
137 .set('Accept', 'application/json')
138 .set('Authorization', 'Bearer ' + accessToken)
140 .expect('Content-Type', /json/)
143 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
144 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
148 .set('Accept', 'application/json')
149 .set('Authorization', 'Bearer ' + accessToken)
150 .expect(specialStatus)
151 .expect('Content-Type', /json/)
154 function getUsersList (url: string, accessToken: string) {
155 const path = '/api/v1/users'
159 .set('Accept', 'application/json')
160 .set('Authorization', 'Bearer ' + accessToken)
162 .expect('Content-Type', /json/)
165 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
166 const path = '/api/v1/users'
178 .set('Accept', 'application/json')
179 .set('Authorization', 'Bearer ' + accessToken)
181 .expect('Content-Type', /json/)
184 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
185 const path = '/api/v1/users'
188 .delete(path + '/' + userId)
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + accessToken)
191 .expect(expectedStatus)
194 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
195 const path = '/api/v1/users'
197 if (reason) body = { reason }
200 .post(path + '/' + userId + '/block')
202 .set('Accept', 'application/json')
203 .set('Authorization', 'Bearer ' + accessToken)
204 .expect(expectedStatus)
207 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
208 const path = '/api/v1/users'
211 .post(path + '/' + userId + '/unblock')
212 .set('Accept', 'application/json')
213 .set('Authorization', 'Bearer ' + accessToken)
214 .expect(expectedStatus)
217 function updateMyUser (options: {
220 currentPassword?: string
222 nsfwPolicy?: NSFWPolicyType
224 autoPlayVideo?: boolean
227 videosHistoryEnabled?: boolean
230 const path = '/api/v1/users/me'
232 const toSend: UserUpdateMe = {}
233 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend.currentPassword = options.currentPassword
234 if (options.newPassword !== undefined && options.newPassword !== null) toSend.password = options.newPassword
235 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend.nsfwPolicy = options.nsfwPolicy
236 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend.autoPlayVideo = options.autoPlayVideo
237 if (options.email !== undefined && options.email !== null) toSend.email = options.email
238 if (options.description !== undefined && options.description !== null) toSend.description = options.description
239 if (options.displayName !== undefined && options.displayName !== null) toSend.displayName = options.displayName
240 if (options.theme !== undefined && options.theme !== null) toSend.theme = options.theme
241 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
242 toSend.videosHistoryEnabled = options.videosHistoryEnabled
245 return makePutBodyRequest({
248 token: options.accessToken,
250 statusCodeExpected: 204
254 function updateMyAvatar (options: {
259 const path = '/api/v1/users/me/avatar/pick'
261 return updateAvatarRequest(Object.assign(options, { path }))
264 function updateUser (options: {
269 emailVerified?: boolean,
271 videoQuotaDaily?: number,
273 adminFlags?: UserAdminFlag,
276 const path = '/api/v1/users/' + options.userId
279 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
280 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
281 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
282 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
283 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
284 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
285 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
287 return makePutBodyRequest({
290 token: options.accessToken,
292 statusCodeExpected: 204
296 function askResetPassword (url: string, email: string) {
297 const path = '/api/v1/users/ask-reset-password'
299 return makePostBodyRequest({
303 statusCodeExpected: 204
307 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
308 const path = '/api/v1/users/' + userId + '/reset-password'
310 return makePostBodyRequest({
313 fields: { password, verificationString },
318 function askSendVerifyEmail (url: string, email: string) {
319 const path = '/api/v1/users/ask-send-verify-email'
321 return makePostBodyRequest({
325 statusCodeExpected: 204
329 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
330 const path = '/api/v1/users/' + userId + '/verify-email'
332 return makePostBodyRequest({
343 // ---------------------------------------------------------------------------
348 getMyUserInformation,
349 getMyUserVideoRating,
351 registerUserWithChannel,
352 getMyUserVideoQuotaUsed,
354 getUsersListPaginationAndSort,
365 generateUserAccessToken,