1 import * as request from 'supertest'
2 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
4 import { UserCreate, UserRole } from '../../index'
5 import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
6 import { ServerInfo, userLogin } from '..'
7 import { UserAdminFlag } from '../../models/users/user-flag.model'
8 import { UserRegister } from '../../models/users/user-register.model'
10 type CreateUserArgs = { url: string,
15 videoQuotaDaily?: number,
17 adminFlags?: UserAdminFlag,
18 specialStatus?: number
20 function createUser (parameters: CreateUserArgs) {
26 password = 'password',
33 const path = '/api/v1/users'
39 email: username + '@example.com',
46 .set('Accept', 'application/json')
47 .set('Authorization', 'Bearer ' + accessToken)
49 .expect(specialStatus)
52 async function generateUserAccessToken (server: ServerInfo, username: string) {
53 const password = 'my super password'
54 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
56 return userLogin(server, { username, password })
59 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
60 const path = '/api/v1/users/register'
64 email: username + '@example.com'
69 .set('Accept', 'application/json')
71 .expect(specialStatus)
74 function registerUserWithChannel (options: {
76 user: { username: string, password: string, displayName?: string },
77 channel: { name: string, displayName: string }
79 const path = '/api/v1/users/register'
80 const body: UserRegister = {
81 username: options.user.username,
82 password: options.user.password,
83 email: options.user.username + '@example.com',
84 channel: options.channel
87 if (options.user.displayName) {
88 Object.assign(body, { displayName: options.user.displayName })
91 return makePostBodyRequest({
95 statusCodeExpected: 204
99 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
100 const path = '/api/v1/users/me'
104 .set('Accept', 'application/json')
105 .set('Authorization', 'Bearer ' + accessToken)
106 .expect(specialStatus)
107 .expect('Content-Type', /json/)
110 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
111 const path = '/api/v1/users/me'
115 .set('Accept', 'application/json')
116 .set('Authorization', 'Bearer ' + accessToken)
117 .expect(specialStatus)
120 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
121 const path = '/api/v1/users/me/video-quota-used'
125 .set('Accept', 'application/json')
126 .set('Authorization', 'Bearer ' + accessToken)
127 .expect(specialStatus)
128 .expect('Content-Type', /json/)
131 function getUserInformation (url: string, accessToken: string, userId: number) {
132 const path = '/api/v1/users/' + userId
136 .set('Accept', 'application/json')
137 .set('Authorization', 'Bearer ' + accessToken)
139 .expect('Content-Type', /json/)
142 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
143 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + accessToken)
149 .expect(specialStatus)
150 .expect('Content-Type', /json/)
153 function getUsersList (url: string, accessToken: string) {
154 const path = '/api/v1/users'
158 .set('Accept', 'application/json')
159 .set('Authorization', 'Bearer ' + accessToken)
161 .expect('Content-Type', /json/)
164 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
165 const path = '/api/v1/users'
177 .set('Accept', 'application/json')
178 .set('Authorization', 'Bearer ' + accessToken)
180 .expect('Content-Type', /json/)
183 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
184 const path = '/api/v1/users'
187 .delete(path + '/' + userId)
188 .set('Accept', 'application/json')
189 .set('Authorization', 'Bearer ' + accessToken)
190 .expect(expectedStatus)
193 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
194 const path = '/api/v1/users'
196 if (reason) body = { reason }
199 .post(path + '/' + userId + '/block')
201 .set('Accept', 'application/json')
202 .set('Authorization', 'Bearer ' + accessToken)
203 .expect(expectedStatus)
206 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
207 const path = '/api/v1/users'
210 .post(path + '/' + userId + '/unblock')
211 .set('Accept', 'application/json')
212 .set('Authorization', 'Bearer ' + accessToken)
213 .expect(expectedStatus)
216 function updateMyUser (options: {
219 currentPassword?: string
221 nsfwPolicy?: NSFWPolicyType
223 autoPlayVideo?: boolean
226 videosHistoryEnabled?: boolean
228 const path = '/api/v1/users/me'
231 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
232 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
233 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
234 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
235 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
236 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
237 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
238 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
239 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
242 return makePutBodyRequest({
245 token: options.accessToken,
247 statusCodeExpected: 204
251 function updateMyAvatar (options: {
256 const path = '/api/v1/users/me/avatar/pick'
258 return updateAvatarRequest(Object.assign(options, { path }))
261 function updateUser (options: {
266 emailVerified?: boolean,
268 videoQuotaDaily?: number,
270 adminFlags?: UserAdminFlag,
273 const path = '/api/v1/users/' + options.userId
276 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
277 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
278 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
279 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
280 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
281 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
282 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
284 return makePutBodyRequest({
287 token: options.accessToken,
289 statusCodeExpected: 204
293 function askResetPassword (url: string, email: string) {
294 const path = '/api/v1/users/ask-reset-password'
296 return makePostBodyRequest({
300 statusCodeExpected: 204
304 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
305 const path = '/api/v1/users/' + userId + '/reset-password'
307 return makePostBodyRequest({
310 fields: { password, verificationString },
315 function askSendVerifyEmail (url: string, email: string) {
316 const path = '/api/v1/users/ask-send-verify-email'
318 return makePostBodyRequest({
322 statusCodeExpected: 204
326 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
327 const path = '/api/v1/users/' + userId + '/verify-email'
329 return makePostBodyRequest({
340 // ---------------------------------------------------------------------------
345 getMyUserInformation,
346 getMyUserVideoRating,
348 registerUserWithChannel,
349 getMyUserVideoQuotaUsed,
351 getUsersListPaginationAndSort,
362 generateUserAccessToken,