]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/redundancy.ts
Move test functions outside extra-utils
[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 { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode, VideoCreateResult } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 makeDeleteRequest,
11 makeGetRequest,
12 makePostBodyRequest,
13 makePutBodyRequest,
14 PeerTubeServer,
15 setAccessTokensToServers,
16 waitJobs
17 } from '@shared/server-commands'
18
19 describe('Test server redundancy API validators', function () {
20 let servers: PeerTubeServer[]
21 let userAccessToken = null
22 let videoIdLocal: number
23 let videoRemote: VideoCreateResult
24
25 // ---------------------------------------------------------------
26
27 before(async function () {
28 this.timeout(80000)
29
30 servers = await createMultipleServers(2)
31
32 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
34
35 const user = {
36 username: 'user1',
37 password: 'password'
38 }
39
40 await servers[0].users.create({ username: user.username, password: user.password })
41 userAccessToken = await servers[0].login.getAccessToken(user)
42
43 videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id
44
45 const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid
46
47 await waitJobs(servers)
48
49 videoRemote = await servers[0].videos.get({ id: remoteUUID })
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 () {
64 await makeGetRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
65 })
66
67 it('Should fail if the user is not an administrator', async function () {
68 await makeGetRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
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 () {
92 await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, expectedStatus: HttpStatusCode.OK_200 })
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 () {
108 await makePostBodyRequest({ url, path, token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
109 })
110
111 it('Should fail if the user is not an administrator', async function () {
112 await makePostBodyRequest({ url, path, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
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 () {
124 await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
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 () {
132 await makePostBodyRequest({
133 url,
134 path,
135 token,
136 fields: { videoId: videoRemote.shortUUID },
137 expectedStatus: HttpStatusCode.NO_CONTENT_204
138 })
139 })
140
141 it('Should fail if the video is already duplicated', async function () {
142 this.timeout(30000)
143
144 await waitJobs(servers)
145
146 await makePostBodyRequest({
147 url,
148 path,
149 token,
150 fields: { videoId: videoRemote.uuid },
151 expectedStatus: HttpStatusCode.CONFLICT_409
152 })
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 () {
168 await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
169 })
170
171 it('Should fail if the user is not an administrator', async function () {
172 await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
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 () {
180 await makeDeleteRequest({ url, path: path + '454545', token, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
181 })
182 })
183
184 describe('When updating server redundancy', function () {
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,
190 path: path + '/localhost:' + servers[1].port,
191 fields: { redundancyAllowed: true },
192 token: 'fake_token',
193 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
194 })
195 })
196
197 it('Should fail if the user is not an administrator', async function () {
198 await makePutBodyRequest({
199 url: servers[0].url,
200 path: path + '/localhost:' + servers[1].port,
201 fields: { redundancyAllowed: true },
202 token: userAccessToken,
203 expectedStatus: HttpStatusCode.FORBIDDEN_403
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,
213 expectedStatus: HttpStatusCode.NOT_FOUND_404
214 })
215 })
216
217 it('Should fail without de redundancyAllowed param', async function () {
218 await makePutBodyRequest({
219 url: servers[0].url,
220 path: path + '/localhost:' + servers[1].port,
221 fields: { blabla: true },
222 token: servers[0].accessToken,
223 expectedStatus: HttpStatusCode.BAD_REQUEST_400
224 })
225 })
226
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
229 url: servers[0].url,
230 path: path + '/localhost:' + servers[1].port,
231 fields: { redundancyAllowed: true },
232 token: servers[0].accessToken,
233 expectedStatus: HttpStatusCode.NO_CONTENT_204
234 })
235 })
236 })
237
238 after(async function () {
239 await cleanupTests(servers)
240 })
241 })