1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { pathExists, readFile } from 'fs-extra'
5 import { join } from 'path'
6 import { root } from '@shared/core-utils'
7 import { HttpStatusCode } from '@shared/models'
8 import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
10 // Default interval -> 5 minutes
11 function dateIsValid (dateString: string, interval = 300000) {
12 const dateToCheck = new Date(dateString)
13 const now = new Date()
15 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
18 function expectStartWith (str: string, start: string) {
19 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
22 function expectNotStartWith (str: string, start: string) {
23 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
26 async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
27 const content = await server.servers.getLogContent()
29 expect(content.toString()).to.not.contain(str)
32 async function expectLogContain (server: PeerTubeServer, str: string) {
33 const content = await server.servers.getLogContent()
35 expect(content.toString()).to.contain(str)
38 async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
39 const res = await makeGetRequest({
42 expectedStatus: HttpStatusCode.OK_200
47 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
48 const minLength = data.length - ((40 * data.length) / 100)
49 const maxLength = data.length + ((40 * data.length) / 100)
51 expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
52 expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
55 async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
56 const base = server.servers.buildDirectory(directory)
58 expect(await pathExists(join(base, filePath))).to.equal(exist)
61 function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
62 return makeGetRequest({
66 query: { ...query, start: 'hello' },
67 expectedStatus: HttpStatusCode.BAD_REQUEST_400
71 async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
72 await makeGetRequest({
76 query: { ...query, count: 'hello' },
77 expectedStatus: HttpStatusCode.BAD_REQUEST_400
80 await makeGetRequest({
84 query: { ...query, count: 2000 },
85 expectedStatus: HttpStatusCode.BAD_REQUEST_400
89 function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
90 return makeGetRequest({
94 query: { ...query, sort: 'hello' },
95 expectedStatus: HttpStatusCode.BAD_REQUEST_400
102 expectLogDoesNotContain,
106 checkBadStartPagination,
107 checkBadCountPagination,
108 checkBadSortPagination,