]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/search.ts
emit more specific status codes on video upload (#3423)
[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 channels', function () {
144 const path = '/api/v1/search/video-channels/'
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('Search target', function () {
168
169 it('Should fail/succeed depending on the search target', async function () {
170 this.timeout(10000)
171
172 const query = { search: 'coucou' }
173 const paths = [
174 '/api/v1/search/video-channels/',
175 '/api/v1/search/videos/'
176 ]
177
178 for (const path of paths) {
179 {
180 const customQuery = immutableAssign(query, { searchTarget: 'hello' })
181 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 })
182 }
183
184 {
185 const customQuery = immutableAssign(query, { searchTarget: undefined })
186 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 })
187 }
188
189 {
190 const customQuery = immutableAssign(query, { searchTarget: 'local' })
191 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 })
192 }
193
194 {
195 const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
196 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 })
197 }
198
199 await updateSearchIndex(server, true, true)
200
201 {
202 const customQuery = immutableAssign(query, { searchTarget: 'local' })
203 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 })
204 }
205
206 {
207 const customQuery = immutableAssign(query, { searchTarget: 'search-index' })
208 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 })
209 }
210
211 await updateSearchIndex(server, true, false)
212
213 {
214 const customQuery = immutableAssign(query, { searchTarget: 'local' })
215 await makeGetRequest({ url: server.url, path, query: customQuery, statusCodeExpected: HttpStatusCode.OK_200 })
216 }
217
218 await updateSearchIndex(server, false, false)
219 }
220 })
221 })
222
223 after(async function () {
224 await cleanupTests([ server ])
225 })
226 })