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