]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/search/search-index.ts
Search channels against handles and not names
[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'
4c7e60bc 5import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/extra-utils'
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
17describe('Test videos 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
113 it('Should make a simple search and not have results', async function () {
af971e06 114 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
3521ab8f 115
af971e06
C
116 expect(body.total).to.equal(0)
117 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
118 })
119
120 it('Should make a simple search and have results', async function () {
af971e06 121 const body = await command.searchVideos({ search: 'What is PeerTube' })
3521ab8f 122
af971e06 123 expect(body.total).to.be.greaterThan(1)
3521ab8f
C
124 })
125
126 it('Should make a complex search', async function () {
127
128 async function check (search: VideosSearchQuery, exists = true) {
af971e06 129 const body = await command.advancedVideoSearch({ search })
3521ab8f
C
130
131 if (exists === false) {
af971e06
C
132 expect(body.total).to.equal(0)
133 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
134 return
135 }
136
af971e06
C
137 expect(body.total).to.equal(1)
138 expect(body.data).to.have.lengthOf(1)
3521ab8f 139
af971e06 140 const video = body.data[0]
3521ab8f
C
141
142 expect(video.name).to.equal('What is PeerTube?')
143 expect(video.category.label).to.equal('Science & Technology')
144 expect(video.licence.label).to.equal('Attribution - Share Alike')
145 expect(video.privacy.label).to.equal('Public')
146 expect(video.duration).to.equal(113)
147 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
148
149 expect(video.account.host).to.equal('framatube.org')
150 expect(video.account.name).to.equal('framasoft')
151 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
152 expect(video.account.avatar).to.exist
153
154 expect(video.channel.host).to.equal('framatube.org')
155 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
156 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
157 expect(video.channel.avatar).to.exist
158 }
159
160 const baseSearch: VideosSearchQuery = {
161 search: 'what is peertube',
162 start: 0,
163 count: 2,
164 categoryOneOf: [ 15 ],
165 licenceOneOf: [ 2 ],
166 tagsAllOf: [ 'framasoft', 'peertube' ],
167 startDate: '2018-10-01T10:50:46.396Z',
168 endDate: '2018-10-01T10:55:46.396Z'
169 }
170
171 {
172 await check(baseSearch)
173 }
174
175 {
6c5065a0 176 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
3521ab8f
C
177 await check(search, false)
178 }
179
180 {
6c5065a0 181 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
3521ab8f
C
182 await check(search, false)
183 }
184
185 {
6c5065a0 186 const search = { ...baseSearch, durationMin: 2000 }
3521ab8f
C
187 await check(search, false)
188 }
189
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 }
164c8d46
C
204
205 {
206 const search = { ...baseSearch, host: 'example.com' }
207 await check(search, false)
208 }
209
210 {
211 const search = { ...baseSearch, host: 'framatube.org' }
212 await check(search, true)
213 }
3521ab8f
C
214 })
215
216 it('Should have a correct pagination', async function () {
217 const search = {
218 search: 'video',
219 start: 0,
220 count: 5
221 }
222
af971e06 223 const body = await command.advancedVideoSearch({ search })
3521ab8f 224
af971e06
C
225 expect(body.total).to.be.greaterThan(5)
226 expect(body.data).to.have.lengthOf(5)
3521ab8f 227 })
1a40132c
C
228
229 it('Should use the nsfw instance policy as default', async function () {
230 let nsfwUUID: string
231
232 {
89d241a7 233 await server.config.updateCustomSubConfig({
65e6e260
C
234 newConfig: {
235 instance: { defaultNSFWPolicy: 'display' }
236 }
237 })
1a40132c 238
af971e06
C
239 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
240 expect(body.data).to.have.length.greaterThan(0)
1a40132c 241
af971e06 242 const video = body.data[0]
1a40132c
C
243 expect(video.nsfw).to.be.true
244
245 nsfwUUID = video.uuid
246 }
247
248 {
89d241a7 249 await server.config.updateCustomSubConfig({
65e6e260
C
250 newConfig: {
251 instance: { defaultNSFWPolicy: 'do_not_list' }
252 }
253 })
1a40132c 254
af971e06 255 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
1a40132c
C
256
257 try {
af971e06
C
258 expect(body.data).to.have.lengthOf(0)
259 } catch {
260 const video = body.data[0]
1a40132c
C
261
262 expect(video.uuid).not.equal(nsfwUUID)
263 }
264 }
265 })
3521ab8f
C
266 })
267
268 describe('Channels search', async function () {
269
270 it('Should make a simple search and not have results', async function () {
af971e06 271 const body = await command.searchChannels({ search: 'a'.repeat(500) })
3521ab8f 272
af971e06
C
273 expect(body.total).to.equal(0)
274 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
275 })
276
277 it('Should make a search and have results', async function () {
3521ab8f 278
164c8d46
C
279 async function check (search: VideoChannelsSearchQuery, exists = true) {
280 const body = await command.advancedChannelSearch({ search })
281
282 if (exists === false) {
283 expect(body.total).to.equal(0)
284 expect(body.data).to.have.lengthOf(0)
285 return
286 }
287
288 expect(body.total).to.be.greaterThan(0)
289 expect(body.data).to.have.length.greaterThan(0)
290
291 const videoChannel = body.data[0]
292 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
293 expect(videoChannel.host).to.equal('framatube.org')
294 expect(videoChannel.avatar).to.exist
295 expect(videoChannel.displayName).to.exist
3521ab8f 296
164c8d46
C
297 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
298 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
299 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
300 expect(videoChannel.ownerAccount.avatar).to.exist
301 }
3521ab8f 302
164c8d46
C
303 await check({ search: 'Framasoft', sort: 'createdAt' }, true)
304 await check({ search: 'Framasoft', host: 'example.com' }, false)
305 await check({ search: 'Framasoft', host: 'framatube.org' }, true)
3521ab8f
C
306 })
307
308 it('Should have a correct pagination', async function () {
af971e06 309 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
3521ab8f 310
af971e06
C
311 expect(body.total).to.be.greaterThan(2)
312 expect(body.data).to.have.lengthOf(2)
3521ab8f
C
313 })
314 })
315
37a44fc9
C
316 describe('Playlists search', async function () {
317
318 it('Should make a simple search and not have results', async function () {
af971e06 319 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
37a44fc9 320
af971e06
C
321 expect(body.total).to.equal(0)
322 expect(body.data).to.have.lengthOf(0)
37a44fc9
C
323 })
324
325 it('Should make a search and have results', async function () {
37a44fc9 326
164c8d46
C
327 async function check (search: VideoPlaylistsSearchQuery, exists = true) {
328 const body = await command.advancedPlaylistSearch({ search })
329
330 if (exists === false) {
331 expect(body.total).to.equal(0)
332 expect(body.data).to.have.lengthOf(0)
333 return
334 }
335
336 expect(body.total).to.be.greaterThan(0)
337 expect(body.data).to.have.length.greaterThan(0)
338
339 const videoPlaylist = body.data[0]
37a44fc9 340
164c8d46
C
341 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
342 expect(videoPlaylist.thumbnailUrl).to.exist
343 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
37a44fc9 344
164c8d46
C
345 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
346 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
347 expect(videoPlaylist.videosLength).to.exist
37a44fc9 348
164c8d46
C
349 expect(videoPlaylist.createdAt).to.exist
350 expect(videoPlaylist.updatedAt).to.exist
37a44fc9 351
164c8d46
C
352 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
353 expect(videoPlaylist.displayName).to.exist
37a44fc9 354
164c8d46
C
355 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
356 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
357 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
358 expect(videoPlaylist.ownerAccount.avatar).to.exist
37a44fc9 359
164c8d46
C
360 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
361 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
362 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
363 expect(videoPlaylist.videoChannel.avatar).to.exist
364 }
37a44fc9 365
164c8d46
C
366 await check({ search: 'E2E playlist', sort: '-match' }, true)
367 await check({ search: 'E2E playlist', host: 'example.com' }, false)
368 await check({ search: 'E2E playlist', host: 'peertube2.cpy.re' }, true)
37a44fc9
C
369 })
370
371 it('Should have a correct pagination', async function () {
af971e06 372 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
37a44fc9 373
af971e06
C
374 expect(body.total).to.be.greaterThan(2)
375 expect(body.data).to.have.lengthOf(2)
37a44fc9
C
376 })
377 })
378
3521ab8f
C
379 after(async function () {
380 await cleanupTests([ server ])
381 })
382})