aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/users.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-07 14:32:36 +0100
committerGitHub <noreply@github.com>2020-12-07 14:32:36 +0100
commit2d53be0267acc49cda46707b885096193a1f4e9c (patch)
tree887061a34bc67f40acbb96a6278f9544bf83caeb /shared/extra-utils/users/users.ts
parentadc1f09c0dbd997f34028c1c82d1c118dc8ead80 (diff)
downloadPeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.gz
PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.zst
PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.zip
replace numbers with typed http status codes (#3409)
Diffstat (limited to 'shared/extra-utils/users/users.ts')
-rw-r--r--shared/extra-utils/users/users.ts63
1 files changed, 41 insertions, 22 deletions
diff --git a/shared/extra-utils/users/users.ts b/shared/extra-utils/users/users.ts
index ebb8bc257..c683dcdd1 100644
--- a/shared/extra-utils/users/users.ts
+++ b/shared/extra-utils/users/users.ts
@@ -7,6 +7,7 @@ import { UserRole } from '../../models/users/user-role'
7import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' 7import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
8import { ServerInfo } from '../server/servers' 8import { ServerInfo } from '../server/servers'
9import { userLogin } from './login' 9import { userLogin } from './login'
10import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
10 11
11type CreateUserArgs = { 12type CreateUserArgs = {
12 url: string 13 url: string
@@ -29,7 +30,7 @@ function createUser (parameters: CreateUserArgs) {
29 videoQuota = 1000000, 30 videoQuota = 1000000,
30 videoQuotaDaily = -1, 31 videoQuotaDaily = -1,
31 role = UserRole.USER, 32 role = UserRole.USER,
32 specialStatus = 200 33 specialStatus = HttpStatusCode.OK_200
33 } = parameters 34 } = parameters
34 35
35 const path = '/api/v1/users' 36 const path = '/api/v1/users'
@@ -58,7 +59,7 @@ async function generateUserAccessToken (server: ServerInfo, username: string) {
58 return userLogin(server, { username, password }) 59 return userLogin(server, { username, password })
59} 60}
60 61
61function registerUser (url: string, username: string, password: string, specialStatus = 204) { 62function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
62 const path = '/api/v1/users/register' 63 const path = '/api/v1/users/register'
63 const body = { 64 const body = {
64 username, 65 username,
@@ -94,11 +95,11 @@ function registerUserWithChannel (options: {
94 url: options.url, 95 url: options.url,
95 path, 96 path,
96 fields: body, 97 fields: body,
97 statusCodeExpected: 204 98 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
98 }) 99 })
99} 100}
100 101
101function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { 102function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
102 const path = '/api/v1/users/me' 103 const path = '/api/v1/users/me'
103 104
104 return request(url) 105 return request(url)
@@ -109,7 +110,7 @@ function getMyUserInformation (url: string, accessToken: string, specialStatus =
109 .expect('Content-Type', /json/) 110 .expect('Content-Type', /json/)
110} 111}
111 112
112function getUserScopedTokens (url: string, token: string, statusCodeExpected = 200) { 113function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
113 const path = '/api/v1/users/scoped-tokens' 114 const path = '/api/v1/users/scoped-tokens'
114 115
115 return makeGetRequest({ 116 return makeGetRequest({
@@ -120,7 +121,7 @@ function getUserScopedTokens (url: string, token: string, statusCodeExpected = 2
120 }) 121 })
121} 122}
122 123
123function renewUserScopedTokens (url: string, token: string, statusCodeExpected = 200) { 124function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
124 const path = '/api/v1/users/scoped-tokens' 125 const path = '/api/v1/users/scoped-tokens'
125 126
126 return makePostBodyRequest({ 127 return makePostBodyRequest({
@@ -131,7 +132,7 @@ function renewUserScopedTokens (url: string, token: string, statusCodeExpected =
131 }) 132 })
132} 133}
133 134
134function deleteMe (url: string, accessToken: string, specialStatus = 204) { 135function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
135 const path = '/api/v1/users/me' 136 const path = '/api/v1/users/me'
136 137
137 return request(url) 138 return request(url)
@@ -141,7 +142,7 @@ function deleteMe (url: string, accessToken: string, specialStatus = 204) {
141 .expect(specialStatus) 142 .expect(specialStatus)
142} 143}
143 144
144function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { 145function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
145 const path = '/api/v1/users/me/video-quota-used' 146 const path = '/api/v1/users/me/video-quota-used'
146 147
147 return request(url) 148 return request(url)
@@ -160,11 +161,11 @@ function getUserInformation (url: string, accessToken: string, userId: number, w
160 .query({ withStats }) 161 .query({ withStats })
161 .set('Accept', 'application/json') 162 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken) 163 .set('Authorization', 'Bearer ' + accessToken)
163 .expect(200) 164 .expect(HttpStatusCode.OK_200)
164 .expect('Content-Type', /json/) 165 .expect('Content-Type', /json/)
165} 166}
166 167
167function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { 168function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
168 const path = '/api/v1/users/me/videos/' + videoId + '/rating' 169 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
169 170
170 return request(url) 171 return request(url)
@@ -182,7 +183,7 @@ function getUsersList (url: string, accessToken: string) {
182 .get(path) 183 .get(path)
183 .set('Accept', 'application/json') 184 .set('Accept', 'application/json')
184 .set('Authorization', 'Bearer ' + accessToken) 185 .set('Authorization', 'Bearer ' + accessToken)
185 .expect(200) 186 .expect(HttpStatusCode.OK_200)
186 .expect('Content-Type', /json/) 187 .expect('Content-Type', /json/)
187} 188}
188 189
@@ -210,11 +211,11 @@ function getUsersListPaginationAndSort (
210 .query(query) 211 .query(query)
211 .set('Accept', 'application/json') 212 .set('Accept', 'application/json')
212 .set('Authorization', 'Bearer ' + accessToken) 213 .set('Authorization', 'Bearer ' + accessToken)
213 .expect(200) 214 .expect(HttpStatusCode.OK_200)
214 .expect('Content-Type', /json/) 215 .expect('Content-Type', /json/)
215} 216}
216 217
217function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { 218function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
218 const path = '/api/v1/users' 219 const path = '/api/v1/users'
219 220
220 return request(url) 221 return request(url)
@@ -224,7 +225,13 @@ function removeUser (url: string, userId: number | string, accessToken: string,
224 .expect(expectedStatus) 225 .expect(expectedStatus)
225} 226}
226 227
227function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { 228function blockUser (
229 url: string,
230 userId: number | string,
231 accessToken: string,
232 expectedStatus = HttpStatusCode.NO_CONTENT_204,
233 reason?: string
234) {
228 const path = '/api/v1/users' 235 const path = '/api/v1/users'
229 let body: any 236 let body: any
230 if (reason) body = { reason } 237 if (reason) body = { reason }
@@ -237,7 +244,7 @@ function blockUser (url: string, userId: number | string, accessToken: string, e
237 .expect(expectedStatus) 244 .expect(expectedStatus)
238} 245}
239 246
240function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { 247function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
241 const path = '/api/v1/users' 248 const path = '/api/v1/users'
242 249
243 return request(url) 250 return request(url)
@@ -247,7 +254,7 @@ function unblockUser (url: string, userId: number | string, accessToken: string,
247 .expect(expectedStatus) 254 .expect(expectedStatus)
248} 255}
249 256
250function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) { 257function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
251 const path = '/api/v1/users/me' 258 const path = '/api/v1/users/me'
252 259
253 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') 260 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
@@ -257,7 +264,7 @@ function updateMyUser (options: { url: string, accessToken: string, statusCodeEx
257 path, 264 path,
258 token: options.accessToken, 265 token: options.accessToken,
259 fields: toSend, 266 fields: toSend,
260 statusCodeExpected: options.statusCodeExpected || 204 267 statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
261 }) 268 })
262} 269}
263 270
@@ -299,7 +306,7 @@ function updateUser (options: {
299 path, 306 path,
300 token: options.accessToken, 307 token: options.accessToken,
301 fields: toSend, 308 fields: toSend,
302 statusCodeExpected: 204 309 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
303 }) 310 })
304} 311}
305 312
@@ -310,11 +317,17 @@ function askResetPassword (url: string, email: string) {
310 url, 317 url,
311 path, 318 path,
312 fields: { email }, 319 fields: { email },
313 statusCodeExpected: 204 320 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
314 }) 321 })
315} 322}
316 323
317function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { 324function resetPassword (
325 url: string,
326 userId: number,
327 verificationString: string,
328 password: string,
329 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
330) {
318 const path = '/api/v1/users/' + userId + '/reset-password' 331 const path = '/api/v1/users/' + userId + '/reset-password'
319 332
320 return makePostBodyRequest({ 333 return makePostBodyRequest({
@@ -332,11 +345,17 @@ function askSendVerifyEmail (url: string, email: string) {
332 url, 345 url,
333 path, 346 path,
334 fields: { email }, 347 fields: { email },
335 statusCodeExpected: 204 348 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
336 }) 349 })
337} 350}
338 351
339function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { 352function verifyEmail (
353 url: string,
354 userId: number,
355 verificationString: string,
356 isPendingEmail = false,
357 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
358) {
340 const path = '/api/v1/users/' + userId + '/verify-email' 359 const path = '/api/v1/users/' + userId + '/verify-email'
341 360
342 return makePostBodyRequest({ 361 return makePostBodyRequest({