]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
Merge branch 'feature/improve-live' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { XMLParser, XMLValidator } from 'fast-xml-parser'
5 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 createSingleServer,
10 doubleFollow,
11 makeGetRequest,
12 makeRawRequest,
13 PeerTubeServer,
14 setAccessTokensToServers,
15 setDefaultChannelAvatar,
16 stopFfmpeg,
17 waitJobs
18 } from '@shared/server-commands'
19
20 chai.use(require('chai-xml'))
21 chai.use(require('chai-json-schema'))
22 chai.config.includeStack = true
23
24 const expect = chai.expect
25
26 describe('Test syndication feeds', () => {
27 let servers: PeerTubeServer[] = []
28 let serverHLSOnly: PeerTubeServer
29 let userAccessToken: string
30 let rootAccountId: number
31 let rootChannelId: number
32 let userAccountId: number
33 let userChannelId: number
34 let userFeedToken: string
35 let liveId: string
36
37 before(async function () {
38 this.timeout(120000)
39
40 // Run servers
41 servers = await createMultipleServers(2)
42 serverHLSOnly = await createSingleServer(3, {
43 transcoding: {
44 enabled: true,
45 webtorrent: { enabled: false },
46 hls: { enabled: true }
47 }
48 })
49
50 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
51 await setDefaultChannelAvatar(servers[0])
52 await doubleFollow(servers[0], servers[1])
53
54 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
55
56 {
57 const user = await servers[0].users.getMyInfo()
58 rootAccountId = user.account.id
59 rootChannelId = user.videoChannels[0].id
60 }
61
62 {
63 userAccessToken = await servers[0].users.generateUserAndToken('john')
64
65 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
66 userAccountId = user.account.id
67 userChannelId = user.videoChannels[0].id
68
69 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
70 userFeedToken = token.feedToken
71 }
72
73 {
74 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
75 }
76
77 {
78 const attributes = {
79 name: 'my super name for server 1',
80 description: 'my super description for server 1',
81 fixture: 'video_short.webm'
82 }
83 const { id } = await servers[0].videos.upload({ attributes })
84
85 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
86 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
87 }
88
89 {
90 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
91 const { id } = await servers[0].videos.upload({ attributes })
92
93 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
94 }
95
96 await waitJobs(servers)
97 })
98
99 describe('All feed', function () {
100
101 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
102 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
103 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
104 expect(rss).xml.to.be.valid()
105
106 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
107 expect(atom).xml.to.be.valid()
108 }
109 })
110
111 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
112 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
113 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
114 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
115 }
116 })
117
118 it('Should serve the endpoint with a classic request', async function () {
119 await makeGetRequest({
120 url: servers[0].url,
121 path: '/feeds/videos.xml',
122 accept: 'application/xml',
123 expectedStatus: HttpStatusCode.OK_200
124 })
125 })
126
127 it('Should serve the endpoint as a cached request', async function () {
128 const res = await makeGetRequest({
129 url: servers[0].url,
130 path: '/feeds/videos.xml',
131 accept: 'application/xml',
132 expectedStatus: HttpStatusCode.OK_200
133 })
134
135 expect(res.headers['x-api-cache-cached']).to.equal('true')
136 })
137
138 it('Should not serve the endpoint as a cached request', async function () {
139 const res = await makeGetRequest({
140 url: servers[0].url,
141 path: '/feeds/videos.xml?v=186',
142 accept: 'application/xml',
143 expectedStatus: HttpStatusCode.OK_200
144 })
145
146 expect(res.headers['x-api-cache-cached']).to.not.exist
147 })
148
149 it('Should refuse to serve the endpoint without accept header', async function () {
150 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
151 })
152 })
153
154 describe('Videos feed', function () {
155
156 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
157 for (const server of servers) {
158 const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true })
159 expect(XMLValidator.validate(rss)).to.be.true
160
161 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
162 const xmlDoc = parser.parse(rss)
163
164 const enclosure = xmlDoc.rss.channel.item[0].enclosure
165 expect(enclosure).to.exist
166
167 expect(enclosure['@_type']).to.equal('video/webm')
168 expect(enclosure['@_length']).to.equal(218910)
169 expect(enclosure['@_url']).to.contain('-720.webm')
170 }
171 })
172
173 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
174 for (const server of servers) {
175 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
176 const jsonObj = JSON.parse(json)
177 expect(jsonObj.items.length).to.be.equal(2)
178 expect(jsonObj.items[0].attachments).to.exist
179 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
180 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
181 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
182 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
183 }
184 })
185
186 it('Should filter by account', async function () {
187 {
188 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
189 const jsonObj = JSON.parse(json)
190 expect(jsonObj.items.length).to.be.equal(1)
191 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
192 expect(jsonObj.items[0].author.name).to.equal('Main root channel')
193 }
194
195 {
196 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
197 const jsonObj = JSON.parse(json)
198 expect(jsonObj.items.length).to.be.equal(1)
199 expect(jsonObj.items[0].title).to.equal('user video')
200 expect(jsonObj.items[0].author.name).to.equal('Main john channel')
201 }
202
203 for (const server of servers) {
204 {
205 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
206 const jsonObj = JSON.parse(json)
207 expect(jsonObj.items.length).to.be.equal(1)
208 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
209 }
210
211 {
212 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
213 const jsonObj = JSON.parse(json)
214 expect(jsonObj.items.length).to.be.equal(1)
215 expect(jsonObj.items[0].title).to.equal('user video')
216 }
217 }
218 })
219
220 it('Should filter by video channel', async function () {
221 {
222 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
223 const jsonObj = JSON.parse(json)
224 expect(jsonObj.items.length).to.be.equal(1)
225 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
226 expect(jsonObj.items[0].author.name).to.equal('Main root channel')
227 }
228
229 {
230 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
231 const jsonObj = JSON.parse(json)
232 expect(jsonObj.items.length).to.be.equal(1)
233 expect(jsonObj.items[0].title).to.equal('user video')
234 expect(jsonObj.items[0].author.name).to.equal('Main john channel')
235 }
236
237 for (const server of servers) {
238 {
239 const query = { videoChannelName: 'root_channel@' + servers[0].host }
240 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
241 const jsonObj = JSON.parse(json)
242 expect(jsonObj.items.length).to.be.equal(1)
243 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
244 }
245
246 {
247 const query = { videoChannelName: 'john_channel@' + servers[0].host }
248 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
249 const jsonObj = JSON.parse(json)
250 expect(jsonObj.items.length).to.be.equal(1)
251 expect(jsonObj.items[0].title).to.equal('user video')
252 }
253 }
254 })
255
256 it('Should correctly have videos feed with HLS only', async function () {
257 this.timeout(120000)
258
259 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
260
261 await waitJobs([ serverHLSOnly ])
262
263 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
264 const jsonObj = JSON.parse(json)
265 expect(jsonObj.items.length).to.be.equal(1)
266 expect(jsonObj.items[0].attachments).to.exist
267 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
268
269 for (let i = 0; i < 4; i++) {
270 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
271 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
272 expect(jsonObj.items[0].attachments[i].url).to.exist
273 }
274 })
275
276 it('Should not display waiting live videos', async function () {
277 const { uuid } = await servers[0].live.create({
278 fields: {
279 name: 'live',
280 privacy: VideoPrivacy.PUBLIC,
281 channelId: rootChannelId
282 }
283 })
284 liveId = uuid
285
286 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
287
288 const jsonObj = JSON.parse(json)
289 expect(jsonObj.items.length).to.be.equal(2)
290 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
291 expect(jsonObj.items[1].title).to.equal('user video')
292 })
293
294 it('Should display published live videos', async function () {
295 this.timeout(120000)
296
297 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
298 await servers[0].live.waitUntilPublished({ videoId: liveId })
299
300 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
301
302 const jsonObj = JSON.parse(json)
303 expect(jsonObj.items.length).to.be.equal(3)
304 expect(jsonObj.items[0].title).to.equal('live')
305 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
306 expect(jsonObj.items[2].title).to.equal('user video')
307
308 await stopFfmpeg(ffmpeg)
309 })
310
311 it('Should have the channel avatar as feed icon', async function () {
312 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
313
314 const jsonObj = JSON.parse(json)
315 const imageUrl = jsonObj.icon
316 expect(imageUrl).to.include('/lazy-static/avatars/')
317 await makeRawRequest({ url: imageUrl, expectedStatus: HttpStatusCode.OK_200 })
318 })
319 })
320
321 describe('Video comments feed', function () {
322
323 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
324 for (const server of servers) {
325 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
326
327 const jsonObj = JSON.parse(json)
328 expect(jsonObj.items.length).to.be.equal(2)
329 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
330 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
331 }
332 })
333
334 it('Should not list comments from muted accounts or instances', async function () {
335 this.timeout(30000)
336
337 const remoteHandle = 'root@' + servers[0].host
338
339 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
340
341 {
342 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
343 const jsonObj = JSON.parse(json)
344 expect(jsonObj.items.length).to.be.equal(0)
345 }
346
347 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
348
349 {
350 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
351 await waitJobs(servers)
352 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
353 await waitJobs(servers)
354
355 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
356 const jsonObj = JSON.parse(json)
357 expect(jsonObj.items.length).to.be.equal(3)
358 }
359
360 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
361
362 {
363 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
364 const jsonObj = JSON.parse(json)
365 expect(jsonObj.items.length).to.be.equal(2)
366 }
367 })
368 })
369
370 describe('Video feed from my subscriptions', function () {
371 let feeduserAccountId: number
372 let feeduserFeedToken: string
373
374 it('Should list no videos for a user with no videos and no subscriptions', async function () {
375 const attr = { username: 'feeduser', password: 'password' }
376 await servers[0].users.create({ username: attr.username, password: attr.password })
377 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
378
379 {
380 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
381 feeduserAccountId = user.account.id
382 }
383
384 {
385 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
386 feeduserFeedToken = token.feedToken
387 }
388
389 {
390 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
391 expect(body.total).to.equal(0)
392
393 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
394 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
395 const jsonObj = JSON.parse(json)
396 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
397 }
398 })
399
400 it('Should fail with an invalid token', async function () {
401 const query = { accountId: feeduserAccountId, token: 'toto' }
402 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
403 })
404
405 it('Should fail with a token of another user', async function () {
406 const query = { accountId: feeduserAccountId, token: userFeedToken }
407 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
408 })
409
410 it('Should list no videos for a user with videos but no subscriptions', async function () {
411 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
412 expect(body.total).to.equal(0)
413
414 const query = { accountId: userAccountId, token: userFeedToken }
415 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
416 const jsonObj = JSON.parse(json)
417 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
418 })
419
420 it('Should list self videos for a user with a subscription to themselves', async function () {
421 this.timeout(30000)
422
423 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
424 await waitJobs(servers)
425
426 {
427 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
428 expect(body.total).to.equal(1)
429 expect(body.data[0].name).to.equal('user video')
430
431 const query = { accountId: userAccountId, token: userFeedToken }
432 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
433 const jsonObj = JSON.parse(json)
434 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
435 }
436 })
437
438 it('Should list videos of a user\'s subscription', async function () {
439 this.timeout(30000)
440
441 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
442 await waitJobs(servers)
443
444 {
445 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
446 expect(body.total).to.equal(2, 'there should be 2 videos part of the subscription')
447
448 const query = { accountId: userAccountId, token: userFeedToken }
449 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
450 const jsonObj = JSON.parse(json)
451 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
452 }
453 })
454
455 it('Should renew the token, and so have an invalid old token', async function () {
456 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
457
458 const query = { accountId: userAccountId, token: userFeedToken }
459 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
460 })
461
462 it('Should succeed with the new token', async function () {
463 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
464 userFeedToken = token.feedToken
465
466 const query = { accountId: userAccountId, token: userFeedToken }
467 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
468 })
469
470 })
471
472 after(async function () {
473 await cleanupTests([ ...servers, serverHLSOnly ])
474 })
475 })