1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai 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 const expect = chai.expect
24 describe('Test syndication feeds', () => {
25 let servers: PeerTubeServer[] = []
26 let serverHLSOnly: PeerTubeServer
27 let userAccessToken: string
28 let rootAccountId: number
29 let rootChannelId: number
30 let userAccountId: number
31 let userChannelId: number
32 let userFeedToken: string
35 before(async function () {
39 servers = await createMultipleServers(2)
40 serverHLSOnly = await createSingleServer(3, {
43 webtorrent: { enabled: false },
44 hls: { enabled: true }
48 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
49 await setDefaultChannelAvatar(servers[0])
50 await doubleFollow(servers[0], servers[1])
52 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
55 const user = await servers[0].users.getMyInfo()
56 rootAccountId = user.account.id
57 rootChannelId = user.videoChannels[0].id
61 userAccessToken = await servers[0].users.generateUserAndToken('john')
63 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
64 userAccountId = user.account.id
65 userChannelId = user.videoChannels[0].id
67 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
68 userFeedToken = token.feedToken
72 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
77 name: 'my super name for server 1',
78 description: 'my super description for server 1',
79 fixture: 'video_short.webm'
81 const { id } = await servers[0].videos.upload({ attributes })
83 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
84 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
88 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
89 const { id } = await servers[0].videos.upload({ attributes })
91 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
94 await waitJobs(servers)
97 describe('All feed', function () {
99 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
100 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
101 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
102 expect(rss).xml.to.be.valid()
104 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
105 expect(atom).xml.to.be.valid()
109 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
110 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
111 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
112 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
116 it('Should serve the endpoint with a classic request', async function () {
117 await makeGetRequest({
119 path: '/feeds/videos.xml',
120 accept: 'application/xml',
121 expectedStatus: HttpStatusCode.OK_200
125 it('Should serve the endpoint as a cached request', async function () {
126 const res = await makeGetRequest({
128 path: '/feeds/videos.xml',
129 accept: 'application/xml',
130 expectedStatus: HttpStatusCode.OK_200
133 expect(res.headers['x-api-cache-cached']).to.equal('true')
136 it('Should not serve the endpoint as a cached request', async function () {
137 const res = await makeGetRequest({
139 path: '/feeds/videos.xml?v=186',
140 accept: 'application/xml',
141 expectedStatus: HttpStatusCode.OK_200
144 expect(res.headers['x-api-cache-cached']).to.not.exist
147 it('Should refuse to serve the endpoint without accept header', async function () {
148 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
152 describe('Videos feed', function () {
154 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
155 for (const server of servers) {
156 const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true })
157 expect(XMLValidator.validate(rss)).to.be.true
159 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
160 const xmlDoc = parser.parse(rss)
162 const enclosure = xmlDoc.rss.channel.item[0].enclosure
163 expect(enclosure).to.exist
165 expect(enclosure['@_type']).to.equal('video/webm')
166 expect(enclosure['@_length']).to.equal(218910)
167 expect(enclosure['@_url']).to.contain('-720.webm')
171 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
172 for (const server of servers) {
173 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
174 const jsonObj = JSON.parse(json)
175 expect(jsonObj.items.length).to.be.equal(2)
176 expect(jsonObj.items[0].attachments).to.exist
177 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
178 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
179 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
180 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
184 it('Should filter by account', async function () {
186 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
187 const jsonObj = JSON.parse(json)
188 expect(jsonObj.items.length).to.be.equal(1)
189 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
190 expect(jsonObj.items[0].author.name).to.equal('root')
194 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
195 const jsonObj = JSON.parse(json)
196 expect(jsonObj.items.length).to.be.equal(1)
197 expect(jsonObj.items[0].title).to.equal('user video')
198 expect(jsonObj.items[0].author.name).to.equal('john')
201 for (const server of servers) {
203 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
204 const jsonObj = JSON.parse(json)
205 expect(jsonObj.items.length).to.be.equal(1)
206 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
210 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
211 const jsonObj = JSON.parse(json)
212 expect(jsonObj.items.length).to.be.equal(1)
213 expect(jsonObj.items[0].title).to.equal('user video')
218 it('Should filter by video channel', async function () {
220 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
221 const jsonObj = JSON.parse(json)
222 expect(jsonObj.items.length).to.be.equal(1)
223 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
224 expect(jsonObj.items[0].author.name).to.equal('root')
228 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
229 const jsonObj = JSON.parse(json)
230 expect(jsonObj.items.length).to.be.equal(1)
231 expect(jsonObj.items[0].title).to.equal('user video')
232 expect(jsonObj.items[0].author.name).to.equal('john')
235 for (const server of servers) {
237 const query = { videoChannelName: 'root_channel@' + servers[0].host }
238 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
239 const jsonObj = JSON.parse(json)
240 expect(jsonObj.items.length).to.be.equal(1)
241 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
245 const query = { videoChannelName: 'john_channel@' + servers[0].host }
246 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
247 const jsonObj = JSON.parse(json)
248 expect(jsonObj.items.length).to.be.equal(1)
249 expect(jsonObj.items[0].title).to.equal('user video')
254 it('Should correctly have videos feed with HLS only', async function () {
257 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
259 await waitJobs([ serverHLSOnly ])
261 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
262 const jsonObj = JSON.parse(json)
263 expect(jsonObj.items.length).to.be.equal(1)
264 expect(jsonObj.items[0].attachments).to.exist
265 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
267 for (let i = 0; i < 4; i++) {
268 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
269 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
270 expect(jsonObj.items[0].attachments[i].url).to.exist
274 it('Should not display waiting live videos', async function () {
275 const { uuid } = await servers[0].live.create({
278 privacy: VideoPrivacy.PUBLIC,
279 channelId: rootChannelId
284 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
286 const jsonObj = JSON.parse(json)
287 expect(jsonObj.items.length).to.be.equal(2)
288 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
289 expect(jsonObj.items[1].title).to.equal('user video')
292 it('Should display published live videos', async function () {
295 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
296 await servers[0].live.waitUntilPublished({ videoId: liveId })
298 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
300 const jsonObj = JSON.parse(json)
301 expect(jsonObj.items.length).to.be.equal(3)
302 expect(jsonObj.items[0].title).to.equal('live')
303 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
304 expect(jsonObj.items[2].title).to.equal('user video')
306 await stopFfmpeg(ffmpeg)
310 describe('Video comments feed', function () {
312 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
313 for (const server of servers) {
314 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
316 const jsonObj = JSON.parse(json)
317 expect(jsonObj.items.length).to.be.equal(2)
318 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
319 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
323 it('Should not list comments from muted accounts or instances', async function () {
326 const remoteHandle = 'root@' + servers[0].host
328 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
331 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
332 const jsonObj = JSON.parse(json)
333 expect(jsonObj.items.length).to.be.equal(0)
336 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
339 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
340 await waitJobs(servers)
341 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
342 await waitJobs(servers)
344 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
345 const jsonObj = JSON.parse(json)
346 expect(jsonObj.items.length).to.be.equal(3)
349 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
352 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
353 const jsonObj = JSON.parse(json)
354 expect(jsonObj.items.length).to.be.equal(2)
359 describe('Video feed from my subscriptions', function () {
360 let feeduserAccountId: number
361 let feeduserFeedToken: string
363 it('Should list no videos for a user with no videos and no subscriptions', async function () {
364 const attr = { username: 'feeduser', password: 'password' }
365 await servers[0].users.create({ username: attr.username, password: attr.password })
366 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
369 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
370 feeduserAccountId = user.account.id
374 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
375 feeduserFeedToken = token.feedToken
379 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
380 expect(body.total).to.equal(0)
382 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
383 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
384 const jsonObj = JSON.parse(json)
385 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
389 it('Should fail with an invalid token', async function () {
390 const query = { accountId: feeduserAccountId, token: 'toto' }
391 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
394 it('Should fail with a token of another user', async function () {
395 const query = { accountId: feeduserAccountId, token: userFeedToken }
396 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
399 it('Should list no videos for a user with videos but no subscriptions', async function () {
400 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
401 expect(body.total).to.equal(0)
403 const query = { accountId: userAccountId, token: userFeedToken }
404 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
405 const jsonObj = JSON.parse(json)
406 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
409 it('Should list self videos for a user with a subscription to themselves', async function () {
412 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
413 await waitJobs(servers)
416 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
417 expect(body.total).to.equal(1)
418 expect(body.data[0].name).to.equal('user video')
420 const query = { accountId: userAccountId, token: userFeedToken }
421 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
422 const jsonObj = JSON.parse(json)
423 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
427 it('Should list videos of a user\'s subscription', async function () {
430 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
431 await waitJobs(servers)
434 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
435 expect(body.total).to.equal(2, 'there should be 2 videos part of the subscription')
437 const query = { accountId: userAccountId, token: userFeedToken }
438 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
439 const jsonObj = JSON.parse(json)
440 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
444 it('Should renew the token, and so have an invalid old token', async function () {
445 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
447 const query = { accountId: userAccountId, token: userFeedToken }
448 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
451 it('Should succeed with the new token', async function () {
452 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
453 userFeedToken = token.feedToken
455 const query = { accountId: userAccountId, token: userFeedToken }
456 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
461 after(async function () {
462 await cleanupTests([ ...servers, serverHLSOnly ])