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
23 const expect = chai.expect
25 describe('Test syndication feeds', () => {
26 let servers: PeerTubeServer[] = []
27 let serverHLSOnly: PeerTubeServer
28 let userAccessToken: string
29 let rootAccountId: number
30 let rootChannelId: number
31 let userAccountId: number
32 let userChannelId: number
33 let userFeedToken: string
36 before(async function () {
40 servers = await createMultipleServers(2)
41 serverHLSOnly = await createSingleServer(3, {
44 webtorrent: { enabled: false },
45 hls: { enabled: true }
49 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
50 await setDefaultChannelAvatar(servers[0])
51 await doubleFollow(servers[0], servers[1])
53 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
56 const user = await servers[0].users.getMyInfo()
57 rootAccountId = user.account.id
58 rootChannelId = user.videoChannels[0].id
62 userAccessToken = await servers[0].users.generateUserAndToken('john')
64 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
65 userAccountId = user.account.id
66 userChannelId = user.videoChannels[0].id
68 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
69 userFeedToken = token.feedToken
73 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
78 name: 'my super name for server 1',
79 description: 'my super description for server 1',
80 fixture: 'video_short.webm'
82 const { id } = await servers[0].videos.upload({ attributes })
84 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
85 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
89 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
90 const { id } = await servers[0].videos.upload({ attributes })
92 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
95 await waitJobs(servers)
98 describe('All feed', function () {
100 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
101 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
102 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
103 expect(rss).xml.to.be.valid()
105 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
106 expect(atom).xml.to.be.valid()
110 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
111 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
112 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
113 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
117 it('Should serve the endpoint with a classic request', async function () {
118 await makeGetRequest({
120 path: '/feeds/videos.xml',
121 accept: 'application/xml',
122 expectedStatus: HttpStatusCode.OK_200
126 it('Should serve the endpoint as a cached request', async function () {
127 const res = await makeGetRequest({
129 path: '/feeds/videos.xml',
130 accept: 'application/xml',
131 expectedStatus: HttpStatusCode.OK_200
134 expect(res.headers['x-api-cache-cached']).to.equal('true')
137 it('Should not serve the endpoint as a cached request', async function () {
138 const res = await makeGetRequest({
140 path: '/feeds/videos.xml?v=186',
141 accept: 'application/xml',
142 expectedStatus: HttpStatusCode.OK_200
145 expect(res.headers['x-api-cache-cached']).to.not.exist
148 it('Should refuse to serve the endpoint without accept header', async function () {
149 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
153 describe('Videos feed', function () {
155 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
156 for (const server of servers) {
157 const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true })
158 expect(XMLValidator.validate(rss)).to.be.true
160 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
161 const xmlDoc = parser.parse(rss)
163 const enclosure = xmlDoc.rss.channel.item[0].enclosure
164 expect(enclosure).to.exist
166 expect(enclosure['@_type']).to.equal('video/webm')
167 expect(enclosure['@_length']).to.equal(218910)
168 expect(enclosure['@_url']).to.contain('-720.webm')
172 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
173 for (const server of servers) {
174 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
175 const jsonObj = JSON.parse(json)
176 expect(jsonObj.items.length).to.be.equal(2)
177 expect(jsonObj.items[0].attachments).to.exist
178 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
179 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
180 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
181 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
185 it('Should filter by account', async function () {
187 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
188 const jsonObj = JSON.parse(json)
189 expect(jsonObj.items.length).to.be.equal(1)
190 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
191 expect(jsonObj.items[0].author.name).to.equal('root')
195 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
196 const jsonObj = JSON.parse(json)
197 expect(jsonObj.items.length).to.be.equal(1)
198 expect(jsonObj.items[0].title).to.equal('user video')
199 expect(jsonObj.items[0].author.name).to.equal('john')
202 for (const server of servers) {
204 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
205 const jsonObj = JSON.parse(json)
206 expect(jsonObj.items.length).to.be.equal(1)
207 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
211 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
212 const jsonObj = JSON.parse(json)
213 expect(jsonObj.items.length).to.be.equal(1)
214 expect(jsonObj.items[0].title).to.equal('user video')
219 it('Should filter by video channel', async function () {
221 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
222 const jsonObj = JSON.parse(json)
223 expect(jsonObj.items.length).to.be.equal(1)
224 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
225 expect(jsonObj.items[0].author.name).to.equal('root')
229 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
230 const jsonObj = JSON.parse(json)
231 expect(jsonObj.items.length).to.be.equal(1)
232 expect(jsonObj.items[0].title).to.equal('user video')
233 expect(jsonObj.items[0].author.name).to.equal('john')
236 for (const server of servers) {
238 const query = { videoChannelName: 'root_channel@' + servers[0].host }
239 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
240 const jsonObj = JSON.parse(json)
241 expect(jsonObj.items.length).to.be.equal(1)
242 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
246 const query = { videoChannelName: 'john_channel@' + servers[0].host }
247 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
248 const jsonObj = JSON.parse(json)
249 expect(jsonObj.items.length).to.be.equal(1)
250 expect(jsonObj.items[0].title).to.equal('user video')
255 it('Should correctly have videos feed with HLS only', async function () {
258 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
260 await waitJobs([ serverHLSOnly ])
262 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
263 const jsonObj = JSON.parse(json)
264 expect(jsonObj.items.length).to.be.equal(1)
265 expect(jsonObj.items[0].attachments).to.exist
266 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
268 for (let i = 0; i < 4; i++) {
269 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
270 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
271 expect(jsonObj.items[0].attachments[i].url).to.exist
275 it('Should not display waiting live videos', async function () {
276 const { uuid } = await servers[0].live.create({
279 privacy: VideoPrivacy.PUBLIC,
280 channelId: rootChannelId
285 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
287 const jsonObj = JSON.parse(json)
288 expect(jsonObj.items.length).to.be.equal(2)
289 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
290 expect(jsonObj.items[1].title).to.equal('user video')
293 it('Should display published live videos', async function () {
296 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
297 await servers[0].live.waitUntilPublished({ videoId: liveId })
299 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
301 const jsonObj = JSON.parse(json)
302 expect(jsonObj.items.length).to.be.equal(3)
303 expect(jsonObj.items[0].title).to.equal('live')
304 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
305 expect(jsonObj.items[2].title).to.equal('user video')
307 await stopFfmpeg(ffmpeg)
311 describe('Video comments feed', function () {
313 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
314 for (const server of servers) {
315 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
317 const jsonObj = JSON.parse(json)
318 expect(jsonObj.items.length).to.be.equal(2)
319 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
320 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
324 it('Should not list comments from muted accounts or instances', async function () {
327 const remoteHandle = 'root@' + servers[0].host
329 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
332 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
333 const jsonObj = JSON.parse(json)
334 expect(jsonObj.items.length).to.be.equal(0)
337 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
340 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
341 await waitJobs(servers)
342 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
343 await waitJobs(servers)
345 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
346 const jsonObj = JSON.parse(json)
347 expect(jsonObj.items.length).to.be.equal(3)
350 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
353 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
354 const jsonObj = JSON.parse(json)
355 expect(jsonObj.items.length).to.be.equal(2)
360 describe('Video feed from my subscriptions', function () {
361 let feeduserAccountId: number
362 let feeduserFeedToken: string
364 it('Should list no videos for a user with no videos and no subscriptions', async function () {
365 const attr = { username: 'feeduser', password: 'password' }
366 await servers[0].users.create({ username: attr.username, password: attr.password })
367 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
370 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
371 feeduserAccountId = user.account.id
375 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
376 feeduserFeedToken = token.feedToken
380 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
381 expect(body.total).to.equal(0)
383 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
384 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
385 const jsonObj = JSON.parse(json)
386 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
390 it('Should fail with an invalid token', async function () {
391 const query = { accountId: feeduserAccountId, token: 'toto' }
392 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
395 it('Should fail with a token of another user', async function () {
396 const query = { accountId: feeduserAccountId, token: userFeedToken }
397 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
400 it('Should list no videos for a user with videos but no subscriptions', async function () {
401 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
402 expect(body.total).to.equal(0)
404 const query = { accountId: userAccountId, token: userFeedToken }
405 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
406 const jsonObj = JSON.parse(json)
407 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
410 it('Should list self videos for a user with a subscription to themselves', async function () {
413 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
414 await waitJobs(servers)
417 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
418 expect(body.total).to.equal(1)
419 expect(body.data[0].name).to.equal('user video')
421 const query = { accountId: userAccountId, token: userFeedToken }
422 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
423 const jsonObj = JSON.parse(json)
424 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
428 it('Should list videos of a user\'s subscription', async function () {
431 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
432 await waitJobs(servers)
435 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
436 expect(body.total).to.equal(2, 'there should be 2 videos part of the subscription')
438 const query = { accountId: userAccountId, token: userFeedToken }
439 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
440 const jsonObj = JSON.parse(json)
441 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
445 it('Should renew the token, and so have an invalid old token', async function () {
446 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
448 const query = { accountId: userAccountId, token: userFeedToken }
449 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
452 it('Should succeed with the new token', async function () {
453 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
454 userFeedToken = token.feedToken
456 const query = { accountId: userAccountId, token: userFeedToken }
457 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
462 after(async function () {
463 await cleanupTests([ ...servers, serverHLSOnly ])