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 currentPassword?: string,
166 newPassword?: string,
167 nsfwPolicy?: NSFWPolicyType,
169 autoPlayVideo?: boolean
170 displayName?: string,
173 const path = '/api/v1/users/me'
176 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
177 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
178 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
179 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
180 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
181 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
182 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
184 return makePutBodyRequest({
187 token: options.accessToken,
189 statusCodeExpected: 204
193 function updateMyAvatar (options: {
198 const path = '/api/v1/users/me/avatar/pick'
200 return updateAvatarRequest(Object.assign(options, { path }))
203 function updateUser (options: {
209 videoQuotaDaily?: number,
212 const path = '/api/v1/users/' + options.userId
215 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
216 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
217 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
218 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
220 return makePutBodyRequest({
223 token: options.accessToken,
225 statusCodeExpected: 204
229 function askResetPassword (url: string, email: string) {
230 const path = '/api/v1/users/ask-reset-password'
232 return makePostBodyRequest({
236 statusCodeExpected: 204
240 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
241 const path = '/api/v1/users/' + userId + '/reset-password'
243 return makePostBodyRequest({
246 fields: { password, verificationString },
251 function askSendVerifyEmail (url: string, email: string) {
252 const path = '/api/v1/users/ask-send-verify-email'
254 return makePostBodyRequest({
258 statusCodeExpected: 204
262 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
263 const path = '/api/v1/users/' + userId + '/verify-email'
265 return makePostBodyRequest({
268 fields: { verificationString },
273 // ---------------------------------------------------------------------------
278 getMyUserInformation,
279 getMyUserVideoRating,
281 getMyUserVideoQuotaUsed,
283 getUsersListPaginationAndSort,