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,
23 } from '../../../../shared/extra-utils'
25 describe('Test server redundancy API validators', function () {
26 let servers: ServerInfo[]
27 let userAccessToken = null
28 let videoIdLocal: number
29 let videoRemote: VideoCreateResult
31 // ---------------------------------------------------------------
33 before(async function () {
36 servers = await flushAndRunMultipleServers(2)
38 await setAccessTokensToServers(servers)
39 await doubleFollow(servers[0], servers[1])
46 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
47 userAccessToken = await servers[0].loginCommand.getAccessToken(user)
49 videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id
51 const remoteUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video' })).uuid
53 await waitJobs(servers)
55 const resVideo = await getVideo(servers[0].url, remoteUUID)
56 videoRemote = resVideo.body
59 describe('When listing redundancies', function () {
60 const path = '/api/v1/server/redundancy/videos'
67 token = servers[0].accessToken
70 it('Should fail with an invalid token', async function () {
71 await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
74 it('Should fail if the user is not an administrator', async function () {
75 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
78 it('Should fail with a bad start pagination', async function () {
79 await checkBadStartPagination(url, path, servers[0].accessToken)
82 it('Should fail with a bad count pagination', async function () {
83 await checkBadCountPagination(url, path, servers[0].accessToken)
86 it('Should fail with an incorrect sort', async function () {
87 await checkBadSortPagination(url, path, servers[0].accessToken)
90 it('Should fail with a bad target', async function () {
91 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
94 it('Should fail without target', async function () {
95 await makeGetRequest({ url, path, token })
98 it('Should succeed with the correct params', async function () {
99 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 })
103 describe('When manually adding a redundancy', function () {
104 const path = '/api/v1/server/redundancy/videos'
111 token = servers[0].accessToken
114 it('Should fail with an invalid token', async function () {
115 await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
118 it('Should fail if the user is not an administrator', async function () {
119 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
122 it('Should fail without a video id', async function () {
123 await makePostBodyRequest({ url, path, token })
126 it('Should fail with an incorrect video id', async function () {
127 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
130 it('Should fail with a not found video id', async function () {
131 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
134 it('Should fail with a local a video id', async function () {
135 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
138 it('Should succeed with the correct params', async function () {
139 await makePostBodyRequest({
143 fields: { videoId: videoRemote.shortUUID },
144 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
148 it('Should fail if the video is already duplicated', async function () {
151 await waitJobs(servers)
153 await makePostBodyRequest({
157 fields: { videoId: videoRemote.uuid },
158 statusCodeExpected: HttpStatusCode.CONFLICT_409
163 describe('When manually removing a redundancy', function () {
164 const path = '/api/v1/server/redundancy/videos/'
171 token = servers[0].accessToken
174 it('Should fail with an invalid token', async function () {
175 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
178 it('Should fail if the user is not an administrator', async function () {
179 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
182 it('Should fail with an incorrect video id', async function () {
183 await makeDeleteRequest({ url, path: path + 'toto', token })
186 it('Should fail with a not found video redundancy', async function () {
187 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
191 describe('When updating server redundancy', function () {
192 const path = '/api/v1/server/redundancy'
194 it('Should fail with an invalid token', async function () {
195 await makePutBodyRequest({
197 path: path + '/localhost:' + servers[1].port,
198 fields: { redundancyAllowed: true },
200 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
204 it('Should fail if the user is not an administrator', async function () {
205 await makePutBodyRequest({
207 path: path + '/localhost:' + servers[1].port,
208 fields: { redundancyAllowed: true },
209 token: userAccessToken,
210 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
214 it('Should fail if we do not follow this server', async function () {
215 await makePutBodyRequest({
217 path: path + '/example.com',
218 fields: { redundancyAllowed: true },
219 token: servers[0].accessToken,
220 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
224 it('Should fail without de redundancyAllowed param', async function () {
225 await makePutBodyRequest({
227 path: path + '/localhost:' + servers[1].port,
228 fields: { blabla: true },
229 token: servers[0].accessToken,
230 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
234 it('Should succeed with the correct parameters', async function () {
235 await makePutBodyRequest({
237 path: path + '/localhost:' + servers[1].port,
238 fields: { redundancyAllowed: true },
239 token: servers[0].accessToken,
240 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
245 after(async function () {
246 await cleanupTests(servers)