1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
4 import { HttpStatusCode } from '@shared/models'
5 import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
7 function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
8 return server.config.updateCustomSubConfig({
20 describe('Test videos API validator', function () {
21 let server: PeerTubeServer
23 // ---------------------------------------------------------------
25 before(async function () {
28 server = await createSingleServer(1)
29 await setAccessTokensToServers([ server ])
32 describe('When searching videos', function () {
33 const path = '/api/v1/search/videos/'
39 it('Should fail with a bad start pagination', async function () {
40 await checkBadStartPagination(server.url, path, null, query)
43 it('Should fail with a bad count pagination', async function () {
44 await checkBadCountPagination(server.url, path, null, query)
47 it('Should fail with an incorrect sort', async function () {
48 await checkBadSortPagination(server.url, path, null, query)
51 it('Should succeed with the correct parameters', async function () {
52 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
55 it('Should fail with an invalid category', async function () {
56 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
57 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
59 const customQuery2 = { ...query, categoryOneOf: 'a' }
60 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
63 it('Should succeed with a valid category', async function () {
64 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
65 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
67 const customQuery2 = { ...query, categoryOneOf: 1 }
68 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
71 it('Should fail with an invalid licence', async function () {
72 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
73 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
75 const customQuery2 = { ...query, licenceOneOf: 'a' }
76 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
79 it('Should succeed with a valid licence', async function () {
80 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
81 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
83 const customQuery2 = { ...query, licenceOneOf: 1 }
84 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
87 it('Should succeed with a valid language', async function () {
88 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
89 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
91 const customQuery2 = { ...query, languageOneOf: 'fr' }
92 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
95 it('Should succeed with valid tags', async function () {
96 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
97 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
99 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
100 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
102 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
103 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
105 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
106 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
109 it('Should fail with invalid durations', async function () {
110 const customQuery1 = { ...query, durationMin: 'hello' }
111 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
113 const customQuery2 = { ...query, durationMax: 'hello' }
114 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
117 it('Should fail with invalid dates', async function () {
118 const customQuery1 = { ...query, startDate: 'hello' }
119 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
121 const customQuery2 = { ...query, endDate: 'hello' }
122 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
124 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
125 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
127 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
128 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
131 it('Should fail with an invalid host', async function () {
132 const customQuery = { ...query, host: '6565' }
133 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
136 it('Should succeed with a host', async function () {
137 const customQuery = { ...query, host: 'example.com' }
138 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
141 it('Should fail with invalid uuids', async function () {
142 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
143 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
146 it('Should succeed with valid uuids', async function () {
147 const customQuery = { ...query, uuids: [ 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
148 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
152 describe('When searching video playlists', function () {
153 const path = '/api/v1/search/video-playlists/'
160 it('Should fail with a bad start pagination', async function () {
161 await checkBadStartPagination(server.url, path, null, query)
164 it('Should fail with a bad count pagination', async function () {
165 await checkBadCountPagination(server.url, path, null, query)
168 it('Should fail with an incorrect sort', async function () {
169 await checkBadSortPagination(server.url, path, null, query)
172 it('Should fail with an invalid host', async function () {
173 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
176 it('Should fail with invalid uuids', async function () {
177 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
178 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
181 it('Should succeed with the correct parameters', async function () {
182 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
186 describe('When searching video channels', function () {
187 const path = '/api/v1/search/video-channels/'
194 it('Should fail with a bad start pagination', async function () {
195 await checkBadStartPagination(server.url, path, null, query)
198 it('Should fail with a bad count pagination', async function () {
199 await checkBadCountPagination(server.url, path, null, query)
202 it('Should fail with an incorrect sort', async function () {
203 await checkBadSortPagination(server.url, path, null, query)
206 it('Should fail with an invalid host', async function () {
207 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
210 it('Should fail with invalid handles', async function () {
211 await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
214 it('Should succeed with the correct parameters', async function () {
215 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
219 describe('Search target', function () {
221 it('Should fail/succeed depending on the search target', async function () {
224 const query = { search: 'coucou' }
226 '/api/v1/search/video-playlists/',
227 '/api/v1/search/video-channels/',
228 '/api/v1/search/videos/'
231 for (const path of paths) {
233 const customQuery = { ...query, searchTarget: 'hello' }
234 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
238 const customQuery = { ...query, searchTarget: undefined }
239 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
243 const customQuery = { ...query, searchTarget: 'local' }
244 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
248 const customQuery = { ...query, searchTarget: 'search-index' }
249 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
252 await updateSearchIndex(server, true, true)
255 const customQuery = { ...query, searchTarget: 'local' }
256 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
260 const customQuery = { ...query, searchTarget: 'search-index' }
261 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
264 await updateSearchIndex(server, true, false)
267 const customQuery = { ...query, searchTarget: 'local' }
268 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
271 await updateSearchIndex(server, false, false)
276 after(async function () {
277 await cleanupTests([ server ])