aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/search.ts
blob: 72ad6c842b2e568f2b87fb6bfb1f14f535ee46cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import {
  checkBadCountPagination,
  checkBadSortPagination,
  checkBadStartPagination,
  cleanupTests,
  createSingleServer,
  makeGetRequest,
  PeerTubeServer,
  setAccessTokensToServers
} from '@shared/extra-utils'
import { HttpStatusCode } from '@shared/models'

function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
  return server.config.updateCustomSubConfig({
    newConfig: {
      search: {
        searchIndex: {
          enabled,
          disableLocalSearch
        }
      }
    }
  })
}

describe('Test videos API validator', function () {
  let server: PeerTubeServer

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(30000)

    server = await createSingleServer(1)
    await setAccessTokensToServers([ server ])
  })

  describe('When searching videos', function () {
    const path = '/api/v1/search/videos/'

    const query = {
      search: 'coucou'
    }

    it('Should fail with a bad start pagination', async function () {
      await checkBadStartPagination(server.url, path, null, query)
    })

    it('Should fail with a bad count pagination', async function () {
      await checkBadCountPagination(server.url, path, null, query)
    })

    it('Should fail with an incorrect sort', async function () {
      await checkBadSortPagination(server.url, path, null, query)
    })

    it('Should succeed with the correct parameters', async function () {
      await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
    })

    it('Should fail with an invalid category', async function () {
      const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery2 = { ...query, categoryOneOf: 'a' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should succeed with a valid category', async function () {
      const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery2 = { ...query, categoryOneOf: 1 }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
    })

    it('Should fail with an invalid licence', async function () {
      const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery2 = { ...query, licenceOneOf: 'a' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should succeed with a valid licence', async function () {
      const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery2 = { ...query, licenceOneOf: 1 }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
    })

    it('Should succeed with a valid language', async function () {
      const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery2 = { ...query, languageOneOf: 'fr' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
    })

    it('Should succeed with valid tags', async function () {
      const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery2 = { ...query, tagsOneOf: 'tag1' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
      await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })

      const customQuery4 = { ...query, tagsAllOf: 'tag1' }
      await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
    })

    it('Should fail with invalid durations', async function () {
      const customQuery1 = { ...query, durationMin: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery2 = { ...query, durationMax: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should fail with invalid dates', async function () {
      const customQuery1 = { ...query, startDate: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery2 = { ...query, endDate: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

      const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
      await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should fail with an invalid host', async function () {
      const customQuery = { ...query, host: '6565' }
      await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should succeed with a host', async function () {
      const customQuery = { ...query, host: 'example.com' }
      await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
    })
  })

  describe('When searching video playlists', function () {
    const path = '/api/v1/search/video-playlists/'

    const query = {
      search: 'coucou',
      host: 'example.com'
    }

    it('Should fail with a bad start pagination', async function () {
      await checkBadStartPagination(server.url, path, null, query)
    })

    it('Should fail with a bad count pagination', async function () {
      await checkBadCountPagination(server.url, path, null, query)
    })

    it('Should fail with an incorrect sort', async function () {
      await checkBadSortPagination(server.url, path, null, query)
    })

    it('Should fail with an invalid host', async function () {
      await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should succeed with the correct parameters', async function () {
      await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
    })
  })

  describe('When searching video channels', function () {
    const path = '/api/v1/search/video-channels/'

    const query = {
      search: 'coucou',
      host: 'example.com'
    }

    it('Should fail with a bad start pagination', async function () {
      await checkBadStartPagination(server.url, path, null, query)
    })

    it('Should fail with a bad count pagination', async function () {
      await checkBadCountPagination(server.url, path, null, query)
    })

    it('Should fail with an incorrect sort', async function () {
      await checkBadSortPagination(server.url, path, null, query)
    })

    it('Should fail with an invalid host', async function () {
      await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should succeed with the correct parameters', async function () {
      await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
    })
  })

  describe('Search target', function () {

    it('Should fail/succeed depending on the search target', async function () {
      this.timeout(10000)

      const query = { search: 'coucou' }
      const paths = [
        '/api/v1/search/video-playlists/',
        '/api/v1/search/video-channels/',
        '/api/v1/search/videos/'
      ]

      for (const path of paths) {
        {
          const customQuery = { ...query, searchTarget: 'hello' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
        }

        {
          const customQuery = { ...query, searchTarget: undefined }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
        }

        {
          const customQuery = { ...query, searchTarget: 'local' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
        }

        {
          const customQuery = { ...query, searchTarget: 'search-index' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
        }

        await updateSearchIndex(server, true, true)

        {
          const customQuery = { ...query, searchTarget: 'local' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
        }

        {
          const customQuery = { ...query, searchTarget: 'search-index' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
        }

        await updateSearchIndex(server, true, false)

        {
          const customQuery = { ...query, searchTarget: 'local' }
          await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
        }

        await updateSearchIndex(server, false, false)
      }
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})