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