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