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'
14 role: UserRole = UserRole.USER,
17 const path = '/api/v1/users'
22 email: username + '@example.com',
29 .set('Accept', 'application/json')
30 .set('Authorization', 'Bearer ' + accessToken)
32 .expect(specialStatus)
35 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
36 const path = '/api/v1/users/register'
40 email: username + '@example.com'
45 .set('Accept', 'application/json')
47 .expect(specialStatus)
50 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
51 const path = '/api/v1/users/me'
55 .set('Accept', 'application/json')
56 .set('Authorization', 'Bearer ' + accessToken)
57 .expect(specialStatus)
58 .expect('Content-Type', /json/)
61 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
62 const path = '/api/v1/users/me'
66 .set('Accept', 'application/json')
67 .set('Authorization', 'Bearer ' + accessToken)
68 .expect(specialStatus)
71 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
72 const path = '/api/v1/users/me/video-quota-used'
76 .set('Accept', 'application/json')
77 .set('Authorization', 'Bearer ' + accessToken)
78 .expect(specialStatus)
79 .expect('Content-Type', /json/)
82 function getUserInformation (url: string, accessToken: string, userId: number) {
83 const path = '/api/v1/users/' + userId
87 .set('Accept', 'application/json')
88 .set('Authorization', 'Bearer ' + accessToken)
90 .expect('Content-Type', /json/)
93 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
94 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + accessToken)
100 .expect(specialStatus)
101 .expect('Content-Type', /json/)
104 function getUsersList (url: string, accessToken: string) {
105 const path = '/api/v1/users'
109 .set('Accept', 'application/json')
110 .set('Authorization', 'Bearer ' + accessToken)
112 .expect('Content-Type', /json/)
115 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
116 const path = '/api/v1/users'
124 .set('Accept', 'application/json')
125 .set('Authorization', 'Bearer ' + accessToken)
127 .expect('Content-Type', /json/)
130 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
131 const path = '/api/v1/users'
134 .delete(path + '/' + userId)
135 .set('Accept', 'application/json')
136 .set('Authorization', 'Bearer ' + accessToken)
137 .expect(expectedStatus)
140 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
141 const path = '/api/v1/users'
143 if (reason) body = { reason }
146 .post(path + '/' + userId + '/block')
148 .set('Accept', 'application/json')
149 .set('Authorization', 'Bearer ' + accessToken)
150 .expect(expectedStatus)
153 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
154 const path = '/api/v1/users'
157 .post(path + '/' + userId + '/unblock')
158 .set('Accept', 'application/json')
159 .set('Authorization', 'Bearer ' + accessToken)
160 .expect(expectedStatus)
163 function updateMyUser (options: {
166 currentPassword?: string
168 nsfwPolicy?: NSFWPolicyType
170 autoPlayVideo?: boolean
173 videosHistoryEnabled?: boolean
175 const path = '/api/v1/users/me'
178 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
179 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
180 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
181 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
182 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
183 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
184 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
185 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
186 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
189 return makePutBodyRequest({
192 token: options.accessToken,
194 statusCodeExpected: 204
198 function updateMyAvatar (options: {
203 const path = '/api/v1/users/me/avatar/pick'
205 return updateAvatarRequest(Object.assign(options, { path }))
208 function updateUser (options: {
213 emailVerified?: boolean,
215 videoQuotaDaily?: number,
219 const path = '/api/v1/users/' + options.userId
222 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
223 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
224 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
225 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
226 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
227 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
229 return makePutBodyRequest({
232 token: options.accessToken,
234 statusCodeExpected: 204
238 function askResetPassword (url: string, email: string) {
239 const path = '/api/v1/users/ask-reset-password'
241 return makePostBodyRequest({
245 statusCodeExpected: 204
249 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
250 const path = '/api/v1/users/' + userId + '/reset-password'
252 return makePostBodyRequest({
255 fields: { password, verificationString },
260 function askSendVerifyEmail (url: string, email: string) {
261 const path = '/api/v1/users/ask-send-verify-email'
263 return makePostBodyRequest({
267 statusCodeExpected: 204
271 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
272 const path = '/api/v1/users/' + userId + '/verify-email'
274 return makePostBodyRequest({
277 fields: { verificationString },
282 // ---------------------------------------------------------------------------
287 getMyUserInformation,
288 getMyUserVideoRating,
290 getMyUserVideoQuotaUsed,
292 getUsersListPaginationAndSort,