+import { omit } from 'lodash'
import * as request from 'supertest'
-import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
-
-import { UserRole } from '../../index'
-import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
-import { ServerInfo, userLogin } from '..'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+import { UserUpdateMe } from '../../models/users'
import { UserAdminFlag } from '../../models/users/user-flag.model'
+import { UserRegister } from '../../models/users/user-register.model'
+import { UserRole } from '../../models/users/user-role'
+import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateImageRequest } from '../requests/requests'
+import { ServerInfo } from '../server/servers'
+import { userLogin } from './login'
-type CreateUserArgs = { url: string,
- accessToken: string,
- username: string,
- password: string,
- videoQuota?: number,
- videoQuotaDaily?: number,
- role?: UserRole,
- adminFlags?: UserAdminFlag,
+function createUser (parameters: {
+ 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,
videoQuota = 1000000,
videoQuotaDaily = -1,
role = UserRole.USER,
- specialStatus = 200
+ specialStatus = HttpStatusCode.OK_200
} = parameters
const path = '/api/v1/users'
.expect(specialStatus)
}
+async function generateUser (server: ServerInfo, username: string) {
+ const password = 'my super password'
+ const resCreate = await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
+
+ const token = await userLogin(server, { username, password })
+
+ const resMe = await getMyUserInformation(server.url, token)
+
+ return {
+ token,
+ userId: resCreate.body.user.id,
+ userChannelId: resMe.body.videoChannels[0].id
+ }
+}
+
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) {
+function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
const path = '/api/v1/users/register'
const body = {
username,
.expect(specialStatus)
}
-function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
+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: HttpStatusCode.NO_CONTENT_204
+ })
+}
+
+function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/me'
return request(url)
.expect('Content-Type', /json/)
}
-function deleteMe (url: string, accessToken: string, specialStatus = 204) {
+function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
+ const path = '/api/v1/users/scoped-tokens'
+
+ return makeGetRequest({
+ url,
+ path,
+ token,
+ statusCodeExpected
+ })
+}
+
+function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
+ const path = '/api/v1/users/scoped-tokens'
+
+ return makePostBodyRequest({
+ url,
+ path,
+ token,
+ statusCodeExpected
+ })
+}
+
+function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
const path = '/api/v1/users/me'
return request(url)
.expect(specialStatus)
}
-function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
+function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/me/video-quota-used'
return request(url)
.expect('Content-Type', /json/)
}
-function getUserInformation (url: string, accessToken: string, userId: number) {
+function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
const path = '/api/v1/users/' + userId
return request(url)
.get(path)
+ .query({ withStats })
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
- .expect(200)
+ .expect(HttpStatusCode.OK_200)
.expect('Content-Type', /json/)
}
-function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
+function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
const path = '/api/v1/users/me/videos/' + videoId + '/rating'
return request(url)
.get(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
- .expect(200)
+ .expect(HttpStatusCode.OK_200)
.expect('Content-Type', /json/)
}
-function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
+function getUsersListPaginationAndSort (
+ url: string,
+ accessToken: string,
+ start: number,
+ count: number,
+ sort: string,
+ search?: string,
+ blocked?: boolean
+) {
const path = '/api/v1/users'
+ const query = {
+ start,
+ count,
+ sort,
+ search,
+ blocked
+ }
+
return request(url)
.get(path)
- .query({ start })
- .query({ count })
- .query({ sort })
- .query({ search })
+ .query(query)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
- .expect(200)
+ .expect(HttpStatusCode.OK_200)
.expect('Content-Type', /json/)
}
-function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
+function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
const path = '/api/v1/users'
return request(url)
.expect(expectedStatus)
}
-function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
+function blockUser (
+ url: string,
+ userId: number | string,
+ accessToken: string,
+ expectedStatus = HttpStatusCode.NO_CONTENT_204,
+ reason?: string
+) {
const path = '/api/v1/users'
let body: any
if (reason) body = { reason }
.expect(expectedStatus)
}
-function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
+function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
const path = '/api/v1/users'
return request(url)
.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
-}) {
+function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
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
- }
+ const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
return makePutBodyRequest({
url: options.url,
path,
token: options.accessToken,
fields: toSend,
- statusCodeExpected: 204
+ statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
})
}
function updateMyAvatar (options: {
- url: string,
- accessToken: string,
+ url: string
+ accessToken: string
fixture: string
}) {
const path = '/api/v1/users/me/avatar/pick'
- return updateAvatarRequest(Object.assign(options, { path }))
+ return updateImageRequest({ ...options, path, fieldname: 'avatarfile' })
}
function updateUser (options: {
url: string
- userId: number,
- accessToken: string,
- email?: string,
- emailVerified?: boolean,
- videoQuota?: number,
- videoQuotaDaily?: number,
- password?: string,
- adminFlags?: UserAdminFlag,
+ userId: number
+ accessToken: string
+ email?: string
+ emailVerified?: boolean
+ videoQuota?: number
+ videoQuotaDaily?: number
+ password?: string
+ adminFlags?: UserAdminFlag
+ pluginAuth?: string
role?: UserRole
}) {
const path = '/api/v1/users/' + options.userId
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
+ if (options.pluginAuth !== undefined) toSend['pluginAuth'] = options.pluginAuth
return makePutBodyRequest({
url: options.url,
path,
token: options.accessToken,
fields: toSend,
- statusCodeExpected: 204
+ statusCodeExpected: HttpStatusCode.NO_CONTENT_204
})
}
url,
path,
fields: { email },
- statusCodeExpected: 204
+ statusCodeExpected: HttpStatusCode.NO_CONTENT_204
})
}
-function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
+function resetPassword (
+ url: string,
+ userId: number,
+ verificationString: string,
+ password: string,
+ statusCodeExpected = HttpStatusCode.NO_CONTENT_204
+) {
const path = '/api/v1/users/' + userId + '/reset-password'
return makePostBodyRequest({
url,
path,
fields: { email },
- statusCodeExpected: 204
+ statusCodeExpected: HttpStatusCode.NO_CONTENT_204
})
}
-function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
+function verifyEmail (
+ url: string,
+ userId: number,
+ verificationString: string,
+ isPendingEmail = false,
+ statusCodeExpected = HttpStatusCode.NO_CONTENT_204
+) {
const path = '/api/v1/users/' + userId + '/verify-email'
return makePostBodyRequest({
url,
path,
- fields: { verificationString },
+ fields: {
+ verificationString,
+ isPendingEmail
+ },
statusCodeExpected
})
}
getMyUserInformation,
getMyUserVideoRating,
deleteMe,
+ registerUserWithChannel,
getMyUserVideoQuotaUsed,
getUsersList,
getUsersListPaginationAndSort,
unblockUser,
askResetPassword,
resetPassword,
+ renewUserScopedTokens,
updateMyAvatar,
+ generateUser,
askSendVerifyEmail,
generateUserAccessToken,
- verifyEmail
+ verifyEmail,
+ getUserScopedTokens
}