]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-index.ts
emit more specific status codes on video upload (#3423)
[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 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 it('Should use the nsfw instance policy as default', async function () {
214 let nsfwUUID: string
215
216 {
217 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'display' } })
218
219 const res = await searchVideo(server.url, 'NSFW search index')
220 const video = res.body.data[0] as Video
221
222 expect(res.body.data).to.have.length.greaterThan(0)
223 expect(video.nsfw).to.be.true
224
225 nsfwUUID = video.uuid
226 }
227
228 {
229 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'do_not_list' } })
230
231 const res = await searchVideo(server.url, 'NSFW search index')
232
233 try {
234 expect(res.body.data).to.have.lengthOf(0)
235 } catch (err) {
236 //
237 const video = res.body.data[0] as Video
238
239 expect(video.uuid).not.equal(nsfwUUID)
240 }
241 }
242 })
243 })
244
245 describe('Channels search', async function () {
246
247 it('Should make a simple search and not have results', async function () {
248 const res = await searchVideoChannel(server.url, 'a'.repeat(500))
249
250 expect(res.body.total).to.equal(0)
251 expect(res.body.data).to.have.lengthOf(0)
252 })
253
254 it('Should make a search and have results', async function () {
255 const res = await advancedVideoChannelSearch(server.url, { search: 'Framasoft', sort: 'createdAt' })
256
257 expect(res.body.total).to.be.greaterThan(0)
258 expect(res.body.data).to.have.length.greaterThan(0)
259
260 const videoChannel: VideoChannel = res.body.data[0]
261 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
262 expect(videoChannel.host).to.equal('framatube.org')
263 expect(videoChannel.avatar).to.exist
264 expect(videoChannel.displayName).to.exist
265
266 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
267 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
268 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
269 expect(videoChannel.ownerAccount.avatar).to.exist
270 })
271
272 it('Should have a correct pagination', async function () {
273 const res = await advancedVideoChannelSearch(server.url, { search: 'root', start: 0, count: 2 })
274
275 expect(res.body.total).to.be.greaterThan(2)
276 expect(res.body.data).to.have.lengthOf(2)
277 })
278 })
279
280 after(async function () {
281 await cleanupTests([ server ])
282 })
283 })