]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/videos-overview.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / videos-overview.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5
6 import {
7 cleanupTests,
8 flushAndRunServer,
9 generateUserAccessToken,
10 ServerInfo,
11 setAccessTokensToServers,
12 uploadVideo,
13 wait
14 } from '../../../../shared/extra-utils'
15 import { getVideosOverview, getVideosOverviewWithToken } from '../../../../shared/extra-utils/overviews/overviews'
16 import { VideosOverview } from '../../../../shared/models/overviews'
17 import { addAccountToAccountBlocklist } from '@shared/extra-utils/users/blocklist'
18 import { Response } from 'superagent'
19
20 const expect = chai.expect
21
22 describe('Test a videos overview', function () {
23 let server: ServerInfo = null
24
25 function testOverviewCount (res: Response, expected: number) {
26 const overview: VideosOverview = res.body
27
28 expect(overview.tags).to.have.lengthOf(expected)
29 expect(overview.categories).to.have.lengthOf(expected)
30 expect(overview.channels).to.have.lengthOf(expected)
31 }
32
33 before(async function () {
34 this.timeout(30000)
35
36 server = await flushAndRunServer(1)
37
38 await setAccessTokensToServers([ server ])
39 })
40
41 it('Should send empty overview', async function () {
42 const res = await getVideosOverview(server.url, 1)
43
44 testOverviewCount(res, 0)
45 })
46
47 it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () {
48 this.timeout(15000)
49
50 await wait(3000)
51
52 await uploadVideo(server.url, server.accessToken, {
53 name: 'video 0',
54 category: 3,
55 tags: [ 'coucou1', 'coucou2' ]
56 })
57
58 const res = await getVideosOverview(server.url, 1)
59
60 testOverviewCount(res, 0)
61 })
62
63 it('Should upload another video and include all videos in the overview', async function () {
64 this.timeout(15000)
65
66 for (let i = 1; i < 6; i++) {
67 await uploadVideo(server.url, server.accessToken, {
68 name: 'video ' + i,
69 category: 3,
70 tags: [ 'coucou1', 'coucou2' ]
71 })
72 }
73
74 await wait(3000)
75
76 {
77 const res = await getVideosOverview(server.url, 1)
78
79 testOverviewCount(res, 1)
80 }
81
82 {
83 const res = await getVideosOverview(server.url, 2)
84
85 const overview: VideosOverview = res.body
86 expect(overview.tags).to.have.lengthOf(1)
87 expect(overview.categories).to.have.lengthOf(0)
88 expect(overview.channels).to.have.lengthOf(0)
89 }
90 })
91
92 it('Should have the correct overview', async function () {
93 const res1 = await getVideosOverview(server.url, 1)
94 const res2 = await getVideosOverview(server.url, 2)
95
96 const overview1: VideosOverview = res1.body
97 const overview2: VideosOverview = res2.body
98
99 const tmp = [
100 overview1.tags,
101 overview1.categories,
102 overview1.channels,
103 overview2.tags
104 ]
105
106 for (const arr of tmp) {
107 expect(arr).to.have.lengthOf(1)
108
109 const obj = arr[0]
110
111 expect(obj.videos).to.have.lengthOf(6)
112 expect(obj.videos[0].name).to.equal('video 5')
113 expect(obj.videos[1].name).to.equal('video 4')
114 expect(obj.videos[2].name).to.equal('video 3')
115 expect(obj.videos[3].name).to.equal('video 2')
116 expect(obj.videos[4].name).to.equal('video 1')
117 expect(obj.videos[5].name).to.equal('video 0')
118 }
119
120 const tags = [ overview1.tags[0].tag, overview2.tags[0].tag ]
121 expect(tags.find(t => t === 'coucou1')).to.not.be.undefined
122 expect(tags.find(t => t === 'coucou2')).to.not.be.undefined
123
124 expect(overview1.categories[0].category.id).to.equal(3)
125
126 expect(overview1.channels[0].channel.name).to.equal('root_channel')
127 })
128
129 it('Should hide muted accounts', async function () {
130 const token = await generateUserAccessToken(server, 'choco')
131
132 await addAccountToAccountBlocklist(server.url, token, 'root@' + server.host)
133
134 {
135 const res = await getVideosOverview(server.url, 1)
136
137 testOverviewCount(res, 1)
138 }
139
140 {
141 const res = await getVideosOverviewWithToken(server.url, 1, token)
142
143 testOverviewCount(res, 0)
144 }
145 })
146
147 after(async function () {
148 await cleanupTests([ server ])
149 })
150 })