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