1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
4 import { 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'
9 type CreateUserArgs = { url: string,
14 videoQuotaDaily?: number,
16 adminFlags?: UserAdminFlag,
17 specialStatus?: number
19 function createUser (parameters: CreateUserArgs) {
25 password = 'password',
32 const path = '/api/v1/users'
38 email: username + '@example.com',
45 .set('Accept', 'application/json')
46 .set('Authorization', 'Bearer ' + accessToken)
48 .expect(specialStatus)
51 async function generateUserAccessToken (server: ServerInfo, username: string) {
52 const password = 'my super password'
53 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
55 return userLogin(server, { username, password })
58 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
59 const path = '/api/v1/users/register'
63 email: username + '@example.com'
68 .set('Accept', 'application/json')
70 .expect(specialStatus)
73 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
74 const path = '/api/v1/users/me'
78 .set('Accept', 'application/json')
79 .set('Authorization', 'Bearer ' + accessToken)
80 .expect(specialStatus)
81 .expect('Content-Type', /json/)
84 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
85 const path = '/api/v1/users/me'
89 .set('Accept', 'application/json')
90 .set('Authorization', 'Bearer ' + accessToken)
91 .expect(specialStatus)
94 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
95 const path = '/api/v1/users/me/video-quota-used'
99 .set('Accept', 'application/json')
100 .set('Authorization', 'Bearer ' + accessToken)
101 .expect(specialStatus)
102 .expect('Content-Type', /json/)
105 function getUserInformation (url: string, accessToken: string, userId: number) {
106 const path = '/api/v1/users/' + userId
110 .set('Accept', 'application/json')
111 .set('Authorization', 'Bearer ' + accessToken)
113 .expect('Content-Type', /json/)
116 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
117 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
121 .set('Accept', 'application/json')
122 .set('Authorization', 'Bearer ' + accessToken)
123 .expect(specialStatus)
124 .expect('Content-Type', /json/)
127 function getUsersList (url: string, accessToken: string) {
128 const path = '/api/v1/users'
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
135 .expect('Content-Type', /json/)
138 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
139 const path = '/api/v1/users'
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + accessToken)
150 .expect('Content-Type', /json/)
153 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
154 const path = '/api/v1/users'
157 .delete(path + '/' + userId)
158 .set('Accept', 'application/json')
159 .set('Authorization', 'Bearer ' + accessToken)
160 .expect(expectedStatus)
163 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
164 const path = '/api/v1/users'
166 if (reason) body = { reason }
169 .post(path + '/' + userId + '/block')
171 .set('Accept', 'application/json')
172 .set('Authorization', 'Bearer ' + accessToken)
173 .expect(expectedStatus)
176 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
177 const path = '/api/v1/users'
180 .post(path + '/' + userId + '/unblock')
181 .set('Accept', 'application/json')
182 .set('Authorization', 'Bearer ' + accessToken)
183 .expect(expectedStatus)
186 function updateMyUser (options: {
189 currentPassword?: string
191 nsfwPolicy?: NSFWPolicyType
193 autoPlayVideo?: boolean
196 videosHistoryEnabled?: boolean
198 const path = '/api/v1/users/me'
201 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
202 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
203 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
204 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
205 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
206 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
207 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
208 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
209 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
212 return makePutBodyRequest({
215 token: options.accessToken,
217 statusCodeExpected: 204
221 function updateMyAvatar (options: {
226 const path = '/api/v1/users/me/avatar/pick'
228 return updateAvatarRequest(Object.assign(options, { path }))
231 function updateUser (options: {
236 emailVerified?: boolean,
238 videoQuotaDaily?: number,
240 adminFlags?: UserAdminFlag,
243 const path = '/api/v1/users/' + options.userId
246 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
247 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
248 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
249 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
250 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
251 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
252 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
254 return makePutBodyRequest({
257 token: options.accessToken,
259 statusCodeExpected: 204
263 function askResetPassword (url: string, email: string) {
264 const path = '/api/v1/users/ask-reset-password'
266 return makePostBodyRequest({
270 statusCodeExpected: 204
274 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
275 const path = '/api/v1/users/' + userId + '/reset-password'
277 return makePostBodyRequest({
280 fields: { password, verificationString },
285 function askSendVerifyEmail (url: string, email: string) {
286 const path = '/api/v1/users/ask-send-verify-email'
288 return makePostBodyRequest({
292 statusCodeExpected: 204
296 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
297 const path = '/api/v1/users/' + userId + '/verify-email'
299 return makePostBodyRequest({
302 fields: { verificationString },
307 // ---------------------------------------------------------------------------
312 getMyUserInformation,
313 getMyUserVideoRating,
315 getMyUserVideoQuotaUsed,
317 getUsersListPaginationAndSort,
328 generateUserAccessToken,