1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode, VideoCreateResult } from '@shared/models'
15 setAccessTokensToServers,
17 } from '@shared/server-commands'
19 describe('Test server redundancy API validators', function () {
20 let servers: PeerTubeServer[]
21 let userAccessToken = null
22 let videoIdLocal: number
23 let videoRemote: VideoCreateResult
25 // ---------------------------------------------------------------
27 before(async function () {
30 servers = await createMultipleServers(2)
32 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
40 await servers[0].users.create({ username: user.username, password: user.password })
41 userAccessToken = await servers[0].login.getAccessToken(user)
43 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
45 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
47 await waitJobs(servers)
49 videoRemote = await servers[0].videos.get({ id: remoteUUID })
52 describe('When listing redundancies', function () {
53 const path = '/api/v1/server/redundancy/videos'
60 token = servers[0].accessToken
63 it('Should fail with an invalid token', async function () {
64 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
67 it('Should fail if the user is not an administrator', async function () {
68 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
71 it('Should fail with a bad start pagination', async function () {
72 await checkBadStartPagination(url, path, servers[0].accessToken)
75 it('Should fail with a bad count pagination', async function () {
76 await checkBadCountPagination(url, path, servers[0].accessToken)
79 it('Should fail with an incorrect sort', async function () {
80 await checkBadSortPagination(url, path, servers[0].accessToken)
83 it('Should fail with a bad target', async function () {
84 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
87 it('Should fail without target', async function () {
88 await makeGetRequest({ url, path, token })
91 it('Should succeed with the correct params', async function () {
92 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
96 describe('When manually adding a redundancy', function () {
97 const path = '/api/v1/server/redundancy/videos'
104 token = servers[0].accessToken
107 it('Should fail with an invalid token', async function () {
108 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
111 it('Should fail if the user is not an administrator', async function () {
112 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
115 it('Should fail without a video id', async function () {
116 await makePostBodyRequest({ url, path, token })
119 it('Should fail with an incorrect video id', async function () {
120 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
123 it('Should fail with a not found video id', async function () {
124 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
127 it('Should fail with a local a video id', async function () {
128 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
131 it('Should succeed with the correct params', async function () {
132 await makePostBodyRequest({
136 fields: { videoId: videoRemote.shortUUID },
137 expectedStatus: HttpStatusCode.NO_CONTENT_204
141 it('Should fail if the video is already duplicated', async function () {
144 await waitJobs(servers)
146 await makePostBodyRequest({
150 fields: { videoId: videoRemote.uuid },
151 expectedStatus: HttpStatusCode.CONFLICT_409
156 describe('When manually removing a redundancy', function () {
157 const path = '/api/v1/server/redundancy/videos/'
164 token = servers[0].accessToken
167 it('Should fail with an invalid token', async function () {
168 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
171 it('Should fail if the user is not an administrator', async function () {
172 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
175 it('Should fail with an incorrect video id', async function () {
176 await makeDeleteRequest({ url, path: path + 'toto', token })
179 it('Should fail with a not found video redundancy', async function () {
180 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
184 describe('When updating server redundancy', function () {
185 const path = '/api/v1/server/redundancy'
187 it('Should fail with an invalid token', async function () {
188 await makePutBodyRequest({
190 path: path + '/localhost:' + servers[1].port,
191 fields: { redundancyAllowed: true },
193 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
197 it('Should fail if the user is not an administrator', async function () {
198 await makePutBodyRequest({
200 path: path + '/localhost:' + servers[1].port,
201 fields: { redundancyAllowed: true },
202 token: userAccessToken,
203 expectedStatus: HttpStatusCode.FORBIDDEN_403
207 it('Should fail if we do not follow this server', async function () {
208 await makePutBodyRequest({
210 path: path + '/example.com',
211 fields: { redundancyAllowed: true },
212 token: servers[0].accessToken,
213 expectedStatus: HttpStatusCode.NOT_FOUND_404
217 it('Should fail without de redundancyAllowed param', async function () {
218 await makePutBodyRequest({
220 path: path + '/localhost:' + servers[1].port,
221 fields: { blabla: true },
222 token: servers[0].accessToken,
223 expectedStatus: HttpStatusCode.BAD_REQUEST_400
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
230 path: path + '/localhost:' + servers[1].port,
231 fields: { redundancyAllowed: true },
232 token: servers[0].accessToken,
233 expectedStatus: HttpStatusCode.NO_CONTENT_204
238 after(async function () {
239 await cleanupTests(servers)