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 flushAndRunMultipleServers,
19 setAccessTokensToServers,
22 } from '../../../../shared/extra-utils'
24 describe('Test server redundancy API validators', function () {
25 let servers: ServerInfo[]
26 let userAccessToken = null
27 let videoIdLocal: number
28 let videoRemote: VideoCreateResult
30 // ---------------------------------------------------------------
32 before(async function () {
35 servers = await flushAndRunMultipleServers(2)
37 await setAccessTokensToServers(servers)
38 await doubleFollow(servers[0], servers[1])
45 await servers[0].usersCommand.create({ username: user.username, password: user.password })
46 userAccessToken = await servers[0].loginCommand.getAccessToken(user)
48 videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id
50 const remoteUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video' })).uuid
52 await waitJobs(servers)
54 const resVideo = await getVideo(servers[0].url, remoteUUID)
55 videoRemote = resVideo.body
58 describe('When listing redundancies', function () {
59 const path = '/api/v1/server/redundancy/videos'
66 token = servers[0].accessToken
69 it('Should fail with an invalid token', async function () {
70 await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
73 it('Should fail if the user is not an administrator', async function () {
74 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
77 it('Should fail with a bad start pagination', async function () {
78 await checkBadStartPagination(url, path, servers[0].accessToken)
81 it('Should fail with a bad count pagination', async function () {
82 await checkBadCountPagination(url, path, servers[0].accessToken)
85 it('Should fail with an incorrect sort', async function () {
86 await checkBadSortPagination(url, path, servers[0].accessToken)
89 it('Should fail with a bad target', async function () {
90 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
93 it('Should fail without target', async function () {
94 await makeGetRequest({ url, path, token })
97 it('Should succeed with the correct params', async function () {
98 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 })
102 describe('When manually adding a redundancy', function () {
103 const path = '/api/v1/server/redundancy/videos'
110 token = servers[0].accessToken
113 it('Should fail with an invalid token', async function () {
114 await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
117 it('Should fail if the user is not an administrator', async function () {
118 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
121 it('Should fail without a video id', async function () {
122 await makePostBodyRequest({ url, path, token })
125 it('Should fail with an incorrect video id', async function () {
126 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
129 it('Should fail with a not found video id', async function () {
130 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
133 it('Should fail with a local a video id', async function () {
134 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
137 it('Should succeed with the correct params', async function () {
138 await makePostBodyRequest({
142 fields: { videoId: videoRemote.shortUUID },
143 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
147 it('Should fail if the video is already duplicated', async function () {
150 await waitJobs(servers)
152 await makePostBodyRequest({
156 fields: { videoId: videoRemote.uuid },
157 statusCodeExpected: HttpStatusCode.CONFLICT_409
162 describe('When manually removing a redundancy', function () {
163 const path = '/api/v1/server/redundancy/videos/'
170 token = servers[0].accessToken
173 it('Should fail with an invalid token', async function () {
174 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
177 it('Should fail if the user is not an administrator', async function () {
178 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
181 it('Should fail with an incorrect video id', async function () {
182 await makeDeleteRequest({ url, path: path + 'toto', token })
185 it('Should fail with a not found video redundancy', async function () {
186 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
190 describe('When updating server redundancy', function () {
191 const path = '/api/v1/server/redundancy'
193 it('Should fail with an invalid token', async function () {
194 await makePutBodyRequest({
196 path: path + '/localhost:' + servers[1].port,
197 fields: { redundancyAllowed: true },
199 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
203 it('Should fail if the user is not an administrator', async function () {
204 await makePutBodyRequest({
206 path: path + '/localhost:' + servers[1].port,
207 fields: { redundancyAllowed: true },
208 token: userAccessToken,
209 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
213 it('Should fail if we do not follow this server', async function () {
214 await makePutBodyRequest({
216 path: path + '/example.com',
217 fields: { redundancyAllowed: true },
218 token: servers[0].accessToken,
219 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
223 it('Should fail without de redundancyAllowed param', async function () {
224 await makePutBodyRequest({
226 path: path + '/localhost:' + servers[1].port,
227 fields: { blabla: true },
228 token: servers[0].accessToken,
229 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
233 it('Should succeed with the correct parameters', async function () {
234 await makePutBodyRequest({
236 path: path + '/localhost:' + servers[1].port,
237 fields: { redundancyAllowed: true },
238 token: servers[0].accessToken,
239 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
244 after(async function () {
245 await cleanupTests(servers)