]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/shared/checks.ts
Upgrade sequelize dependency
[github/Chocobozzz/PeerTube.git] / server / tests / shared / checks.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
3 import { expect } from 'chai'
4 import { pathExists, readFile } from 'fs-extra'
5 import JPEG from 'jpeg-js'
6 import { join } from 'path'
7 import pixelmatch from 'pixelmatch'
8 import { PNG } from 'pngjs'
9 import { root } from '@shared/core-utils'
10 import { HttpStatusCode } from '@shared/models'
11 import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
12
13 // Default interval -> 5 minutes
14 function dateIsValid (dateString: string, interval = 300000) {
15 const dateToCheck = new Date(dateString)
16 const now = new Date()
17
18 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
19 }
20
21 function expectStartWith (str: string, start: string) {
22 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
23 }
24
25 function expectNotStartWith (str: string, start: string) {
26 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
27 }
28
29 function expectEndWith (str: string, end: string) {
30 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
31 }
32
33 // ---------------------------------------------------------------------------
34
35 async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
36 const content = await server.servers.getLogContent()
37
38 expect(content.toString()).to.not.contain(str)
39 }
40
41 async function expectLogContain (server: PeerTubeServer, str: string) {
42 const content = await server.servers.getLogContent()
43
44 expect(content.toString()).to.contain(str)
45 }
46
47 async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
48 const res = await makeGetRequest({
49 url,
50 path: imageHTTPPath,
51 expectedStatus: HttpStatusCode.OK_200
52 })
53
54 const body = res.body
55
56 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
57 const minLength = data.length - ((40 * data.length) / 100)
58 const maxLength = data.length + ((40 * data.length) / 100)
59
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')
62 }
63
64 async 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
87 async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
88 const base = server.servers.buildDirectory(directory)
89
90 expect(await pathExists(join(base, filePath))).to.equal(exist)
91 }
92
93 function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
94 return makeGetRequest({
95 url,
96 path,
97 token,
98 query: { ...query, start: 'hello' },
99 expectedStatus: HttpStatusCode.BAD_REQUEST_400
100 })
101 }
102
103 async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
104 await makeGetRequest({
105 url,
106 path,
107 token,
108 query: { ...query, count: 'hello' },
109 expectedStatus: HttpStatusCode.BAD_REQUEST_400
110 })
111
112 await makeGetRequest({
113 url,
114 path,
115 token,
116 query: { ...query, count: 2000 },
117 expectedStatus: HttpStatusCode.BAD_REQUEST_400
118 })
119 }
120
121 function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
122 return makeGetRequest({
123 url,
124 path,
125 token,
126 query: { ...query, sort: 'hello' },
127 expectedStatus: HttpStatusCode.BAD_REQUEST_400
128 })
129 }
130
131 export {
132 dateIsValid,
133 testImageSize,
134 testImage,
135 expectLogDoesNotContain,
136 testFileExistsOrNot,
137 expectStartWith,
138 expectNotStartWith,
139 expectEndWith,
140 checkBadStartPagination,
141 checkBadCountPagination,
142 checkBadSortPagination,
143 expectLogContain
144 }