1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostUploadRequest, 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 getUserInformation (url: string, accessToken: string, userId: number) {
60 const path = '/api/v1/users/' + userId
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
67 .expect('Content-Type', /json/)
70 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
71 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
75 .set('Accept', 'application/json')
76 .set('Authorization', 'Bearer ' + accessToken)
77 .expect(specialStatus)
78 .expect('Content-Type', /json/)
81 function getUsersList (url: string, accessToken: string) {
82 const path = '/api/v1/users'
86 .set('Accept', 'application/json')
87 .set('Authorization', 'Bearer ' + accessToken)
89 .expect('Content-Type', /json/)
92 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
93 const path = '/api/v1/users'
100 .set('Accept', 'application/json')
101 .set('Authorization', 'Bearer ' + accessToken)
103 .expect('Content-Type', /json/)
106 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
107 const path = '/api/v1/users'
110 .delete(path + '/' + userId)
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
113 .expect(expectedStatus)
116 function updateMyUser (options: {
119 newPassword?: string,
120 displayNSFW?: boolean,
122 autoPlayVideo?: boolean
124 const path = '/api/v1/users/me'
127 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
128 if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
129 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
130 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
132 return makePutBodyRequest({
135 token: options.accessToken,
137 statusCodeExpected: 204
141 function updateMyAvatar (options: {
146 const path = '/api/v1/users/me/avatar/pick'
148 if (isAbsolute(options.fixture)) {
149 filePath = options.fixture
151 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
154 return makePostUploadRequest({
157 token: options.accessToken,
159 attaches: { avatarfile: filePath },
160 statusCodeExpected: 200
164 function updateUser (options: {
172 const path = '/api/v1/users/' + options.userId
175 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
176 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
177 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
179 return makePutBodyRequest({
182 token: options.accessToken,
184 statusCodeExpected: 204
188 // ---------------------------------------------------------------------------
193 getMyUserInformation,
194 getMyUserVideoRating,
196 getUsersListPaginationAndSort,