1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
12 flushAndRunMultipleServers, makeDeleteRequest,
13 makeGetRequest, makePostBodyRequest,
16 setAccessTokensToServers, uploadVideoAndGetId,
17 userLogin, waitJobs, getVideoIdFromUUID
18 } from '../../../../shared/extra-utils'
19 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
21 describe('Test server redundancy API validators', function () {
22 let servers: ServerInfo[]
23 let userAccessToken = null
24 let videoIdLocal: number
25 let videoIdRemote: number
27 // ---------------------------------------------------------------
29 before(async function () {
32 servers = await flushAndRunMultipleServers(2)
34 await setAccessTokensToServers(servers)
35 await doubleFollow(servers[0], servers[1])
42 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
43 userAccessToken = await userLogin(servers[0], user)
45 videoIdLocal = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video' })).id
47 const remoteUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video' })).uuid
49 await waitJobs(servers)
51 videoIdRemote = await getVideoIdFromUUID(servers[0].url, 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', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
69 it('Should fail if the user is not an administrator', async function () {
70 await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: 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' }, statusCodeExpected: 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', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
113 it('Should fail if the user is not an administrator', async function () {
114 await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: 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 }, statusCodeExpected: 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({ url, path, token, fields: { videoId: videoIdRemote }, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
137 it('Should fail if the video is already duplicated', async function () {
140 await waitJobs(servers)
142 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdRemote }, statusCodeExpected: HttpStatusCode.CONFLICT_409 })
146 describe('When manually removing a redundancy', function () {
147 const path = '/api/v1/server/redundancy/videos/'
154 token = servers[0].accessToken
157 it('Should fail with an invalid token', async function () {
158 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
161 it('Should fail if the user is not an administrator', async function () {
162 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
165 it('Should fail with an incorrect video id', async function () {
166 await makeDeleteRequest({ url, path: path + 'toto', token })
169 it('Should fail with a not found video redundancy', async function () {
170 await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
174 describe('When updating server redundancy', function () {
175 const path = '/api/v1/server/redundancy'
177 it('Should fail with an invalid token', async function () {
178 await makePutBodyRequest({
180 path: path + '/localhost:' + servers[1].port,
181 fields: { redundancyAllowed: true },
183 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
187 it('Should fail if the user is not an administrator', async function () {
188 await makePutBodyRequest({
190 path: path + '/localhost:' + servers[1].port,
191 fields: { redundancyAllowed: true },
192 token: userAccessToken,
193 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
197 it('Should fail if we do not follow this server', async function () {
198 await makePutBodyRequest({
200 path: path + '/example.com',
201 fields: { redundancyAllowed: true },
202 token: servers[0].accessToken,
203 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
207 it('Should fail without de redundancyAllowed param', async function () {
208 await makePutBodyRequest({
210 path: path + '/localhost:' + servers[1].port,
211 fields: { blabla: true },
212 token: servers[0].accessToken,
213 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
217 it('Should succeed with the correct parameters', async function () {
218 await makePutBodyRequest({
220 path: path + '/localhost:' + servers[1].port,
221 fields: { redundancyAllowed: true },
222 token: servers[0].accessToken,
223 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
228 after(async function () {
229 await cleanupTests(servers)