]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/user-videos.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / user-videos.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { HttpStatusCode } from '@shared/models'
5 import {
6 cleanupTests,
7 createSingleServer,
8 PeerTubeServer,
9 setAccessTokensToServers,
10 setDefaultAccountAvatar,
11 setDefaultChannelAvatar,
12 waitJobs
13 } from '@shared/server-commands'
14
15 const expect = chai.expect
16
17 describe('Test user videos', function () {
18 let server: PeerTubeServer
19 let videoId: number
20 let videoId2: number
21 let token: string
22 let anotherUserToken: string
23
24 before(async function () {
25 this.timeout(30000)
26
27 server = await createSingleServer(1)
28
29 await setAccessTokensToServers([ server ])
30 await setDefaultChannelAvatar([ server ])
31 await setDefaultAccountAvatar([ server ])
32
33 await server.videos.quickUpload({ name: 'root video' })
34 await server.videos.quickUpload({ name: 'root video 2' })
35
36 token = await server.users.generateUserAndToken('user')
37 anotherUserToken = await server.users.generateUserAndToken('user2')
38 })
39
40 describe('List my videos', function () {
41
42 it('Should list my videos', async function () {
43 const { data, total } = await server.videos.listMyVideos()
44
45 expect(total).to.equal(2)
46 expect(data).to.have.lengthOf(2)
47 })
48 })
49
50 describe('Upload', function () {
51
52 it('Should upload the video with the correct token', async function () {
53 await server.videos.upload({ token })
54 const { data } = await server.videos.list()
55 const video = data[0]
56
57 expect(video.account.name).to.equal('user')
58 videoId = video.id
59 })
60
61 it('Should upload the video again with the correct token', async function () {
62 const { id } = await server.videos.upload({ token })
63 videoId2 = id
64 })
65 })
66
67 describe('Ratings', function () {
68
69 it('Should retrieve a video rating', async function () {
70 await server.videos.rate({ id: videoId, token, rating: 'like' })
71 const rating = await server.users.getMyRating({ token, videoId })
72
73 expect(rating.videoId).to.equal(videoId)
74 expect(rating.rating).to.equal('like')
75 })
76
77 it('Should retrieve ratings list', async function () {
78 await server.videos.rate({ id: videoId, token, rating: 'like' })
79
80 const body = await server.accounts.listRatings({ accountName: 'user', token })
81
82 expect(body.total).to.equal(1)
83 expect(body.data[0].video.id).to.equal(videoId)
84 expect(body.data[0].rating).to.equal('like')
85 })
86
87 it('Should retrieve ratings list by rating type', async function () {
88 {
89 const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'like' })
90 expect(body.data.length).to.equal(1)
91 }
92
93 {
94 const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'dislike' })
95 expect(body.data.length).to.equal(0)
96 }
97 })
98 })
99
100 describe('Remove video', function () {
101
102 it('Should not be able to remove the video with an incorrect token', async function () {
103 await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
104 })
105
106 it('Should not be able to remove the video with the token of another account', async function () {
107 await server.videos.remove({ token: anotherUserToken, id: videoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
108 })
109
110 it('Should be able to remove the video with the correct token', async function () {
111 await server.videos.remove({ token, id: videoId })
112 await server.videos.remove({ token, id: videoId2 })
113 })
114 })
115
116 describe('My videos & quotas', function () {
117
118 it('Should be able to upload a video with a user', async function () {
119 this.timeout(10000)
120
121 const attributes = {
122 name: 'super user video',
123 fixture: 'video_short.webm'
124 }
125 await server.videos.upload({ token, attributes })
126
127 await server.channels.create({ token, attributes: { name: 'other_channel' } })
128 })
129
130 it('Should have video quota updated', async function () {
131 const quota = await server.users.getMyQuotaUsed({ token })
132 expect(quota.videoQuotaUsed).to.equal(218910)
133 expect(quota.videoQuotaUsedDaily).to.equal(218910)
134
135 const { data } = await server.users.list()
136 const tmpUser = data.find(u => u.username === 'user')
137 expect(tmpUser.videoQuotaUsed).to.equal(218910)
138 expect(tmpUser.videoQuotaUsedDaily).to.equal(218910)
139 })
140
141 it('Should be able to list my videos', async function () {
142 const { total, data } = await server.videos.listMyVideos({ token })
143 expect(total).to.equal(1)
144 expect(data).to.have.lengthOf(1)
145
146 const video = data[0]
147 expect(video.name).to.equal('super user video')
148 expect(video.thumbnailPath).to.not.be.null
149 expect(video.previewPath).to.not.be.null
150 })
151
152 it('Should be able to filter by channel in my videos', async function () {
153 const myInfo = await server.users.getMyInfo({ token })
154 const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel')
155 const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel')
156
157 {
158 const { total, data } = await server.videos.listMyVideos({ token, channelId: mainChannel.id })
159 expect(total).to.equal(1)
160 expect(data).to.have.lengthOf(1)
161
162 const video = data[0]
163 expect(video.name).to.equal('super user video')
164 expect(video.thumbnailPath).to.not.be.null
165 expect(video.previewPath).to.not.be.null
166 }
167
168 {
169 const { total, data } = await server.videos.listMyVideos({ token, channelId: otherChannel.id })
170 expect(total).to.equal(0)
171 expect(data).to.have.lengthOf(0)
172 }
173 })
174
175 it('Should be able to search in my videos', async function () {
176 {
177 const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'user video' })
178 expect(total).to.equal(1)
179 expect(data).to.have.lengthOf(1)
180 }
181
182 {
183 const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'toto' })
184 expect(total).to.equal(0)
185 expect(data).to.have.lengthOf(0)
186 }
187 })
188
189 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
190 this.timeout(160000)
191
192 {
193 const config = await server.config.getCustomConfig()
194 config.transcoding.webtorrent.enabled = false
195 config.transcoding.hls.enabled = true
196 config.transcoding.enabled = true
197 await server.config.updateCustomSubConfig({ newConfig: config })
198 }
199
200 {
201 const attributes = {
202 name: 'super user video 2',
203 fixture: 'video_short.webm'
204 }
205 await server.videos.upload({ token, attributes })
206
207 await waitJobs([ server ])
208 }
209
210 {
211 const data = await server.users.getMyQuotaUsed({ token })
212 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
213 expect(data.videoQuotaUsedDaily).to.be.greaterThan(220000)
214 }
215 })
216 })
217
218 after(async function () {
219 await cleanupTests([ server ])
220 })
221 })