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