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