]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/search.ts
Update search index tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / search.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
d525fc39
C
2
3import 'mocha'
3b0bd70a 4import {
65e6e260
C
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
3b0bd70a 8 cleanupTests,
254d3579 9 createSingleServer,
3b0bd70a 10 makeGetRequest,
254d3579 11 PeerTubeServer,
3b0bd70a 12 setAccessTokensToServers
65e6e260 13} from '@shared/extra-utils'
4c7e60bc 14import { HttpStatusCode } from '@shared/models'
d525fc39 15
254d3579 16function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
89d241a7 17 return server.config.updateCustomSubConfig({
65e6e260
C
18 newConfig: {
19 search: {
20 searchIndex: {
21 enabled,
22 disableLocalSearch
23 }
3b0bd70a
C
24 }
25 }
26 })
27}
28
d525fc39 29describe('Test videos API validator', function () {
254d3579 30 let server: PeerTubeServer
d525fc39
C
31
32 // ---------------------------------------------------------------
33
34 before(async function () {
35 this.timeout(30000)
36
254d3579 37 server = await createSingleServer(1)
3b0bd70a 38 await setAccessTokensToServers([ server ])
d525fc39
C
39 })
40
41 describe('When searching videos', function () {
f5b0af50
C
42 const path = '/api/v1/search/videos/'
43
d525fc39
C
44 const query = {
45 search: 'coucou'
46 }
47
48 it('Should fail with a bad start pagination', async function () {
49 await checkBadStartPagination(server.url, path, null, query)
50 })
51
52 it('Should fail with a bad count pagination', async function () {
53 await checkBadCountPagination(server.url, path, null, query)
54 })
55
56 it('Should fail with an incorrect sort', async function () {
57 await checkBadSortPagination(server.url, path, null, query)
58 })
59
164c8d46 60 it('Should succeed with the correct parameters', async function () {
c0e8b12e 61 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
d525fc39
C
62 })
63
64 it('Should fail with an invalid category', async function () {
6c5065a0 65 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
c0e8b12e 66 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39 67
6c5065a0 68 const customQuery2 = { ...query, categoryOneOf: 'a' }
c0e8b12e 69 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39
C
70 })
71
72 it('Should succeed with a valid category', async function () {
6c5065a0 73 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
c0e8b12e 74 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 75
6c5065a0 76 const customQuery2 = { ...query, categoryOneOf: 1 }
c0e8b12e 77 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
d525fc39
C
78 })
79
80 it('Should fail with an invalid licence', async function () {
6c5065a0 81 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
c0e8b12e 82 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39 83
6c5065a0 84 const customQuery2 = { ...query, licenceOneOf: 'a' }
c0e8b12e 85 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39
C
86 })
87
88 it('Should succeed with a valid licence', async function () {
6c5065a0 89 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
c0e8b12e 90 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 91
6c5065a0 92 const customQuery2 = { ...query, licenceOneOf: 1 }
c0e8b12e 93 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
d525fc39
C
94 })
95
96 it('Should succeed with a valid language', async function () {
6c5065a0 97 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
c0e8b12e 98 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 99
6c5065a0 100 const customQuery2 = { ...query, languageOneOf: 'fr' }
c0e8b12e 101 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
d525fc39
C
102 })
103
104 it('Should succeed with valid tags', async function () {
6c5065a0 105 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
c0e8b12e 106 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 107
6c5065a0 108 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
c0e8b12e 109 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 110
6c5065a0 111 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
c0e8b12e 112 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
d525fc39 113
6c5065a0 114 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
c0e8b12e 115 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
d525fc39
C
116 })
117
118 it('Should fail with invalid durations', async function () {
6c5065a0 119 const customQuery1 = { ...query, durationMin: 'hello' }
c0e8b12e 120 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39 121
6c5065a0 122 const customQuery2 = { ...query, durationMax: 'hello' }
c0e8b12e 123 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39
C
124 })
125
126 it('Should fail with invalid dates', async function () {
6c5065a0 127 const customQuery1 = { ...query, startDate: 'hello' }
c0e8b12e 128 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39 129
6c5065a0 130 const customQuery2 = { ...query, endDate: 'hello' }
c0e8b12e 131 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
31d065cc 132
6c5065a0 133 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
c0e8b12e 134 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
31d065cc 135
6c5065a0 136 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
c0e8b12e 137 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
d525fc39 138 })
164c8d46
C
139
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 })
143 })
144
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 })
148 })
d525fc39
C
149 })
150
37a44fc9
C
151 describe('When searching video playlists', function () {
152 const path = '/api/v1/search/video-playlists/'
153
154 const query = {
164c8d46
C
155 search: 'coucou',
156 host: 'example.com'
37a44fc9
C
157 }
158
159 it('Should fail with a bad start pagination', async function () {
160 await checkBadStartPagination(server.url, path, null, query)
161 })
162
163 it('Should fail with a bad count pagination', async function () {
164 await checkBadCountPagination(server.url, path, null, query)
165 })
166
167 it('Should fail with an incorrect sort', async function () {
168 await checkBadSortPagination(server.url, path, null, query)
169 })
170
164c8d46
C
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 })
173 })
174
175 it('Should succeed with the correct parameters', async function () {
c0e8b12e 176 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
37a44fc9
C
177 })
178 })
179
f5b0af50
C
180 describe('When searching video channels', function () {
181 const path = '/api/v1/search/video-channels/'
182
183 const query = {
164c8d46
C
184 search: 'coucou',
185 host: 'example.com'
f5b0af50
C
186 }
187
188 it('Should fail with a bad start pagination', async function () {
189 await checkBadStartPagination(server.url, path, null, query)
190 })
191
192 it('Should fail with a bad count pagination', async function () {
193 await checkBadCountPagination(server.url, path, null, query)
194 })
195
196 it('Should fail with an incorrect sort', async function () {
197 await checkBadSortPagination(server.url, path, null, query)
198 })
199
164c8d46
C
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 })
202 })
203
204 it('Should succeed with the correct parameters', async function () {
c0e8b12e 205 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
f5b0af50
C
206 })
207 })
208
3b0bd70a
C
209 describe('Search target', function () {
210
211 it('Should fail/succeed depending on the search target', async function () {
212 this.timeout(10000)
213
214 const query = { search: 'coucou' }
215 const paths = [
37a44fc9 216 '/api/v1/search/video-playlists/',
3b0bd70a
C
217 '/api/v1/search/video-channels/',
218 '/api/v1/search/videos/'
219 ]
220
221 for (const path of paths) {
222 {
6c5065a0 223 const customQuery = { ...query, searchTarget: 'hello' }
c0e8b12e 224 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
3b0bd70a
C
225 }
226
227 {
6c5065a0 228 const customQuery = { ...query, searchTarget: undefined }
c0e8b12e 229 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
3b0bd70a
C
230 }
231
232 {
6c5065a0 233 const customQuery = { ...query, searchTarget: 'local' }
c0e8b12e 234 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
3b0bd70a
C
235 }
236
237 {
6c5065a0 238 const customQuery = { ...query, searchTarget: 'search-index' }
c0e8b12e 239 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
3b0bd70a
C
240 }
241
242 await updateSearchIndex(server, true, true)
243
244 {
6c5065a0 245 const customQuery = { ...query, searchTarget: 'local' }
c0e8b12e 246 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
3b0bd70a
C
247 }
248
249 {
6c5065a0 250 const customQuery = { ...query, searchTarget: 'search-index' }
c0e8b12e 251 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
3b0bd70a
C
252 }
253
254 await updateSearchIndex(server, true, false)
255
256 {
6c5065a0 257 const customQuery = { ...query, searchTarget: 'local' }
c0e8b12e 258 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
3b0bd70a
C
259 }
260
261 await updateSearchIndex(server, false, false)
262 }
263 })
264 })
265
7c3b7976
C
266 after(async function () {
267 await cleanupTests([ server ])
d525fc39
C
268 })
269})