1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
5 import { UserRole } from '../../../../shared/index'
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 getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
60 const path = '/api/v1/users/me/video-quota-used'
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
66 .expect(specialStatus)
67 .expect('Content-Type', /json/)
70 function getUserInformation (url: string, accessToken: string, userId: number) {
71 const path = '/api/v1/users/' + userId
75 .set('Accept', 'application/json')
76 .set('Authorization', 'Bearer ' + accessToken)
78 .expect('Content-Type', /json/)
81 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
82 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
86 .set('Accept', 'application/json')
87 .set('Authorization', 'Bearer ' + accessToken)
88 .expect(specialStatus)
89 .expect('Content-Type', /json/)
92 function getUsersList (url: string, accessToken: string) {
93 const path = '/api/v1/users'
97 .set('Accept', 'application/json')
98 .set('Authorization', 'Bearer ' + accessToken)
100 .expect('Content-Type', /json/)
103 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
104 const path = '/api/v1/users'
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
114 .expect('Content-Type', /json/)
117 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
118 const path = '/api/v1/users'
121 .delete(path + '/' + userId)
122 .set('Accept', 'application/json')
123 .set('Authorization', 'Bearer ' + accessToken)
124 .expect(expectedStatus)
127 function updateMyUser (options: {
130 newPassword?: string,
131 displayNSFW?: boolean,
133 autoPlayVideo?: boolean
135 const path = '/api/v1/users/me'
138 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
139 if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
140 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
141 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
143 return makePutBodyRequest({
146 token: options.accessToken,
148 statusCodeExpected: 204
152 function updateMyAvatar (options: {
157 const path = '/api/v1/users/me/avatar/pick'
159 if (isAbsolute(options.fixture)) {
160 filePath = options.fixture
162 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
165 return makeUploadRequest({
168 token: options.accessToken,
170 attaches: { avatarfile: filePath },
171 statusCodeExpected: 200
175 function updateUser (options: {
183 const path = '/api/v1/users/' + options.userId
186 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
187 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
188 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
190 return makePutBodyRequest({
193 token: options.accessToken,
195 statusCodeExpected: 204
199 function askResetPassword (url: string, email: string) {
200 const path = '/api/v1/users/ask-reset-password'
202 return makePostBodyRequest({
206 statusCodeExpected: 204
210 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
211 const path = '/api/v1/users/' + userId + '/reset-password'
213 return makePostBodyRequest({
216 fields: { password, verificationString },
221 // ---------------------------------------------------------------------------
226 getMyUserInformation,
227 getMyUserVideoRating,
228 getMyUserVideoQuotaUsed,
230 getUsersListPaginationAndSort,