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