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