]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/tests/shared/checks.ts
Use displayName as label in channelFilters
[github/Chocobozzz/PeerTube.git] / server / tests / shared / checks.ts
... / ...
CommitLineData
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'
6import { root } from '@shared/core-utils'
7import { HttpStatusCode } from '@shared/models'
8import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
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
18function expectStartWith (str: string, start: string) {
19 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
20}
21
22function expectNotStartWith (str: string, start: string) {
23 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
24}
25
26function expectEndWith (str: string, end: string) {
27 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
28}
29
30// ---------------------------------------------------------------------------
31
32async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
33 const content = await server.servers.getLogContent()
34
35 expect(content.toString()).to.not.contain(str)
36}
37
38async function expectLogContain (server: PeerTubeServer, str: string) {
39 const content = await server.servers.getLogContent()
40
41 expect(content.toString()).to.contain(str)
42}
43
44async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
45 const res = await makeGetRequest({
46 url,
47 path: imageHTTPPath,
48 expectedStatus: HttpStatusCode.OK_200
49 })
50
51 const body = res.body
52
53 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
54 const minLength = data.length - ((40 * data.length) / 100)
55 const maxLength = data.length + ((40 * data.length) / 100)
56
57 expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
58 expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
59}
60
61async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
62 const base = server.servers.buildDirectory(directory)
63
64 expect(await pathExists(join(base, filePath))).to.equal(exist)
65}
66
67function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
68 return makeGetRequest({
69 url,
70 path,
71 token,
72 query: { ...query, start: 'hello' },
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400
74 })
75}
76
77async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
78 await makeGetRequest({
79 url,
80 path,
81 token,
82 query: { ...query, count: 'hello' },
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
84 })
85
86 await makeGetRequest({
87 url,
88 path,
89 token,
90 query: { ...query, count: 2000 },
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400
92 })
93}
94
95function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
96 return makeGetRequest({
97 url,
98 path,
99 token,
100 query: { ...query, sort: 'hello' },
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400
102 })
103}
104
105export {
106 dateIsValid,
107 testImage,
108 expectLogDoesNotContain,
109 testFileExistsOrNot,
110 expectStartWith,
111 expectNotStartWith,
112 expectEndWith,
113 checkBadStartPagination,
114 checkBadCountPagination,
115 checkBadSortPagination,
116 expectLogContain
117}