]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/users/users.ts
Move fixtures in tests/
[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
ed56ad11 135 displayName?: string,
2422c46b 136 description?: string
26d21b78 137}) {
5c98d3bf 138 const path = '/api/v1/users/me'
0e1dc3e7
C
139
140 const toSend = {}
26d21b78 141 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
0883b324 142 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
26d21b78
C
143 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
144 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
2422c46b 145 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
ed56ad11 146 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
26d21b78
C
147
148 return makePutBodyRequest({
149 url: options.url,
150 path,
151 token: options.accessToken,
152 fields: toSend,
153 statusCodeExpected: 204
154 })
5c98d3bf
C
155}
156
c5911fd3
C
157function updateMyAvatar (options: {
158 url: string,
159 accessToken: string,
160 fixture: string
161}) {
162 const path = '/api/v1/users/me/avatar/pick'
163 let filePath = ''
164 if (isAbsolute(options.fixture)) {
165 filePath = options.fixture
166 } else {
99d10301 167 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
c5911fd3
C
168 }
169
ac81d1a0 170 return makeUploadRequest({
c5911fd3
C
171 url: options.url,
172 path,
173 token: options.accessToken,
174 fields: {},
175 attaches: { avatarfile: filePath },
176 statusCodeExpected: 200
177 })
178}
179
26d21b78
C
180function updateUser (options: {
181 url: string
182 userId: number,
183 accessToken: string,
184 email?: string,
185 videoQuota?: number,
186 role?: UserRole
187}) {
188 const path = '/api/v1/users/' + options.userId
5c98d3bf
C
189
190 const toSend = {}
26d21b78
C
191 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
192 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
193 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
194
195 return makePutBodyRequest({
196 url: options.url,
197 path,
198 token: options.accessToken,
199 fields: toSend,
200 statusCodeExpected: 204
201 })
0e1dc3e7
C
202}
203
f076daa7
C
204function askResetPassword (url: string, email: string) {
205 const path = '/api/v1/users/ask-reset-password'
206
207 return makePostBodyRequest({
208 url,
209 path,
210 fields: { email },
211 statusCodeExpected: 204
212 })
213}
214
215function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
216 const path = '/api/v1/users/' + userId + '/reset-password'
217
218 return makePostBodyRequest({
219 url,
220 path,
221 fields: { password, verificationString },
222 statusCodeExpected
223 })
224}
225
0e1dc3e7
C
226// ---------------------------------------------------------------------------
227
228export {
229 createUser,
230 registerUser,
5c98d3bf 231 getMyUserInformation,
26d21b78 232 getMyUserVideoRating,
ce5496d6 233 getMyUserVideoQuotaUsed,
0e1dc3e7
C
234 getUsersList,
235 getUsersListPaginationAndSort,
236 removeUser,
5c98d3bf
C
237 updateUser,
238 updateMyUser,
c5911fd3 239 getUserInformation,
f076daa7
C
240 askResetPassword,
241 resetPassword,
c5911fd3 242 updateMyAvatar
0e1dc3e7 243}