d2b0f03129e63f7ac18bdb679d668c7cece568e1
[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   runServer,
10   searchVideo,
11   ServerInfo,
12   setAccessTokensToServers,
13   uploadVideo,
14   wait,
15   immutableAssign
16 } from '../../utils'
17
18 const expect = chai.expect
19
20 describe('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     }
106   })
107
108   it('Should make a simple search and not have results', async function () {
109     const res = await searchVideo(server.url, 'abc')
110
111     expect(res.body.total).to.equal(0)
112     expect(res.body.data).to.have.lengthOf(0)
113   })
114
115   it('Should make a simple search and have results', async function () {
116     const res = await searchVideo(server.url, '4444 5555 duplicate')
117
118     expect(res.body.total).to.equal(2)
119
120     const videos = res.body.data
121     expect(videos).to.have.lengthOf(2)
122
123     // bestmatch
124     expect(videos[0].name).to.equal('3333 4444 5555 duplicate')
125     expect(videos[1].name).to.equal('3333 4444 5555')
126   })
127
128   it('Should search by tags (one of)', async function () {
129     const query = {
130       search: '9999',
131       categoryOneOf: [ 1 ],
132       tagsOneOf: [ 'aaaa', 'ffff' ]
133     }
134     const res1 = await advancedVideosSearch(server.url, query)
135     expect(res1.body.total).to.equal(2)
136
137     const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsOneOf: [ 'blabla' ] }))
138     expect(res2.body.total).to.equal(0)
139   })
140
141   it('Should search by tags (all of)', async function () {
142     const query = {
143       search: '9999',
144       categoryOneOf: [ 1 ],
145       tagsAllOf: [ 'cccc' ]
146     }
147     const res1 = await advancedVideosSearch(server.url, query)
148     expect(res1.body.total).to.equal(2)
149
150     const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'blabla' ] }))
151     expect(res2.body.total).to.equal(0)
152
153     const res3 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'bbbb', 'cccc' ] }))
154     expect(res3.body.total).to.equal(1)
155   })
156
157   it('Should search by category', async function () {
158     const query = {
159       search: '6666',
160       categoryOneOf: [ 3 ]
161     }
162     const res1 = await advancedVideosSearch(server.url, query)
163     expect(res1.body.total).to.equal(1)
164     expect(res1.body.data[0].name).to.equal('6666 7777 8888')
165
166     const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { categoryOneOf: [ 2 ] }))
167     expect(res2.body.total).to.equal(0)
168   })
169
170   it('Should search by licence', async function () {
171     const query = {
172       search: '4444 5555',
173       licenceOneOf: [ 2 ]
174     }
175     const res1 = await advancedVideosSearch(server.url, query)
176     expect(res1.body.total).to.equal(2)
177     expect(res1.body.data[0].name).to.equal('3333 4444 5555')
178     expect(res1.body.data[1].name).to.equal('3333 4444 5555 duplicate')
179
180     const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { licenceOneOf: [ 3 ] }))
181     expect(res2.body.total).to.equal(0)
182   })
183
184   it('Should search by languages', async function () {
185     const query = {
186       search: '1111 2222 3333',
187       languageOneOf: [ 'pl', 'en' ]
188     }
189     const res1 = await advancedVideosSearch(server.url, query)
190     expect(res1.body.total).to.equal(2)
191     expect(res1.body.data[0].name).to.equal('1111 2222 3333 - 3')
192     expect(res1.body.data[1].name).to.equal('1111 2222 3333 - 4')
193
194     const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { languageOneOf: [ 'eo' ] }))
195     expect(res2.body.total).to.equal(0)
196   })
197
198   it('Should search by start date', async function () {
199     const query = {
200       search: '1111 2222 3333',
201       startDate
202     }
203
204     const res = await advancedVideosSearch(server.url, query)
205     expect(res.body.total).to.equal(4)
206
207     const videos = res.body.data
208     expect(videos[0].name).to.equal('1111 2222 3333 - 5')
209     expect(videos[1].name).to.equal('1111 2222 3333 - 6')
210     expect(videos[2].name).to.equal('1111 2222 3333 - 7')
211     expect(videos[3].name).to.equal('1111 2222 3333 - 8')
212   })
213
214   it('Should make an advanced search', async function () {
215     const query = {
216       search: '1111 2222 3333',
217       languageOneOf: [ 'pl', 'fr' ],
218       durationMax: 4,
219       nsfw: 'false' as 'false',
220       licenceOneOf: [ 1, 4 ]
221     }
222
223     const res = await advancedVideosSearch(server.url, query)
224     expect(res.body.total).to.equal(4)
225
226     const videos = res.body.data
227     expect(videos[0].name).to.equal('1111 2222 3333')
228     expect(videos[1].name).to.equal('1111 2222 3333 - 6')
229     expect(videos[2].name).to.equal('1111 2222 3333 - 7')
230     expect(videos[3].name).to.equal('1111 2222 3333 - 8')
231   })
232
233   it('Should make an advanced search and sort results', async function () {
234     const query = {
235       search: '1111 2222 3333',
236       languageOneOf: [ 'pl', 'fr' ],
237       durationMax: 4,
238       nsfw: 'false' as 'false',
239       licenceOneOf: [ 1, 4 ],
240       sort: '-name'
241     }
242
243     const res = await advancedVideosSearch(server.url, query)
244     expect(res.body.total).to.equal(4)
245
246     const videos = res.body.data
247     expect(videos[0].name).to.equal('1111 2222 3333 - 8')
248     expect(videos[1].name).to.equal('1111 2222 3333 - 7')
249     expect(videos[2].name).to.equal('1111 2222 3333 - 6')
250     expect(videos[3].name).to.equal('1111 2222 3333')
251   })
252
253   it('Should make an advanced search and only show the first result', async function () {
254     const query = {
255       search: '1111 2222 3333',
256       languageOneOf: [ 'pl', 'fr' ],
257       durationMax: 4,
258       nsfw: 'false' as 'false',
259       licenceOneOf: [ 1, 4 ],
260       sort: '-name',
261       start: 0,
262       count: 1
263     }
264
265     const res = await advancedVideosSearch(server.url, query)
266     expect(res.body.total).to.equal(4)
267
268     const videos = res.body.data
269     expect(videos[0].name).to.equal('1111 2222 3333 - 8')
270   })
271
272   it('Should make an advanced search and only show the last result', async function () {
273     const query = {
274       search: '1111 2222 3333',
275       languageOneOf: [ 'pl', 'fr' ],
276       durationMax: 4,
277       nsfw: 'false' as 'false',
278       licenceOneOf: [ 1, 4 ],
279       sort: '-name',
280       start: 3,
281       count: 1
282     }
283
284     const res = await advancedVideosSearch(server.url, query)
285     expect(res.body.total).to.equal(4)
286
287     const videos = res.body.data
288     expect(videos[0].name).to.equal('1111 2222 3333')
289   })
290
291   after(async function () {
292     killallServers([ server ])
293
294     // Keep the logs if the test failed
295     if (this['ok']) {
296       await flushTests()
297     }
298   })
299 })