2020-11-25 05:04:18 -05:00
|
|
|
import { omit } from 'lodash'
|
2017-09-04 15:21:47 -04:00
|
|
|
import * as request from 'supertest'
|
2020-11-25 05:04:18 -05:00
|
|
|
import { UserUpdateMe } from '../../models/users'
|
2019-04-15 04:49:46 -04:00
|
|
|
import { UserAdminFlag } from '../../models/users/user-flag.model'
|
2019-05-28 04:46:32 -04:00
|
|
|
import { UserRegister } from '../../models/users/user-register.model'
|
2019-07-11 11:23:24 -04:00
|
|
|
import { UserRole } from '../../models/users/user-role'
|
2020-11-25 05:04:18 -05:00
|
|
|
import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
|
2019-07-11 11:23:24 -04:00
|
|
|
import { ServerInfo } from '../server/servers'
|
|
|
|
import { userLogin } from './login'
|
2020-12-07 08:32:36 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2017-10-27 11:27:06 -04:00
|
|
|
|
2019-12-06 09:59:12 -05:00
|
|
|
type CreateUserArgs = {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
username: string
|
|
|
|
password: string
|
|
|
|
videoQuota?: number
|
|
|
|
videoQuotaDaily?: number
|
|
|
|
role?: UserRole
|
|
|
|
adminFlags?: UserAdminFlag
|
2019-04-15 04:49:46 -04:00
|
|
|
specialStatus?: number
|
|
|
|
}
|
|
|
|
function createUser (parameters: CreateUserArgs) {
|
|
|
|
const {
|
|
|
|
url,
|
|
|
|
accessToken,
|
|
|
|
username,
|
|
|
|
adminFlags,
|
|
|
|
password = 'password',
|
|
|
|
videoQuota = 1000000,
|
|
|
|
videoQuotaDaily = -1,
|
|
|
|
role = UserRole.USER,
|
2020-12-07 08:32:36 -05:00
|
|
|
specialStatus = HttpStatusCode.OK_200
|
2019-04-15 04:49:46 -04:00
|
|
|
} = parameters
|
|
|
|
|
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,
|
2019-04-15 04:49:46 -04:00
|
|
|
adminFlags,
|
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)
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:58:44 -05:00
|
|
|
async function generateUserAccessToken (server: ServerInfo, username: string) {
|
|
|
|
const password = 'my super password'
|
2019-04-15 04:49:46 -04:00
|
|
|
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
|
2019-03-05 04:58:44 -05:00
|
|
|
|
|
|
|
return userLogin(server, { username, password })
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
|
2017-09-04 15:21:47 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-28 04:46:32 -04:00
|
|
|
function registerUserWithChannel (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
user: { username: string, password: string, displayName?: string }
|
2019-05-28 04:46:32 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:59:53 -04:00
|
|
|
if (options.user.displayName) {
|
|
|
|
Object.assign(body, { displayName: options.user.displayName })
|
|
|
|
}
|
|
|
|
|
2019-05-28 04:46:32 -04:00
|
|
|
return makePostBodyRequest({
|
|
|
|
url: options.url,
|
|
|
|
path,
|
|
|
|
fields: body,
|
2020-12-07 08:32:36 -05:00
|
|
|
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
|
2019-05-28 04:46:32 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_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/)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
|
2020-08-13 09:07:23 -04:00
|
|
|
const path = '/api/v1/users/scoped-tokens'
|
|
|
|
|
2020-11-25 05:04:18 -05:00
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token,
|
|
|
|
statusCodeExpected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
|
2020-11-25 05:04:18 -05:00
|
|
|
const path = '/api/v1/users/scoped-tokens'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token,
|
|
|
|
statusCodeExpected
|
|
|
|
})
|
2020-08-13 09:07:23 -04:00
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
|
2018-08-08 04:55:27 -04:00
|
|
|
const path = '/api/v1/users/me'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.delete(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.expect(specialStatus)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
|
2018-01-08 06:53:09 -05:00
|
|
|
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/)
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:19:03 -04:00
|
|
|
function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
|
2017-09-05 16:09:16 -04:00
|
|
|
const path = '/api/v1/users/' + userId
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
2020-03-27 10:19:03 -04:00
|
|
|
.query({ withStats })
|
2017-09-05 16:09:16 -04:00
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2020-12-07 08:32:36 -05:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2017-09-05 16:09:16 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_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)
|
2020-12-07 08:32:36 -05:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:49:51 -04:00
|
|
|
function getUsersListPaginationAndSort (
|
|
|
|
url: string,
|
|
|
|
accessToken: string,
|
|
|
|
start: number,
|
|
|
|
count: number,
|
|
|
|
sort: string,
|
|
|
|
search?: string,
|
|
|
|
blocked?: boolean
|
|
|
|
) {
|
2017-09-04 15:21:47 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
|
2019-06-06 09:39:11 -04:00
|
|
|
const query = {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
2020-07-02 16:49:51 -04:00
|
|
|
search,
|
|
|
|
blocked
|
2019-06-06 09:39:11 -04:00
|
|
|
}
|
|
|
|
|
2017-09-04 15:21:47 -04:00
|
|
|
return request(url)
|
|
|
|
.get(path)
|
2019-06-06 09:39:11 -04:00
|
|
|
.query(query)
|
2017-09-04 15:21:47 -04:00
|
|
|
.set('Accept', 'application/json')
|
2017-11-29 07:18:05 -05:00
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2020-12-07 08:32:36 -05:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2017-09-04 15:21:47 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_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)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function blockUser (
|
|
|
|
url: string,
|
|
|
|
userId: number | string,
|
|
|
|
accessToken: string,
|
|
|
|
expectedStatus = HttpStatusCode.NO_CONTENT_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)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
|
2018-08-08 08:58:21 -04:00
|
|
|
const path = '/api/v1/users'
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.post(path + '/' + userId + '/unblock')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
|
2017-09-05 16:09:16 -04:00
|
|
|
const path = '/api/v1/users/me'
|
2017-09-04 15:21:47 -04:00
|
|
|
|
2019-08-28 08:40:06 -04:00
|
|
|
const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
|
2017-12-28 09:25:31 -05:00
|
|
|
|
|
|
|
return makePutBodyRequest({
|
|
|
|
url: options.url,
|
|
|
|
path,
|
|
|
|
token: options.accessToken,
|
|
|
|
fields: toSend,
|
2020-12-07 08:32:36 -05:00
|
|
|
statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
|
2017-12-28 09:25:31 -05:00
|
|
|
})
|
2017-09-05 16:09:16 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 13:10:13 -05:00
|
|
|
function updateMyAvatar (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
2017-12-29 13:10:13 -05:00
|
|
|
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
|
2020-01-31 10:56:52 -05:00
|
|
|
userId: number
|
|
|
|
accessToken: string
|
|
|
|
email?: string
|
|
|
|
emailVerified?: boolean
|
|
|
|
videoQuota?: number
|
|
|
|
videoQuotaDaily?: number
|
|
|
|
password?: string
|
|
|
|
adminFlags?: UserAdminFlag
|
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 = {}
|
2019-02-11 03:30:29 -05:00
|
|
|
if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
|
2017-12-28 09:25:31 -05:00
|
|
|
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
|
2018-11-21 02:48:29 -05:00
|
|
|
if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
|
2017-12-28 09:25:31 -05:00
|
|
|
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
|
2019-04-15 04:49:46 -04:00
|
|
|
if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
|
2017-12-28 09:25:31 -05:00
|
|
|
|
|
|
|
return makePutBodyRequest({
|
|
|
|
url: options.url,
|
|
|
|
path,
|
|
|
|
token: options.accessToken,
|
|
|
|
fields: toSend,
|
2020-12-07 08:32:36 -05:00
|
|
|
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
|
2017-12-28 09:25:31 -05:00
|
|
|
})
|
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 },
|
2020-12-07 08:32:36 -05:00
|
|
|
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
|
2018-01-30 09:16:24 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function resetPassword (
|
|
|
|
url: string,
|
|
|
|
userId: number,
|
|
|
|
verificationString: string,
|
|
|
|
password: string,
|
|
|
|
statusCodeExpected = HttpStatusCode.NO_CONTENT_204
|
|
|
|
) {
|
2018-01-30 09:16:24 -05:00
|
|
|
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 },
|
2020-12-07 08:32:36 -05:00
|
|
|
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
|
2018-08-31 03:18:19 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function verifyEmail (
|
|
|
|
url: string,
|
|
|
|
userId: number,
|
|
|
|
verificationString: string,
|
|
|
|
isPendingEmail = false,
|
|
|
|
statusCodeExpected = HttpStatusCode.NO_CONTENT_204
|
|
|
|
) {
|
2018-08-31 03:18:19 -04:00
|
|
|
const path = '/api/v1/users/' + userId + '/verify-email'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path,
|
2019-06-11 05:54:33 -04:00
|
|
|
fields: {
|
|
|
|
verificationString,
|
|
|
|
isPendingEmail
|
|
|
|
},
|
2018-08-31 03:18:19 -04:00
|
|
|
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,
|
2019-05-28 04:46:32 -04:00
|
|
|
registerUserWithChannel,
|
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,
|
2020-11-25 05:04:18 -05:00
|
|
|
renewUserScopedTokens,
|
2018-08-31 03:18:19 -04:00
|
|
|
updateMyAvatar,
|
|
|
|
askSendVerifyEmail,
|
2019-03-05 04:58:44 -05:00
|
|
|
generateUserAccessToken,
|
2020-08-13 09:07:23 -04:00
|
|
|
verifyEmail,
|
|
|
|
getUserScopedTokens
|
2017-09-04 15:21:47 -04:00
|
|
|
}
|