]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/users/users.ts
Use popover for help component
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
CommitLineData
c5911fd3 1import { isAbsolute, join } from 'path'
0e1dc3e7 2import * as request from 'supertest'
ac81d1a0 3import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
0e1dc3e7 4
c5d31dba 5import { UserRole } from '../../../../shared/index'
757f0da3
C
6
7function createUser (
8 url: string,
9 accessToken: string,
10 username: string,
11 password: string,
12 videoQuota = 1000000,
13 role: UserRole = UserRole.USER,
f05a1c30 14 specialStatus = 200
757f0da3 15) {
0e1dc3e7
C
16 const path = '/api/v1/users'
17 const body = {
18 username,
19 password,
757f0da3 20 role,
5c98d3bf
C
21 email: username + '@example.com',
22 videoQuota
0e1dc3e7
C
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
33function 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
26d21b78 48function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
0e1dc3e7
C
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)
26d21b78 55 .expect(specialStatus)
0e1dc3e7
C
56 .expect('Content-Type', /json/)
57}
58
ce5496d6
C
59function 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
5c98d3bf
C
70function 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
26d21b78 81function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
0e1dc3e7
C
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)
26d21b78 88 .expect(specialStatus)
0e1dc3e7
C
89 .expect('Content-Type', /json/)
90}
91
86d13ec2 92function getUsersList (url: string, accessToken: string) {
0e1dc3e7
C
93 const path = '/api/v1/users'
94
95 return request(url)
96 .get(path)
97 .set('Accept', 'application/json')
86d13ec2 98 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
99 .expect(200)
100 .expect('Content-Type', /json/)
101}
102
86d13ec2 103function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
0e1dc3e7
C
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')
86d13ec2 112 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
113 .expect(200)
114 .expect('Content-Type', /json/)
115}
116
26d21b78 117function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
0e1dc3e7
C
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
26d21b78
C
127function updateMyUser (options: {
128 url: string
129 accessToken: string,
130 newPassword?: string,
131 displayNSFW?: boolean,
132 email?: string,
133 autoPlayVideo?: boolean
2422c46b 134 description?: string
26d21b78 135}) {
5c98d3bf 136 const path = '/api/v1/users/me'
0e1dc3e7
C
137
138 const toSend = {}
26d21b78
C
139 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
140 if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
141 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
142 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
2422c46b 143 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
26d21b78
C
144
145 return makePutBodyRequest({
146 url: options.url,
147 path,
148 token: options.accessToken,
149 fields: toSend,
150 statusCodeExpected: 204
151 })
5c98d3bf
C
152}
153
c5911fd3
C
154function updateMyAvatar (options: {
155 url: string,
156 accessToken: string,
157 fixture: string
158}) {
159 const path = '/api/v1/users/me/avatar/pick'
160 let filePath = ''
161 if (isAbsolute(options.fixture)) {
162 filePath = options.fixture
163 } else {
164 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
165 }
166
ac81d1a0 167 return makeUploadRequest({
c5911fd3
C
168 url: options.url,
169 path,
170 token: options.accessToken,
171 fields: {},
172 attaches: { avatarfile: filePath },
173 statusCodeExpected: 200
174 })
175}
176
26d21b78
C
177function updateUser (options: {
178 url: string
179 userId: number,
180 accessToken: string,
181 email?: string,
182 videoQuota?: number,
183 role?: UserRole
184}) {
185 const path = '/api/v1/users/' + options.userId
5c98d3bf
C
186
187 const toSend = {}
26d21b78
C
188 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
189 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
190 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
191
192 return makePutBodyRequest({
193 url: options.url,
194 path,
195 token: options.accessToken,
196 fields: toSend,
197 statusCodeExpected: 204
198 })
0e1dc3e7
C
199}
200
f076daa7
C
201function askResetPassword (url: string, email: string) {
202 const path = '/api/v1/users/ask-reset-password'
203
204 return makePostBodyRequest({
205 url,
206 path,
207 fields: { email },
208 statusCodeExpected: 204
209 })
210}
211
212function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
213 const path = '/api/v1/users/' + userId + '/reset-password'
214
215 return makePostBodyRequest({
216 url,
217 path,
218 fields: { password, verificationString },
219 statusCodeExpected
220 })
221}
222
0e1dc3e7
C
223// ---------------------------------------------------------------------------
224
225export {
226 createUser,
227 registerUser,
5c98d3bf 228 getMyUserInformation,
26d21b78 229 getMyUserVideoRating,
ce5496d6 230 getMyUserVideoQuotaUsed,
0e1dc3e7
C
231 getUsersList,
232 getUsersListPaginationAndSort,
233 removeUser,
5c98d3bf
C
234 updateUser,
235 updateMyUser,
c5911fd3 236 getUserInformation,
f076daa7
C
237 askResetPassword,
238 resetPassword,
c5911fd3 239 updateMyAvatar
0e1dc3e7 240}