PeerTube/server/tests/api/check-params/search.ts

273 lines
11 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2018-07-20 07:35:18 -05:00
import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
2021-07-16 07:27:30 -05:00
import { HttpStatusCode } from '@shared/models'
import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
2018-07-20 07:35:18 -05:00
2021-07-16 02:47:51 -05:00
function updateSearchIndex (server: PeerTubeServer, enabled: boolean, disableLocalSearch = false) {
2021-07-16 02:04:35 -05:00
return server.config.updateCustomSubConfig({
2021-07-07 04:51:09 -05:00
newConfig: {
search: {
searchIndex: {
enabled,
disableLocalSearch
}
2020-06-09 09:39:45 -05:00
}
}
})
}
2018-07-20 07:35:18 -05:00
describe('Test videos API validator', function () {
2021-07-16 02:47:51 -05:00
let server: PeerTubeServer
2018-07-20 07:35:18 -05:00
// ---------------------------------------------------------------
before(async function () {
this.timeout(30000)
2021-07-16 02:47:51 -05:00
server = await createSingleServer(1)
2020-06-09 09:39:45 -05:00
await setAccessTokensToServers([ server ])
2018-07-20 07:35:18 -05:00
})
describe('When searching videos', function () {
2018-08-24 04:04:02 -05:00
const path = '/api/v1/search/videos/'
2018-07-20 07:35:18 -05:00
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)
})
2021-07-28 06:40:26 -05:00
it('Should succeed with the correct parameters', async function () {
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
})
it('Should fail with an invalid category', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, categoryOneOf: [ 'aa', 'b' ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, categoryOneOf: 'a' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
})
it('Should succeed with a valid category', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, categoryOneOf: [ 1, 7 ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, categoryOneOf: 1 }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
})
it('Should fail with an invalid licence', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, licenceOneOf: [ 'aa', 'b' ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, licenceOneOf: 'a' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
})
it('Should succeed with a valid licence', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, licenceOneOf: [ 1, 2 ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, licenceOneOf: 1 }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
})
it('Should succeed with a valid language', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, languageOneOf: [ 'fr', 'en' ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, languageOneOf: 'fr' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
})
it('Should succeed with valid tags', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, tagsOneOf: [ 'tag1', 'tag2' ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, tagsOneOf: 'tag1' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery3 = { ...query, tagsAllOf: [ 'tag1', 'tag2' ] }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery4 = { ...query, tagsAllOf: 'tag1' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.OK_200 })
2018-07-20 07:35:18 -05:00
})
it('Should fail with invalid durations', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, durationMin: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, durationMax: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
})
it('Should fail with invalid dates', async function () {
2021-07-13 02:43:59 -05:00
const customQuery1 = { ...query, startDate: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
2021-07-13 02:43:59 -05:00
const customQuery2 = { ...query, endDate: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery2, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-13 02:43:59 -05:00
const customQuery3 = { ...query, originallyPublishedStartDate: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery3, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2021-07-13 02:43:59 -05:00
const customQuery4 = { ...query, originallyPublishedEndDate: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery4, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2018-07-20 07:35:18 -05:00
})
2021-07-28 06:40:26 -05:00
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 })
})
2018-07-20 07:35:18 -05:00
})
2021-06-17 09:02:38 -05:00
describe('When searching video playlists', function () {
const path = '/api/v1/search/video-playlists/'
const query = {
2021-07-28 06:40:26 -05:00
search: 'coucou',
host: 'example.com'
2021-06-17 09:02:38 -05:00
}
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)
})
2021-07-28 06:40:26 -05:00
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 })
})
2021-07-28 06:40:26 -05:00
it('Should succeed with the correct parameters', async function () {
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
2021-06-17 09:02:38 -05:00
})
})
2018-08-24 04:04:02 -05:00
describe('When searching video channels', function () {
const path = '/api/v1/search/video-channels/'
const query = {
2021-07-28 06:40:26 -05:00
search: 'coucou',
host: 'example.com'
2018-08-24 04:04:02 -05:00
}
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)
})
2021-07-28 06:40:26 -05:00
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 })
})
2021-07-28 06:40:26 -05:00
it('Should succeed with the correct parameters', async function () {
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })
2018-08-24 04:04:02 -05:00
})
})
2020-06-09 09:39:45 -05:00
describe('Search target', function () {
it('Should fail/succeed depending on the search target', async function () {
const query = { search: 'coucou' }
const paths = [
2021-06-17 09:02:38 -05:00
'/api/v1/search/video-playlists/',
2020-06-09 09:39:45 -05:00
'/api/v1/search/video-channels/',
'/api/v1/search/videos/'
]
for (const path of paths) {
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: 'hello' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2020-06-09 09:39:45 -05:00
}
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: undefined }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
2020-06-09 09:39:45 -05:00
}
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: 'local' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
2020-06-09 09:39:45 -05:00
}
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: 'search-index' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2020-06-09 09:39:45 -05:00
}
await updateSearchIndex(server, true, true)
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: 'search-index' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
2020-06-09 09:39:45 -05:00
}
await updateSearchIndex(server, true, false)
{
2021-07-13 02:43:59 -05:00
const customQuery = { ...query, searchTarget: 'local' }
2021-07-16 03:42:24 -05:00
await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })
2020-06-09 09:39:45 -05:00
}
await updateSearchIndex(server, false, false)
}
})
})
2019-04-24 08:10:37 -05:00
after(async function () {
await cleanupTests([ server ])
2018-07-20 07:35:18 -05:00
})
})