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,
13 flushAndRunMultipleServers,
20 setAccessTokensToServers,
24 } from '../../../../shared/extra-utils'
26 describe('Test server redundancy API validators', function () {
27 let servers: ServerInfo[]
28 let userAccessToken = null
29 let videoIdLocal: number
30 let videoRemote: VideoCreateResult
32 // ---------------------------------------------------------------
34 before(async function () {
37 servers = await flushAndRunMultipleServers(2)
39 await setAccessTokensToServers(servers)
40 await doubleFollow(servers[0], servers[1])
47 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
48 userAccessToken = await userLogin(servers[0], user)
50 videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id
52 const remoteUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video' })).uuid
54 await waitJobs(servers)
56 const resVideo = await getVideo(servers[0].url, remoteUUID)
57 videoRemote = resVideo.body
60 describe('When listing redundancies', function () {
61 const path = '/api/v1/server/redundancy/videos'
68 token = servers[0].accessToken
71 it('Should fail with an invalid token', async function () {
72 await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
75 it('Should fail if the user is not an administrator', async function () {
76 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
79 it('Should fail with a bad start pagination', async function () {
80 await checkBadStartPagination(url, path, servers[0].accessToken)
83 it('Should fail with a bad count pagination', async function () {
84 await checkBadCountPagination(url, path, servers[0].accessToken)
87 it('Should fail with an incorrect sort', async function () {
88 await checkBadSortPagination(url, path, servers[0].accessToken)
91 it('Should fail with a bad target', async function () {
92 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
95 it('Should fail without target', async function () {
96 await makeGetRequest({ url, path, token })
99 it('Should succeed with the correct params', async function () {
100 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 })
104 describe('When manually adding a redundancy', function () {
105 const path = '/api/v1/server/redundancy/videos'
112 token = servers[0].accessToken
115 it('Should fail with an invalid token', async function () {
116 await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
119 it('Should fail if the user is not an administrator', async function () {
120 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
123 it('Should fail without a video id', async function () {
124 await makePostBodyRequest({ url, path, token })
127 it('Should fail with an incorrect video id', async function () {
128 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
131 it('Should fail with a not found video id', async function () {
132 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
135 it('Should fail with a local a video id', async function () {
136 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
139 it('Should succeed with the correct params', async function () {
140 await makePostBodyRequest({
144 fields: { videoId: videoRemote.shortUUID },
145 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
149 it('Should fail if the video is already duplicated', async function () {
152 await waitJobs(servers)
154 await makePostBodyRequest({
158 fields: { videoId: videoRemote.uuid },
159 statusCodeExpected: HttpStatusCode.CONFLICT_409
164 describe('When manually removing a redundancy', function () {
165 const path = '/api/v1/server/redundancy/videos/'
172 token = servers[0].accessToken
175 it('Should fail with an invalid token', async function () {
176 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
179 it('Should fail if the user is not an administrator', async function () {
180 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
183 it('Should fail with an incorrect video id', async function () {
184 await makeDeleteRequest({ url, path: path + 'toto', token })
187 it('Should fail with a not found video redundancy', async function () {
188 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
192 describe('When updating server redundancy', function () {
193 const path = '/api/v1/server/redundancy'
195 it('Should fail with an invalid token', async function () {
196 await makePutBodyRequest({
198 path: path + '/localhost:' + servers[1].port,
199 fields: { redundancyAllowed: true },
201 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
205 it('Should fail if the user is not an administrator', async function () {
206 await makePutBodyRequest({
208 path: path + '/localhost:' + servers[1].port,
209 fields: { redundancyAllowed: true },
210 token: userAccessToken,
211 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
215 it('Should fail if we do not follow this server', async function () {
216 await makePutBodyRequest({
218 path: path + '/example.com',
219 fields: { redundancyAllowed: true },
220 token: servers[0].accessToken,
221 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
225 it('Should fail without de redundancyAllowed param', async function () {
226 await makePutBodyRequest({
228 path: path + '/localhost:' + servers[1].port,
229 fields: { blabla: true },
230 token: servers[0].accessToken,
231 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
235 it('Should succeed with the correct parameters', async function () {
236 await makePutBodyRequest({
238 path: path + '/localhost:' + servers[1].port,
239 fields: { redundancyAllowed: true },
240 token: servers[0].accessToken,
241 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
246 after(async function () {
247 await cleanupTests(servers)