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