PeerTube/shared/extra-utils/users/users.ts

368 lines
11 KiB
TypeScript
Raw Normal View History

2017-09-04 14:21:47 -05:00
import * as request from 'supertest'
import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
2019-04-15 03:49:46 -05:00
import { UserAdminFlag } from '../../models/users/user-flag.model'
import { UserRegister } from '../../models/users/user-register.model'
import { UserRole } from '../../models/users/user-role'
import { ServerInfo } from '../server/servers'
import { userLogin } from './login'
2019-07-19 03:37:35 -05:00
import { UserUpdateMe } from '../../models/users'
2017-10-27 10:27:06 -05:00
2019-04-15 03:49:46 -05:00
type CreateUserArgs = { url: string,
2017-10-27 10:27:06 -05:00
accessToken: string,
username: string,
password: string,
2019-04-15 03:49:46 -05:00
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
2017-09-04 14:21:47 -05:00
const path = '/api/v1/users'
const body = {
username,
password,
2017-10-27 10:27:06 -05:00
role,
2019-04-15 03:49:46 -05:00
adminFlags,
2017-09-05 15:09:16 -05:00
email: username + '@example.com',
videoQuota,
videoQuotaDaily
2017-09-04 14:21:47 -05:00
}
return request(url)
.post(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
.send(body)
.expect(specialStatus)
}
2019-03-05 03:58:44 -06:00
async function generateUserAccessToken (server: ServerInfo, username: string) {
const password = 'my super password'
2019-04-15 03:49:46 -05:00
await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
2019-03-05 03:58:44 -06:00
return userLogin(server, { username, password })
}
2017-09-04 14:21:47 -05:00
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
})
}
2017-12-28 08:25:31 -06:00
function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
2017-09-04 14:21:47 -05:00
const path = '/api/v1/users/me'
return request(url)
.get(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
2017-12-28 08:25:31 -06:00
.expect(specialStatus)
2017-09-04 14:21:47 -05:00
.expect('Content-Type', /json/)
}
2018-08-08 03:55:27 -05: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 05:53:09 -06: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 15:09:16 -05: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 08:25:31 -06:00
function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
2017-09-04 14:21:47 -05: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 08:25:31 -06:00
.expect(specialStatus)
2017-09-04 14:21:47 -05:00
.expect('Content-Type', /json/)
}
function getUsersList (url: string, accessToken: string) {
2017-09-04 14:21:47 -05:00
const path = '/api/v1/users'
return request(url)
.get(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
2017-09-04 14:21:47 -05:00
.expect(200)
.expect('Content-Type', /json/)
}
2018-10-08 08:51:38 -05:00
function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
2017-09-04 14:21:47 -05:00
const path = '/api/v1/users'
2019-06-06 08:39:11 -05:00
const query = {
start,
count,
sort,
search
}
2017-09-04 14:21:47 -05:00
return request(url)
.get(path)
2019-06-06 08:39:11 -05:00
.query(query)
2017-09-04 14:21:47 -05:00
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
2017-09-04 14:21:47 -05:00
.expect(200)
.expect('Content-Type', /json/)
}
2017-12-28 08:25:31 -06:00
function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
2017-09-04 14:21:47 -05: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 10:36:10 -05:00
function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
2018-08-08 07:58:21 -05:00
const path = '/api/v1/users'
2018-08-08 10:36:10 -05:00
let body: any
if (reason) body = { reason }
2018-08-08 07:58:21 -05:00
return request(url)
.post(path + '/' + userId + '/block')
2018-08-08 10:36:10 -05:00
.send(body)
2018-08-08 07:58:21 -05: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 08:25:31 -06:00
function updateMyUser (options: {
url: string
accessToken: string
currentPassword?: string
newPassword?: string
nsfwPolicy?: NSFWPolicyType
email?: string
2017-12-28 08:25:31 -06:00
autoPlayVideo?: boolean
displayName?: string
description?: string
videosHistoryEnabled?: boolean
2019-07-19 03:37:35 -05:00
theme?: string
2017-12-28 08:25:31 -06:00
}) {
2017-09-05 15:09:16 -05:00
const path = '/api/v1/users/me'
2017-09-04 14:21:47 -05:00
2019-07-19 03:37:35 -05:00
const toSend: UserUpdateMe = {}
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.theme !== undefined && options.theme !== null) toSend.theme = options.theme
if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
2019-07-19 03:37:35 -05:00
toSend.videosHistoryEnabled = options.videosHistoryEnabled
}
2017-12-28 08:25:31 -06:00
return makePutBodyRequest({
url: options.url,
path,
token: options.accessToken,
fields: toSend,
statusCodeExpected: 204
})
2017-09-05 15:09:16 -05:00
}
2017-12-29 12:10:13 -06:00
function updateMyAvatar (options: {
url: string,
accessToken: string,
fixture: string
}) {
const path = '/api/v1/users/me/avatar/pick'
return updateAvatarRequest(Object.assign(options, { path }))
2017-12-29 12:10:13 -06:00
}
2017-12-28 08:25:31 -06:00
function updateUser (options: {
url: string
userId: number,
accessToken: string,
email?: string,
emailVerified?: boolean,
2017-12-28 08:25:31 -06:00
videoQuota?: number,
videoQuotaDaily?: number,
password?: string,
2019-04-15 03:49:46 -05:00
adminFlags?: UserAdminFlag,
2017-12-28 08:25:31 -06:00
role?: UserRole
}) {
const path = '/api/v1/users/' + options.userId
2017-09-05 15:09:16 -05:00
const toSend = {}
if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
2017-12-28 08:25:31 -06:00
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
2017-12-28 08:25:31 -06:00
if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
2017-12-28 08:25:31 -06:00
if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
2019-04-15 03:49:46 -05:00
if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
2017-12-28 08:25:31 -06:00
return makePutBodyRequest({
url: options.url,
path,
token: options.accessToken,
fields: toSend,
statusCodeExpected: 204
})
2017-09-04 14:21:47 -05:00
}
2018-01-30 08:16:24 -06: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
})
}
function askSendVerifyEmail (url: string, email: string) {
const path = '/api/v1/users/ask-send-verify-email'
return makePostBodyRequest({
url,
path,
fields: { email },
statusCodeExpected: 204
})
}
2019-06-11 04:54:33 -05:00
function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
const path = '/api/v1/users/' + userId + '/verify-email'
return makePostBodyRequest({
url,
path,
2019-06-11 04:54:33 -05:00
fields: {
verificationString,
isPendingEmail
},
statusCodeExpected
})
}
2017-09-04 14:21:47 -05:00
// ---------------------------------------------------------------------------
export {
createUser,
registerUser,
2017-09-05 15:09:16 -05:00
getMyUserInformation,
2017-12-28 08:25:31 -06:00
getMyUserVideoRating,
2018-08-08 03:55:27 -05:00
deleteMe,
registerUserWithChannel,
2018-01-08 05:53:09 -06:00
getMyUserVideoQuotaUsed,
2017-09-04 14:21:47 -05:00
getUsersList,
getUsersListPaginationAndSort,
removeUser,
2017-09-05 15:09:16 -05:00
updateUser,
updateMyUser,
2017-12-29 12:10:13 -06:00
getUserInformation,
2018-08-08 07:58:21 -05:00
blockUser,
unblockUser,
2018-01-30 08:16:24 -06:00
askResetPassword,
resetPassword,
updateMyAvatar,
askSendVerifyEmail,
2019-03-05 03:58:44 -06:00
generateUserAccessToken,
verifyEmail
2017-09-04 14:21:47 -05:00
}