1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
6 import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
9 return server.config.updateCustomSubConfig({
21 describe('Test videos API validator', function () {
22 let server: PeerTubeServer
24 // ---------------------------------------------------------------
26 before(async function () {
29 server = await createSingleServer(1)
30 await setAccessTokensToServers([ server ])
33 describe('When searching videos', function () {
34 const path = '/api/v1/search/videos/'
40 it('Should fail with a bad start pagination', async function () {
41 await checkBadStartPagination(server.url, path, null, query)
44 it('Should fail with a bad count pagination', async function () {
45 await checkBadCountPagination(server.url, path, null, query)
48 it('Should fail with an incorrect sort', async function () {
49 await checkBadSortPagination(server.url, path, null, query)
52 it('Should succeed with the correct parameters', async function () {
53 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
56 it('Should fail with an invalid category', async function () {
57 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
58 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
60 const customQuery2 = { ...query, categoryOneOf: 'a' }
61 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
64 it('Should succeed with a valid category', async function () {
65 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
66 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
68 const customQuery2 = { ...query, categoryOneOf: 1 }
69 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
72 it('Should fail with an invalid licence', async function () {
73 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
74 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
76 const customQuery2 = { ...query, licenceOneOf: 'a' }
77 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
80 it('Should succeed with a valid licence', async function () {
81 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
82 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
84 const customQuery2 = { ...query, licenceOneOf: 1 }
85 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
88 it('Should succeed with a valid language', async function () {
89 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
90 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
92 const customQuery2 = { ...query, languageOneOf: 'fr' }
93 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
96 it('Should succeed with valid tags', async function () {
97 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
98 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
100 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
101 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
103 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
104 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
106 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
107 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
110 it('Should fail with invalid durations', async function () {
111 const customQuery1 = { ...query, durationMin: 'hello' }
112 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
114 const customQuery2 = { ...query, durationMax: 'hello' }
115 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
118 it('Should fail with invalid dates', async function () {
119 const customQuery1 = { ...query, startDate: 'hello' }
120 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
122 const customQuery2 = { ...query, endDate: 'hello' }
123 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
125 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
126 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
128 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
129 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
132 it('Should fail with an invalid host', async function () {
133 const customQuery = { ...query, host: '6565' }
134 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
137 it('Should succeed with a host', async function () {
138 const customQuery = { ...query, host: 'example.com' }
139 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
142 it('Should fail with invalid uuids', async function () {
143 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
144 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
147 it('Should succeed with valid uuids', async function () {
148 const customQuery = { ...query, uuids: [ 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
149 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
153 describe('When searching video playlists', function () {
154 const path = '/api/v1/search/video-playlists/'
161 it('Should fail with a bad start pagination', async function () {
162 await checkBadStartPagination(server.url, path, null, query)
165 it('Should fail with a bad count pagination', async function () {
166 await checkBadCountPagination(server.url, path, null, query)
169 it('Should fail with an incorrect sort', async function () {
170 await checkBadSortPagination(server.url, path, null, query)
173 it('Should fail with an invalid host', async function () {
174 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
177 it('Should fail with invalid uuids', async function () {
178 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
179 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
182 it('Should succeed with the correct parameters', async function () {
183 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
187 describe('When searching video channels', function () {
188 const path = '/api/v1/search/video-channels/'
195 it('Should fail with a bad start pagination', async function () {
196 await checkBadStartPagination(server.url, path, null, query)
199 it('Should fail with a bad count pagination', async function () {
200 await checkBadCountPagination(server.url, path, null, query)
203 it('Should fail with an incorrect sort', async function () {
204 await checkBadSortPagination(server.url, path, null, query)
207 it('Should fail with an invalid host', async function () {
208 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
211 it('Should fail with invalid handles', async function () {
212 await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
215 it('Should succeed with the correct parameters', async function () {
216 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
220 describe('Search target', function () {
222 it('Should fail/succeed depending on the search target', async function () {
225 const query = { search: 'coucou' }
227 '/api/v1/search/video-playlists/',
228 '/api/v1/search/video-channels/',
229 '/api/v1/search/videos/'
232 for (const path of paths) {
234 const customQuery = { ...query, searchTarget: 'hello' }
235 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
239 const customQuery = { ...query, searchTarget: undefined }
240 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
244 const customQuery = { ...query, searchTarget: 'local' }
245 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
249 const customQuery = { ...query, searchTarget: 'search-index' }
250 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
253 await updateSearchIndex(server, true, true)
256 const customQuery = { ...query, searchTarget: 'local' }
257 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
261 const customQuery = { ...query, searchTarget: 'search-index' }
262 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
265 await updateSearchIndex(server, true, false)
268 const customQuery = { ...query, searchTarget: 'local' }
269 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
272 await updateSearchIndex(server, false, false)
277 after(async function () {
278 await cleanupTests([ server ])