]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-index.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-index.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import {
5 BooleanBothQuery,
6 VideoChannelsSearchQuery,
7 VideoPlaylistPrivacy,
8 VideoPlaylistsSearchQuery,
9 VideoPlaylistType,
10 VideosSearchQuery
11 } from '@shared/models'
12 import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/server-commands'
13
14 const expect = chai.expect
15
16 describe('Test index search', function () {
17 const localVideoName = 'local video' + new Date().toISOString()
18
19 let server: PeerTubeServer = null
20 let command: SearchCommand
21
22 before(async function () {
23 this.timeout(30000)
24
25 server = await createSingleServer(1)
26
27 await setAccessTokensToServers([ server ])
28
29 await server.videos.upload({ attributes: { name: localVideoName } })
30
31 command = server.search
32 })
33
34 describe('Default search', async function () {
35
36 it('Should make a local videos search by default', async function () {
37 this.timeout(10000)
38
39 await server.config.updateCustomSubConfig({
40 newConfig: {
41 search: {
42 searchIndex: {
43 enabled: true,
44 isDefaultSearch: false,
45 disableLocalSearch: false
46 }
47 }
48 }
49 })
50
51 const body = await command.searchVideos({ search: 'local video' })
52
53 expect(body.total).to.equal(1)
54 expect(body.data[0].name).to.equal(localVideoName)
55 })
56
57 it('Should make a local channels search by default', async function () {
58 const body = await command.searchChannels({ search: 'root' })
59
60 expect(body.total).to.equal(1)
61 expect(body.data[0].name).to.equal('root_channel')
62 expect(body.data[0].host).to.equal('localhost:' + server.port)
63 })
64
65 it('Should make an index videos search by default', async function () {
66 await server.config.updateCustomSubConfig({
67 newConfig: {
68 search: {
69 searchIndex: {
70 enabled: true,
71 isDefaultSearch: true,
72 disableLocalSearch: false
73 }
74 }
75 }
76 })
77
78 const body = await command.searchVideos({ search: 'local video' })
79 expect(body.total).to.be.greaterThan(2)
80 })
81
82 it('Should make an index channels search by default', async function () {
83 const body = await command.searchChannels({ search: 'root' })
84 expect(body.total).to.be.greaterThan(2)
85 })
86
87 it('Should make an index videos search if local search is disabled', async function () {
88 await server.config.updateCustomSubConfig({
89 newConfig: {
90 search: {
91 searchIndex: {
92 enabled: true,
93 isDefaultSearch: false,
94 disableLocalSearch: true
95 }
96 }
97 }
98 })
99
100 const body = await command.searchVideos({ search: 'local video' })
101 expect(body.total).to.be.greaterThan(2)
102 })
103
104 it('Should make an index channels search if local search is disabled', async function () {
105 const body = await command.searchChannels({ search: 'root' })
106 expect(body.total).to.be.greaterThan(2)
107 })
108 })
109
110 describe('Videos search', async function () {
111
112 async function check (search: VideosSearchQuery, exists = true) {
113 const body = await command.advancedVideoSearch({ search })
114
115 if (exists === false) {
116 expect(body.total).to.equal(0)
117 expect(body.data).to.have.lengthOf(0)
118 return
119 }
120
121 expect(body.total).to.equal(1)
122 expect(body.data).to.have.lengthOf(1)
123
124 const video = body.data[0]
125
126 expect(video.name).to.equal('What is PeerTube?')
127 expect(video.category.label).to.equal('Science & Technology')
128 expect(video.licence.label).to.equal('Attribution - Share Alike')
129 expect(video.privacy.label).to.equal('Public')
130 expect(video.duration).to.equal(113)
131 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
132
133 expect(video.account.host).to.equal('framatube.org')
134 expect(video.account.name).to.equal('framasoft')
135 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
136 // TODO: remove, deprecated in 4.2
137 expect(video.account.avatar).to.exist
138 expect(video.account.avatars.length).to.equal(2, 'Account should have one avatar image')
139
140 expect(video.channel.host).to.equal('framatube.org')
141 expect(video.channel.name).to.equal('joinpeertube')
142 expect(video.channel.url).to.equal('https://framatube.org/video-channels/joinpeertube')
143 // TODO: remove, deprecated in 4.2
144 expect(video.channel.avatar).to.exist
145 expect(video.channel.avatars.length).to.equal(2, 'Channel should have one avatar image')
146 }
147
148 const baseSearch: VideosSearchQuery = {
149 search: 'what is peertube',
150 start: 0,
151 count: 2,
152 categoryOneOf: [ 15 ],
153 licenceOneOf: [ 2 ],
154 tagsAllOf: [ 'framasoft', 'peertube' ],
155 startDate: '2018-10-01T10:50:46.396Z',
156 endDate: '2018-10-01T10:55:46.396Z'
157 }
158
159 it('Should make a simple search and not have results', async function () {
160 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
161
162 expect(body.total).to.equal(0)
163 expect(body.data).to.have.lengthOf(0)
164 })
165
166 it('Should make a simple search and have results', async function () {
167 const body = await command.searchVideos({ search: 'What is PeerTube' })
168
169 expect(body.total).to.be.greaterThan(1)
170 })
171
172 it('Should make a simple search', async function () {
173 await check(baseSearch)
174 })
175
176 it('Should search by start date', async function () {
177 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
178 await check(search, false)
179 })
180
181 it('Should search by tags', async function () {
182 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
183 await check(search, false)
184 })
185
186 it('Should search by duration', async function () {
187 const search = { ...baseSearch, durationMin: 2000 }
188 await check(search, false)
189 })
190
191 it('Should search by nsfw attribute', async function () {
192 {
193 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
194 await check(search, false)
195 }
196
197 {
198 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
199 await check(search, true)
200 }
201
202 {
203 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
204 await check(search, true)
205 }
206 })
207
208 it('Should search by host', async function () {
209 {
210 const search = { ...baseSearch, host: 'example.com' }
211 await check(search, false)
212 }
213
214 {
215 const search = { ...baseSearch, host: 'framatube.org' }
216 await check(search, true)
217 }
218 })
219
220 it('Should search by uuids', async function () {
221 const goodUUID = '9c9de5e8-0a1e-484a-b099-e80766180a6d'
222 const goodShortUUID = 'kkGMgK9ZtnKfYAgnEtQxbv'
223 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
224 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
225
226 {
227 const uuidsMatrix = [
228 [ goodUUID ],
229 [ goodUUID, badShortUUID ],
230 [ badShortUUID, goodShortUUID ],
231 [ goodUUID, goodShortUUID ]
232 ]
233
234 for (const uuids of uuidsMatrix) {
235 const search = { ...baseSearch, uuids }
236 await check(search, true)
237 }
238 }
239
240 {
241 const uuidsMatrix = [
242 [ badUUID ],
243 [ badShortUUID ]
244 ]
245
246 for (const uuids of uuidsMatrix) {
247 const search = { ...baseSearch, uuids }
248 await check(search, false)
249 }
250 }
251 })
252
253 it('Should have a correct pagination', async function () {
254 const search = {
255 search: 'video',
256 start: 0,
257 count: 5
258 }
259
260 const body = await command.advancedVideoSearch({ search })
261
262 expect(body.total).to.be.greaterThan(5)
263 expect(body.data).to.have.lengthOf(5)
264 })
265
266 it('Should use the nsfw instance policy as default', async function () {
267 let nsfwUUID: string
268
269 {
270 await server.config.updateCustomSubConfig({
271 newConfig: {
272 instance: { defaultNSFWPolicy: 'display' }
273 }
274 })
275
276 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
277 expect(body.data).to.have.length.greaterThan(0)
278
279 const video = body.data[0]
280 expect(video.nsfw).to.be.true
281
282 nsfwUUID = video.uuid
283 }
284
285 {
286 await server.config.updateCustomSubConfig({
287 newConfig: {
288 instance: { defaultNSFWPolicy: 'do_not_list' }
289 }
290 })
291
292 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
293
294 try {
295 expect(body.data).to.have.lengthOf(0)
296 } catch {
297 const video = body.data[0]
298
299 expect(video.uuid).not.equal(nsfwUUID)
300 }
301 }
302 })
303 })
304
305 describe('Channels search', async function () {
306
307 async function check (search: VideoChannelsSearchQuery, exists = true) {
308 const body = await command.advancedChannelSearch({ search })
309
310 if (exists === false) {
311 expect(body.total).to.equal(0)
312 expect(body.data).to.have.lengthOf(0)
313 return
314 }
315
316 expect(body.total).to.be.greaterThan(0)
317 expect(body.data).to.have.length.greaterThan(0)
318
319 const videoChannel = body.data[0]
320 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
321 expect(videoChannel.host).to.equal('framatube.org')
322 // TODO: remove, deprecated in 4.2
323 expect(videoChannel.avatar).to.exist
324 expect(videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
325 expect(videoChannel.displayName).to.exist
326
327 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
328 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
329 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
330 // TODO: remove, deprecated in 4.2
331 expect(videoChannel.ownerAccount.avatar).to.exist
332 expect(videoChannel.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
333 }
334
335 it('Should make a simple search and not have results', async function () {
336 const body = await command.searchChannels({ search: 'a'.repeat(500) })
337
338 expect(body.total).to.equal(0)
339 expect(body.data).to.have.lengthOf(0)
340 })
341
342 it('Should make a search and have results', async function () {
343 await check({ search: 'Framasoft', sort: 'createdAt' }, true)
344 })
345
346 it('Should make host search and have appropriate results', async function () {
347 await check({ search: 'Framasoft videos', host: 'example.com' }, false)
348 await check({ search: 'Framasoft videos', host: 'framatube.org' }, true)
349 })
350
351 it('Should make handles search and have appropriate results', async function () {
352 await check({ handles: [ 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
353 await check({ handles: [ 'jeanine', 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
354 await check({ handles: [ 'jeanine', 'chocobozzz_channel2@peertube2.cpy.re' ] }, false)
355 })
356
357 it('Should have a correct pagination', async function () {
358 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
359
360 expect(body.total).to.be.greaterThan(2)
361 expect(body.data).to.have.lengthOf(2)
362 })
363 })
364
365 describe('Playlists search', async function () {
366
367 async function check (search: VideoPlaylistsSearchQuery, exists = true) {
368 const body = await command.advancedPlaylistSearch({ search })
369
370 if (exists === false) {
371 expect(body.total).to.equal(0)
372 expect(body.data).to.have.lengthOf(0)
373 return
374 }
375
376 expect(body.total).to.be.greaterThan(0)
377 expect(body.data).to.have.length.greaterThan(0)
378
379 const videoPlaylist = body.data[0]
380
381 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
382 expect(videoPlaylist.thumbnailUrl).to.exist
383 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
384
385 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
386 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
387 expect(videoPlaylist.videosLength).to.exist
388
389 expect(videoPlaylist.createdAt).to.exist
390 expect(videoPlaylist.updatedAt).to.exist
391
392 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
393 expect(videoPlaylist.displayName).to.exist
394
395 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
396 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
397 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
398 // TODO: remove, deprecated in 4.2
399 expect(videoPlaylist.ownerAccount.avatar).to.exist
400 expect(videoPlaylist.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
401
402 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
403 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
404 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
405 // TODO: remove, deprecated in 4.2
406 expect(videoPlaylist.videoChannel.avatar).to.exist
407 expect(videoPlaylist.videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
408 }
409
410 it('Should make a simple search and not have results', async function () {
411 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
412
413 expect(body.total).to.equal(0)
414 expect(body.data).to.have.lengthOf(0)
415 })
416
417 it('Should make a search and have results', async function () {
418 await check({ search: 'E2E playlist', sort: '-match' }, true)
419 })
420
421 it('Should make host search and have appropriate results', async function () {
422 await check({ search: 'E2E playlist', host: 'example.com' }, false)
423 await check({ search: 'E2E playlist', host: 'peertube2.cpy.re', sort: '-match' }, true)
424 })
425
426 it('Should make a search by uuids and have appropriate results', async function () {
427 const goodUUID = '73804a40-da9a-40c2-b1eb-2c6d9eec8f0a'
428 const goodShortUUID = 'fgei1ws1oa6FCaJ2qZPG29'
429 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
430 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
431
432 {
433 const uuidsMatrix = [
434 [ goodUUID ],
435 [ goodUUID, badShortUUID ],
436 [ badShortUUID, goodShortUUID ],
437 [ goodUUID, goodShortUUID ]
438 ]
439
440 for (const uuids of uuidsMatrix) {
441 const search = { search: 'E2E playlist', sort: '-match', uuids }
442 await check(search, true)
443 }
444 }
445
446 {
447 const uuidsMatrix = [
448 [ badUUID ],
449 [ badShortUUID ]
450 ]
451
452 for (const uuids of uuidsMatrix) {
453 const search = { search: 'E2E playlist', sort: '-match', uuids }
454 await check(search, false)
455 }
456 }
457 })
458
459 it('Should have a correct pagination', async function () {
460 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
461
462 expect(body.total).to.be.greaterThan(2)
463 expect(body.data).to.have.lengthOf(2)
464 })
465 })
466
467 after(async function () {
468 await cleanupTests([ server ])
469 })
470 })