aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/users/users.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-15 15:26:15 +0200
committerChocobozzz <me@florianbigard.com>2019-04-24 16:25:52 +0200
commit94565d52bb2883e09f16d1363170ac9c0dccb7a1 (patch)
tree3dcd20cd7b5a5cca80bce32b655cdbfaddf7aa59 /shared/utils/users/users.ts
parent4ee7a4c9ac9280cda930a281c2d5a9a4c409cc14 (diff)
downloadPeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.gz
PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.zst
PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.zip
Shared utils -> extra-utils
Because they need dev dependencies
Diffstat (limited to 'shared/utils/users/users.ts')
-rw-r--r--shared/utils/users/users.ts330
1 files changed, 0 insertions, 330 deletions
diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts
deleted file mode 100644
index 2bd37b8be..000000000
--- a/shared/utils/users/users.ts
+++ /dev/null
@@ -1,330 +0,0 @@
1import * as request from 'supertest'
2import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
3
4import { UserRole } from '../../index'
5import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
6import { ServerInfo, userLogin } from '..'
7import { UserAdminFlag } from '../../models/users/user-flag.model'
8
9type CreateUserArgs = { url: string,
10 accessToken: string,
11 username: string,
12 password: string,
13 videoQuota?: number,
14 videoQuotaDaily?: number,
15 role?: UserRole,
16 adminFlags?: UserAdminFlag,
17 specialStatus?: number
18}
19function createUser (parameters: CreateUserArgs) {
20 const {
21 url,
22 accessToken,
23 username,
24 adminFlags,
25 password = 'password',
26 videoQuota = 1000000,
27 videoQuotaDaily = -1,
28 role = UserRole.USER,
29 specialStatus = 200
30 } = parameters
31
32 const path = '/api/v1/users'
33 const body = {
34 username,
35 password,
36 role,
37 adminFlags,
38 email: username + '@example.com',
39 videoQuota,
40 videoQuotaDaily
41 }
42
43 return request(url)
44 .post(path)
45 .set('Accept', 'application/json')
46 .set('Authorization', 'Bearer ' + accessToken)
47 .send(body)
48 .expect(specialStatus)
49}
50
51async function generateUserAccessToken (server: ServerInfo, username: string) {
52 const password = 'my super password'
53 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
54
55 return userLogin(server, { username, password })
56}
57
58function registerUser (url: string, username: string, password: string, specialStatus = 204) {
59 const path = '/api/v1/users/register'
60 const body = {
61 username,
62 password,
63 email: username + '@example.com'
64 }
65
66 return request(url)
67 .post(path)
68 .set('Accept', 'application/json')
69 .send(body)
70 .expect(specialStatus)
71}
72
73function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
74 const path = '/api/v1/users/me'
75
76 return request(url)
77 .get(path)
78 .set('Accept', 'application/json')
79 .set('Authorization', 'Bearer ' + accessToken)
80 .expect(specialStatus)
81 .expect('Content-Type', /json/)
82}
83
84function deleteMe (url: string, accessToken: string, specialStatus = 204) {
85 const path = '/api/v1/users/me'
86
87 return request(url)
88 .delete(path)
89 .set('Accept', 'application/json')
90 .set('Authorization', 'Bearer ' + accessToken)
91 .expect(specialStatus)
92}
93
94function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
95 const path = '/api/v1/users/me/video-quota-used'
96
97 return request(url)
98 .get(path)
99 .set('Accept', 'application/json')
100 .set('Authorization', 'Bearer ' + accessToken)
101 .expect(specialStatus)
102 .expect('Content-Type', /json/)
103}
104
105function getUserInformation (url: string, accessToken: string, userId: number) {
106 const path = '/api/v1/users/' + userId
107
108 return request(url)
109 .get(path)
110 .set('Accept', 'application/json')
111 .set('Authorization', 'Bearer ' + accessToken)
112 .expect(200)
113 .expect('Content-Type', /json/)
114}
115
116function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
117 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
118
119 return request(url)
120 .get(path)
121 .set('Accept', 'application/json')
122 .set('Authorization', 'Bearer ' + accessToken)
123 .expect(specialStatus)
124 .expect('Content-Type', /json/)
125}
126
127function getUsersList (url: string, accessToken: string) {
128 const path = '/api/v1/users'
129
130 return request(url)
131 .get(path)
132 .set('Accept', 'application/json')
133 .set('Authorization', 'Bearer ' + accessToken)
134 .expect(200)
135 .expect('Content-Type', /json/)
136}
137
138function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
139 const path = '/api/v1/users'
140
141 return request(url)
142 .get(path)
143 .query({ start })
144 .query({ count })
145 .query({ sort })
146 .query({ search })
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + accessToken)
149 .expect(200)
150 .expect('Content-Type', /json/)
151}
152
153function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
154 const path = '/api/v1/users'
155
156 return request(url)
157 .delete(path + '/' + userId)
158 .set('Accept', 'application/json')
159 .set('Authorization', 'Bearer ' + accessToken)
160 .expect(expectedStatus)
161}
162
163function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
164 const path = '/api/v1/users'
165 let body: any
166 if (reason) body = { reason }
167
168 return request(url)
169 .post(path + '/' + userId + '/block')
170 .send(body)
171 .set('Accept', 'application/json')
172 .set('Authorization', 'Bearer ' + accessToken)
173 .expect(expectedStatus)
174}
175
176function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
177 const path = '/api/v1/users'
178
179 return request(url)
180 .post(path + '/' + userId + '/unblock')
181 .set('Accept', 'application/json')
182 .set('Authorization', 'Bearer ' + accessToken)
183 .expect(expectedStatus)
184}
185
186function updateMyUser (options: {
187 url: string
188 accessToken: string
189 currentPassword?: string
190 newPassword?: string
191 nsfwPolicy?: NSFWPolicyType
192 email?: string
193 autoPlayVideo?: boolean
194 displayName?: string
195 description?: string
196 videosHistoryEnabled?: boolean
197}) {
198 const path = '/api/v1/users/me'
199
200 const toSend = {}
201 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
202 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
203 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
204 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
205 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
206 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
207 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
208 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
209 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
210 }
211
212 return makePutBodyRequest({
213 url: options.url,
214 path,
215 token: options.accessToken,
216 fields: toSend,
217 statusCodeExpected: 204
218 })
219}
220
221function updateMyAvatar (options: {
222 url: string,
223 accessToken: string,
224 fixture: string
225}) {
226 const path = '/api/v1/users/me/avatar/pick'
227
228 return updateAvatarRequest(Object.assign(options, { path }))
229}
230
231function updateUser (options: {
232 url: string
233 userId: number,
234 accessToken: string,
235 email?: string,
236 emailVerified?: boolean,
237 videoQuota?: number,
238 videoQuotaDaily?: number,
239 password?: string,
240 adminFlags?: UserAdminFlag,
241 role?: UserRole
242}) {
243 const path = '/api/v1/users/' + options.userId
244
245 const toSend = {}
246 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
247 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
248 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
249 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
250 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
251 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
252 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
253
254 return makePutBodyRequest({
255 url: options.url,
256 path,
257 token: options.accessToken,
258 fields: toSend,
259 statusCodeExpected: 204
260 })
261}
262
263function askResetPassword (url: string, email: string) {
264 const path = '/api/v1/users/ask-reset-password'
265
266 return makePostBodyRequest({
267 url,
268 path,
269 fields: { email },
270 statusCodeExpected: 204
271 })
272}
273
274function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
275 const path = '/api/v1/users/' + userId + '/reset-password'
276
277 return makePostBodyRequest({
278 url,
279 path,
280 fields: { password, verificationString },
281 statusCodeExpected
282 })
283}
284
285function askSendVerifyEmail (url: string, email: string) {
286 const path = '/api/v1/users/ask-send-verify-email'
287
288 return makePostBodyRequest({
289 url,
290 path,
291 fields: { email },
292 statusCodeExpected: 204
293 })
294}
295
296function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
297 const path = '/api/v1/users/' + userId + '/verify-email'
298
299 return makePostBodyRequest({
300 url,
301 path,
302 fields: { verificationString },
303 statusCodeExpected
304 })
305}
306
307// ---------------------------------------------------------------------------
308
309export {
310 createUser,
311 registerUser,
312 getMyUserInformation,
313 getMyUserVideoRating,
314 deleteMe,
315 getMyUserVideoQuotaUsed,
316 getUsersList,
317 getUsersListPaginationAndSort,
318 removeUser,
319 updateUser,
320 updateMyUser,
321 getUserInformation,
322 blockUser,
323 unblockUser,
324 askResetPassword,
325 resetPassword,
326 updateMyAvatar,
327 askSendVerifyEmail,
328 generateUserAccessToken,
329 verifyEmail
330}