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