1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
5 import { UserRole } from '../../../../shared/index'
6 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',
28 .set('Accept', 'application/json')
29 .set('Authorization', 'Bearer ' + accessToken)
31 .expect(specialStatus)
34 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
35 const path = '/api/v1/users/register'
39 email: username + '@example.com'
44 .set('Accept', 'application/json')
46 .expect(specialStatus)
49 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
50 const path = '/api/v1/users/me'
54 .set('Accept', 'application/json')
55 .set('Authorization', 'Bearer ' + accessToken)
56 .expect(specialStatus)
57 .expect('Content-Type', /json/)
60 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
61 const path = '/api/v1/users/me/video-quota-used'
65 .set('Accept', 'application/json')
66 .set('Authorization', 'Bearer ' + accessToken)
67 .expect(specialStatus)
68 .expect('Content-Type', /json/)
71 function getUserInformation (url: string, accessToken: string, userId: number) {
72 const path = '/api/v1/users/' + userId
76 .set('Accept', 'application/json')
77 .set('Authorization', 'Bearer ' + accessToken)
79 .expect('Content-Type', /json/)
82 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
83 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
87 .set('Accept', 'application/json')
88 .set('Authorization', 'Bearer ' + accessToken)
89 .expect(specialStatus)
90 .expect('Content-Type', /json/)
93 function getUsersList (url: string, accessToken: string) {
94 const path = '/api/v1/users'
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + accessToken)
101 .expect('Content-Type', /json/)
104 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
105 const path = '/api/v1/users'
112 .set('Accept', 'application/json')
113 .set('Authorization', 'Bearer ' + accessToken)
115 .expect('Content-Type', /json/)
118 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
119 const path = '/api/v1/users'
122 .delete(path + '/' + userId)
123 .set('Accept', 'application/json')
124 .set('Authorization', 'Bearer ' + accessToken)
125 .expect(expectedStatus)
128 function updateMyUser (options: {
131 newPassword?: string,
132 nsfwPolicy?: NSFWPolicyType,
134 autoPlayVideo?: boolean
137 const path = '/api/v1/users/me'
140 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
141 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
142 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
143 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
144 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
146 return makePutBodyRequest({
149 token: options.accessToken,
151 statusCodeExpected: 204
155 function updateMyAvatar (options: {
160 const path = '/api/v1/users/me/avatar/pick'
162 if (isAbsolute(options.fixture)) {
163 filePath = options.fixture
165 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
168 return makeUploadRequest({
171 token: options.accessToken,
173 attaches: { avatarfile: filePath },
174 statusCodeExpected: 200
178 function updateUser (options: {
186 const path = '/api/v1/users/' + options.userId
189 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
190 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
191 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
193 return makePutBodyRequest({
196 token: options.accessToken,
198 statusCodeExpected: 204
202 function askResetPassword (url: string, email: string) {
203 const path = '/api/v1/users/ask-reset-password'
205 return makePostBodyRequest({
209 statusCodeExpected: 204
213 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
214 const path = '/api/v1/users/' + userId + '/reset-password'
216 return makePostBodyRequest({
219 fields: { password, verificationString },
224 // ---------------------------------------------------------------------------
229 getMyUserInformation,
230 getMyUserVideoRating,
231 getMyUserVideoQuotaUsed,
233 getUsersListPaginationAndSort,