1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
4 import { UserRole } from '../../../../shared/index'
5 import { NSFWPolicyType } from '../../../../shared/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) {
116 const path = '/api/v1/users'
123 .set('Accept', 'application/json')
124 .set('Authorization', 'Bearer ' + accessToken)
126 .expect('Content-Type', /json/)
129 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
130 const path = '/api/v1/users'
133 .delete(path + '/' + userId)
134 .set('Accept', 'application/json')
135 .set('Authorization', 'Bearer ' + accessToken)
136 .expect(expectedStatus)
139 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
140 const path = '/api/v1/users'
142 if (reason) body = { reason }
145 .post(path + '/' + userId + '/block')
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + accessToken)
149 .expect(expectedStatus)
152 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
153 const path = '/api/v1/users'
156 .post(path + '/' + userId + '/unblock')
157 .set('Accept', 'application/json')
158 .set('Authorization', 'Bearer ' + accessToken)
159 .expect(expectedStatus)
162 function updateMyUser (options: {
165 newPassword?: string,
166 nsfwPolicy?: NSFWPolicyType,
168 autoPlayVideo?: boolean
169 displayName?: string,
172 const path = '/api/v1/users/me'
175 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
176 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
177 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
178 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
179 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
180 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
182 return makePutBodyRequest({
185 token: options.accessToken,
187 statusCodeExpected: 204
191 function updateMyAvatar (options: {
196 const path = '/api/v1/users/me/avatar/pick'
198 return updateAvatarRequest(Object.assign(options, { path }))
201 function updateUser (options: {
207 videoQuotaDaily?: number,
210 const path = '/api/v1/users/' + options.userId
213 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
214 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
215 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
216 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
218 return makePutBodyRequest({
221 token: options.accessToken,
223 statusCodeExpected: 204
227 function askResetPassword (url: string, email: string) {
228 const path = '/api/v1/users/ask-reset-password'
230 return makePostBodyRequest({
234 statusCodeExpected: 204
238 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
239 const path = '/api/v1/users/' + userId + '/reset-password'
241 return makePostBodyRequest({
244 fields: { password, verificationString },
249 // ---------------------------------------------------------------------------
254 getMyUserInformation,
255 getMyUserVideoRating,
257 getMyUserVideoQuotaUsed,
259 getUsersListPaginationAndSort,