2354aaa8b1c03df6c71ccc598974a800dfd80dd4
[oweals/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   searchVideo,
9   ServerInfo,
10   setAccessTokensToServers,
11   updateCustomSubConfig,
12   uploadVideo,
13   advancedVideosSearch,
14   immutableAssign
15 } from '../../../../shared/extra-utils'
16 import { searchVideoChannel, advancedVideoChannelSearch } from '@shared/extra-utils/search/video-channels'
17 import { VideosSearchQuery, Video, VideoChannel } from '@shared/models'
18
19 const expect = chai.expect
20
21 describe('Test videos search', function () {
22   let server: ServerInfo = null
23   const localVideoName = 'local video' + new Date().toISOString()
24
25   before(async function () {
26     this.timeout(30000)
27
28     server = await flushAndRunServer(1)
29
30     await setAccessTokensToServers([ server ])
31
32     await uploadVideo(server.url, server.accessToken, { name: localVideoName })
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
40       await updateCustomSubConfig(server.url, server.accessToken, {
41         search: {
42           searchIndex: {
43             enabled: true,
44             isDefaultSearch: false,
45             disableLocalSearch: false
46           }
47         }
48       })
49
50       const res = await searchVideo(server.url, 'local video')
51
52       expect(res.body.total).to.equal(1)
53       expect(res.body.data[0].name).to.equal(localVideoName)
54     })
55
56     it('Should make a local channels search by default', async function () {
57       const res = await searchVideoChannel(server.url, 'root')
58
59       expect(res.body.total).to.equal(1)
60       expect(res.body.data[0].name).to.equal('root_channel')
61       expect(res.body.data[0].host).to.equal('localhost:' + server.port)
62     })
63
64     it('Should make an index videos search by default', async function () {
65       await updateCustomSubConfig(server.url, server.accessToken, {
66         search: {
67           searchIndex: {
68             enabled: true,
69             isDefaultSearch: true,
70             disableLocalSearch: false
71           }
72         }
73       })
74
75       const res = await searchVideo(server.url, 'local video')
76       expect(res.body.total).to.be.greaterThan(2)
77     })
78
79     it('Should make an index channels search by default', async function () {
80       const res = await searchVideoChannel(server.url, 'root')
81       expect(res.body.total).to.be.greaterThan(2)
82     })
83
84     it('Should make an index videos search if local search is disabled', async function () {
85       await updateCustomSubConfig(server.url, server.accessToken, {
86         search: {
87           searchIndex: {
88             enabled: true,
89             isDefaultSearch: false,
90             disableLocalSearch: true
91           }
92         }
93       })
94
95       const res = await searchVideo(server.url, 'local video')
96       expect(res.body.total).to.be.greaterThan(2)
97     })
98
99     it('Should make an index channels search if local search is disabled', async function () {
100       const res = await searchVideoChannel(server.url, 'root')
101       expect(res.body.total).to.be.greaterThan(2)
102     })
103   })
104
105   describe('Videos search', async function () {
106
107     it('Should make a simple search and not have results', async function () {
108       const res = await searchVideo(server.url, 'a'.repeat(500))
109
110       expect(res.body.total).to.equal(0)
111       expect(res.body.data).to.have.lengthOf(0)
112     })
113
114     it('Should make a simple search and have results', async function () {
115       const res = await searchVideo(server.url, 'What is PeerTube')
116
117       expect(res.body.total).to.be.greaterThan(1)
118     })
119
120     it('Should make a complex search', async function () {
121
122       async function check (search: VideosSearchQuery, exists = true) {
123         const res = await advancedVideosSearch(server.url, search)
124
125         if (exists === false) {
126           expect(res.body.total).to.equal(0)
127           expect(res.body.data).to.have.lengthOf(0)
128           return
129         }
130
131         expect(res.body.total).to.equal(1)
132         expect(res.body.data).to.have.lengthOf(1)
133
134         const video: Video = res.body.data[0]
135
136         expect(video.name).to.equal('What is PeerTube?')
137         expect(video.category.label).to.equal('Science & Technology')
138         expect(video.licence.label).to.equal('Attribution - Share Alike')
139         expect(video.privacy.label).to.equal('Public')
140         expect(video.duration).to.equal(113)
141         expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
142
143         expect(video.account.host).to.equal('framatube.org')
144         expect(video.account.name).to.equal('framasoft')
145         expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
146         expect(video.account.avatar).to.exist
147
148         expect(video.channel.host).to.equal('framatube.org')
149         expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
150         expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
151         expect(video.channel.avatar).to.exist
152       }
153
154       const baseSearch: VideosSearchQuery = {
155         search: 'what is peertube',
156         start: 0,
157         count: 2,
158         categoryOneOf: [ 15 ],
159         licenceOneOf: [ 2 ],
160         tagsAllOf: [ 'framasoft', 'peertube' ],
161         startDate: '2018-10-01T10:50:46.396Z',
162         endDate: '2018-10-01T10:55:46.396Z'
163       }
164
165       {
166         await check(baseSearch)
167       }
168
169       {
170         const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' })
171         await check(search, false)
172       }
173
174       {
175         const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] })
176         await check(search, false)
177       }
178
179       {
180         const search = immutableAssign(baseSearch, { durationMin: 2000 })
181         await check(search, false)
182       }
183
184       {
185         const search = immutableAssign(baseSearch, { nsfw: 'true' })
186         await check(search, false)
187       }
188
189       {
190         const search = immutableAssign(baseSearch, { nsfw: 'false' })
191         await check(search, true)
192       }
193
194       {
195         const search = immutableAssign(baseSearch, { nsfw: 'both' })
196         await check(search, true)
197       }
198     })
199
200     it('Should have a correct pagination', async function () {
201       const search = {
202         search: 'video',
203         start: 0,
204         count: 5
205       }
206
207       const res = await advancedVideosSearch(server.url, search)
208
209       expect(res.body.total).to.be.greaterThan(5)
210       expect(res.body.data).to.have.lengthOf(5)
211     })
212   })
213
214   describe('Channels search', async function () {
215
216     it('Should make a simple search and not have results', async function () {
217       const res = await searchVideoChannel(server.url, 'a'.repeat(500))
218
219       expect(res.body.total).to.equal(0)
220       expect(res.body.data).to.have.lengthOf(0)
221     })
222
223     it('Should make a search and have results', async function () {
224       const res = await advancedVideoChannelSearch(server.url, { search: 'Framasoft', sort: 'createdAt' })
225
226       expect(res.body.total).to.be.greaterThan(0)
227       expect(res.body.data).to.have.length.greaterThan(0)
228
229       const videoChannel: VideoChannel = res.body.data[0]
230       expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
231       expect(videoChannel.host).to.equal('framatube.org')
232       expect(videoChannel.avatar).to.exist
233       expect(videoChannel.displayName).to.exist
234
235       expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
236       expect(videoChannel.ownerAccount.name).to.equal('framasoft')
237       expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
238       expect(videoChannel.ownerAccount.avatar).to.exist
239     })
240
241     it('Should have a correct pagination', async function () {
242       const res = await advancedVideoChannelSearch(server.url, { search: 'root', start: 0, count: 2 })
243
244       expect(res.body.total).to.be.greaterThan(2)
245       expect(res.body.data).to.have.lengthOf(2)
246     })
247   })
248
249   after(async function () {
250     await cleanupTests([ server ])
251   })
252 })