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