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'
6 import { ServerInfo, userLogin } from '..'
15 role: UserRole = UserRole.USER,
18 const path = '/api/v1/users'
23 email: username + '@example.com',
30 .set('Accept', 'application/json')
31 .set('Authorization', 'Bearer ' + accessToken)
33 .expect(specialStatus)
36 async function generateUserAccessToken (server: ServerInfo, username: string) {
37 const password = 'my super password'
38 await createUser(server.url, server.accessToken, username, password)
40 return userLogin(server, { username, password })
43 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
44 const path = '/api/v1/users/register'
48 email: username + '@example.com'
53 .set('Accept', 'application/json')
55 .expect(specialStatus)
58 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
59 const path = '/api/v1/users/me'
63 .set('Accept', 'application/json')
64 .set('Authorization', 'Bearer ' + accessToken)
65 .expect(specialStatus)
66 .expect('Content-Type', /json/)
69 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
70 const path = '/api/v1/users/me'
74 .set('Accept', 'application/json')
75 .set('Authorization', 'Bearer ' + accessToken)
76 .expect(specialStatus)
79 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
80 const path = '/api/v1/users/me/video-quota-used'
84 .set('Accept', 'application/json')
85 .set('Authorization', 'Bearer ' + accessToken)
86 .expect(specialStatus)
87 .expect('Content-Type', /json/)
90 function getUserInformation (url: string, accessToken: string, userId: number) {
91 const path = '/api/v1/users/' + userId
95 .set('Accept', 'application/json')
96 .set('Authorization', 'Bearer ' + accessToken)
98 .expect('Content-Type', /json/)
101 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
102 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
106 .set('Accept', 'application/json')
107 .set('Authorization', 'Bearer ' + accessToken)
108 .expect(specialStatus)
109 .expect('Content-Type', /json/)
112 function getUsersList (url: string, accessToken: string) {
113 const path = '/api/v1/users'
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + accessToken)
120 .expect('Content-Type', /json/)
123 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
124 const path = '/api/v1/users'
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
135 .expect('Content-Type', /json/)
138 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
139 const path = '/api/v1/users'
142 .delete(path + '/' + userId)
143 .set('Accept', 'application/json')
144 .set('Authorization', 'Bearer ' + accessToken)
145 .expect(expectedStatus)
148 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
149 const path = '/api/v1/users'
151 if (reason) body = { reason }
154 .post(path + '/' + userId + '/block')
156 .set('Accept', 'application/json')
157 .set('Authorization', 'Bearer ' + accessToken)
158 .expect(expectedStatus)
161 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
162 const path = '/api/v1/users'
165 .post(path + '/' + userId + '/unblock')
166 .set('Accept', 'application/json')
167 .set('Authorization', 'Bearer ' + accessToken)
168 .expect(expectedStatus)
171 function updateMyUser (options: {
174 currentPassword?: string
176 nsfwPolicy?: NSFWPolicyType
178 autoPlayVideo?: boolean
181 videosHistoryEnabled?: boolean
183 const path = '/api/v1/users/me'
186 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
187 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
188 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
189 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
190 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
191 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
192 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
193 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
194 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
197 return makePutBodyRequest({
200 token: options.accessToken,
202 statusCodeExpected: 204
206 function updateMyAvatar (options: {
211 const path = '/api/v1/users/me/avatar/pick'
213 return updateAvatarRequest(Object.assign(options, { path }))
216 function updateUser (options: {
221 emailVerified?: boolean,
223 videoQuotaDaily?: number,
227 const path = '/api/v1/users/' + options.userId
230 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
231 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
232 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
233 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
234 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
235 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
237 return makePutBodyRequest({
240 token: options.accessToken,
242 statusCodeExpected: 204
246 function askResetPassword (url: string, email: string) {
247 const path = '/api/v1/users/ask-reset-password'
249 return makePostBodyRequest({
253 statusCodeExpected: 204
257 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
258 const path = '/api/v1/users/' + userId + '/reset-password'
260 return makePostBodyRequest({
263 fields: { password, verificationString },
268 function askSendVerifyEmail (url: string, email: string) {
269 const path = '/api/v1/users/ask-send-verify-email'
271 return makePostBodyRequest({
275 statusCodeExpected: 204
279 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
280 const path = '/api/v1/users/' + userId + '/verify-email'
282 return makePostBodyRequest({
285 fields: { verificationString },
290 // ---------------------------------------------------------------------------
295 getMyUserInformation,
296 getMyUserVideoRating,
298 getMyUserVideoQuotaUsed,
300 getUsersListPaginationAndSort,
311 generateUserAccessToken,