]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/search.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / search.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
4 import { HttpStatusCode } from '@shared/models'
5 import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
6
7 function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
8 return server.config.updateCustomSubConfig({
9 newConfig: {
10 search: {
11 searchIndex: {
12 enabled,
13 disableLocalSearch
14 }
15 }
16 }
17 })
18 }
19
20 describe('Test videos API validator', function () {
21 let server: PeerTubeServer
22
23 // ---------------------------------------------------------------
24
25 before(async function () {
26 this.timeout(30000)
27
28 server = await createSingleServer(1)
29 await setAccessTokensToServers([ server ])
30 })
31
32 describe('When searching videos', function () {
33 const path = '/api/v1/search/videos/'
34
35 const query = {
36 search: 'coucou'
37 }
38
39 it('Should fail with a bad start pagination', async function () {
40 await checkBadStartPagination(server.url, path, null, query)
41 })
42
43 it('Should fail with a bad count pagination', async function () {
44 await checkBadCountPagination(server.url, path, null, query)
45 })
46
47 it('Should fail with an incorrect sort', async function () {
48 await checkBadSortPagination(server.url, path, null, query)
49 })
50
51 it('Should succeed with the correct parameters', async function () {
52 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
53 })
54
55 it('Should fail with an invalid category', async function () {
56 const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
57 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
58
59 const customQuery2 = { ...query, categoryOneOf: 'a' }
60 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
61 })
62
63 it('Should succeed with a valid category', async function () {
64 const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
65 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
66
67 const customQuery2 = { ...query, categoryOneOf: 1 }
68 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
69 })
70
71 it('Should fail with an invalid licence', async function () {
72 const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
73 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
74
75 const customQuery2 = { ...query, licenceOneOf: 'a' }
76 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77 })
78
79 it('Should succeed with a valid licence', async function () {
80 const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
81 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
82
83 const customQuery2 = { ...query, licenceOneOf: 1 }
84 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
85 })
86
87 it('Should succeed with a valid language', async function () {
88 const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
89 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
90
91 const customQuery2 = { ...query, languageOneOf: 'fr' }
92 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
93 })
94
95 it('Should succeed with valid tags', async function () {
96 const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
97 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
98
99 const customQuery2 = { ...query, tagsOneOf: 'tag1' }
100 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
101
102 const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
103 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
104
105 const customQuery4 = { ...query, tagsAllOf: 'tag1' }
106 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
107 })
108
109 it('Should fail with invalid durations', async function () {
110 const customQuery1 = { ...query, durationMin: 'hello' }
111 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
112
113 const customQuery2 = { ...query, durationMax: 'hello' }
114 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
115 })
116
117 it('Should fail with invalid dates', async function () {
118 const customQuery1 = { ...query, startDate: 'hello' }
119 await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
120
121 const customQuery2 = { ...query, endDate: 'hello' }
122 await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
123
124 const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
125 await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
126
127 const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
128 await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
129 })
130
131 it('Should fail with an invalid host', async function () {
132 const customQuery = { ...query, host: '6565' }
133 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
134 })
135
136 it('Should succeed with a host', async function () {
137 const customQuery = { ...query, host: 'example.com' }
138 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
139 })
140
141 it('Should fail with invalid uuids', async function () {
142 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
143 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
144 })
145
146 it('Should succeed with valid uuids', async function () {
147 const customQuery = { ...query, uuids: [ 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
148 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
149 })
150 })
151
152 describe('When searching video playlists', function () {
153 const path = '/api/v1/search/video-playlists/'
154
155 const query = {
156 search: 'coucou',
157 host: 'example.com'
158 }
159
160 it('Should fail with a bad start pagination', async function () {
161 await checkBadStartPagination(server.url, path, null, query)
162 })
163
164 it('Should fail with a bad count pagination', async function () {
165 await checkBadCountPagination(server.url, path, null, query)
166 })
167
168 it('Should fail with an incorrect sort', async function () {
169 await checkBadSortPagination(server.url, path, null, query)
170 })
171
172 it('Should fail with an invalid host', async function () {
173 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
174 })
175
176 it('Should fail with invalid uuids', async function () {
177 const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
178 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
179 })
180
181 it('Should succeed with the correct parameters', async function () {
182 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
183 })
184 })
185
186 describe('When searching video channels', function () {
187 const path = '/api/v1/search/video-channels/'
188
189 const query = {
190 search: 'coucou',
191 host: 'example.com'
192 }
193
194 it('Should fail with a bad start pagination', async function () {
195 await checkBadStartPagination(server.url, path, null, query)
196 })
197
198 it('Should fail with a bad count pagination', async function () {
199 await checkBadCountPagination(server.url, path, null, query)
200 })
201
202 it('Should fail with an incorrect sort', async function () {
203 await checkBadSortPagination(server.url, path, null, query)
204 })
205
206 it('Should fail with an invalid host', async function () {
207 await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
208 })
209
210 it('Should fail with invalid handles', async function () {
211 await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
212 })
213
214 it('Should succeed with the correct parameters', async function () {
215 await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
216 })
217 })
218
219 describe('Search target', function () {
220
221 it('Should fail/succeed depending on the search target', async function () {
222 const query = { search: 'coucou' }
223 const paths = [
224 '/api/v1/search/video-playlists/',
225 '/api/v1/search/video-channels/',
226 '/api/v1/search/videos/'
227 ]
228
229 for (const path of paths) {
230 {
231 const customQuery = { ...query, searchTarget: 'hello' }
232 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
233 }
234
235 {
236 const customQuery = { ...query, searchTarget: undefined }
237 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
238 }
239
240 {
241 const customQuery = { ...query, searchTarget: 'local' }
242 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
243 }
244
245 {
246 const customQuery = { ...query, searchTarget: 'search-index' }
247 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
248 }
249
250 await updateSearchIndex(server, true, true)
251
252 {
253 const customQuery = { ...query, searchTarget: 'search-index' }
254 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
255 }
256
257 await updateSearchIndex(server, true, false)
258
259 {
260 const customQuery = { ...query, searchTarget: 'local' }
261 await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
262 }
263
264 await updateSearchIndex(server, false, false)
265 }
266 })
267 })
268
269 after(async function () {
270 await cleanupTests([ server ])
271 })
272 })