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