]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/check-params/search.ts
Improve remote runner config UX
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / search.ts
index 7b7e07784f96d965f075faa834b86fbb0146c48a..72c25ba331e735bf81c9778d9c9754867b608cad 100644 (file)
@@ -1,25 +1,32 @@
-/* tslint:disable:no-unused-expression */
-
-import 'mocha'
-
-import { flushTests, immutableAssign, killallServers, makeGetRequest, runServer, ServerInfo } from '../../../../shared/utils'
-import {
-  checkBadCountPagination,
-  checkBadSortPagination,
-  checkBadStartPagination
-} from '../../../../shared/utils/requests/check-api-params'
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
+
+import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
+import { HttpStatusCode } from '@shared/models'
+import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
+
+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: ServerInfo
+  let server: PeerTubeServer
 
   // ---------------------------------------------------------------
 
   before(async function () {
     this.timeout(30000)
 
-    await flushTests()
-
-    server = await runServer(1)
+    server = await createSingleServer(1)
+    await setAccessTokensToServers([ server ])
   })
 
   describe('When searching videos', function () {
@@ -41,84 +48,138 @@ describe('Test videos API validator', function () {
       await checkBadSortPagination(server.url, path, null, query)
     })
 
-    it('Should success with the correct parameters', async function () {
-      await makeGetRequest({ url: server.url, path, query, statusCodeExpected: 200 })
+    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 = immutableAssign(query, { categoryOneOf: [ 'aa', 'b' ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
+      const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
 
-      const customQuery2 = immutableAssign(query, { categoryOneOf: 'a' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { categoryOneOf: [ 1, 7 ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
+      const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery2 = immutableAssign(query, { categoryOneOf: 1 })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { licenceOneOf: [ 'aa', 'b' ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
+      const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
 
-      const customQuery2 = immutableAssign(query, { licenceOneOf: 'a' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { licenceOneOf: [ 1, 2 ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
+      const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery2 = immutableAssign(query, { licenceOneOf: 1 })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { languageOneOf: [ 'fr', 'en' ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
+      const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery2 = immutableAssign(query, { languageOneOf: 'fr' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { tagsOneOf: [ 'tag1', 'tag2' ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 200 })
+      const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery2 = immutableAssign(query, { tagsOneOf: 'tag1' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 200 })
+      const customQuery2 = { ...query, tagsOneOf: 'tag1' }
+      await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery3 = immutableAssign(query, { tagsAllOf: [ 'tag1', 'tag2' ] })
-      await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: 200 })
+      const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
 
-      const customQuery4 = immutableAssign(query, { tagsAllOf: 'tag1' })
-      await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: 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 = immutableAssign(query, { durationMin: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
+      const customQuery1 = { ...query, durationMin: 'hello' }
+      await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
 
-      const customQuery2 = immutableAssign(query, { durationMax: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 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 = immutableAssign(query, { startDate: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery1, statusCodeExpected: 400 })
+      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 customQuery2 = immutableAssign(query, { endDate: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery2, statusCodeExpected: 400 })
+      const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
+      await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
 
-      const customQuery3 = immutableAssign(query, { originallyPublishedStartDate: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery3, statusCodeExpected: 400 })
+      const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
+      await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
+    })
 
-      const customQuery4 = immutableAssign(query, { originallyPublishedEndDate: 'hello' })
-      await makeGetRequest({ url: server.url, path, query: customQuery4, statusCodeExpected: 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 })
+    })
+
+    it('Should fail with invalid uuids', async function () {
+      const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
+    })
+
+    it('Should succeed with valid uuids', async function () {
+      const customQuery = { ...query, uuids: [ 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
+      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 fail with invalid uuids', async function () {
+      const customQuery = { ...query, uuids: [ '6565', 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }
+      await makeGetRequest({ url: server.url, path, query: customQuery, 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 })
     })
   })
 
@@ -126,7 +187,8 @@ describe('Test videos API validator', function () {
     const path = '/api/v1/search/video-channels/'
 
     const query = {
-      search: 'coucou'
+      search: 'coucou',
+      host: 'example.com'
     }
 
     it('Should fail with a bad start pagination', async function () {
@@ -141,17 +203,72 @@ describe('Test videos API validator', function () {
       await checkBadSortPagination(server.url, path, null, query)
     })
 
-    it('Should success with the correct parameters', async function () {
-      await makeGetRequest({ url: server.url, path, query, statusCodeExpected: 200 })
+    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 fail with invalid handles', async function () {
+      await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, 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 })
     })
   })
 
-  after(async function () {
-    killallServers([ server ])
+  describe('Search target', function () {
 
-    // Keep the logs if the test failed
-    if (this['ok']) {
-      await flushTests()
-    }
+    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: '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 ])
   })
 })