1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
12 setAccessTokensToServers
13 } from '@shared/extra-utils'
14 import { HttpStatusCode } from '@shared/models'
16 function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
17 return server.config.updateCustomSubConfig({
29 describe('Test videos API validator', function () {
30 let server: PeerTubeServer
32 // ---------------------------------------------------------------
34 before(async function () {
37 server = await createSingleServer(1)
38 await setAccessTokensToServers([ server ])
41 describe('When searching videos', function () {
42 const path = '/api/v1/search/videos/'
48 it('Should fail with a bad start pagination', async function () {
49 await checkBadStartPagination(server.url, path, null, query)
52 it('Should fail with a bad count pagination', async function () {
53 await checkBadCountPagination(server.url, path, null, query)
56 it('Should fail with an incorrect sort', async function () {
57 await checkBadSortPagination(server.url, path, null, query)
60 it('Should succeed with the correct parameters', async function () {
61 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
64 it('Should fail with an invalid category', async function () {
65 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
66 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
68 const customQuery2 = { ...query, categoryOneOf: 'a' }
69 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
72 it('Should succeed with a valid category', async function () {
73 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
74 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
76 const customQuery2 = { ...query, categoryOneOf: 1 }
77 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
80 it('Should fail with an invalid licence', async function () {
81 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
82 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
84 const customQuery2 = { ...query, licenceOneOf: 'a' }
85 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
88 it('Should succeed with a valid licence', async function () {
89 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
90 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
92 const customQuery2 = { ...query, licenceOneOf: 1 }
93 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
96 it('Should succeed with a valid language', async function () {
97 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
98 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
100 const customQuery2 = { ...query, languageOneOf: 'fr' }
101 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
104 it('Should succeed with valid tags', async function () {
105 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
106 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
108 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
109 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
111 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
112 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
114 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
115 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
118 it('Should fail with invalid durations', async function () {
119 const customQuery1 = { ...query, durationMin: 'hello' }
120 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
122 const customQuery2 = { ...query, durationMax: 'hello' }
123 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
126 it('Should fail with invalid dates', async function () {
127 const customQuery1 = { ...query, startDate: 'hello' }
128 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
130 const customQuery2 = { ...query, endDate: 'hello' }
131 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
133 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
134 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
136 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
137 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
140 it('Should fail with an invalid host', async function () {
141 const customQuery = { ...query, host: '6565' }
142 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
145 it('Should succeed with a host', async function () {
146 const customQuery = { ...query, host: 'example.com' }
147 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
151 describe('When searching video playlists', function () {
152 const path = '/api/v1/search/video-playlists/'
159 it('Should fail with a bad start pagination', async function () {
160 await checkBadStartPagination(server.url, path, null, query)
163 it('Should fail with a bad count pagination', async function () {
164 await checkBadCountPagination(server.url, path, null, query)
167 it('Should fail with an incorrect sort', async function () {
168 await checkBadSortPagination(server.url, path, null, query)
171 it('Should fail with an invalid host', async function () {
172 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
175 it('Should succeed with the correct parameters', async function () {
176 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
180 describe('When searching video channels', function () {
181 const path = '/api/v1/search/video-channels/'
188 it('Should fail with a bad start pagination', async function () {
189 await checkBadStartPagination(server.url, path, null, query)
192 it('Should fail with a bad count pagination', async function () {
193 await checkBadCountPagination(server.url, path, null, query)
196 it('Should fail with an incorrect sort', async function () {
197 await checkBadSortPagination(server.url, path, null, query)
200 it('Should fail with an invalid host', async function () {
201 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
204 it('Should succeed with the correct parameters', async function () {
205 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
209 describe('Search target', function () {
211 it('Should fail/succeed depending on the search target', async function () {
214 const query = { search: 'coucou' }
216 '/api/v1/search/video-playlists/',
217 '/api/v1/search/video-channels/',
218 '/api/v1/search/videos/'
221 for (const path of paths) {
223 const customQuery = { ...query, searchTarget: 'hello' }
224 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
228 const customQuery = { ...query, searchTarget: undefined }
229 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
233 const customQuery = { ...query, searchTarget: 'local' }
234 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
238 const customQuery = { ...query, searchTarget: 'search-index' }
239 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
242 await updateSearchIndex(server, true, true)
245 const customQuery = { ...query, searchTarget: 'local' }
246 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
250 const customQuery = { ...query, searchTarget: 'search-index' }
251 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
254 await updateSearchIndex(server, true, false)
257 const customQuery = { ...query, searchTarget: 'local' }
258 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
261 await updateSearchIndex(server, false, false)
266 after(async function () {
267 await cleanupTests([ server ])