]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/shared/checks.ts
Add runner server tests
[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'
d41f4a6d 5import JPEG from 'jpeg-js'
6c5065a0 6import { join } from 'path'
d41f4a6d
C
7import pixelmatch from 'pixelmatch'
8import { PNG } from 'pngjs'
06aad801 9import { root } from '@shared/core-utils'
c0e8b12e 10import { HttpStatusCode } from '@shared/models'
c55e3d72 11import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
6c5065a0
C
12
13// Default interval -> 5 minutes
d102de1b 14function dateIsValid (dateString: string | Date, interval = 300000) {
6c5065a0
C
15 const dateToCheck = new Date(dateString)
16 const now = new Date()
17
18 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
19}
20
0305db28
JB
21function expectStartWith (str: string, start: string) {
22 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
23}
24
ca3d5912
C
25function expectNotStartWith (str: string, start: string) {
26 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
27}
28
71e3e879
C
29function expectEndWith (str: string, end: string) {
30 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
31}
32
33// ---------------------------------------------------------------------------
34
1f6125be
C
35async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
36 const content = await server.servers.getLogContent()
37
38 expect(content.toString()).to.not.contain(str)
39}
40
630d0a1b
C
41async function expectLogContain (server: PeerTubeServer, str: string) {
42 const content = await server.servers.getLogContent()
43
44 expect(content.toString()).to.contain(str)
45}
46
d41f4a6d 47async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
6c5065a0
C
48 const res = await makeGetRequest({
49 url,
4c6d99e5 50 path: imageHTTPPath,
c0e8b12e 51 expectedStatus: HttpStatusCode.OK_200
6c5065a0
C
52 })
53
54 const body = res.body
55
56 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
c47c3bcb
C
57 const minLength = data.length - ((40 * data.length) / 100)
58 const maxLength = data.length + ((40 * data.length) / 100)
6c5065a0 59
4c6d99e5
C
60 expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
61 expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
6c5065a0
C
62}
63
d41f4a6d
C
64async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
65 const res = await makeGetRequest({
66 url,
67 path: imageHTTPPath,
68 expectedStatus: HttpStatusCode.OK_200
69 })
70
71 const body = res.body
72 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
73
74 const img1 = imageHTTPPath.endsWith('.png')
75 ? PNG.sync.read(body)
76 : JPEG.decode(body)
77
78 const img2 = extension === '.png'
79 ? PNG.sync.read(data)
80 : JPEG.decode(data)
81
82 const result = pixelmatch(img1.data, img2.data, null, img1.width, img1.height, { threshold: 0.1 })
83
84 expect(result).to.equal(0, `${imageHTTPPath} image is not the same as ${imageName}${extension}`)
85}
86
254d3579 87async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
89d241a7 88 const base = server.servers.buildDirectory(directory)
6c5065a0
C
89
90 expect(await pathExists(join(base, filePath))).to.equal(exist)
91}
92
d102de1b
C
93// ---------------------------------------------------------------------------
94
c55e3d72
C
95function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
96 return makeGetRequest({
97 url,
98 path,
99 token,
100 query: { ...query, start: 'hello' },
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400
102 })
103}
104
105async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
106 await makeGetRequest({
107 url,
108 path,
109 token,
110 query: { ...query, count: 'hello' },
111 expectedStatus: HttpStatusCode.BAD_REQUEST_400
112 })
113
114 await makeGetRequest({
115 url,
116 path,
117 token,
118 query: { ...query, count: 2000 },
119 expectedStatus: HttpStatusCode.BAD_REQUEST_400
120 })
121}
122
123function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
124 return makeGetRequest({
125 url,
126 path,
127 token,
128 query: { ...query, sort: 'hello' },
129 expectedStatus: HttpStatusCode.BAD_REQUEST_400
130 })
131}
132
6c5065a0
C
133export {
134 dateIsValid,
d41f4a6d 135 testImageSize,
6c5065a0 136 testImage,
1f6125be 137 expectLogDoesNotContain,
0305db28 138 testFileExistsOrNot,
c55e3d72 139 expectStartWith,
ca3d5912 140 expectNotStartWith,
71e3e879 141 expectEndWith,
c55e3d72
C
142 checkBadStartPagination,
143 checkBadCountPagination,
630d0a1b
C
144 checkBadSortPagination,
145 expectLogContain
6c5065a0 146}