Add about page
[oweals/peertube.git] / server / tests / utils / users / users.ts
1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostBodyRequest, makePostUploadRequest, makePutBodyRequest } from '../'
4
5 import { UserRole } from '../../../../shared/index'
6
7 function createUser (
8   url: string,
9   accessToken: string,
10   username: string,
11   password: string,
12   videoQuota = 1000000,
13   role: UserRole = UserRole.USER,
14   specialStatus = 200
15 ) {
16   const path = '/api/v1/users'
17   const body = {
18     username,
19     password,
20     role,
21     email: username + '@example.com',
22     videoQuota
23   }
24
25   return request(url)
26           .post(path)
27           .set('Accept', 'application/json')
28           .set('Authorization', 'Bearer ' + accessToken)
29           .send(body)
30           .expect(specialStatus)
31 }
32
33 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
34   const path = '/api/v1/users/register'
35   const body = {
36     username,
37     password,
38     email: username + '@example.com'
39   }
40
41   return request(url)
42           .post(path)
43           .set('Accept', 'application/json')
44           .send(body)
45           .expect(specialStatus)
46 }
47
48 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
49   const path = '/api/v1/users/me'
50
51   return request(url)
52           .get(path)
53           .set('Accept', 'application/json')
54           .set('Authorization', 'Bearer ' + accessToken)
55           .expect(specialStatus)
56           .expect('Content-Type', /json/)
57 }
58
59 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
60   const path = '/api/v1/users/me/video-quota-used'
61
62   return request(url)
63           .get(path)
64           .set('Accept', 'application/json')
65           .set('Authorization', 'Bearer ' + accessToken)
66           .expect(specialStatus)
67           .expect('Content-Type', /json/)
68 }
69
70 function getUserInformation (url: string, accessToken: string, userId: number) {
71   const path = '/api/v1/users/' + userId
72
73   return request(url)
74     .get(path)
75     .set('Accept', 'application/json')
76     .set('Authorization', 'Bearer ' + accessToken)
77     .expect(200)
78     .expect('Content-Type', /json/)
79 }
80
81 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
82   const path = '/api/v1/users/me/videos/' + videoId + '/rating'
83
84   return request(url)
85           .get(path)
86           .set('Accept', 'application/json')
87           .set('Authorization', 'Bearer ' + accessToken)
88           .expect(specialStatus)
89           .expect('Content-Type', /json/)
90 }
91
92 function getUsersList (url: string, accessToken: string) {
93   const path = '/api/v1/users'
94
95   return request(url)
96           .get(path)
97           .set('Accept', 'application/json')
98           .set('Authorization', 'Bearer ' + accessToken)
99           .expect(200)
100           .expect('Content-Type', /json/)
101 }
102
103 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
104   const path = '/api/v1/users'
105
106   return request(url)
107           .get(path)
108           .query({ start })
109           .query({ count })
110           .query({ sort })
111           .set('Accept', 'application/json')
112           .set('Authorization', 'Bearer ' + accessToken)
113           .expect(200)
114           .expect('Content-Type', /json/)
115 }
116
117 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
118   const path = '/api/v1/users'
119
120   return request(url)
121           .delete(path + '/' + userId)
122           .set('Accept', 'application/json')
123           .set('Authorization', 'Bearer ' + accessToken)
124           .expect(expectedStatus)
125 }
126
127 function updateMyUser (options: {
128   url: string
129   accessToken: string,
130   newPassword?: string,
131   displayNSFW?: boolean,
132   email?: string,
133   autoPlayVideo?: boolean
134 }) {
135   const path = '/api/v1/users/me'
136
137   const toSend = {}
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
142
143   return makePutBodyRequest({
144     url: options.url,
145     path,
146     token: options.accessToken,
147     fields: toSend,
148     statusCodeExpected: 204
149   })
150 }
151
152 function updateMyAvatar (options: {
153   url: string,
154   accessToken: string,
155   fixture: string
156 }) {
157   const path = '/api/v1/users/me/avatar/pick'
158   let filePath = ''
159   if (isAbsolute(options.fixture)) {
160     filePath = options.fixture
161   } else {
162     filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
163   }
164
165   return makePostUploadRequest({
166     url: options.url,
167     path,
168     token: options.accessToken,
169     fields: {},
170     attaches: { avatarfile: filePath },
171     statusCodeExpected: 200
172   })
173 }
174
175 function updateUser (options: {
176   url: string
177   userId: number,
178   accessToken: string,
179   email?: string,
180   videoQuota?: number,
181   role?: UserRole
182 }) {
183   const path = '/api/v1/users/' + options.userId
184
185   const toSend = {}
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
189
190   return makePutBodyRequest({
191     url: options.url,
192     path,
193     token: options.accessToken,
194     fields: toSend,
195     statusCodeExpected: 204
196   })
197 }
198
199 function askResetPassword (url: string, email: string) {
200   const path = '/api/v1/users/ask-reset-password'
201
202   return makePostBodyRequest({
203     url,
204     path,
205     fields: { email },
206     statusCodeExpected: 204
207   })
208 }
209
210 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
211   const path = '/api/v1/users/' + userId + '/reset-password'
212
213   return makePostBodyRequest({
214     url,
215     path,
216     fields: { password, verificationString },
217     statusCodeExpected
218   })
219 }
220
221 // ---------------------------------------------------------------------------
222
223 export {
224   createUser,
225   registerUser,
226   getMyUserInformation,
227   getMyUserVideoRating,
228   getMyUserVideoQuotaUsed,
229   getUsersList,
230   getUsersListPaginationAndSort,
231   removeUser,
232   updateUser,
233   updateMyUser,
234   getUserInformation,
235   askResetPassword,
236   resetPassword,
237   updateMyAvatar
238 }