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