]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/shared/checks.ts
Live supports object storage
[github/Chocobozzz/PeerTube.git] / server / tests / shared / checks.ts
CommitLineData
6c5065a0
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
3import { expect } from 'chai'
4import { pathExists, readFile } from 'fs-extra'
5import { join } from 'path'
06aad801 6import { root } from '@shared/core-utils'
c0e8b12e 7import { HttpStatusCode } from '@shared/models'
c55e3d72 8import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
6c5065a0
C
9
10// Default interval -> 5 minutes
11function dateIsValid (dateString: string, interval = 300000) {
12 const dateToCheck = new Date(dateString)
13 const now = new Date()
14
15 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
16}
17
0305db28
JB
18function expectStartWith (str: string, start: string) {
19 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
20}
21
ca3d5912
C
22function expectNotStartWith (str: string, start: string) {
23 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
24}
25
1f6125be
C
26async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
27 const content = await server.servers.getLogContent()
28
29 expect(content.toString()).to.not.contain(str)
30}
31
630d0a1b
C
32async function expectLogContain (server: PeerTubeServer, str: string) {
33 const content = await server.servers.getLogContent()
34
35 expect(content.toString()).to.contain(str)
36}
37
4c6d99e5 38async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
6c5065a0
C
39 const res = await makeGetRequest({
40 url,
4c6d99e5 41 path: imageHTTPPath,
c0e8b12e 42 expectedStatus: HttpStatusCode.OK_200
6c5065a0
C
43 })
44
45 const body = res.body
46
47 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
c47c3bcb
C
48 const minLength = data.length - ((40 * data.length) / 100)
49 const maxLength = data.length + ((40 * data.length) / 100)
6c5065a0 50
4c6d99e5
C
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')
6c5065a0
C
53}
54
254d3579 55async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
89d241a7 56 const base = server.servers.buildDirectory(directory)
6c5065a0
C
57
58 expect(await pathExists(join(base, filePath))).to.equal(exist)
59}
60
c55e3d72
C
61function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
62 return makeGetRequest({
63 url,
64 path,
65 token,
66 query: { ...query, start: 'hello' },
67 expectedStatus: HttpStatusCode.BAD_REQUEST_400
68 })
69}
70
71async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
72 await makeGetRequest({
73 url,
74 path,
75 token,
76 query: { ...query, count: 'hello' },
77 expectedStatus: HttpStatusCode.BAD_REQUEST_400
78 })
79
80 await makeGetRequest({
81 url,
82 path,
83 token,
84 query: { ...query, count: 2000 },
85 expectedStatus: HttpStatusCode.BAD_REQUEST_400
86 })
87}
88
89function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
90 return makeGetRequest({
91 url,
92 path,
93 token,
94 query: { ...query, sort: 'hello' },
95 expectedStatus: HttpStatusCode.BAD_REQUEST_400
96 })
97}
98
6c5065a0
C
99export {
100 dateIsValid,
101 testImage,
1f6125be 102 expectLogDoesNotContain,
0305db28 103 testFileExistsOrNot,
c55e3d72 104 expectStartWith,
ca3d5912 105 expectNotStartWith,
c55e3d72
C
106 checkBadStartPagination,
107 checkBadCountPagination,
630d0a1b
C
108 checkBadSortPagination,
109 expectLogContain
6c5065a0 110}