1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { VideoCreateResult } from '@shared/models'
5 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
12 createMultipleServers,
18 setAccessTokensToServers,
20 } from '../../../../shared/extra-utils'
22 describe('Test server redundancy API validators', function () {
23 let servers: PeerTubeServer[]
24 let userAccessToken = null
25 let videoIdLocal: number
26 let videoRemote: VideoCreateResult
28 // ---------------------------------------------------------------
30 before(async function () {
33 servers = await createMultipleServers(2)
35 await setAccessTokensToServers(servers)
36 await doubleFollow(servers[0], servers[1])
43 await servers[0].users.create({ username: user.username, password: user.password })
44 userAccessToken = await servers[0].login.getAccessToken(user)
46 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
48 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
50 await waitJobs(servers)
52 videoRemote = await servers[0].videos.get({ id: remoteUUID })
55 describe('When listing redundancies', function () {
56 const path = '/api/v1/server/redundancy/videos'
63 token = servers[0].accessToken
66 it('Should fail with an invalid token', async function () {
67 await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
70 it('Should fail if the user is not an administrator', async function () {
71 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
74 it('Should fail with a bad start pagination', async function () {
75 await checkBadStartPagination(url, path, servers[0].accessToken)
78 it('Should fail with a bad count pagination', async function () {
79 await checkBadCountPagination(url, path, servers[0].accessToken)
82 it('Should fail with an incorrect sort', async function () {
83 await checkBadSortPagination(url, path, servers[0].accessToken)
86 it('Should fail with a bad target', async function () {
87 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
90 it('Should fail without target', async function () {
91 await makeGetRequest({ url, path, token })
94 it('Should succeed with the correct params', async function () {
95 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 })
99 describe('When manually adding a redundancy', function () {
100 const path = '/api/v1/server/redundancy/videos'
107 token = servers[0].accessToken
110 it('Should fail with an invalid token', async function () {
111 await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
114 it('Should fail if the user is not an administrator', async function () {
115 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
118 it('Should fail without a video id', async function () {
119 await makePostBodyRequest({ url, path, token })
122 it('Should fail with an incorrect video id', async function () {
123 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
126 it('Should fail with a not found video id', async function () {
127 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
130 it('Should fail with a local a video id', async function () {
131 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
134 it('Should succeed with the correct params', async function () {
135 await makePostBodyRequest({
139 fields: { videoId: videoRemote.shortUUID },
140 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
144 it('Should fail if the video is already duplicated', async function () {
147 await waitJobs(servers)
149 await makePostBodyRequest({
153 fields: { videoId: videoRemote.uuid },
154 statusCodeExpected: HttpStatusCode.CONFLICT_409
159 describe('When manually removing a redundancy', function () {
160 const path = '/api/v1/server/redundancy/videos/'
167 token = servers[0].accessToken
170 it('Should fail with an invalid token', async function () {
171 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
174 it('Should fail if the user is not an administrator', async function () {
175 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
178 it('Should fail with an incorrect video id', async function () {
179 await makeDeleteRequest({ url, path: path + 'toto', token })
182 it('Should fail with a not found video redundancy', async function () {
183 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
187 describe('When updating server redundancy', function () {
188 const path = '/api/v1/server/redundancy'
190 it('Should fail with an invalid token', async function () {
191 await makePutBodyRequest({
193 path: path + '/localhost:' + servers[1].port,
194 fields: { redundancyAllowed: true },
196 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
200 it('Should fail if the user is not an administrator', async function () {
201 await makePutBodyRequest({
203 path: path + '/localhost:' + servers[1].port,
204 fields: { redundancyAllowed: true },
205 token: userAccessToken,
206 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
210 it('Should fail if we do not follow this server', async function () {
211 await makePutBodyRequest({
213 path: path + '/example.com',
214 fields: { redundancyAllowed: true },
215 token: servers[0].accessToken,
216 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
220 it('Should fail without de redundancyAllowed param', async function () {
221 await makePutBodyRequest({
223 path: path + '/localhost:' + servers[1].port,
224 fields: { blabla: true },
225 token: servers[0].accessToken,
226 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
230 it('Should succeed with the correct parameters', async function () {
231 await makePutBodyRequest({
233 path: path + '/localhost:' + servers[1].port,
234 fields: { redundancyAllowed: true },
235 token: servers[0].accessToken,
236 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
241 after(async function () {
242 await cleanupTests(servers)