]>
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 | ||
71e3e879 C |
26 | function expectEndWith (str: string, end: string) { |
27 | expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true | |
28 | } | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
1f6125be C |
32 | async function expectLogDoesNotContain (server: PeerTubeServer, str: string) { |
33 | const content = await server.servers.getLogContent() | |
34 | ||
35 | expect(content.toString()).to.not.contain(str) | |
36 | } | |
37 | ||
630d0a1b C |
38 | async function expectLogContain (server: PeerTubeServer, str: string) { |
39 | const content = await server.servers.getLogContent() | |
40 | ||
41 | expect(content.toString()).to.contain(str) | |
42 | } | |
43 | ||
4c6d99e5 | 44 | async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { |
6c5065a0 C |
45 | const res = await makeGetRequest({ |
46 | url, | |
4c6d99e5 | 47 | path: imageHTTPPath, |
c0e8b12e | 48 | expectedStatus: HttpStatusCode.OK_200 |
6c5065a0 C |
49 | }) |
50 | ||
51 | const body = res.body | |
52 | ||
53 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) | |
c47c3bcb C |
54 | const minLength = data.length - ((40 * data.length) / 100) |
55 | const maxLength = data.length + ((40 * data.length) / 100) | |
6c5065a0 | 56 | |
4c6d99e5 C |
57 | expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture') |
58 | expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture') | |
6c5065a0 C |
59 | } |
60 | ||
254d3579 | 61 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { |
89d241a7 | 62 | const base = server.servers.buildDirectory(directory) |
6c5065a0 C |
63 | |
64 | expect(await pathExists(join(base, filePath))).to.equal(exist) | |
65 | } | |
66 | ||
c55e3d72 C |
67 | function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { |
68 | return makeGetRequest({ | |
69 | url, | |
70 | path, | |
71 | token, | |
72 | query: { ...query, start: 'hello' }, | |
73 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
74 | }) | |
75 | } | |
76 | ||
77 | async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) { | |
78 | await makeGetRequest({ | |
79 | url, | |
80 | path, | |
81 | token, | |
82 | query: { ...query, count: 'hello' }, | |
83 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
84 | }) | |
85 | ||
86 | await makeGetRequest({ | |
87 | url, | |
88 | path, | |
89 | token, | |
90 | query: { ...query, count: 2000 }, | |
91 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
92 | }) | |
93 | } | |
94 | ||
95 | function checkBadSortPagination (url: string, path: string, token?: string, query = {}) { | |
96 | return makeGetRequest({ | |
97 | url, | |
98 | path, | |
99 | token, | |
100 | query: { ...query, sort: 'hello' }, | |
101 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
102 | }) | |
103 | } | |
104 | ||
6c5065a0 C |
105 | export { |
106 | dateIsValid, | |
107 | testImage, | |
1f6125be | 108 | expectLogDoesNotContain, |
0305db28 | 109 | testFileExistsOrNot, |
c55e3d72 | 110 | expectStartWith, |
ca3d5912 | 111 | expectNotStartWith, |
71e3e879 | 112 | expectEndWith, |
c55e3d72 C |
113 | checkBadStartPagination, |
114 | checkBadCountPagination, | |
630d0a1b C |
115 | checkBadSortPagination, |
116 | expectLogContain | |
6c5065a0 | 117 | } |