]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/search/search-videos.ts
Move utils to /shared
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-videos.ts
CommitLineData
d525fc39
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import {
6 advancedVideosSearch,
7 flushTests,
8 killallServers,
9 runServer,
10 searchVideo,
11 ServerInfo,
12 setAccessTokensToServers,
13 uploadVideo,
14 wait,
15 immutableAssign
9639bd17 16} from '../../../../shared/utils'
d525fc39
C
17
18const expect = chai.expect
19
20describe('Test a videos search', function () {
21 let server: ServerInfo = null
22 let startDate: string
23
24 before(async function () {
25 this.timeout(30000)
26
27 await flushTests()
28
29 server = await runServer(1)
30
31 await setAccessTokensToServers([ server ])
32
33 {
34 const attributes1 = {
35 name: '1111 2222 3333',
36 fixture: '60fps_720p_small.mp4', // 2 seconds
37 category: 1,
38 licence: 1,
39 nsfw: false,
40 language: 'fr'
41 }
42 await uploadVideo(server.url, server.accessToken, attributes1)
43
44 const attributes2 = immutableAssign(attributes1, { name: attributes1.name + ' - 2', fixture: 'video_short.mp4' })
45 await uploadVideo(server.url, server.accessToken, attributes2)
46
47 const attributes3 = immutableAssign(attributes1, { name: attributes1.name + ' - 3', language: 'en' })
48 await uploadVideo(server.url, server.accessToken, attributes3)
49
50 const attributes4 = immutableAssign(attributes1, { name: attributes1.name + ' - 4', language: 'pl', nsfw: true })
51 await uploadVideo(server.url, server.accessToken, attributes4)
52
53 await wait(1000)
54
55 startDate = new Date().toISOString()
56
57 const attributes5 = immutableAssign(attributes1, { name: attributes1.name + ' - 5', licence: 2 })
58 await uploadVideo(server.url, server.accessToken, attributes5)
59
60 const attributes6 = immutableAssign(attributes1, { name: attributes1.name + ' - 6', tags: [ 't1', 't2 '] })
61 await uploadVideo(server.url, server.accessToken, attributes6)
62
63 const attributes7 = immutableAssign(attributes1, { name: attributes1.name + ' - 7' })
64 await uploadVideo(server.url, server.accessToken, attributes7)
65
66 const attributes8 = immutableAssign(attributes1, { name: attributes1.name + ' - 8', licence: 4 })
67 await uploadVideo(server.url, server.accessToken, attributes8)
68 }
69
70 {
71 const attributes = {
72 name: '3333 4444 5555',
73 fixture: 'video_short.mp4',
74 category: 2,
75 licence: 2,
76 language: 'en'
77 }
78 await uploadVideo(server.url, server.accessToken, attributes)
79
80 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes, { name: attributes.name + ' duplicate' }))
81 }
82
83 {
84 const attributes = {
85 name: '6666 7777 8888',
86 fixture: 'video_short.mp4',
87 category: 3,
88 licence: 3,
89 language: 'pl'
90 }
91 await uploadVideo(server.url, server.accessToken, attributes)
92 }
93
94 {
95 const attributes1 = {
96 name: '9999',
97 tags: [ 'aaaa', 'bbbb', 'cccc' ],
98 category: 1
99 }
100 await uploadVideo(server.url, server.accessToken, attributes1)
101 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { category: 2 }))
102
103 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { tags: [ 'cccc', 'dddd' ] }))
104 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { tags: [ 'eeee', 'ffff' ] }))
105 }
d4112450
C
106
107 {
108 const attributes1 = {
109 name: 'aaaa 2',
110 category: 1
111 }
112 await uploadVideo(server.url, server.accessToken, attributes1)
113 await uploadVideo(server.url, server.accessToken, immutableAssign(attributes1, { category: 2 }))
114 }
d525fc39
C
115 })
116
117 it('Should make a simple search and not have results', async function () {
118 const res = await searchVideo(server.url, 'abc')
119
120 expect(res.body.total).to.equal(0)
121 expect(res.body.data).to.have.lengthOf(0)
122 })
123
124 it('Should make a simple search and have results', async function () {
125 const res = await searchVideo(server.url, '4444 5555 duplicate')
126
127 expect(res.body.total).to.equal(2)
128
129 const videos = res.body.data
130 expect(videos).to.have.lengthOf(2)
131
132 // bestmatch
133 expect(videos[0].name).to.equal('3333 4444 5555 duplicate')
134 expect(videos[1].name).to.equal('3333 4444 5555')
135 })
136
d4112450
C
137 it('Should make a search on tags too, and have results', async function () {
138 const query = {
139 search: 'aaaa',
140 categoryOneOf: [ 1 ]
141 }
142 const res = await advancedVideosSearch(server.url, query)
143
144 expect(res.body.total).to.equal(2)
145
146 const videos = res.body.data
147 expect(videos).to.have.lengthOf(2)
148
149 // bestmatch
150 expect(videos[0].name).to.equal('aaaa 2')
151 expect(videos[1].name).to.equal('9999')
152 })
153
154 it('Should filter on tags without a search', async function () {
155 const query = {
156 tagsAllOf: [ 'bbbb' ]
157 }
158 const res = await advancedVideosSearch(server.url, query)
159
160 expect(res.body.total).to.equal(2)
161
162 const videos = res.body.data
163 expect(videos).to.have.lengthOf(2)
164
165 expect(videos[0].name).to.equal('9999')
166 expect(videos[1].name).to.equal('9999')
167 })
168
169 it('Should filter on category without a search', async function () {
170 const query = {
171 categoryOneOf: [ 3 ]
172 }
173 const res = await advancedVideosSearch(server.url, query)
174
175 expect(res.body.total).to.equal(1)
176
177 const videos = res.body.data
178 expect(videos).to.have.lengthOf(1)
179
180 expect(videos[0].name).to.equal('6666 7777 8888')
181 })
182
d525fc39
C
183 it('Should search by tags (one of)', async function () {
184 const query = {
185 search: '9999',
186 categoryOneOf: [ 1 ],
187 tagsOneOf: [ 'aaaa', 'ffff' ]
188 }
189 const res1 = await advancedVideosSearch(server.url, query)
190 expect(res1.body.total).to.equal(2)
191
192 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsOneOf: [ 'blabla' ] }))
193 expect(res2.body.total).to.equal(0)
194 })
195
196 it('Should search by tags (all of)', async function () {
197 const query = {
198 search: '9999',
199 categoryOneOf: [ 1 ],
200 tagsAllOf: [ 'cccc' ]
201 }
202 const res1 = await advancedVideosSearch(server.url, query)
203 expect(res1.body.total).to.equal(2)
204
205 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'blabla' ] }))
206 expect(res2.body.total).to.equal(0)
207
208 const res3 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'bbbb', 'cccc' ] }))
209 expect(res3.body.total).to.equal(1)
210 })
211
212 it('Should search by category', async function () {
213 const query = {
214 search: '6666',
215 categoryOneOf: [ 3 ]
216 }
217 const res1 = await advancedVideosSearch(server.url, query)
218 expect(res1.body.total).to.equal(1)
219 expect(res1.body.data[0].name).to.equal('6666 7777 8888')
220
221 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { categoryOneOf: [ 2 ] }))
222 expect(res2.body.total).to.equal(0)
223 })
224
225 it('Should search by licence', async function () {
226 const query = {
227 search: '4444 5555',
228 licenceOneOf: [ 2 ]
229 }
230 const res1 = await advancedVideosSearch(server.url, query)
231 expect(res1.body.total).to.equal(2)
232 expect(res1.body.data[0].name).to.equal('3333 4444 5555')
233 expect(res1.body.data[1].name).to.equal('3333 4444 5555 duplicate')
234
235 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { licenceOneOf: [ 3 ] }))
236 expect(res2.body.total).to.equal(0)
237 })
238
239 it('Should search by languages', async function () {
240 const query = {
241 search: '1111 2222 3333',
242 languageOneOf: [ 'pl', 'en' ]
243 }
244 const res1 = await advancedVideosSearch(server.url, query)
245 expect(res1.body.total).to.equal(2)
246 expect(res1.body.data[0].name).to.equal('1111 2222 3333 - 3')
247 expect(res1.body.data[1].name).to.equal('1111 2222 3333 - 4')
248
249 const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { languageOneOf: [ 'eo' ] }))
250 expect(res2.body.total).to.equal(0)
251 })
252
253 it('Should search by start date', async function () {
254 const query = {
255 search: '1111 2222 3333',
256 startDate
257 }
258
259 const res = await advancedVideosSearch(server.url, query)
260 expect(res.body.total).to.equal(4)
261
262 const videos = res.body.data
263 expect(videos[0].name).to.equal('1111 2222 3333 - 5')
264 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
265 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
266 expect(videos[3].name).to.equal('1111 2222 3333 - 8')
267 })
268
269 it('Should make an advanced search', async function () {
270 const query = {
271 search: '1111 2222 3333',
272 languageOneOf: [ 'pl', 'fr' ],
273 durationMax: 4,
0b18f4aa 274 nsfw: 'false' as 'false',
d525fc39
C
275 licenceOneOf: [ 1, 4 ]
276 }
277
278 const res = await advancedVideosSearch(server.url, query)
279 expect(res.body.total).to.equal(4)
280
281 const videos = res.body.data
282 expect(videos[0].name).to.equal('1111 2222 3333')
283 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
284 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
285 expect(videos[3].name).to.equal('1111 2222 3333 - 8')
286 })
287
288 it('Should make an advanced search and sort results', async function () {
289 const query = {
290 search: '1111 2222 3333',
291 languageOneOf: [ 'pl', 'fr' ],
292 durationMax: 4,
0b18f4aa 293 nsfw: 'false' as 'false',
d525fc39
C
294 licenceOneOf: [ 1, 4 ],
295 sort: '-name'
296 }
297
298 const res = await advancedVideosSearch(server.url, query)
299 expect(res.body.total).to.equal(4)
300
301 const videos = res.body.data
302 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
303 expect(videos[1].name).to.equal('1111 2222 3333 - 7')
304 expect(videos[2].name).to.equal('1111 2222 3333 - 6')
305 expect(videos[3].name).to.equal('1111 2222 3333')
306 })
307
308 it('Should make an advanced search and only show the first result', async function () {
309 const query = {
310 search: '1111 2222 3333',
311 languageOneOf: [ 'pl', 'fr' ],
312 durationMax: 4,
0b18f4aa 313 nsfw: 'false' as 'false',
d525fc39
C
314 licenceOneOf: [ 1, 4 ],
315 sort: '-name',
316 start: 0,
317 count: 1
318 }
319
320 const res = await advancedVideosSearch(server.url, query)
321 expect(res.body.total).to.equal(4)
322
323 const videos = res.body.data
324 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
325 })
326
327 it('Should make an advanced search and only show the last result', async function () {
328 const query = {
329 search: '1111 2222 3333',
330 languageOneOf: [ 'pl', 'fr' ],
331 durationMax: 4,
0b18f4aa 332 nsfw: 'false' as 'false',
d525fc39
C
333 licenceOneOf: [ 1, 4 ],
334 sort: '-name',
335 start: 3,
336 count: 1
337 }
338
339 const res = await advancedVideosSearch(server.url, query)
340 expect(res.body.total).to.equal(4)
341
342 const videos = res.body.data
343 expect(videos[0].name).to.equal('1111 2222 3333')
344 })
345
346 after(async function () {
347 killallServers([ server ])
348
349 // Keep the logs if the test failed
350 if (this['ok']) {
351 await flushTests()
352 }
353 })
354})