1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { XMLParser, XMLValidator } from 'fast-xml-parser'
5 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
13 setAccessTokensToServers,
14 setDefaultChannelAvatar,
17 } from '@shared/server-commands'
19 chai.use(require('chai-xml'))
20 chai.use(require('chai-json-schema'))
21 chai.config.includeStack = true
22 describe('Test syndication feeds', () => {
23 let servers: PeerTubeServer[] = []
24 let serverHLSOnly: PeerTubeServer
25 let userAccessToken: string
26 let rootAccountId: number
27 let rootChannelId: number
28 let userAccountId: number
29 let userChannelId: number
30 let userFeedToken: string
33 before(async function () {
37 servers = await createMultipleServers(2)
38 serverHLSOnly = await createSingleServer(3, {
41 webtorrent: { enabled: false },
42 hls: { enabled: true }
46 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
47 await setDefaultChannelAvatar(servers[0])
48 await doubleFollow(servers[0], servers[1])
50 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
53 const user = await servers[0].users.getMyInfo()
54 rootAccountId = user.account.id
55 rootChannelId = user.videoChannels[0].id
59 userAccessToken = await servers[0].users.generateUserAndToken('john')
61 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
62 userAccountId = user.account.id
63 userChannelId = user.videoChannels[0].id
65 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
66 userFeedToken = token.feedToken
70 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
75 name: 'my super name for server 1',
76 description: 'my super description for server 1',
77 fixture: 'video_short.webm'
79 const { id } = await servers[0].videos.upload({ attributes })
81 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
82 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
86 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
87 const { id } = await servers[0].videos.upload({ attributes })
89 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
92 await waitJobs(servers)
95 describe('All feed', function () {
97 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
98 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
99 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
100 expect(rss).xml.to.be.valid()
102 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
103 expect(atom).xml.to.be.valid()
107 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
108 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
109 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
110 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
114 it('Should serve the endpoint with a classic request', async function () {
115 await makeGetRequest({
117 path: '/feeds/videos.xml',
118 accept: 'application/xml',
119 expectedStatus: HttpStatusCode.OK_200
123 it('Should serve the endpoint as a cached request', async function () {
124 const res = await makeGetRequest({
126 path: '/feeds/videos.xml',
127 accept: 'application/xml',
128 expectedStatus: HttpStatusCode.OK_200
131 expect(res.headers['x-api-cache-cached']).to.equal('true')
134 it('Should not serve the endpoint as a cached request', async function () {
135 const res = await makeGetRequest({
137 path: '/feeds/videos.xml?v=186',
138 accept: 'application/xml',
139 expectedStatus: HttpStatusCode.OK_200
142 expect(res.headers['x-api-cache-cached']).to.not.exist
145 it('Should refuse to serve the endpoint without accept header', async function () {
146 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
150 describe('Videos feed', function () {
152 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
153 for (const server of servers) {
154 const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true })
155 expect(XMLValidator.validate(rss)).to.be.true
157 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
158 const xmlDoc = parser.parse(rss)
160 const enclosure = xmlDoc.rss.channel.item[0].enclosure
161 expect(enclosure).to.exist
163 expect(enclosure['@_type']).to.equal('video/webm')
164 expect(enclosure['@_length']).to.equal(218910)
165 expect(enclosure['@_url']).to.contain('-720.webm')
169 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
170 for (const server of servers) {
171 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
172 const jsonObj = JSON.parse(json)
173 expect(jsonObj.items.length).to.be.equal(2)
174 expect(jsonObj.items[0].attachments).to.exist
175 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
176 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
177 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
178 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
182 it('Should filter by account', async function () {
184 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
185 const jsonObj = JSON.parse(json)
186 expect(jsonObj.items.length).to.be.equal(1)
187 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
188 expect(jsonObj.items[0].author.name).to.equal('root')
192 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
193 const jsonObj = JSON.parse(json)
194 expect(jsonObj.items.length).to.be.equal(1)
195 expect(jsonObj.items[0].title).to.equal('user video')
196 expect(jsonObj.items[0].author.name).to.equal('john')
199 for (const server of servers) {
201 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
202 const jsonObj = JSON.parse(json)
203 expect(jsonObj.items.length).to.be.equal(1)
204 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
208 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
209 const jsonObj = JSON.parse(json)
210 expect(jsonObj.items.length).to.be.equal(1)
211 expect(jsonObj.items[0].title).to.equal('user video')
216 it('Should filter by video channel', async function () {
218 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
219 const jsonObj = JSON.parse(json)
220 expect(jsonObj.items.length).to.be.equal(1)
221 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
222 expect(jsonObj.items[0].author.name).to.equal('root')
226 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
227 const jsonObj = JSON.parse(json)
228 expect(jsonObj.items.length).to.be.equal(1)
229 expect(jsonObj.items[0].title).to.equal('user video')
230 expect(jsonObj.items[0].author.name).to.equal('john')
233 for (const server of servers) {
235 const query = { videoChannelName: 'root_channel@' + servers[0].host }
236 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
237 const jsonObj = JSON.parse(json)
238 expect(jsonObj.items.length).to.be.equal(1)
239 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
243 const query = { videoChannelName: 'john_channel@' + servers[0].host }
244 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
245 const jsonObj = JSON.parse(json)
246 expect(jsonObj.items.length).to.be.equal(1)
247 expect(jsonObj.items[0].title).to.equal('user video')
252 it('Should correctly have videos feed with HLS only', async function () {
255 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
257 await waitJobs([ serverHLSOnly ])
259 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
260 const jsonObj = JSON.parse(json)
261 expect(jsonObj.items.length).to.be.equal(1)
262 expect(jsonObj.items[0].attachments).to.exist
263 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
265 for (let i = 0; i < 4; i++) {
266 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
267 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
268 expect(jsonObj.items[0].attachments[i].url).to.exist
272 it('Should not display waiting live videos', async function () {
273 const { uuid } = await servers[0].live.create({
276 privacy: VideoPrivacy.PUBLIC,
277 channelId: rootChannelId
282 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
284 const jsonObj = JSON.parse(json)
285 expect(jsonObj.items.length).to.be.equal(2)
286 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
287 expect(jsonObj.items[1].title).to.equal('user video')
290 it('Should display published live videos', async function () {
293 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
294 await servers[0].live.waitUntilPublished({ videoId: liveId })
296 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
298 const jsonObj = JSON.parse(json)
299 expect(jsonObj.items.length).to.be.equal(3)
300 expect(jsonObj.items[0].title).to.equal('live')
301 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
302 expect(jsonObj.items[2].title).to.equal('user video')
304 await stopFfmpeg(ffmpeg)
308 describe('Video comments feed', function () {
310 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
311 for (const server of servers) {
312 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
314 const jsonObj = JSON.parse(json)
315 expect(jsonObj.items.length).to.be.equal(2)
316 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
317 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
321 it('Should not list comments from muted accounts or instances', async function () {
324 const remoteHandle = 'root@' + servers[0].host
326 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
329 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
330 const jsonObj = JSON.parse(json)
331 expect(jsonObj.items.length).to.be.equal(0)
334 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
337 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
338 await waitJobs(servers)
339 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
340 await waitJobs(servers)
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(3)
347 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
350 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
351 const jsonObj = JSON.parse(json)
352 expect(jsonObj.items.length).to.be.equal(2)
357 describe('Video feed from my subscriptions', function () {
358 let feeduserAccountId: number
359 let feeduserFeedToken: string
361 it('Should list no videos for a user with no videos and no subscriptions', async function () {
362 const attr = { username: 'feeduser', password: 'password' }
363 await servers[0].users.create({ username: attr.username, password: attr.password })
364 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
367 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
368 feeduserAccountId = user.account.id
372 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
373 feeduserFeedToken = token.feedToken
377 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
378 expect(body.total).to.equal(0)
380 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
381 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
382 const jsonObj = JSON.parse(json)
383 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
387 it('Should fail with an invalid token', async function () {
388 const query = { accountId: feeduserAccountId, token: 'toto' }
389 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
392 it('Should fail with a token of another user', async function () {
393 const query = { accountId: feeduserAccountId, token: userFeedToken }
394 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
397 it('Should list no videos for a user with videos but no subscriptions', async function () {
398 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
399 expect(body.total).to.equal(0)
401 const query = { accountId: userAccountId, token: userFeedToken }
402 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
403 const jsonObj = JSON.parse(json)
404 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
407 it('Should list self videos for a user with a subscription to themselves', async function () {
410 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
411 await waitJobs(servers)
414 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
415 expect(body.total).to.equal(1)
416 expect(body.data[0].name).to.equal('user video')
418 const query = { accountId: userAccountId, token: userFeedToken }
419 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
420 const jsonObj = JSON.parse(json)
421 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
425 it('Should list videos of a user\'s subscription', async function () {
428 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
429 await waitJobs(servers)
432 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
433 expect(body.total).to.equal(2, 'there should be 2 videos part of the subscription')
435 const query = { accountId: userAccountId, token: userFeedToken }
436 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
437 const jsonObj = JSON.parse(json)
438 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
442 it('Should renew the token, and so have an invalid old token', async function () {
443 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
445 const query = { accountId: userAccountId, token: userFeedToken }
446 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
449 it('Should succeed with the new token', async function () {
450 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
451 userFeedToken = token.feedToken
453 const query = { accountId: userAccountId, token: userFeedToken }
454 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
459 after(async function () {
460 await cleanupTests([ ...servers, serverHLSOnly ])