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

373 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 */
2017-11-17 08:20:42 -06:00
import 'mocha'
import {
2021-07-16 03:42:24 -05:00
checkBadCountPagination,
checkBadSortPagination,
checkBadStartPagination,
2019-04-24 08:10:37 -05:00
cleanupTests,
2021-07-16 02:47:51 -05:00
createSingleServer,
2021-07-16 03:42:24 -05:00
makeDeleteRequest,
makeGetRequest,
2019-04-24 08:10:37 -05:00
makePostBodyRequest,
2021-07-16 02:47:51 -05:00
PeerTubeServer,
2021-07-13 04:05:15 -05:00
setAccessTokensToServers
2021-07-16 03:42:24 -05:00
} from '@shared/extra-utils'
import { HttpStatusCode } from '@shared/models'
2017-11-17 08:20:42 -06:00
describe('Test server follows API validators', function () {
2021-07-16 02:47:51 -05:00
let server: PeerTubeServer
2017-11-17 08:20:42 -06:00
// ---------------------------------------------------------------
before(async function () {
2018-01-18 11:10:45 -06:00
this.timeout(30000)
2017-11-17 08:20:42 -06:00
2021-07-16 02:47:51 -05:00
server = await createSingleServer(1)
2017-11-17 08:20:42 -06:00
await setAccessTokensToServers([ server ])
})
describe('When managing following', function () {
let userAccessToken = null
before(async function () {
userAccessToken = await server.users.generateUserAndToken('user1')
2017-11-17 08:20:42 -06:00
})
describe('When adding follows', function () {
const path = '/api/v1/server/following'
it('Should fail with nothing', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail if hosts is not composed by hosts', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
2017-12-28 07:29:57 -06:00
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail if hosts is composed with http schemes', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
2017-12-28 07:29:57 -06:00
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail if hosts are not unique', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
2017-12-28 07:29:57 -06:00
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail if handles is not composed by handles', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { handles: [ 'hello@example.com', 'localhost:9001' ] },
token: server.accessToken,
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
it('Should fail if handles are not unique', async function () {
await makePostBodyRequest({
url: server.url,
path,
fields: { urls: [ 'hello@example.com', 'hello@example.com' ] },
2017-12-28 07:29:57 -06:00
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail with an invalid token', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { hosts: [ 'localhost:9002' ] },
token: 'fake_token',
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
it('Should fail if the user is not an administrator', async function () {
2017-12-28 07:29:57 -06:00
await makePostBodyRequest({
url: server.url,
path,
fields: { hosts: [ 'localhost:9002' ] },
token: userAccessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.FORBIDDEN_403
2017-12-28 07:29:57 -06:00
})
2017-11-17 08:20:42 -06:00
})
})
describe('When listing followings', function () {
const path = '/api/v1/server/following'
it('Should fail with a bad start pagination', async function () {
2017-12-28 07:29:57 -06:00
await checkBadStartPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with a bad count pagination', async function () {
2017-12-28 07:29:57 -06:00
await checkBadCountPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with an incorrect sort', async function () {
2017-12-28 07:29:57 -06:00
await checkBadSortPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with an incorrect state', async function () {
await makeGetRequest({
url: server.url,
path,
query: {
state: 'blabla'
}
})
})
it('Should fail with an incorrect actor type', async function () {
await makeGetRequest({
url: server.url,
path,
query: {
actorType: 'blabla'
}
})
})
it('Should fail succeed with the correct params', async function () {
await makeGetRequest({
url: server.url,
path,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.OK_200,
query: {
state: 'accepted',
actorType: 'Application'
}
})
})
2017-11-17 08:20:42 -06:00
})
describe('When listing followers', function () {
const path = '/api/v1/server/followers'
it('Should fail with a bad start pagination', async function () {
2017-12-28 07:29:57 -06:00
await checkBadStartPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with a bad count pagination', async function () {
2017-12-28 07:29:57 -06:00
await checkBadCountPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with an incorrect sort', async function () {
2017-12-28 07:29:57 -06:00
await checkBadSortPagination(server.url, path)
2017-11-17 08:20:42 -06:00
})
it('Should fail with an incorrect actor type', async function () {
await makeGetRequest({
url: server.url,
path,
query: {
actorType: 'blabla'
}
})
})
it('Should fail with an incorrect state', async function () {
await makeGetRequest({
url: server.url,
path,
query: {
state: 'blabla',
actorType: 'Application'
}
})
})
it('Should fail succeed with the correct params', async function () {
await makeGetRequest({
url: server.url,
path,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.OK_200,
query: {
state: 'accepted'
}
})
})
2017-11-17 08:20:42 -06:00
})
2019-04-08 07:04:57 -05:00
describe('When removing a follower', function () {
const path = '/api/v1/server/followers'
it('Should fail with an invalid token', async function () {
await makeDeleteRequest({
url: server.url,
path: path + '/toto@localhost:9002',
token: 'fake_token',
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2019-04-08 07:04:57 -05:00
})
})
it('Should fail if the user is not an administrator', async function () {
await makeDeleteRequest({
url: server.url,
path: path + '/toto@localhost:9002',
token: userAccessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.FORBIDDEN_403
2019-04-08 07:04:57 -05:00
})
})
it('Should fail with an invalid follower', async function () {
await makeDeleteRequest({
url: server.url,
path: path + '/toto',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
2019-04-08 07:04:57 -05:00
})
})
it('Should fail with an unknown follower', async function () {
await makeDeleteRequest({
url: server.url,
path: path + '/toto@localhost:9003',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.NOT_FOUND_404
2019-04-08 07:04:57 -05:00
})
})
})
describe('When accepting a follower', function () {
const path = '/api/v1/server/followers'
it('Should fail with an invalid token', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9002/accept',
token: 'fake_token',
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.UNAUTHORIZED_401
})
})
it('Should fail if the user is not an administrator', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9002/accept',
token: userAccessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.FORBIDDEN_403
})
})
it('Should fail with an invalid follower', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto/accept',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
it('Should fail with an unknown follower', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9003/accept',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.NOT_FOUND_404
})
})
})
describe('When rejecting a follower', function () {
const path = '/api/v1/server/followers'
it('Should fail with an invalid token', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9002/reject',
token: 'fake_token',
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.UNAUTHORIZED_401
})
})
it('Should fail if the user is not an administrator', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9002/reject',
token: userAccessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.FORBIDDEN_403
})
})
it('Should fail with an invalid follower', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto/reject',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.BAD_REQUEST_400
})
})
it('Should fail with an unknown follower', async function () {
await makePostBodyRequest({
url: server.url,
path: path + '/toto@localhost:9003/reject',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.NOT_FOUND_404
})
})
})
2017-11-17 08:20:42 -06:00
describe('When removing following', function () {
2017-11-21 06:43:29 -06:00
const path = '/api/v1/server/following'
it('Should fail with an invalid token', async function () {
2017-12-28 07:29:57 -06:00
await makeDeleteRequest({
url: server.url,
path: path + '/localhost:9002',
token: 'fake_token',
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2017-12-28 07:29:57 -06:00
})
2017-11-21 06:43:29 -06:00
})
it('Should fail if the user is not an administrator', async function () {
2017-12-28 07:29:57 -06:00
await makeDeleteRequest({
url: server.url,
path: path + '/localhost:9002',
token: userAccessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.FORBIDDEN_403
2017-12-28 07:29:57 -06:00
})
})
it('Should fail if we do not follow this server', async function () {
await makeDeleteRequest({
url: server.url,
path: path + '/example.com',
token: server.accessToken,
2021-07-16 03:42:24 -05:00
expectedStatus: HttpStatusCode.NOT_FOUND_404
2017-12-28 07:29:57 -06:00
})
})
2017-11-17 08:20:42 -06:00
})
})
2019-04-24 08:10:37 -05:00
after(async function () {
await cleanupTests([ server ])
2017-11-17 08:20:42 -06:00
})
})