Add search target check params
[oweals/peertube.git] / server / tests / api / check-params / search.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import {
5   cleanupTests,
6   flushAndRunServer,
7   immutableAssign,
8   makeGetRequest,
9   ServerInfo,
10   updateCustomSubConfig,
11   setAccessTokensToServers
12 } from '../../../../shared/extra-utils'
13 import {
14   checkBadCountPagination,
15   checkBadSortPagination,
16   checkBadStartPagination
17 } from '../../../../shared/extra-utils/requests/check-api-params'
18
19 function updateSearchIndex (server: ServerInfo, enabled: boolean, disableLocalSearch = false) {
20   return updateCustomSubConfig(server.url, server.accessToken, {
21     search: {
22       searchIndex: {
23         enabled,
24         disableLocalSearch
25       }
26     }
27   })
28 }
29
30 describe('Test videos API validator', function () {
31   let server: ServerInfo
32
33   // ---------------------------------------------------------------
34
35   before(async function () {
36     this.timeout(30000)
37
38     server = await flushAndRunServer(1)
39     await setAccessTokensToServers([ server ])
40   })
41
42   describe('When searching videos', function () {
43     const path = '/api/v1/search/videos/'
44
45     const query = {
46       search: 'coucou'
47     }
48
49     it('Should fail with a bad start pagination', async function () {
50       await checkBadStartPagination(server.url, path, null, query)
51     })
52
53     it('Should fail with a bad count pagination', async function () {
54       await checkBadCountPagination(server.url, path, null, query)
55     })
56
57     it('Should fail with an incorrect sort', async function () {
58       await checkBadSortPagination(server.url, path, null, query)
59     })
60
61     it('Should success with the correct parameters', async function () {
62       await makeGetRequest({ url: server.url, path, query, statusCodeExpected: 200 })
63     })
64
65     it('Should fail with an invalid category', async function () {
66       const customQuery1 = immutableAssign(query, { categoryOneOf: [ 'aa', 'b' ] })
67       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
68
69       const customQuery2 = immutableAssign(query, { categoryOneOf: 'a' })
70       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 400 })
71     })
72
73     it('Should succeed with a valid category', async function () {
74       const customQuery1 = immutableAssign(query, { categoryOneOf: [ 1, 7 ] })
75       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
76
77       const customQuery2 = immutableAssign(query, { categoryOneOf: 1 })
78       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 200 })
79     })
80
81     it('Should fail with an invalid licence', async function () {
82       const customQuery1 = immutableAssign(query, { licenceOneOf: [ 'aa', 'b' ] })
83       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
84
85       const customQuery2 = immutableAssign(query, { licenceOneOf: 'a' })
86       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 400 })
87     })
88
89     it('Should succeed with a valid licence', async function () {
90       const customQuery1 = immutableAssign(query, { licenceOneOf: [ 1, 2 ] })
91       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
92
93       const customQuery2 = immutableAssign(query, { licenceOneOf: 1 })
94       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 200 })
95     })
96
97     it('Should succeed with a valid language', async function () {
98       const customQuery1 = immutableAssign(query, { languageOneOf: [ 'fr', 'en' ] })
99       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
100
101       const customQuery2 = immutableAssign(query, { languageOneOf: 'fr' })
102       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 200 })
103     })
104
105     it('Should succeed with valid tags', async function () {
106       const customQuery1 = immutableAssign(query, { tagsOneOf: [ 'tag1', 'tag2' ] })
107       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
108
109       const customQuery2 = immutableAssign(query, { tagsOneOf: 'tag1' })
110       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 200 })
111
112       const customQuery3 = immutableAssign(query, { tagsAllOf: [ 'tag1', 'tag2' ] })
113       await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: 200 })
114
115       const customQuery4 = immutableAssign(query, { tagsAllOf: 'tag1' })
116       await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: 200 })
117     })
118
119     it('Should fail with invalid durations', async function () {
120       const customQuery1 = immutableAssign(query, { durationMin: 'hello' })
121       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
122
123       const customQuery2 = immutableAssign(query, { durationMax: 'hello' })
124       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 400 })
125     })
126
127     it('Should fail with invalid dates', async function () {
128       const customQuery1 = immutableAssign(query, { startDate: 'hello' })
129       await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
130
131       const customQuery2 = immutableAssign(query, { endDate: 'hello' })
132       await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 400 })
133
134       const customQuery3 = immutableAssign(query, { originallyPublishedStartDate: 'hello' })
135       await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: 400 })
136
137       const customQuery4 = immutableAssign(query, { originallyPublishedEndDate: 'hello' })
138       await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: 400 })
139     })
140   })
141
142   describe('When searching video channels', function () {
143     const path = '/api/v1/search/video-channels/'
144
145     const query = {
146       search: 'coucou'
147     }
148
149     it('Should fail with a bad start pagination', async function () {
150       await checkBadStartPagination(server.url, path, null, query)
151     })
152
153     it('Should fail with a bad count pagination', async function () {
154       await checkBadCountPagination(server.url, path, null, query)
155     })
156
157     it('Should fail with an incorrect sort', async function () {
158       await checkBadSortPagination(server.url, path, null, query)
159     })
160
161     it('Should success with the correct parameters', async function () {
162       await makeGetRequest({ url: server.url, path, query, statusCodeExpected: 200 })
163     })
164   })
165
166   describe('Search target', function () {
167
168     it('Should fail/succeed depending on the search target', async function () {
169       this.timeout(10000)
170
171       const query = { search: 'coucou' }
172       const paths = [
173         '/api/v1/search/video-channels/',
174         '/api/v1/search/videos/'
175       ]
176
177       for (const path of paths) {
178         {
179           const customQuery = immutableAssign(query, { searchTarget: 'hello' })
180           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
181         }
182
183         {
184           const customQuery = immutableAssign(query, { searchTarget: undefined })
185           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
186         }
187
188         {
189           const customQuery = immutableAssign(query, { searchTarget: 'local' })
190           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
191         }
192
193         {
194           const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
195           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
196         }
197
198         await updateSearchIndex(server, true, true)
199
200         {
201           const customQuery = immutableAssign(query, { searchTarget: 'local' })
202           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 400 })
203         }
204
205         {
206           const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
207           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
208         }
209
210         await updateSearchIndex(server, true, false)
211
212         {
213           const customQuery = immutableAssign(query, { searchTarget: 'local' })
214           await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: 200 })
215         }
216
217         await updateSearchIndex(server, false, false)
218       }
219     })
220   })
221
222   after(async function () {
223     await cleanupTests([ server ])
224   })
225 })