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'
13 role: UserRole = UserRole.USER,
16 const path = '/api/v1/users'
21 email: username + '@example.com',
27 .set('Accept', 'application/json')
28 .set('Authorization', 'Bearer ' + accessToken)
30 .expect(specialStatus)
33 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
34 const path = '/api/v1/users/register'
38 email: username + '@example.com'
43 .set('Accept', 'application/json')
45 .expect(specialStatus)
48 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
49 const path = '/api/v1/users/me'
53 .set('Accept', 'application/json')
54 .set('Authorization', 'Bearer ' + accessToken)
55 .expect(specialStatus)
56 .expect('Content-Type', /json/)
59 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
60 const path = '/api/v1/users/me'
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
66 .expect(specialStatus)
69 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
70 const path = '/api/v1/users/me/video-quota-used'
74 .set('Accept', 'application/json')
75 .set('Authorization', 'Bearer ' + accessToken)
76 .expect(specialStatus)
77 .expect('Content-Type', /json/)
80 function getUserInformation (url: string, accessToken: string, userId: number) {
81 const path = '/api/v1/users/' + userId
85 .set('Accept', 'application/json')
86 .set('Authorization', 'Bearer ' + accessToken)
88 .expect('Content-Type', /json/)
91 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
92 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
96 .set('Accept', 'application/json')
97 .set('Authorization', 'Bearer ' + accessToken)
98 .expect(specialStatus)
99 .expect('Content-Type', /json/)
102 function getUsersList (url: string, accessToken: string) {
103 const path = '/api/v1/users'
107 .set('Accept', 'application/json')
108 .set('Authorization', 'Bearer ' + accessToken)
110 .expect('Content-Type', /json/)
113 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
114 const path = '/api/v1/users'
121 .set('Accept', 'application/json')
122 .set('Authorization', 'Bearer ' + accessToken)
124 .expect('Content-Type', /json/)
127 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
128 const path = '/api/v1/users'
131 .delete(path + '/' + userId)
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
134 .expect(expectedStatus)
137 function updateMyUser (options: {
140 newPassword?: string,
141 nsfwPolicy?: NSFWPolicyType,
143 autoPlayVideo?: boolean
144 displayName?: string,
147 const path = '/api/v1/users/me'
150 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
151 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
152 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
153 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
154 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
155 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
157 return makePutBodyRequest({
160 token: options.accessToken,
162 statusCodeExpected: 204
166 function updateMyAvatar (options: {
171 const path = '/api/v1/users/me/avatar/pick'
173 return updateAvatarRequest(Object.assign(options, { path }))
176 function updateUser (options: {
184 const path = '/api/v1/users/' + options.userId
187 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
188 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
189 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
191 return makePutBodyRequest({
194 token: options.accessToken,
196 statusCodeExpected: 204
200 function askResetPassword (url: string, email: string) {
201 const path = '/api/v1/users/ask-reset-password'
203 return makePostBodyRequest({
207 statusCodeExpected: 204
211 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
212 const path = '/api/v1/users/' + userId + '/reset-password'
214 return makePostBodyRequest({
217 fields: { password, verificationString },
222 // ---------------------------------------------------------------------------
227 getMyUserInformation,
228 getMyUserVideoRating,
230 getMyUserVideoQuotaUsed,
232 getUsersListPaginationAndSort,