]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/redundancy.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / redundancy.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
c48e82b5
C
2
3import 'mocha'
c55e3d72
C
4import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5import { HttpStatusCode, VideoCreateResult } from '@shared/models'
c48e82b5 6import {
7c3b7976 7 cleanupTests,
254d3579 8 createMultipleServers,
c0e8b12e 9 doubleFollow,
d4a8e7a6
C
10 makeDeleteRequest,
11 makeGetRequest,
12 makePostBodyRequest,
c48e82b5 13 makePutBodyRequest,
254d3579 14 PeerTubeServer,
d4a8e7a6 15 setAccessTokensToServers,
d4a8e7a6 16 waitJobs
bf54587a 17} from '@shared/server-commands'
c48e82b5
C
18
19describe('Test server redundancy API validators', function () {
254d3579 20 let servers: PeerTubeServer[]
c48e82b5 21 let userAccessToken = null
b764380a 22 let videoIdLocal: number
d4a8e7a6 23 let videoRemote: VideoCreateResult
c48e82b5
C
24
25 // ---------------------------------------------------------------
26
27 before(async function () {
496b5a5b 28 this.timeout(80000)
c48e82b5 29
254d3579 30 servers = await createMultipleServers(2)
c48e82b5
C
31
32 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
34
35 const user = {
36 username: 'user1',
37 password: 'password'
38 }
39
89d241a7
C
40 await servers[0].users.create({ username: user.username, password: user.password })
41 userAccessToken = await servers[0].login.getAccessToken(user)
b764380a 42
89d241a7 43 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
f9b6d51f 44
89d241a7 45 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
b764380a
C
46
47 await waitJobs(servers)
f9b6d51f 48
89d241a7 49 videoRemote = await servers[0].videos.get({ id: remoteUUID })
b764380a
C
50 })
51
52 describe('When listing redundancies', function () {
53 const path = '/api/v1/server/redundancy/videos'
54
55 let url: string
56 let token: string
57
58 before(function () {
59 url = servers[0].url
60 token = servers[0].accessToken
61 })
62
63 it('Should fail with an invalid token', async function () {
c0e8b12e 64 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
b764380a
C
65 })
66
67 it('Should fail if the user is not an administrator', async function () {
c0e8b12e 68 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
b764380a
C
69 })
70
71 it('Should fail with a bad start pagination', async function () {
72 await checkBadStartPagination(url, path, servers[0].accessToken)
73 })
74
75 it('Should fail with a bad count pagination', async function () {
76 await checkBadCountPagination(url, path, servers[0].accessToken)
77 })
78
79 it('Should fail with an incorrect sort', async function () {
80 await checkBadSortPagination(url, path, servers[0].accessToken)
81 })
82
83 it('Should fail with a bad target', async function () {
84 await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
85 })
86
87 it('Should fail without target', async function () {
88 await makeGetRequest({ url, path, token })
89 })
90
91 it('Should succeed with the correct params', async function () {
c0e8b12e 92 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
b764380a
C
93 })
94 })
95
96 describe('When manually adding a redundancy', function () {
97 const path = '/api/v1/server/redundancy/videos'
98
99 let url: string
100 let token: string
101
102 before(function () {
103 url = servers[0].url
104 token = servers[0].accessToken
105 })
106
107 it('Should fail with an invalid token', async function () {
c0e8b12e 108 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
b764380a
C
109 })
110
111 it('Should fail if the user is not an administrator', async function () {
c0e8b12e 112 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
b764380a
C
113 })
114
115 it('Should fail without a video id', async function () {
116 await makePostBodyRequest({ url, path, token })
117 })
118
119 it('Should fail with an incorrect video id', async function () {
120 await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
121 })
122
123 it('Should fail with a not found video id', async function () {
c0e8b12e 124 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
b764380a
C
125 })
126
127 it('Should fail with a local a video id', async function () {
128 await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
129 })
130
131 it('Should succeed with the correct params', async function () {
d4a8e7a6
C
132 await makePostBodyRequest({
133 url,
134 path,
135 token,
136 fields: { videoId: videoRemote.shortUUID },
c0e8b12e 137 expectedStatus: HttpStatusCode.NO_CONTENT_204
d4a8e7a6 138 })
b764380a
C
139 })
140
141 it('Should fail if the video is already duplicated', async function () {
142 this.timeout(30000)
143
144 await waitJobs(servers)
145
d4a8e7a6
C
146 await makePostBodyRequest({
147 url,
148 path,
149 token,
150 fields: { videoId: videoRemote.uuid },
c0e8b12e 151 expectedStatus: HttpStatusCode.CONFLICT_409
d4a8e7a6 152 })
b764380a
C
153 })
154 })
155
156 describe('When manually removing a redundancy', function () {
157 const path = '/api/v1/server/redundancy/videos/'
158
159 let url: string
160 let token: string
161
162 before(function () {
163 url = servers[0].url
164 token = servers[0].accessToken
165 })
166
167 it('Should fail with an invalid token', async function () {
c0e8b12e 168 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
b764380a
C
169 })
170
171 it('Should fail if the user is not an administrator', async function () {
c0e8b12e 172 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
b764380a
C
173 })
174
175 it('Should fail with an incorrect video id', async function () {
176 await makeDeleteRequest({ url, path: path + 'toto', token })
177 })
178
179 it('Should fail with a not found video redundancy', async function () {
c0e8b12e 180 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
b764380a 181 })
c48e82b5
C
182 })
183
b764380a 184 describe('When updating server redundancy', function () {
c48e82b5
C
185 const path = '/api/v1/server/redundancy'
186
187 it('Should fail with an invalid token', async function () {
188 await makePutBodyRequest({
189 url: servers[0].url,
7c3b7976 190 path: path + '/localhost:' + servers[1].port,
c48e82b5
C
191 fields: { redundancyAllowed: true },
192 token: 'fake_token',
c0e8b12e 193 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
c48e82b5
C
194 })
195 })
196
197 it('Should fail if the user is not an administrator', async function () {
198 await makePutBodyRequest({
199 url: servers[0].url,
7c3b7976 200 path: path + '/localhost:' + servers[1].port,
c48e82b5
C
201 fields: { redundancyAllowed: true },
202 token: userAccessToken,
c0e8b12e 203 expectedStatus: HttpStatusCode.FORBIDDEN_403
c48e82b5
C
204 })
205 })
206
207 it('Should fail if we do not follow this server', async function () {
208 await makePutBodyRequest({
209 url: servers[0].url,
210 path: path + '/example.com',
211 fields: { redundancyAllowed: true },
212 token: servers[0].accessToken,
c0e8b12e 213 expectedStatus: HttpStatusCode.NOT_FOUND_404
c48e82b5
C
214 })
215 })
216
217 it('Should fail without de redundancyAllowed param', async function () {
218 await makePutBodyRequest({
219 url: servers[0].url,
7c3b7976 220 path: path + '/localhost:' + servers[1].port,
c48e82b5
C
221 fields: { blabla: true },
222 token: servers[0].accessToken,
c0e8b12e 223 expectedStatus: HttpStatusCode.BAD_REQUEST_400
c48e82b5
C
224 })
225 })
226
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
229 url: servers[0].url,
7c3b7976 230 path: path + '/localhost:' + servers[1].port,
c48e82b5
C
231 fields: { redundancyAllowed: true },
232 token: servers[0].accessToken,
c0e8b12e 233 expectedStatus: HttpStatusCode.NO_CONTENT_204
c48e82b5
C
234 })
235 })
236 })
237
7c3b7976
C
238 after(async function () {
239 await cleanupTests(servers)
c48e82b5
C
240 })
241})