aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/video-files.ts
blob: c698bea823067b2b4c37bfbafc1572c2a26aaf21 (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { HttpStatusCode, UserRole } from '@shared/models'
import {
  cleanupTests,
  createMultipleServers,
  doubleFollow,
  PeerTubeServer,
  setAccessTokensToServers,
  waitJobs
} from '@shared/server-commands'

describe('Test videos files', function () {
  let servers: PeerTubeServer[]

  let webtorrentId: string
  let hlsId: string
  let remoteId: string

  let userToken: string
  let moderatorToken: string

  let validId1: string
  let validId2: string

  let hlsFileId: number
  let webtorrentFileId: number

  let remoteHLSFileId: number
  let remoteWebtorrentFileId: number

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

  before(async function () {
    this.timeout(300_000)

    servers = await createMultipleServers(2)
    await setAccessTokensToServers(servers)

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

    userToken = await servers[0].users.generateUserAndToken('user', UserRole.USER)
    moderatorToken = await servers[0].users.generateUserAndToken('moderator', UserRole.MODERATOR)

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

      const video = await servers[1].videos.get({ id: uuid })
      remoteId = video.uuid
      remoteHLSFileId = video.streamingPlaylists[0].files[0].id
      remoteWebtorrentFileId = video.files[0].id
    }

    {
      await servers[0].config.enableTranscoding(true, true)

      {
        const { uuid } = await servers[0].videos.quickUpload({ name: 'both 1' })
        await waitJobs(servers)

        const video = await servers[0].videos.get({ id: uuid })
        validId1 = video.uuid
        hlsFileId = video.streamingPlaylists[0].files[0].id
        webtorrentFileId = video.files[0].id
      }

      {
        const { uuid } = await servers[0].videos.quickUpload({ name: 'both 2' })
        validId2 = uuid
      }
    }

    await waitJobs(servers)

    {
      await servers[0].config.enableTranscoding(false, true)
      const { uuid } = await servers[0].videos.quickUpload({ name: 'hls' })
      hlsId = uuid
    }

    await waitJobs(servers)

    {
      await servers[0].config.enableTranscoding(false, true)
      const { uuid } = await servers[0].videos.quickUpload({ name: 'webtorrent' })
      webtorrentId = uuid
    }

    await waitJobs(servers)
  })

  it('Should not delete files of a unknown video', async function () {
    const expectedStatus = HttpStatusCode.NOT_FOUND_404

    await servers[0].videos.removeHLSPlaylist({ videoId: 404, expectedStatus })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: 404, expectedStatus })

    await servers[0].videos.removeHLSFile({ videoId: 404, fileId: hlsFileId, expectedStatus })
    await servers[0].videos.removeWebTorrentFile({ videoId: 404, fileId: webtorrentFileId, expectedStatus })
  })

  it('Should not delete unknown files', async function () {
    const expectedStatus = HttpStatusCode.NOT_FOUND_404

    await servers[0].videos.removeHLSFile({ videoId: validId1, fileId: webtorrentFileId, expectedStatus })
    await servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: hlsFileId, expectedStatus })
  })

  it('Should not delete files of a remote video', async function () {
    const expectedStatus = HttpStatusCode.BAD_REQUEST_400

    await servers[0].videos.removeHLSPlaylist({ videoId: remoteId, expectedStatus })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: remoteId, expectedStatus })

    await servers[0].videos.removeHLSFile({ videoId: remoteId, fileId: remoteHLSFileId, expectedStatus })
    await servers[0].videos.removeWebTorrentFile({ videoId: remoteId, fileId: remoteWebtorrentFileId, expectedStatus })
  })

  it('Should not delete files by a non admin user', async function () {
    const expectedStatus = HttpStatusCode.FORBIDDEN_403

    await servers[0].videos.removeHLSPlaylist({ videoId: validId1, token: userToken, expectedStatus })
    await servers[0].videos.removeHLSPlaylist({ videoId: validId1, token: moderatorToken, expectedStatus })

    await servers[0].videos.removeAllWebTorrentFiles({ videoId: validId1, token: userToken, expectedStatus })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: validId1, token: moderatorToken, expectedStatus })

    await servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId, token: userToken, expectedStatus })
    await servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId, token: moderatorToken, expectedStatus })

    await servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId, token: userToken, expectedStatus })
    await servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId, token: moderatorToken, expectedStatus })
  })

  it('Should not delete files if the files are not available', async function () {
    await servers[0].videos.removeHLSPlaylist({ videoId: hlsId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: webtorrentId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })

    await servers[0].videos.removeHLSFile({ videoId: hlsId, fileId: 404, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
    await servers[0].videos.removeWebTorrentFile({ videoId: webtorrentId, fileId: 404, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  })

  it('Should not delete files if no both versions are available', async function () {
    await servers[0].videos.removeHLSPlaylist({ videoId: hlsId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: webtorrentId, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  })

  it('Should delete files if both versions are available', async function () {
    await servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId })
    await servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId })

    await servers[0].videos.removeHLSPlaylist({ videoId: validId1 })
    await servers[0].videos.removeAllWebTorrentFiles({ videoId: validId2 })
  })

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