aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/users/user-videos.ts
blob: 87040d2e4146db1fdd054e390ced676c01f64bdd (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import * as chai from 'chai'
import { HttpStatusCode } from '@shared/models'
import {
  cleanupTests,
  createSingleServer,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultAccountAvatar,
  setDefaultChannelAvatar,
  waitJobs
} from '@shared/server-commands'

const expect = chai.expect

describe('Test user videos', function () {
  let server: PeerTubeServer
  let videoId: number
  let videoId2: number
  let token: string
  let anotherUserToken: string

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

    server = await createSingleServer(1)

    await setAccessTokensToServers([ server ])
    await setDefaultChannelAvatar([ server ])
    await setDefaultAccountAvatar([ server ])

    await server.videos.quickUpload({ name: 'root video' })
    await server.videos.quickUpload({ name: 'root video 2' })

    token = await server.users.generateUserAndToken('user')
    anotherUserToken = await server.users.generateUserAndToken('user2')
  })

  describe('List my videos', function () {

    it('Should list my videos', async function () {
      const { data, total } = await server.videos.listMyVideos()

      expect(total).to.equal(2)
      expect(data).to.have.lengthOf(2)
    })
  })

  describe('Upload', function () {

    it('Should upload the video with the correct token', async function () {
      await server.videos.upload({ token })
      const { data } = await server.videos.list()
      const video = data[0]

      expect(video.account.name).to.equal('user')
      videoId = video.id
    })

    it('Should upload the video again with the correct token', async function () {
      const { id } = await server.videos.upload({ token })
      videoId2 = id
    })
  })

  describe('Ratings', function () {

    it('Should retrieve a video rating', async function () {
      await server.videos.rate({ id: videoId, token, rating: 'like' })
      const rating = await server.users.getMyRating({ token, videoId })

      expect(rating.videoId).to.equal(videoId)
      expect(rating.rating).to.equal('like')
    })

    it('Should retrieve ratings list', async function () {
      await server.videos.rate({ id: videoId, token, rating: 'like' })

      const body = await server.accounts.listRatings({ accountName: 'user', token })

      expect(body.total).to.equal(1)
      expect(body.data[0].video.id).to.equal(videoId)
      expect(body.data[0].rating).to.equal('like')
    })

    it('Should retrieve ratings list by rating type', async function () {
      {
        const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'like' })
        expect(body.data.length).to.equal(1)
      }

      {
        const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'dislike' })
        expect(body.data.length).to.equal(0)
      }
    })
  })

  describe('Remove video', function () {

    it('Should not be able to remove the video with an incorrect token', async function () {
      await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
    })

    it('Should not be able to remove the video with the token of another account', async function () {
      await server.videos.remove({ token: anotherUserToken, id: videoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
    })

    it('Should be able to remove the video with the correct token', async function () {
      await server.videos.remove({ token, id: videoId })
      await server.videos.remove({ token, id: videoId2 })
    })
  })

  describe('My videos & quotas', function () {

    it('Should be able to upload a video with a user', async function () {
      this.timeout(10000)

      const attributes = {
        name: 'super user video',
        fixture: 'video_short.webm'
      }
      await server.videos.upload({ token, attributes })

      await server.channels.create({ token, attributes: { name: 'other_channel' } })
    })

    it('Should have video quota updated', async function () {
      const quota = await server.users.getMyQuotaUsed({ token })
      expect(quota.videoQuotaUsed).to.equal(218910)
      expect(quota.videoQuotaUsedDaily).to.equal(218910)

      const { data } = await server.users.list()
      const tmpUser = data.find(u => u.username === 'user')
      expect(tmpUser.videoQuotaUsed).to.equal(218910)
      expect(tmpUser.videoQuotaUsedDaily).to.equal(218910)
    })

    it('Should be able to list my videos', async function () {
      const { total, data } = await server.videos.listMyVideos({ token })
      expect(total).to.equal(1)
      expect(data).to.have.lengthOf(1)

      const video = data[0]
      expect(video.name).to.equal('super user video')
      expect(video.thumbnailPath).to.not.be.null
      expect(video.previewPath).to.not.be.null
    })

    it('Should be able to filter by channel in my videos', async function () {
      const myInfo = await server.users.getMyInfo({ token })
      const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel')
      const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel')

      {
        const { total, data } = await server.videos.listMyVideos({ token, channelId: mainChannel.id })
        expect(total).to.equal(1)
        expect(data).to.have.lengthOf(1)

        const video = data[0]
        expect(video.name).to.equal('super user video')
        expect(video.thumbnailPath).to.not.be.null
        expect(video.previewPath).to.not.be.null
      }

      {
        const { total, data } = await server.videos.listMyVideos({ token, channelId: otherChannel.id })
        expect(total).to.equal(0)
        expect(data).to.have.lengthOf(0)
      }
    })

    it('Should be able to search in my videos', async function () {
      {
        const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'user video' })
        expect(total).to.equal(1)
        expect(data).to.have.lengthOf(1)
      }

      {
        const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'toto' })
        expect(total).to.equal(0)
        expect(data).to.have.lengthOf(0)
      }
    })

    it('Should disable webtorrent, enable HLS, and update my quota', async function () {
      this.timeout(160000)

      {
        const config = await server.config.getCustomConfig()
        config.transcoding.webtorrent.enabled = false
        config.transcoding.hls.enabled = true
        config.transcoding.enabled = true
        await server.config.updateCustomSubConfig({ newConfig: config })
      }

      {
        const attributes = {
          name: 'super user video 2',
          fixture: 'video_short.webm'
        }
        await server.videos.upload({ token, attributes })

        await waitJobs([ server ])
      }

      {
        const data = await server.users.getMyQuotaUsed({ token })
        expect(data.videoQuotaUsed).to.be.greaterThan(220000)
        expect(data.videoQuotaUsedDaily).to.be.greaterThan(220000)
      }
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})