1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
16 setAccessTokensToServers,
18 } from '@shared/extra-utils'
19 import { HttpStatusCode, VideoCreateResult } from '@shared/models'
21 describe('Test server redundancy API validators', function () {
22 let servers: PeerTubeServer[]
23 let userAccessToken = null
24 let videoIdLocal: number
25 let videoRemote: VideoCreateResult
27 // ---------------------------------------------------------------
29 before(async function () {
32 servers = await createMultipleServers(2)
34 await setAccessTokensToServers(servers)
35 await doubleFollow(servers[0], servers[1])
42 await servers[0].users.create({ username: user.username, password: user.password })
43 userAccessToken = await servers[0].login.getAccessToken(user)
45 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
47 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
49 await waitJobs(servers)
51 videoRemote = await servers[0].videos.get({ id: remoteUUID })
54 describe('When listing redundancies', function () {
55 const path = '/api/v1/server/redundancy/videos'
62 token = servers[0].accessToken
65 it('Should fail with an invalid token', async function () {
66 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
69 it('Should fail if the user is not an administrator', async function () {
70 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
73 it('Should fail with a bad start pagination', async function () {
74 await checkBadStartPagination(url, path, servers[0].accessToken)
77 it('Should fail with a bad count pagination', async function () {
78 await checkBadCountPagination(url, path, servers[0].accessToken)
81 it('Should fail with an incorrect sort', async function () {
82 await checkBadSortPagination(url, path, servers[0].accessToken)
85 it('Should fail with a bad target', async function () {
86 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
89 it('Should fail without target', async function () {
90 await makeGetRequest({ url, path, token })
93 it('Should succeed with the correct params', async function () {
94 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
98 describe('When manually adding a redundancy', function () {
99 const path = '/api/v1/server/redundancy/videos'
106 token = servers[0].accessToken
109 it('Should fail with an invalid token', async function () {
110 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
113 it('Should fail if the user is not an administrator', async function () {
114 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
117 it('Should fail without a video id', async function () {
118 await makePostBodyRequest({ url, path, token })
121 it('Should fail with an incorrect video id', async function () {
122 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
125 it('Should fail with a not found video id', async function () {
126 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
129 it('Should fail with a local a video id', async function () {
130 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
133 it('Should succeed with the correct params', async function () {
134 await makePostBodyRequest({
138 fields: { videoId: videoRemote.shortUUID },
139 expectedStatus: HttpStatusCode.NO_CONTENT_204
143 it('Should fail if the video is already duplicated', async function () {
146 await waitJobs(servers)
148 await makePostBodyRequest({
152 fields: { videoId: videoRemote.uuid },
153 expectedStatus: HttpStatusCode.CONFLICT_409
158 describe('When manually removing a redundancy', function () {
159 const path = '/api/v1/server/redundancy/videos/'
166 token = servers[0].accessToken
169 it('Should fail with an invalid token', async function () {
170 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
173 it('Should fail if the user is not an administrator', async function () {
174 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
177 it('Should fail with an incorrect video id', async function () {
178 await makeDeleteRequest({ url, path: path + 'toto', token })
181 it('Should fail with a not found video redundancy', async function () {
182 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
186 describe('When updating server redundancy', function () {
187 const path = '/api/v1/server/redundancy'
189 it('Should fail with an invalid token', async function () {
190 await makePutBodyRequest({
192 path: path + '/localhost:' + servers[1].port,
193 fields: { redundancyAllowed: true },
195 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
199 it('Should fail if the user is not an administrator', async function () {
200 await makePutBodyRequest({
202 path: path + '/localhost:' + servers[1].port,
203 fields: { redundancyAllowed: true },
204 token: userAccessToken,
205 expectedStatus: HttpStatusCode.FORBIDDEN_403
209 it('Should fail if we do not follow this server', async function () {
210 await makePutBodyRequest({
212 path: path + '/example.com',
213 fields: { redundancyAllowed: true },
214 token: servers[0].accessToken,
215 expectedStatus: HttpStatusCode.NOT_FOUND_404
219 it('Should fail without de redundancyAllowed param', async function () {
220 await makePutBodyRequest({
222 path: path + '/localhost:' + servers[1].port,
223 fields: { blabla: true },
224 token: servers[0].accessToken,
225 expectedStatus: HttpStatusCode.BAD_REQUEST_400
229 it('Should succeed with the correct parameters', async function () {
230 await makePutBodyRequest({
232 path: path + '/localhost:' + servers[1].port,
233 fields: { redundancyAllowed: true },
234 token: servers[0].accessToken,
235 expectedStatus: HttpStatusCode.NO_CONTENT_204
240 after(async function () {
241 await cleanupTests(servers)