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