PeerTube/server/tests/utils/users/users.ts

215 lines
5.8 KiB
TypeScript
Raw Normal View History

2017-12-29 12:10:13 -06:00
import { isAbsolute, join } from 'path'
2017-09-04 14:21:47 -05:00
import * as request from 'supertest'
2017-12-29 12:10:13 -06:00
import { makePostUploadRequest, makePutBodyRequest } from '../'
2017-09-04 14:21:47 -05:00
2017-12-28 06:59:22 -06:00
import { UserRole } from '../../../../shared/index'
2017-10-27 10:27:06 -05:00
function createUser (
url: string,
accessToken: string,
username: string,
password: string,
videoQuota = 1000000,
role: UserRole = UserRole.USER,
specialStatus = 200
2017-10-27 10:27:06 -05:00
) {
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,
2017-09-05 15:09:16 -05:00
email: username + '@example.com',
videoQuota
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)
}
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 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-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/)
}
function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
2017-09-04 14:21:47 -05:00
const path = '/api/v1/users'
return request(url)
.get(path)
.query({ start })
.query({ count })
.query({ sort })
.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)
}
2017-12-28 08:25:31 -06:00
function updateMyUser (options: {
url: string
accessToken: string,
newPassword?: string,
displayNSFW?: boolean,
email?: string,
autoPlayVideo?: boolean
}) {
2017-09-05 15:09:16 -05:00
const path = '/api/v1/users/me'
2017-09-04 14:21:47 -05:00
const toSend = {}
2017-12-28 08:25:31 -06:00
if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
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'
let filePath = ''
if (isAbsolute(options.fixture)) {
filePath = options.fixture
} else {
filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
}
return makePostUploadRequest({
url: options.url,
path,
token: options.accessToken,
fields: {},
attaches: { avatarfile: filePath },
statusCodeExpected: 200
})
}
2017-12-28 08:25:31 -06:00
function updateUser (options: {
url: string
userId: number,
accessToken: string,
email?: string,
videoQuota?: number,
role?: UserRole
}) {
const path = '/api/v1/users/' + options.userId
2017-09-05 15:09:16 -05:00
const toSend = {}
2017-12-28 08:25:31 -06:00
if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
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 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-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,
updateMyAvatar
2017-09-04 14:21:47 -05:00
}