2017-09-04 15:21:47 -04:00
|
|
|
import * as request from 'supertest'
|
2018-06-29 05:29:23 -04:00
|
|
|
import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
|
2017-09-04 15:21:47 -04:00
|
|
|
|
2017-12-28 07:59:22 -05:00
|
|
|
import { UserRole } from '../../../../shared/index'
|
2018-04-19 05:01:34 -04:00
|
|
|
import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type'
|
2017-10-27 11:27:06 -04:00
|
|
|
|
|
|
|
function createUser (
|
|
|
|
url: string,
|
|
|
|
accessToken: string,
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
videoQuota = 1000000,
|
2018-08-28 03:01:35 -04:00
|
|
|
videoQuotaDaily = -1,
|
2017-10-27 11:27:06 -04:00
|
|
|
role: UserRole = UserRole.USER,
|
2018-01-18 04:53:54 -05:00
|
|
|
specialStatus = 200
|
2017-10-27 11:27:06 -04:00
|
|
|
) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
const body = {
|
|
|
|
username,
|
|
|
|
password,
|
2017-10-27 11:27:06 -04:00
|
|
|
role,
|
2017-09-05 16:09:16 -04:00
|
|
|
email: username + '@example.com',
|
2018-08-28 03:01:35 -04:00
|
|
|
videoQuota,
|
|
|
|
videoQuotaDaily
|
2017-09-04 15:21:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.post(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.send(body)
|
|
|
|
.expect(specialStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:25:31 -05:00
|
|
|
function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users/me'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2017-12-28 09:25:31 -05:00
|
|
|
.expect(specialStatus)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2018-08-08 04:55:27 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-01-08 06:53:09 -05:00
|
|
|
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/)
|
|
|
|
}
|
|
|
|
|
2017-09-05 16:09:16 -04:00
|
|
|
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/)
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:25:31 -05:00
|
|
|
function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users/me/videos/' + videoId + '/rating'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2017-12-28 09:25:31 -05:00
|
|
|
.expect(specialStatus)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2017-11-29 07:18:05 -05:00
|
|
|
function getUsersList (url: string, accessToken: string) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
2017-11-29 07:18:05 -05:00
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2017-11-29 07:18:05 -05:00
|
|
|
function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
|
|
|
.query({ start })
|
|
|
|
.query({ count })
|
|
|
|
.query({ sort })
|
|
|
|
.set('Accept', 'application/json')
|
2017-11-29 07:18:05 -05:00
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:25:31 -05:00
|
|
|
function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.delete(path + '/' + userId)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2018-08-08 11:36:10 -04:00
|
|
|
function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
|
2018-08-08 08:58:21 -04:00
|
|
|
const path = '/api/v1/users'
|
2018-08-08 11:36:10 -04:00
|
|
|
let body: any
|
|
|
|
if (reason) body = { reason }
|
2018-08-08 08:58:21 -04:00
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.post(path + '/' + userId + '/block')
|
2018-08-08 11:36:10 -04:00
|
|
|
.send(body)
|
2018-08-08 08:58:21 -04:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:25:31 -05:00
|
|
|
function updateMyUser (options: {
|
|
|
|
url: string
|
|
|
|
accessToken: string,
|
2018-09-26 10:28:15 -04:00
|
|
|
currentPassword?: string,
|
2017-12-28 09:25:31 -05:00
|
|
|
newPassword?: string,
|
2018-04-19 05:01:34 -04:00
|
|
|
nsfwPolicy?: NSFWPolicyType,
|
2017-12-28 09:25:31 -05:00
|
|
|
email?: string,
|
|
|
|
autoPlayVideo?: boolean
|
2018-04-26 04:03:40 -04:00
|
|
|
displayName?: string,
|
2018-02-15 08:46:26 -05:00
|
|
|
description?: string
|
2017-12-28 09:25:31 -05:00
|
|
|
}) {
|
2017-09-05 16:09:16 -04:00
|
|
|
const path = '/api/v1/users/me'
|
2017-09-04 15:21:47 -04:00
|
|
|
|
|
|
|
const toSend = {}
|
2018-09-26 10:28:15 -04:00
|
|
|
if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
|
2017-12-28 09:25:31 -05:00
|
|
|
if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
|
2018-04-19 05:01:34 -04:00
|
|
|
if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
|
2017-12-28 09:25:31 -05:00
|
|
|
if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
|
|
|
|
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
|
2018-02-15 08:46:26 -05:00
|
|
|
if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
|
2018-04-26 04:03:40 -04:00
|
|
|
if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
|
2017-12-28 09:25:31 -05:00
|
|
|
|
|
|
|
return makePutBodyRequest({
|
|
|
|
url: options.url,
|
|
|
|
path,
|
|
|
|
token: options.accessToken,
|
|
|
|
fields: toSend,
|
|
|
|
statusCodeExpected: 204
|
|
|
|
})
|
2017-09-05 16:09:16 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 13:10:13 -05:00
|
|
|
function updateMyAvatar (options: {
|
|
|
|
url: string,
|
|
|
|
accessToken: string,
|
|
|
|
fixture: string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/users/me/avatar/pick'
|
|
|
|
|
2018-06-29 05:29:23 -04:00
|
|
|
return updateAvatarRequest(Object.assign(options, { path }))
|
2017-12-29 13:10:13 -05:00
|
|
|
}
|
|
|
|
|
2017-12-28 09:25:31 -05:00
|
|
|
function updateUser (options: {
|
|
|
|
url: string
|
|
|
|
userId: number,
|
|
|
|
accessToken: string,
|
|
|
|
email?: string,
|
|
|
|
videoQuota?: number,
|
2018-08-28 03:01:35 -04:00
|
|
|
videoQuotaDaily?: number,
|
2017-12-28 09:25:31 -05:00
|
|
|
role?: UserRole
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/users/' + options.userId
|
2017-09-05 16:09:16 -04:00
|
|
|
|
|
|
|
const toSend = {}
|
2017-12-28 09:25:31 -05:00
|
|
|
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
|
|
|
|
if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
|
2018-08-28 03:01:35 -04:00
|
|
|
if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
|
2017-12-28 09:25:31 -05:00
|
|
|
if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
|
|
|
|
|
|
|
|
return makePutBodyRequest({
|
|
|
|
url: options.url,
|
|
|
|
path,
|
|
|
|
token: options.accessToken,
|
|
|
|
fields: toSend,
|
|
|
|
statusCodeExpected: 204
|
|
|
|
})
|
2017-09-04 15:21:47 -04:00
|
|
|
}
|
|
|
|
|
2018-01-30 09:16:24 -05:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:18:19 -04:00
|
|
|
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, statusCodeExpected = 204) {
|
|
|
|
const path = '/api/v1/users/' + userId + '/verify-email'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
fields: { verificationString },
|
|
|
|
statusCodeExpected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-04 15:21:47 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
createUser,
|
|
|
|
registerUser,
|
2017-09-05 16:09:16 -04:00
|
|
|
getMyUserInformation,
|
2017-12-28 09:25:31 -05:00
|
|
|
getMyUserVideoRating,
|
2018-08-08 04:55:27 -04:00
|
|
|
deleteMe,
|
2018-01-08 06:53:09 -05:00
|
|
|
getMyUserVideoQuotaUsed,
|
2017-09-04 15:21:47 -04:00
|
|
|
getUsersList,
|
|
|
|
getUsersListPaginationAndSort,
|
|
|
|
removeUser,
|
2017-09-05 16:09:16 -04:00
|
|
|
updateUser,
|
|
|
|
updateMyUser,
|
2017-12-29 13:10:13 -05:00
|
|
|
getUserInformation,
|
2018-08-08 08:58:21 -04:00
|
|
|
blockUser,
|
|
|
|
unblockUser,
|
2018-01-30 09:16:24 -05:00
|
|
|
askResetPassword,
|
|
|
|
resetPassword,
|
2018-08-31 03:18:19 -04:00
|
|
|
updateMyAvatar,
|
|
|
|
askSendVerifyEmail,
|
|
|
|
verifyEmail
|
2017-09-04 15:21:47 -04:00
|
|
|
}
|