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