aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/redundancy.ts
blob: fca92fde44482a0ef219c31d45088cbab154251f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { VideoCreateResult } from '@shared/models'
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
import {
  checkBadCountPagination,
  checkBadSortPagination,
  checkBadStartPagination,
  cleanupTests,
  doubleFollow,
  flushAndRunMultipleServers,
  makeDeleteRequest,
  makeGetRequest,
  makePostBodyRequest,
  makePutBodyRequest,
  ServerInfo,
  setAccessTokensToServers,
  waitJobs
} from '../../../../shared/extra-utils'

describe('Test server redundancy API validators', function () {
  let servers: ServerInfo[]
  let userAccessToken = null
  let videoIdLocal: number
  let videoRemote: VideoCreateResult

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(80000)

    servers = await flushAndRunMultipleServers(2)

    await setAccessTokensToServers(servers)
    await doubleFollow(servers[0], servers[1])

    const user = {
      username: 'user1',
      password: 'password'
    }

    await servers[0].users.create({ username: user.username, password: user.password })
    userAccessToken = await servers[0].login.getAccessToken(user)

    videoIdLocal = (await servers[0].videos.quickUpload({ name: 'video' })).id

    const remoteUUID = (await servers[1].videos.quickUpload({ name: 'video' })).uuid

    await waitJobs(servers)

    videoRemote = await servers[0].videos.get({ id: remoteUUID })
  })

  describe('When listing redundancies', function () {
    const path = '/api/v1/server/redundancy/videos'

    let url: string
    let token: string

    before(function () {
      url = servers[0].url
      token = servers[0].accessToken
    })

    it('Should fail with an invalid token', async function () {
      await makeGetRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
    })

    it('Should fail if the user is not an administrator', async function () {
      await makeGetRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
    })

    it('Should fail with a bad start pagination', async function () {
      await checkBadStartPagination(url, path, servers[0].accessToken)
    })

    it('Should fail with a bad count pagination', async function () {
      await checkBadCountPagination(url, path, servers[0].accessToken)
    })

    it('Should fail with an incorrect sort', async function () {
      await checkBadSortPagination(url, path, servers[0].accessToken)
    })

    it('Should fail with a bad target', async function () {
      await makeGetRequest({ url, path, token, query: { target: 'bad target' } })
    })

    it('Should fail without target', async function () {
      await makeGetRequest({ url, path, token })
    })

    it('Should succeed with the correct params', async function () {
      await makeGetRequest({ url, path, token, query: { target: 'my-videos' }, statusCodeExpected: HttpStatusCode.OK_200 })
    })
  })

  describe('When manually adding a redundancy', function () {
    const path = '/api/v1/server/redundancy/videos'

    let url: string
    let token: string

    before(function () {
      url = servers[0].url
      token = servers[0].accessToken
    })

    it('Should fail with an invalid token', async function () {
      await makePostBodyRequest({ url, path, token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
    })

    it('Should fail if the user is not an administrator', async function () {
      await makePostBodyRequest({ url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
    })

    it('Should fail without a video id', async function () {
      await makePostBodyRequest({ url, path, token })
    })

    it('Should fail with an incorrect video id', async function () {
      await makePostBodyRequest({ url, path, token, fields: { videoId: 'peertube' } })
    })

    it('Should fail with a not found video id', async function () {
      await makePostBodyRequest({ url, path, token, fields: { videoId: 6565 }, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
    })

    it('Should fail with a local a video id', async function () {
      await makePostBodyRequest({ url, path, token, fields: { videoId: videoIdLocal } })
    })

    it('Should succeed with the correct params', async function () {
      await makePostBodyRequest({
        url,
        path,
        token,
        fields: { videoId: videoRemote.shortUUID },
        statusCodeExpected: HttpStatusCode.NO_CONTENT_204
      })
    })

    it('Should fail if the video is already duplicated', async function () {
      this.timeout(30000)

      await waitJobs(servers)

      await makePostBodyRequest({
        url,
        path,
        token,
        fields: { videoId: videoRemote.uuid },
        statusCodeExpected: HttpStatusCode.CONFLICT_409
      })
    })
  })

  describe('When manually removing a redundancy', function () {
    const path = '/api/v1/server/redundancy/videos/'

    let url: string
    let token: string

    before(function () {
      url = servers[0].url
      token = servers[0].accessToken
    })

    it('Should fail with an invalid token', async function () {
      await makeDeleteRequest({ url, path: path + '1', token: 'fake_token', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
    })

    it('Should fail if the user is not an administrator', async function () {
      await makeDeleteRequest({ url, path: path + '1', token: userAccessToken, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
    })

    it('Should fail with an incorrect video id', async function () {
      await makeDeleteRequest({ url, path: path + 'toto', token })
    })

    it('Should fail with a not found video redundancy', async function () {
      await makeDeleteRequest({ url, path: path + '454545', token, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
    })
  })

  describe('When updating server redundancy', function () {
    const path = '/api/v1/server/redundancy'

    it('Should fail with an invalid token', async function () {
      await makePutBodyRequest({
        url: servers[0].url,
        path: path + '/localhost:' + servers[1].port,
        fields: { redundancyAllowed: true },
        token: 'fake_token',
        statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
      })
    })

    it('Should fail if the user is not an administrator', async function () {
      await makePutBodyRequest({
        url: servers[0].url,
        path: path + '/localhost:' + servers[1].port,
        fields: { redundancyAllowed: true },
        token: userAccessToken,
        statusCodeExpected: HttpStatusCode.FORBIDDEN_403
      })
    })

    it('Should fail if we do not follow this server', async function () {
      await makePutBodyRequest({
        url: servers[0].url,
        path: path + '/example.com',
        fields: { redundancyAllowed: true },
        token: servers[0].accessToken,
        statusCodeExpected: HttpStatusCode.NOT_FOUND_404
      })
    })

    it('Should fail without de redundancyAllowed param', async function () {
      await makePutBodyRequest({
        url: servers[0].url,
        path: path + '/localhost:' + servers[1].port,
        fields: { blabla: true },
        token: servers[0].accessToken,
        statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should succeed with the correct parameters', async function () {
      await makePutBodyRequest({
        url: servers[0].url,
        path: path + '/localhost:' + servers[1].port,
        fields: { redundancyAllowed: true },
        token: servers[0].accessToken,
        statusCodeExpected: HttpStatusCode.NO_CONTENT_204
      })
    })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})