]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/users/users.ts
Graceful job queue shutdown
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
CommitLineData
0e1dc3e7 1import * as request from 'supertest'
4bbfc6c6 2import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
0e1dc3e7 3
c5d31dba 4import { UserRole } from '../../../../shared/index'
0883b324 5import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type'
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,
0883b324 131 nsfwPolicy?: NSFWPolicyType,
26d21b78
C
132 email?: string,
133 autoPlayVideo?: boolean
ed56ad11 134 displayName?: string,
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
ed56ad11 145 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
26d21b78
C
146
147 return makePutBodyRequest({
148 url: options.url,
149 path,
150 token: options.accessToken,
151 fields: toSend,
152 statusCodeExpected: 204
153 })
5c98d3bf
C
154}
155
c5911fd3
C
156function updateMyAvatar (options: {
157 url: string,
158 accessToken: string,
159 fixture: string
160}) {
161 const path = '/api/v1/users/me/avatar/pick'
c5911fd3 162
4bbfc6c6 163 return updateAvatarRequest(Object.assign(options, { path }))
c5911fd3
C
164}
165
26d21b78
C
166function updateUser (options: {
167 url: string
168 userId: number,
169 accessToken: string,
170 email?: string,
171 videoQuota?: number,
172 role?: UserRole
173}) {
174 const path = '/api/v1/users/' + options.userId
5c98d3bf
C
175
176 const toSend = {}
26d21b78
C
177 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
178 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
179 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
180
181 return makePutBodyRequest({
182 url: options.url,
183 path,
184 token: options.accessToken,
185 fields: toSend,
186 statusCodeExpected: 204
187 })
0e1dc3e7
C
188}
189
f076daa7
C
190function askResetPassword (url: string, email: string) {
191 const path = '/api/v1/users/ask-reset-password'
192
193 return makePostBodyRequest({
194 url,
195 path,
196 fields: { email },
197 statusCodeExpected: 204
198 })
199}
200
201function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
202 const path = '/api/v1/users/' + userId + '/reset-password'
203
204 return makePostBodyRequest({
205 url,
206 path,
207 fields: { password, verificationString },
208 statusCodeExpected
209 })
210}
211
0e1dc3e7
C
212// ---------------------------------------------------------------------------
213
214export {
215 createUser,
216 registerUser,
5c98d3bf 217 getMyUserInformation,
26d21b78 218 getMyUserVideoRating,
ce5496d6 219 getMyUserVideoQuotaUsed,
0e1dc3e7
C
220 getUsersList,
221 getUsersListPaginationAndSort,
222 removeUser,
5c98d3bf
C
223 updateUser,
224 updateMyUser,
c5911fd3 225 getUserInformation,
f076daa7
C
226 askResetPassword,
227 resetPassword,
c5911fd3 228 updateMyAvatar
0e1dc3e7 229}