]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
Bumped to version v5.2.1
[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 PluginsCommand,
15 setAccessTokensToServers,
16 setDefaultChannelAvatar,
17 setDefaultVideoChannel,
18 stopFfmpeg,
19 waitJobs
20 } from '@shared/server-commands'
21
22 chai.use(require('chai-xml'))
23 chai.use(require('chai-json-schema'))
24 chai.config.includeStack = true
25
26 const expect = chai.expect
27
28 describe('Test syndication feeds', () => {
29 let servers: PeerTubeServer[] = []
30 let serverHLSOnly: PeerTubeServer
31
32 let userAccessToken: string
33 let rootAccountId: number
34 let rootChannelId: number
35
36 let userAccountId: number
37 let userChannelId: number
38 let userFeedToken: string
39
40 let liveId: string
41
42 before(async function () {
43 this.timeout(120000)
44
45 // Run servers
46 servers = await createMultipleServers(2)
47 serverHLSOnly = await createSingleServer(3, {
48 transcoding: {
49 enabled: true,
50 webtorrent: { enabled: false },
51 hls: { enabled: true }
52 }
53 })
54
55 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
56 await setDefaultChannelAvatar(servers[0])
57 await setDefaultVideoChannel(servers)
58 await doubleFollow(servers[0], servers[1])
59
60 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
61
62 {
63 const user = await servers[0].users.getMyInfo()
64 rootAccountId = user.account.id
65 rootChannelId = user.videoChannels[0].id
66 }
67
68 {
69 userAccessToken = await servers[0].users.generateUserAndToken('john')
70
71 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
72 userAccountId = user.account.id
73 userChannelId = user.videoChannels[0].id
74
75 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
76 userFeedToken = token.feedToken
77 }
78
79 {
80 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
81 }
82
83 {
84 const attributes = {
85 name: 'my super name for server 1',
86 description: 'my super description for server 1',
87 fixture: 'video_short.webm'
88 }
89 const { id } = await servers[0].videos.upload({ attributes })
90
91 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
92 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
93 }
94
95 {
96 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
97 const { id } = await servers[0].videos.upload({ attributes })
98
99 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
100 }
101
102 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
103
104 await waitJobs([ ...servers, serverHLSOnly ])
105
106 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-podcast-custom-tags') })
107 })
108
109 describe('All feed', function () {
110
111 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
112 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
113 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
114 expect(rss).xml.to.be.valid()
115
116 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
117 expect(atom).xml.to.be.valid()
118 }
119 })
120
121 it('Should be well formed XML (covers Podcast endpoint)', async function () {
122 const podcast = await servers[0].feed.getPodcastXML({ ignoreCache: true, channelId: rootChannelId })
123 expect(podcast).xml.to.be.valid()
124 })
125
126 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
127 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
128 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
129 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
130 }
131 })
132
133 it('Should serve the endpoint with a classic request', async function () {
134 await makeGetRequest({
135 url: servers[0].url,
136 path: '/feeds/videos.xml',
137 accept: 'application/xml',
138 expectedStatus: HttpStatusCode.OK_200
139 })
140 })
141
142 it('Should refuse to serve the endpoint without accept header', async function () {
143 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
144 })
145 })
146
147 describe('Videos feed', function () {
148
149 describe('Podcast feed', function () {
150
151 it('Should contain a valid podcast:alternateEnclosure', async function () {
152 // Since podcast feeds should only work on the server they originate on,
153 // only test the first server where the videos reside
154 const rss = await servers[0].feed.getPodcastXML({ ignoreCache: false, channelId: rootChannelId })
155 expect(XMLValidator.validate(rss)).to.be.true
156
157 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
158 const xmlDoc = parser.parse(rss)
159
160 const itemGuid = xmlDoc.rss.channel.item.guid
161 expect(itemGuid).to.exist
162 expect(itemGuid['@_isPermaLink']).to.equal(true)
163
164 const enclosure = xmlDoc.rss.channel.item.enclosure
165 expect(enclosure).to.exist
166 const alternateEnclosure = xmlDoc.rss.channel.item['podcast:alternateEnclosure']
167 expect(alternateEnclosure).to.exist
168
169 expect(alternateEnclosure['@_type']).to.equal('video/webm')
170 expect(alternateEnclosure['@_length']).to.equal(218910)
171 expect(alternateEnclosure['@_lang']).to.equal('zh')
172 expect(alternateEnclosure['@_title']).to.equal('720p')
173 expect(alternateEnclosure['@_default']).to.equal(true)
174
175 expect(alternateEnclosure['podcast:source'][0]['@_uri']).to.contain('-720.webm')
176 expect(alternateEnclosure['podcast:source'][0]['@_uri']).to.equal(enclosure['@_url'])
177 expect(alternateEnclosure['podcast:source'][1]['@_uri']).to.contain('-720.torrent')
178 expect(alternateEnclosure['podcast:source'][1]['@_contentType']).to.equal('application/x-bittorrent')
179 expect(alternateEnclosure['podcast:source'][2]['@_uri']).to.contain('magnet:?')
180 })
181
182 it('Should contain a valid podcast:alternateEnclosure with HLS only', async function () {
183 const rss = await serverHLSOnly.feed.getPodcastXML({ ignoreCache: false, channelId: rootChannelId })
184 expect(XMLValidator.validate(rss)).to.be.true
185
186 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
187 const xmlDoc = parser.parse(rss)
188
189 const itemGuid = xmlDoc.rss.channel.item.guid
190 expect(itemGuid).to.exist
191 expect(itemGuid['@_isPermaLink']).to.equal(true)
192
193 const enclosure = xmlDoc.rss.channel.item.enclosure
194 const alternateEnclosure = xmlDoc.rss.channel.item['podcast:alternateEnclosure']
195 expect(alternateEnclosure).to.exist
196
197 expect(alternateEnclosure['@_type']).to.equal('application/x-mpegURL')
198 expect(alternateEnclosure['@_lang']).to.equal('zh')
199 expect(alternateEnclosure['@_title']).to.equal('HLS')
200 expect(alternateEnclosure['@_default']).to.equal(true)
201
202 expect(alternateEnclosure['podcast:source']['@_uri']).to.contain('-master.m3u8')
203 expect(alternateEnclosure['podcast:source']['@_uri']).to.equal(enclosure['@_url'])
204 })
205
206 it('Should contain a valid podcast:socialInteract', async function () {
207 const rss = await servers[0].feed.getPodcastXML({ ignoreCache: false, channelId: rootChannelId })
208 expect(XMLValidator.validate(rss)).to.be.true
209
210 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
211 const xmlDoc = parser.parse(rss)
212
213 const item = xmlDoc.rss.channel.item
214 const socialInteract = item['podcast:socialInteract']
215 expect(socialInteract).to.exist
216 expect(socialInteract['@_protocol']).to.equal('activitypub')
217 expect(socialInteract['@_uri']).to.exist
218 expect(socialInteract['@_accountUrl']).to.exist
219 })
220
221 it('Should contain a valid support custom tags for plugins', async function () {
222 const rss = await servers[0].feed.getPodcastXML({ ignoreCache: false, channelId: userChannelId })
223 expect(XMLValidator.validate(rss)).to.be.true
224
225 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
226 const xmlDoc = parser.parse(rss)
227
228 const fooTag = xmlDoc.rss.channel.fooTag
229 expect(fooTag).to.exist
230 expect(fooTag['@_bar']).to.equal('baz')
231 expect(fooTag['#text']).to.equal(42)
232
233 const bizzBuzzItem = xmlDoc.rss.channel['biz:buzzItem']
234 expect(bizzBuzzItem).to.exist
235
236 let nestedTag = bizzBuzzItem.nestedTag
237 expect(nestedTag).to.exist
238 expect(nestedTag).to.equal('example nested tag')
239
240 const item = xmlDoc.rss.channel.item
241 const fizzTag = item.fizzTag
242 expect(fizzTag).to.exist
243 expect(fizzTag['@_bar']).to.equal('baz')
244 expect(fizzTag['#text']).to.equal(21)
245
246 const bizzBuzz = item['biz:buzz']
247 expect(bizzBuzz).to.exist
248
249 nestedTag = bizzBuzz.nestedTag
250 expect(nestedTag).to.exist
251 expect(nestedTag).to.equal('example nested tag')
252 })
253
254 it('Should contain a valid podcast:liveItem for live streams', async function () {
255 this.timeout(120000)
256
257 const { uuid } = await servers[0].live.create({
258 fields: {
259 name: 'live-0',
260 privacy: VideoPrivacy.PUBLIC,
261 channelId: rootChannelId,
262 permanentLive: false
263 }
264 })
265 liveId = uuid
266
267 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
268 await servers[0].live.waitUntilPublished({ videoId: liveId })
269
270 const rss = await servers[0].feed.getPodcastXML({ ignoreCache: false, channelId: rootChannelId })
271 expect(XMLValidator.validate(rss)).to.be.true
272
273 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
274 const xmlDoc = parser.parse(rss)
275 const liveItem = xmlDoc.rss.channel['podcast:liveItem']
276 expect(liveItem.title).to.equal('live-0')
277 expect(liveItem.guid['@_isPermaLink']).to.equal(false)
278 expect(liveItem.guid['#text']).to.contain(`${uuid}_`)
279 expect(liveItem['@_status']).to.equal('live')
280
281 const enclosure = liveItem.enclosure
282 const alternateEnclosure = liveItem['podcast:alternateEnclosure']
283 expect(alternateEnclosure).to.exist
284 expect(alternateEnclosure['@_type']).to.equal('application/x-mpegURL')
285 expect(alternateEnclosure['@_title']).to.equal('HLS live stream')
286 expect(alternateEnclosure['@_default']).to.equal(true)
287
288 expect(alternateEnclosure['podcast:source']['@_uri']).to.contain('/master.m3u8')
289 expect(alternateEnclosure['podcast:source']['@_uri']).to.equal(enclosure['@_url'])
290
291 await stopFfmpeg(ffmpeg)
292
293 await servers[0].live.waitUntilEnded({ videoId: liveId })
294
295 await waitJobs(servers)
296 })
297 })
298
299 describe('JSON feed', function () {
300
301 it('Should contain a valid \'attachments\' object', async function () {
302 for (const server of servers) {
303 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
304 const jsonObj = JSON.parse(json)
305 expect(jsonObj.items.length).to.be.equal(2)
306 expect(jsonObj.items[0].attachments).to.exist
307 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
308 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
309 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
310 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
311 }
312 })
313
314 it('Should filter by account', async function () {
315 {
316 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
317 const jsonObj = JSON.parse(json)
318 expect(jsonObj.items.length).to.be.equal(1)
319 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
320 expect(jsonObj.items[0].author.name).to.equal('Main root channel')
321 }
322
323 {
324 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
325 const jsonObj = JSON.parse(json)
326 expect(jsonObj.items.length).to.be.equal(1)
327 expect(jsonObj.items[0].title).to.equal('user video')
328 expect(jsonObj.items[0].author.name).to.equal('Main john channel')
329 }
330
331 for (const server of servers) {
332 {
333 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
334 const jsonObj = JSON.parse(json)
335 expect(jsonObj.items.length).to.be.equal(1)
336 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
337 }
338
339 {
340 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
341 const jsonObj = JSON.parse(json)
342 expect(jsonObj.items.length).to.be.equal(1)
343 expect(jsonObj.items[0].title).to.equal('user video')
344 }
345 }
346 })
347
348 it('Should filter by video channel', async function () {
349 {
350 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
351 const jsonObj = JSON.parse(json)
352 expect(jsonObj.items.length).to.be.equal(1)
353 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
354 expect(jsonObj.items[0].author.name).to.equal('Main root channel')
355 }
356
357 {
358 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
359 const jsonObj = JSON.parse(json)
360 expect(jsonObj.items.length).to.be.equal(1)
361 expect(jsonObj.items[0].title).to.equal('user video')
362 expect(jsonObj.items[0].author.name).to.equal('Main john channel')
363 }
364
365 for (const server of servers) {
366 {
367 const query = { videoChannelName: 'root_channel@' + servers[0].host }
368 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
369 const jsonObj = JSON.parse(json)
370 expect(jsonObj.items.length).to.be.equal(1)
371 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
372 }
373
374 {
375 const query = { videoChannelName: 'john_channel@' + servers[0].host }
376 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
377 const jsonObj = JSON.parse(json)
378 expect(jsonObj.items.length).to.be.equal(1)
379 expect(jsonObj.items[0].title).to.equal('user video')
380 }
381 }
382 })
383
384 it('Should correctly have videos feed with HLS only', async function () {
385 this.timeout(120000)
386
387 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
388 const jsonObj = JSON.parse(json)
389 expect(jsonObj.items.length).to.be.equal(1)
390 expect(jsonObj.items[0].attachments).to.exist
391 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
392
393 for (let i = 0; i < 4; i++) {
394 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
395 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
396 expect(jsonObj.items[0].attachments[i].url).to.exist
397 }
398 })
399
400 it('Should not display waiting live videos', async function () {
401 const { uuid } = await servers[0].live.create({
402 fields: {
403 name: 'live',
404 privacy: VideoPrivacy.PUBLIC,
405 channelId: rootChannelId
406 }
407 })
408 liveId = uuid
409
410 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
411
412 const jsonObj = JSON.parse(json)
413 expect(jsonObj.items.length).to.be.equal(2)
414 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
415 expect(jsonObj.items[1].title).to.equal('user video')
416 })
417
418 it('Should display published live videos', async function () {
419 this.timeout(120000)
420
421 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
422 await servers[0].live.waitUntilPublished({ videoId: liveId })
423
424 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
425
426 const jsonObj = JSON.parse(json)
427 expect(jsonObj.items.length).to.be.equal(3)
428 expect(jsonObj.items[0].title).to.equal('live')
429 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
430 expect(jsonObj.items[2].title).to.equal('user video')
431
432 await stopFfmpeg(ffmpeg)
433 })
434
435 it('Should have the channel avatar as feed icon', async function () {
436 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
437
438 const jsonObj = JSON.parse(json)
439 const imageUrl = jsonObj.icon
440 expect(imageUrl).to.include('/lazy-static/avatars/')
441 await makeRawRequest({ url: imageUrl, expectedStatus: HttpStatusCode.OK_200 })
442 })
443 })
444 })
445
446 describe('Video comments feed', function () {
447
448 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
449 for (const server of servers) {
450 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
451
452 const jsonObj = JSON.parse(json)
453 expect(jsonObj.items.length).to.be.equal(2)
454 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
455 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
456 }
457 })
458
459 it('Should not list comments from muted accounts or instances', async function () {
460 this.timeout(30000)
461
462 const remoteHandle = 'root@' + servers[0].host
463
464 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
465
466 {
467 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
468 const jsonObj = JSON.parse(json)
469 expect(jsonObj.items.length).to.be.equal(0)
470 }
471
472 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
473
474 {
475 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
476 await waitJobs(servers)
477 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
478 await waitJobs(servers)
479
480 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
481 const jsonObj = JSON.parse(json)
482 expect(jsonObj.items.length).to.be.equal(3)
483 }
484
485 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
486
487 {
488 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
489 const jsonObj = JSON.parse(json)
490 expect(jsonObj.items.length).to.be.equal(2)
491 }
492 })
493 })
494
495 describe('Video feed from my subscriptions', function () {
496 let feeduserAccountId: number
497 let feeduserFeedToken: string
498
499 it('Should list no videos for a user with no videos and no subscriptions', async function () {
500 const attr = { username: 'feeduser', password: 'password' }
501 await servers[0].users.create({ username: attr.username, password: attr.password })
502 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
503
504 {
505 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
506 feeduserAccountId = user.account.id
507 }
508
509 {
510 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
511 feeduserFeedToken = token.feedToken
512 }
513
514 {
515 const body = await servers[0].videos.listMySubscriptionVideos({ token: feeduserAccessToken })
516 expect(body.total).to.equal(0)
517
518 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
519 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
520 const jsonObj = JSON.parse(json)
521 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
522 }
523 })
524
525 it('Should fail with an invalid token', async function () {
526 const query = { accountId: feeduserAccountId, token: 'toto' }
527 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
528 })
529
530 it('Should fail with a token of another user', async function () {
531 const query = { accountId: feeduserAccountId, token: userFeedToken }
532 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
533 })
534
535 it('Should list no videos for a user with videos but no subscriptions', async function () {
536 const body = await servers[0].videos.listMySubscriptionVideos({ token: userAccessToken })
537 expect(body.total).to.equal(0)
538
539 const query = { accountId: userAccountId, token: userFeedToken }
540 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
541 const jsonObj = JSON.parse(json)
542 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
543 })
544
545 it('Should list self videos for a user with a subscription to themselves', async function () {
546 this.timeout(30000)
547
548 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
549 await waitJobs(servers)
550
551 {
552 const body = await servers[0].videos.listMySubscriptionVideos({ token: userAccessToken })
553 expect(body.total).to.equal(1)
554 expect(body.data[0].name).to.equal('user video')
555
556 const query = { accountId: userAccountId, token: userFeedToken }
557 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
558 const jsonObj = JSON.parse(json)
559 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
560 }
561 })
562
563 it('Should list videos of a user\'s subscription', async function () {
564 this.timeout(30000)
565
566 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
567 await waitJobs(servers)
568
569 {
570 const body = await servers[0].videos.listMySubscriptionVideos({ token: userAccessToken })
571 expect(body.total).to.equal(2, 'there should be 2 videos part of the subscription')
572
573 const query = { accountId: userAccountId, token: userFeedToken }
574 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
575 const jsonObj = JSON.parse(json)
576 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
577 }
578 })
579
580 it('Should renew the token, and so have an invalid old token', async function () {
581 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
582
583 const query = { accountId: userAccountId, token: userFeedToken }
584 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
585 })
586
587 it('Should succeed with the new token', async function () {
588 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
589 userFeedToken = token.feedToken
590
591 const query = { accountId: userAccountId, token: userFeedToken }
592 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
593 })
594
595 })
596
597 describe('Cache', function () {
598 const uuids: string[] = []
599
600 function doPodcastRequest () {
601 return makeGetRequest({
602 url: servers[0].url,
603 path: '/feeds/podcast/videos.xml',
604 query: { videoChannelId: servers[0].store.channel.id },
605 accept: 'application/xml',
606 expectedStatus: HttpStatusCode.OK_200
607 })
608 }
609
610 function doVideosRequest (query: { [id: string]: string } = {}) {
611 return makeGetRequest({
612 url: servers[0].url,
613 path: '/feeds/videos.xml',
614 query,
615 accept: 'application/xml',
616 expectedStatus: HttpStatusCode.OK_200
617 })
618 }
619
620 before(async function () {
621 {
622 const { uuid } = await servers[0].videos.quickUpload({ name: 'cache 1' })
623 uuids.push(uuid)
624 }
625
626 {
627 const { uuid } = await servers[0].videos.quickUpload({ name: 'cache 2' })
628 uuids.push(uuid)
629 }
630 })
631
632 it('Should serve the videos endpoint as a cached request', async function () {
633 await doVideosRequest()
634
635 const res = await doVideosRequest()
636
637 expect(res.headers['x-api-cache-cached']).to.equal('true')
638 })
639
640 it('Should not serve the videos endpoint as a cached request', async function () {
641 const res = await doVideosRequest({ v: '186' })
642
643 expect(res.headers['x-api-cache-cached']).to.not.exist
644 })
645
646 it('Should invalidate the podcast feed cache after video deletion', async function () {
647 await doPodcastRequest()
648
649 {
650 const res = await doPodcastRequest()
651 expect(res.headers['x-api-cache-cached']).to.exist
652 }
653
654 await servers[0].videos.remove({ id: uuids[0] })
655
656 {
657 const res = await doPodcastRequest()
658 expect(res.headers['x-api-cache-cached']).to.not.exist
659 }
660 })
661
662 it('Should invalidate the podcast feed cache after video deletion, even after server restart', async function () {
663 this.timeout(120000)
664
665 await doPodcastRequest()
666
667 {
668 const res = await doPodcastRequest()
669 expect(res.headers['x-api-cache-cached']).to.exist
670 }
671
672 await servers[0].kill()
673 await servers[0].run()
674
675 await servers[0].videos.remove({ id: uuids[1] })
676
677 const res = await doPodcastRequest()
678 expect(res.headers['x-api-cache-cached']).to.not.exist
679 })
680
681 })
682
683 after(async function () {
684 await servers[0].plugins.uninstall({ npmName: 'peertube-plugin-test-podcast-custom-tags' })
685
686 await cleanupTests([ ...servers, serverHLSOnly ])
687 })
688 })