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