1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
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,
167 newPassword?: string,
168 nsfwPolicy?: NSFWPolicyType,
170 autoPlayVideo?: boolean
171 displayName?: string,
174 const path = '/api/v1/users/me'
177 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
178 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
179 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
180 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
181 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
182 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
183 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
185 return makePutBodyRequest({
188 token: options.accessToken,
190 statusCodeExpected: 204
194 function updateMyAvatar (options: {
199 const path = '/api/v1/users/me/avatar/pick'
201 return updateAvatarRequest(Object.assign(options, { path }))
204 function updateUser (options: {
210 videoQuotaDaily?: number,
213 const path = '/api/v1/users/' + options.userId
216 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
217 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
218 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
219 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
221 return makePutBodyRequest({
224 token: options.accessToken,
226 statusCodeExpected: 204
230 function askResetPassword (url: string, email: string) {
231 const path = '/api/v1/users/ask-reset-password'
233 return makePostBodyRequest({
237 statusCodeExpected: 204
241 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
242 const path = '/api/v1/users/' + userId + '/reset-password'
244 return makePostBodyRequest({
247 fields: { password, verificationString },
252 function askSendVerifyEmail (url: string, email: string) {
253 const path = '/api/v1/users/ask-send-verify-email'
255 return makePostBodyRequest({
259 statusCodeExpected: 204
263 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
264 const path = '/api/v1/users/' + userId + '/verify-email'
266 return makePostBodyRequest({
269 fields: { verificationString },
274 // ---------------------------------------------------------------------------
279 getMyUserInformation,
280 getMyUserVideoRating,
282 getMyUserVideoQuotaUsed,
284 getUsersListPaginationAndSort,