1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
4 import { HttpStatusCode, VideoCreateResult } from '@shared/models'
14 setAccessTokensToServers,
16 } from '@shared/server-commands'
18 describe('Test server redundancy API validators', function () {
19 let servers: PeerTubeServer[]
20 let userAccessToken = null
21 let videoIdLocal: number
22 let videoRemote: VideoCreateResult
24 // ---------------------------------------------------------------
26 before(async function () {
29 servers = await createMultipleServers(2)
31 await setAccessTokensToServers(servers)
32 await doubleFollow(servers[0], servers[1])
39 await servers[0].users.create({ username: user.username, password: user.password })
40 userAccessToken = await servers[0].login.getAccessToken(user)
42 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
44 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
46 await waitJobs(servers)
48 videoRemote = await servers[0].videos.get({ id: remoteUUID })
51 describe('When listing redundancies', function () {
52 const path = '/api/v1/server/redundancy/videos'
59 token = servers[0].accessToken
62 it('Should fail with an invalid token', async function () {
63 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
66 it('Should fail if the user is not an administrator', async function () {
67 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
70 it('Should fail with a bad start pagination', async function () {
71 await checkBadStartPagination(url, path, servers[0].accessToken)
74 it('Should fail with a bad count pagination', async function () {
75 await checkBadCountPagination(url, path, servers[0].accessToken)
78 it('Should fail with an incorrect sort', async function () {
79 await checkBadSortPagination(url, path, servers[0].accessToken)
82 it('Should fail with a bad target', async function () {
83 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
86 it('Should fail without target', async function () {
87 await makeGetRequest({ url, path, token })
90 it('Should succeed with the correct params', async function () {
91 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
95 describe('When manually adding a redundancy', function () {
96 const path = '/api/v1/server/redundancy/videos'
103 token = servers[0].accessToken
106 it('Should fail with an invalid token', async function () {
107 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
110 it('Should fail if the user is not an administrator', async function () {
111 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
114 it('Should fail without a video id', async function () {
115 await makePostBodyRequest({ url, path, token })
118 it('Should fail with an incorrect video id', async function () {
119 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
122 it('Should fail with a not found video id', async function () {
123 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
126 it('Should fail with a local a video id', async function () {
127 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
130 it('Should succeed with the correct params', async function () {
131 await makePostBodyRequest({
135 fields: { videoId: videoRemote.shortUUID },
136 expectedStatus: HttpStatusCode.NO_CONTENT_204
140 it('Should fail if the video is already duplicated', async function () {
143 await waitJobs(servers)
145 await makePostBodyRequest({
149 fields: { videoId: videoRemote.uuid },
150 expectedStatus: HttpStatusCode.CONFLICT_409
155 describe('When manually removing a redundancy', function () {
156 const path = '/api/v1/server/redundancy/videos/'
163 token = servers[0].accessToken
166 it('Should fail with an invalid token', async function () {
167 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
170 it('Should fail if the user is not an administrator', async function () {
171 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
174 it('Should fail with an incorrect video id', async function () {
175 await makeDeleteRequest({ url, path: path + 'toto', token })
178 it('Should fail with a not found video redundancy', async function () {
179 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
183 describe('When updating server redundancy', function () {
184 const path = '/api/v1/server/redundancy'
186 it('Should fail with an invalid token', async function () {
187 await makePutBodyRequest({
189 path: path + '/' + servers[1].host,
190 fields: { redundancyAllowed: true },
192 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
196 it('Should fail if the user is not an administrator', async function () {
197 await makePutBodyRequest({
199 path: path + '/' + servers[1].host,
200 fields: { redundancyAllowed: true },
201 token: userAccessToken,
202 expectedStatus: HttpStatusCode.FORBIDDEN_403
206 it('Should fail if we do not follow this server', async function () {
207 await makePutBodyRequest({
209 path: path + '/example.com',
210 fields: { redundancyAllowed: true },
211 token: servers[0].accessToken,
212 expectedStatus: HttpStatusCode.NOT_FOUND_404
216 it('Should fail without de redundancyAllowed param', async function () {
217 await makePutBodyRequest({
219 path: path + '/' + servers[1].host,
220 fields: { blabla: true },
221 token: servers[0].accessToken,
222 expectedStatus: HttpStatusCode.BAD_REQUEST_400
226 it('Should succeed with the correct parameters', async function () {
227 await makePutBodyRequest({
229 path: path + '/' + servers[1].host,
230 fields: { redundancyAllowed: true },
231 token: servers[0].accessToken,
232 expectedStatus: HttpStatusCode.NO_CONTENT_204
237 after(async function () {
238 await cleanupTests(servers)