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