]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-index.ts
Introduce config command
[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 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 flushAndRunServer,
8 immutableAssign,
9 SearchCommand,
10 ServerInfo,
11 setAccessTokensToServers,
12 uploadVideo
13 } from '@shared/extra-utils'
14 import { VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models'
15
16 const expect = chai.expect
17
18 describe('Test videos search', function () {
19 const localVideoName = 'local video' + new Date().toISOString()
20
21 let server: ServerInfo = null
22 let command: SearchCommand
23
24 before(async function () {
25 this.timeout(30000)
26
27 server = await flushAndRunServer(1)
28
29 await setAccessTokensToServers([ server ])
30
31 await uploadVideo(server.url, server.accessToken, { name: localVideoName })
32
33 command = server.searchCommand
34 })
35
36 describe('Default search', async function () {
37
38 it('Should make a local videos search by default', async function () {
39 this.timeout(10000)
40
41 await server.configCommand.updateCustomSubConfig({
42 newConfig: {
43 search: {
44 searchIndex: {
45 enabled: true,
46 isDefaultSearch: false,
47 disableLocalSearch: false
48 }
49 }
50 }
51 })
52
53 const body = await command.searchVideos({ search: 'local video' })
54
55 expect(body.total).to.equal(1)
56 expect(body.data[0].name).to.equal(localVideoName)
57 })
58
59 it('Should make a local channels search by default', async function () {
60 const body = await command.searchChannels({ search: 'root' })
61
62 expect(body.total).to.equal(1)
63 expect(body.data[0].name).to.equal('root_channel')
64 expect(body.data[0].host).to.equal('localhost:' + server.port)
65 })
66
67 it('Should make an index videos search by default', async function () {
68 await server.configCommand.updateCustomSubConfig({
69 newConfig: {
70 search: {
71 searchIndex: {
72 enabled: true,
73 isDefaultSearch: true,
74 disableLocalSearch: false
75 }
76 }
77 }
78 })
79
80 const body = await command.searchVideos({ search: 'local video' })
81 expect(body.total).to.be.greaterThan(2)
82 })
83
84 it('Should make an index channels search by default', async function () {
85 const body = await command.searchChannels({ search: 'root' })
86 expect(body.total).to.be.greaterThan(2)
87 })
88
89 it('Should make an index videos search if local search is disabled', async function () {
90 await server.configCommand.updateCustomSubConfig({
91 newConfig: {
92 search: {
93 searchIndex: {
94 enabled: true,
95 isDefaultSearch: false,
96 disableLocalSearch: true
97 }
98 }
99 }
100 })
101
102 const body = await command.searchVideos({ search: 'local video' })
103 expect(body.total).to.be.greaterThan(2)
104 })
105
106 it('Should make an index channels search if local search is disabled', async function () {
107 const body = await command.searchChannels({ search: 'root' })
108 expect(body.total).to.be.greaterThan(2)
109 })
110 })
111
112 describe('Videos search', async function () {
113
114 it('Should make a simple search and not have results', async function () {
115 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
116
117 expect(body.total).to.equal(0)
118 expect(body.data).to.have.lengthOf(0)
119 })
120
121 it('Should make a simple search and have results', async function () {
122 const body = await command.searchVideos({ search: 'What is PeerTube' })
123
124 expect(body.total).to.be.greaterThan(1)
125 })
126
127 it('Should make a complex search', async function () {
128
129 async function check (search: VideosSearchQuery, exists = true) {
130 const body = await command.advancedVideoSearch({ search })
131
132 if (exists === false) {
133 expect(body.total).to.equal(0)
134 expect(body.data).to.have.lengthOf(0)
135 return
136 }
137
138 expect(body.total).to.equal(1)
139 expect(body.data).to.have.lengthOf(1)
140
141 const video = body.data[0]
142
143 expect(video.name).to.equal('What is PeerTube?')
144 expect(video.category.label).to.equal('Science & Technology')
145 expect(video.licence.label).to.equal('Attribution - Share Alike')
146 expect(video.privacy.label).to.equal('Public')
147 expect(video.duration).to.equal(113)
148 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
149
150 expect(video.account.host).to.equal('framatube.org')
151 expect(video.account.name).to.equal('framasoft')
152 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
153 expect(video.account.avatar).to.exist
154
155 expect(video.channel.host).to.equal('framatube.org')
156 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
157 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
158 expect(video.channel.avatar).to.exist
159 }
160
161 const baseSearch: VideosSearchQuery = {
162 search: 'what is peertube',
163 start: 0,
164 count: 2,
165 categoryOneOf: [ 15 ],
166 licenceOneOf: [ 2 ],
167 tagsAllOf: [ 'framasoft', 'peertube' ],
168 startDate: '2018-10-01T10:50:46.396Z',
169 endDate: '2018-10-01T10:55:46.396Z'
170 }
171
172 {
173 await check(baseSearch)
174 }
175
176 {
177 const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' })
178 await check(search, false)
179 }
180
181 {
182 const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] })
183 await check(search, false)
184 }
185
186 {
187 const search = immutableAssign(baseSearch, { durationMin: 2000 })
188 await check(search, false)
189 }
190
191 {
192 const search = immutableAssign(baseSearch, { nsfw: 'true' })
193 await check(search, false)
194 }
195
196 {
197 const search = immutableAssign(baseSearch, { nsfw: 'false' })
198 await check(search, true)
199 }
200
201 {
202 const search = immutableAssign(baseSearch, { nsfw: 'both' })
203 await check(search, true)
204 }
205 })
206
207 it('Should have a correct pagination', async function () {
208 const search = {
209 search: 'video',
210 start: 0,
211 count: 5
212 }
213
214 const body = await command.advancedVideoSearch({ search })
215
216 expect(body.total).to.be.greaterThan(5)
217 expect(body.data).to.have.lengthOf(5)
218 })
219
220 it('Should use the nsfw instance policy as default', async function () {
221 let nsfwUUID: string
222
223 {
224 await server.configCommand.updateCustomSubConfig({
225 newConfig: {
226 instance: { defaultNSFWPolicy: 'display' }
227 }
228 })
229
230 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
231 expect(body.data).to.have.length.greaterThan(0)
232
233 const video = body.data[0]
234 expect(video.nsfw).to.be.true
235
236 nsfwUUID = video.uuid
237 }
238
239 {
240 await server.configCommand.updateCustomSubConfig({
241 newConfig: {
242 instance: { defaultNSFWPolicy: 'do_not_list' }
243 }
244 })
245
246 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
247
248 try {
249 expect(body.data).to.have.lengthOf(0)
250 } catch {
251 const video = body.data[0]
252
253 expect(video.uuid).not.equal(nsfwUUID)
254 }
255 }
256 })
257 })
258
259 describe('Channels search', async function () {
260
261 it('Should make a simple search and not have results', async function () {
262 const body = await command.searchChannels({ search: 'a'.repeat(500) })
263
264 expect(body.total).to.equal(0)
265 expect(body.data).to.have.lengthOf(0)
266 })
267
268 it('Should make a search and have results', async function () {
269 const body = await command.advancedChannelSearch({ search: { search: 'Framasoft', sort: 'createdAt' } })
270
271 expect(body.total).to.be.greaterThan(0)
272 expect(body.data).to.have.length.greaterThan(0)
273
274 const videoChannel = body.data[0]
275 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
276 expect(videoChannel.host).to.equal('framatube.org')
277 expect(videoChannel.avatar).to.exist
278 expect(videoChannel.displayName).to.exist
279
280 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
281 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
282 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
283 expect(videoChannel.ownerAccount.avatar).to.exist
284 })
285
286 it('Should have a correct pagination', async function () {
287 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
288
289 expect(body.total).to.be.greaterThan(2)
290 expect(body.data).to.have.lengthOf(2)
291 })
292 })
293
294 describe('Playlists search', async function () {
295
296 it('Should make a simple search and not have results', async function () {
297 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
298
299 expect(body.total).to.equal(0)
300 expect(body.data).to.have.lengthOf(0)
301 })
302
303 it('Should make a search and have results', async function () {
304 const body = await command.advancedPlaylistSearch({ search: { search: 'E2E playlist', sort: '-match' } })
305
306 expect(body.total).to.be.greaterThan(0)
307 expect(body.data).to.have.length.greaterThan(0)
308
309 const videoPlaylist = body.data[0]
310
311 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
312 expect(videoPlaylist.thumbnailUrl).to.exist
313 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
314
315 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
316 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
317 expect(videoPlaylist.videosLength).to.exist
318
319 expect(videoPlaylist.createdAt).to.exist
320 expect(videoPlaylist.updatedAt).to.exist
321
322 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
323 expect(videoPlaylist.displayName).to.exist
324
325 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
326 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
327 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
328 expect(videoPlaylist.ownerAccount.avatar).to.exist
329
330 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
331 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
332 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
333 expect(videoPlaylist.videoChannel.avatar).to.exist
334 })
335
336 it('Should have a correct pagination', async function () {
337 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
338
339 expect(body.total).to.be.greaterThan(2)
340 expect(body.data).to.have.lengthOf(2)
341 })
342 })
343
344 after(async function () {
345 await cleanupTests([ server ])
346 })
347 })