]>
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 | ||
ca3d5912 C |
22 | function expectNotStartWith (str: string, start: string) { |
23 | expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false | |
24 | } | |
25 | ||
1f6125be C |
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 | ||
4c6d99e5 | 32 | async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { |
6c5065a0 C |
33 | const res = await makeGetRequest({ |
34 | url, | |
4c6d99e5 | 35 | path: imageHTTPPath, |
c0e8b12e | 36 | expectedStatus: HttpStatusCode.OK_200 |
6c5065a0 C |
37 | }) |
38 | ||
39 | const body = res.body | |
40 | ||
41 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) | |
c47c3bcb C |
42 | const minLength = data.length - ((40 * data.length) / 100) |
43 | const maxLength = data.length + ((40 * data.length) / 100) | |
6c5065a0 | 44 | |
4c6d99e5 C |
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') | |
6c5065a0 C |
47 | } |
48 | ||
254d3579 | 49 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { |
89d241a7 | 50 | const base = server.servers.buildDirectory(directory) |
6c5065a0 C |
51 | |
52 | expect(await pathExists(join(base, filePath))).to.equal(exist) | |
53 | } | |
54 | ||
c55e3d72 C |
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 | ||
6c5065a0 C |
93 | export { |
94 | dateIsValid, | |
95 | testImage, | |
1f6125be | 96 | expectLogDoesNotContain, |
0305db28 | 97 | testFileExistsOrNot, |
c55e3d72 | 98 | expectStartWith, |
ca3d5912 | 99 | expectNotStartWith, |
c55e3d72 C |
100 | checkBadStartPagination, |
101 | checkBadCountPagination, | |
102 | checkBadSortPagination | |
6c5065a0 | 103 | } |