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