]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
Fix RSS feed when HLS only is enabled
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import * as libxmljs from 'libxmljs'
6 import {
7 addAccountToAccountBlocklist,
8 addAccountToServerBlocklist,
9 removeAccountFromServerBlocklist
10 } from '@shared/extra-utils/users/blocklist'
11 import { VideoPrivacy } from '@shared/models'
12 import {
13 cleanupTests,
14 createUser,
15 doubleFollow,
16 flushAndRunMultipleServers,
17 getJSONfeed,
18 getMyUserInformation,
19 getXMLfeed,
20 ServerInfo,
21 setAccessTokensToServers,
22 uploadVideo,
23 uploadVideoAndGetId,
24 userLogin,
25 flushAndRunServer
26 } from '../../../shared/extra-utils'
27 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
28 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
29 import { User } from '../../../shared/models/users'
30
31 chai.use(require('chai-xml'))
32 chai.use(require('chai-json-schema'))
33 chai.config.includeStack = true
34 const expect = chai.expect
35
36 describe('Test syndication feeds', () => {
37 let servers: ServerInfo[] = []
38 let serverHLSOnly: ServerInfo
39 let userAccessToken: string
40 let rootAccountId: number
41 let rootChannelId: number
42 let userAccountId: number
43 let userChannelId: number
44
45 before(async function () {
46 this.timeout(120000)
47
48 // Run servers
49 servers = await flushAndRunMultipleServers(2)
50 serverHLSOnly = await flushAndRunServer(3, {
51 transcoding: {
52 enabled: true,
53 webtorrent: { enabled: false },
54 hls: { enabled: true }
55 }
56 })
57
58 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
59 await doubleFollow(servers[0], servers[1])
60
61 {
62 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
63 const user: User = res.body
64 rootAccountId = user.account.id
65 rootChannelId = user.videoChannels[0].id
66 }
67
68 {
69 const attr = { username: 'john', password: 'password' }
70 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
71 userAccessToken = await userLogin(servers[0], attr)
72
73 const res = await getMyUserInformation(servers[0].url, userAccessToken)
74 const user: User = res.body
75 userAccountId = user.account.id
76 userChannelId = user.videoChannels[0].id
77 }
78
79 {
80 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
81 }
82
83 {
84 const videoAttributes = {
85 name: 'my super name for server 1',
86 description: 'my super description for server 1',
87 fixture: 'video_short.webm'
88 }
89 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
90 const videoId = res.body.video.id
91
92 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
93 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
94 }
95
96 {
97 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
98 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
99 const videoId = res.body.video.id
100
101 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
102 }
103
104 await waitJobs(servers)
105 })
106
107 describe('All feed', function () {
108
109 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
110 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
111 const rss = await getXMLfeed(servers[0].url, feed)
112 expect(rss.text).xml.to.be.valid()
113
114 const atom = await getXMLfeed(servers[0].url, feed, 'atom')
115 expect(atom.text).xml.to.be.valid()
116 }
117 })
118
119 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
120 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
121 const json = await getJSONfeed(servers[0].url, feed)
122 expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
123 }
124 })
125 })
126
127 describe('Videos feed', function () {
128
129 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
130 for (const server of servers) {
131 const rss = await getXMLfeed(server.url, 'videos')
132 const xmlDoc = libxmljs.parseXmlString(rss.text)
133 const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
134 expect(xmlEnclosure).to.exist
135 expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
136 expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
137 expect(xmlEnclosure.attr('url').value()).to.contain('720.torrent')
138 }
139 })
140
141 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
142 for (const server of servers) {
143 const json = await getJSONfeed(server.url, 'videos')
144 const jsonObj = JSON.parse(json.text)
145 expect(jsonObj.items.length).to.be.equal(2)
146 expect(jsonObj.items[0].attachments).to.exist
147 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
148 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
149 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
150 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
151 }
152 })
153
154 it('Should filter by account', async function () {
155 {
156 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: rootAccountId })
157 const jsonObj = JSON.parse(json.text)
158 expect(jsonObj.items.length).to.be.equal(1)
159 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
160 expect(jsonObj.items[0].author.name).to.equal('root')
161 }
162
163 {
164 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId })
165 const jsonObj = JSON.parse(json.text)
166 expect(jsonObj.items.length).to.be.equal(1)
167 expect(jsonObj.items[0].title).to.equal('user video')
168 expect(jsonObj.items[0].author.name).to.equal('john')
169 }
170
171 for (const server of servers) {
172 {
173 const json = await getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
174 const jsonObj = JSON.parse(json.text)
175 expect(jsonObj.items.length).to.be.equal(1)
176 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
177 }
178
179 {
180 const json = await getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
181 const jsonObj = JSON.parse(json.text)
182 expect(jsonObj.items.length).to.be.equal(1)
183 expect(jsonObj.items[0].title).to.equal('user video')
184 }
185 }
186 })
187
188 it('Should filter by video channel', async function () {
189 {
190 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
191 const jsonObj = JSON.parse(json.text)
192 expect(jsonObj.items.length).to.be.equal(1)
193 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
194 expect(jsonObj.items[0].author.name).to.equal('root')
195 }
196
197 {
198 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
199 const jsonObj = JSON.parse(json.text)
200 expect(jsonObj.items.length).to.be.equal(1)
201 expect(jsonObj.items[0].title).to.equal('user video')
202 expect(jsonObj.items[0].author.name).to.equal('john')
203 }
204
205 for (const server of servers) {
206 {
207 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
208 const jsonObj = JSON.parse(json.text)
209 expect(jsonObj.items.length).to.be.equal(1)
210 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
211 }
212
213 {
214 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
215 const jsonObj = JSON.parse(json.text)
216 expect(jsonObj.items.length).to.be.equal(1)
217 expect(jsonObj.items[0].title).to.equal('user video')
218 }
219 }
220 })
221
222 it('Should correctly have videos feed with HLS only', async function () {
223 this.timeout(120000)
224
225 await uploadVideo(serverHLSOnly.url, serverHLSOnly.accessToken, { name: 'hls only video' })
226
227 await waitJobs([ serverHLSOnly ])
228
229 const json = await getJSONfeed(serverHLSOnly.url, 'videos')
230 const jsonObj = JSON.parse(json.text)
231 expect(jsonObj.items.length).to.be.equal(1)
232 expect(jsonObj.items[0].attachments).to.exist
233 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
234
235 for (let i = 0; i < 4; i++) {
236 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
237 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
238 expect(jsonObj.items[0].attachments[i].url).to.exist
239 }
240 })
241 })
242
243 describe('Video comments feed', function () {
244
245 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
246 for (const server of servers) {
247 const json = await getJSONfeed(server.url, 'video-comments')
248
249 const jsonObj = JSON.parse(json.text)
250 expect(jsonObj.items.length).to.be.equal(2)
251 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
252 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
253 }
254 })
255
256 it('Should not list comments from muted accounts or instances', async function () {
257 this.timeout(30000)
258
259 const remoteHandle = 'root@localhost:' + servers[0].port
260
261 await addAccountToServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
262
263 {
264 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 2 })
265 const jsonObj = JSON.parse(json.text)
266 expect(jsonObj.items.length).to.be.equal(0)
267 }
268
269 await removeAccountFromServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
270
271 {
272 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
273 await waitJobs(servers)
274 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'super comment')
275 await waitJobs(servers)
276
277 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 3 })
278 const jsonObj = JSON.parse(json.text)
279 expect(jsonObj.items.length).to.be.equal(3)
280 }
281
282 await addAccountToAccountBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
283
284 {
285 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 4 })
286 const jsonObj = JSON.parse(json.text)
287 expect(jsonObj.items.length).to.be.equal(2)
288 }
289 })
290 })
291
292 after(async function () {
293 await cleanupTests([ ...servers, serverHLSOnly ])
294 })
295 })