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