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 },
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 return makePostBodyRequest({
91 statusCodeExpected: 204
95 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
96 const path = '/api/v1/users/me'
100 .set('Accept', 'application/json')
101 .set('Authorization', 'Bearer ' + accessToken)
102 .expect(specialStatus)
103 .expect('Content-Type', /json/)
106 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
107 const path = '/api/v1/users/me'
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
113 .expect(specialStatus)
116 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
117 const path = '/api/v1/users/me/video-quota-used'
121 .set('Accept', 'application/json')
122 .set('Authorization', 'Bearer ' + accessToken)
123 .expect(specialStatus)
124 .expect('Content-Type', /json/)
127 function getUserInformation (url: string, accessToken: string, userId: number) {
128 const path = '/api/v1/users/' + userId
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
135 .expect('Content-Type', /json/)
138 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
139 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
143 .set('Accept', 'application/json')
144 .set('Authorization', 'Bearer ' + accessToken)
145 .expect(specialStatus)
146 .expect('Content-Type', /json/)
149 function getUsersList (url: string, accessToken: string) {
150 const path = '/api/v1/users'
154 .set('Accept', 'application/json')
155 .set('Authorization', 'Bearer ' + accessToken)
157 .expect('Content-Type', /json/)
160 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
161 const path = '/api/v1/users'
169 .set('Accept', 'application/json')
170 .set('Authorization', 'Bearer ' + accessToken)
172 .expect('Content-Type', /json/)
175 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
176 const path = '/api/v1/users'
179 .delete(path + '/' + userId)
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + accessToken)
182 .expect(expectedStatus)
185 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
186 const path = '/api/v1/users'
188 if (reason) body = { reason }
191 .post(path + '/' + userId + '/block')
193 .set('Accept', 'application/json')
194 .set('Authorization', 'Bearer ' + accessToken)
195 .expect(expectedStatus)
198 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
199 const path = '/api/v1/users'
202 .post(path + '/' + userId + '/unblock')
203 .set('Accept', 'application/json')
204 .set('Authorization', 'Bearer ' + accessToken)
205 .expect(expectedStatus)
208 function updateMyUser (options: {
211 currentPassword?: string
213 nsfwPolicy?: NSFWPolicyType
215 autoPlayVideo?: boolean
218 videosHistoryEnabled?: boolean
220 const path = '/api/v1/users/me'
223 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
224 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
225 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
226 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
227 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
228 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
229 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
230 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
231 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
234 return makePutBodyRequest({
237 token: options.accessToken,
239 statusCodeExpected: 204
243 function updateMyAvatar (options: {
248 const path = '/api/v1/users/me/avatar/pick'
250 return updateAvatarRequest(Object.assign(options, { path }))
253 function updateUser (options: {
258 emailVerified?: boolean,
260 videoQuotaDaily?: number,
262 adminFlags?: UserAdminFlag,
265 const path = '/api/v1/users/' + options.userId
268 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
269 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
270 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
271 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
272 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
273 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
274 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
276 return makePutBodyRequest({
279 token: options.accessToken,
281 statusCodeExpected: 204
285 function askResetPassword (url: string, email: string) {
286 const path = '/api/v1/users/ask-reset-password'
288 return makePostBodyRequest({
292 statusCodeExpected: 204
296 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
297 const path = '/api/v1/users/' + userId + '/reset-password'
299 return makePostBodyRequest({
302 fields: { password, verificationString },
307 function askSendVerifyEmail (url: string, email: string) {
308 const path = '/api/v1/users/ask-send-verify-email'
310 return makePostBodyRequest({
314 statusCodeExpected: 204
318 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
319 const path = '/api/v1/users/' + userId + '/verify-email'
321 return makePostBodyRequest({
324 fields: { verificationString },
329 // ---------------------------------------------------------------------------
334 getMyUserInformation,
335 getMyUserVideoRating,
337 registerUserWithChannel,
338 getMyUserVideoQuotaUsed,
340 getUsersListPaginationAndSort,
351 generateUserAccessToken,