1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { parse, validate } from 'fast-xml-parser'
13 setAccessTokensToServers,
15 } from '@shared/extra-utils'
16 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
18 chai.use(require('chai-xml'))
19 chai.use(require('chai-json-schema'))
20 chai.config.includeStack = true
21 const expect = chai.expect
23 describe('Test syndication feeds', () => {
24 let servers: PeerTubeServer[] = []
25 let serverHLSOnly: PeerTubeServer
26 let userAccessToken: string
27 let rootAccountId: number
28 let rootChannelId: number
29 let userAccountId: number
30 let userChannelId: number
31 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 doubleFollow(servers[0], servers[1])
50 const user = await servers[0].users.getMyInfo()
51 rootAccountId = user.account.id
52 rootChannelId = user.videoChannels[0].id
56 userAccessToken = await servers[0].users.generateUserAndToken('john')
58 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
59 userAccountId = user.account.id
60 userChannelId = user.videoChannels[0].id
62 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
63 userFeedToken = token.feedToken
67 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
72 name: 'my super name for server 1',
73 description: 'my super description for server 1',
74 fixture: 'video_short.webm'
76 const { id } = await servers[0].videos.upload({ attributes })
78 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
79 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
83 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
84 const { id } = await servers[0].videos.upload({ attributes })
86 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
89 await waitJobs(servers)
92 describe('All feed', function () {
94 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
95 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
96 const rss = await servers[0].feed.getXML({ feed })
97 expect(rss).xml.to.be.valid()
99 const atom = await servers[0].feed.getXML({ feed, format: 'atom' })
100 expect(atom).xml.to.be.valid()
104 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
105 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
106 const jsonText = await servers[0].feed.getJSON({ feed })
107 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
111 it('Should serve the endpoint with a classic request', async function () {
112 await makeGetRequest({
114 path: '/feeds/videos.xml',
115 accept: 'application/xml',
116 expectedStatus: HttpStatusCode.OK_200
120 it('Should serve the endpoint as a cached request', async function () {
121 const res = await makeGetRequest({
123 path: '/feeds/videos.xml',
124 accept: 'application/xml',
125 expectedStatus: HttpStatusCode.OK_200
128 expect(res.headers['x-api-cache-cached']).to.equal('true')
131 it('Should not serve the endpoint as a cached request', async function () {
132 const res = await makeGetRequest({
134 path: '/feeds/videos.xml?v=186',
135 accept: 'application/xml',
136 expectedStatus: HttpStatusCode.OK_200
139 expect(res.headers['x-api-cache-cached']).to.not.exist
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 })
147 describe('Videos feed', function () {
149 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
150 for (const server of servers) {
151 const rss = await server.feed.getXML({ feed: 'videos' })
152 expect(validate(rss)).to.be.true
154 const xmlDoc = parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
156 const enclosure = xmlDoc.rss.channel.item[0].enclosure
157 expect(enclosure).to.exist
158 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
159 expect(enclosure['@_length']).to.equal(218910)
160 expect(enclosure['@_url']).to.contain('720.torrent')
164 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
165 for (const server of servers) {
166 const json = await server.feed.getJSON({ feed: 'videos' })
167 const jsonObj = JSON.parse(json)
168 expect(jsonObj.items.length).to.be.equal(2)
169 expect(jsonObj.items[0].attachments).to.exist
170 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
171 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
172 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
173 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
177 it('Should filter by account', async function () {
179 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
180 const jsonObj = JSON.parse(json)
181 expect(jsonObj.items.length).to.be.equal(1)
182 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
183 expect(jsonObj.items[0].author.name).to.equal('root')
187 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
188 const jsonObj = JSON.parse(json)
189 expect(jsonObj.items.length).to.be.equal(1)
190 expect(jsonObj.items[0].title).to.equal('user video')
191 expect(jsonObj.items[0].author.name).to.equal('john')
194 for (const server of servers) {
196 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
197 const jsonObj = JSON.parse(json)
198 expect(jsonObj.items.length).to.be.equal(1)
199 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
203 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
204 const jsonObj = JSON.parse(json)
205 expect(jsonObj.items.length).to.be.equal(1)
206 expect(jsonObj.items[0].title).to.equal('user video')
211 it('Should filter by video channel', async function () {
213 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
214 const jsonObj = JSON.parse(json)
215 expect(jsonObj.items.length).to.be.equal(1)
216 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
217 expect(jsonObj.items[0].author.name).to.equal('root')
221 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
222 const jsonObj = JSON.parse(json)
223 expect(jsonObj.items.length).to.be.equal(1)
224 expect(jsonObj.items[0].title).to.equal('user video')
225 expect(jsonObj.items[0].author.name).to.equal('john')
228 for (const server of servers) {
230 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
231 const json = await server.feed.getJSON({ feed: 'videos', query })
232 const jsonObj = JSON.parse(json)
233 expect(jsonObj.items.length).to.be.equal(1)
234 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
238 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
239 const json = await server.feed.getJSON({ feed: 'videos', query })
240 const jsonObj = JSON.parse(json)
241 expect(jsonObj.items.length).to.be.equal(1)
242 expect(jsonObj.items[0].title).to.equal('user video')
247 it('Should correctly have videos feed with HLS only', async function () {
250 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
252 await waitJobs([ serverHLSOnly ])
254 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
255 const jsonObj = JSON.parse(json)
256 expect(jsonObj.items.length).to.be.equal(1)
257 expect(jsonObj.items[0].attachments).to.exist
258 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
260 for (let i = 0; i < 4; i++) {
261 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
262 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
263 expect(jsonObj.items[0].attachments[i].url).to.exist
268 describe('Video comments feed', function () {
270 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
271 for (const server of servers) {
272 const json = await server.feed.getJSON({ feed: 'video-comments' })
274 const jsonObj = JSON.parse(json)
275 expect(jsonObj.items.length).to.be.equal(2)
276 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
277 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
281 it('Should not list comments from muted accounts or instances', async function () {
284 const remoteHandle = 'root@localhost:' + servers[0].port
286 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
289 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
290 const jsonObj = JSON.parse(json)
291 expect(jsonObj.items.length).to.be.equal(0)
294 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
297 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
298 await waitJobs(servers)
299 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
300 await waitJobs(servers)
302 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
303 const jsonObj = JSON.parse(json)
304 expect(jsonObj.items.length).to.be.equal(3)
307 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
310 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
311 const jsonObj = JSON.parse(json)
312 expect(jsonObj.items.length).to.be.equal(2)
317 describe('Video feed from my subscriptions', function () {
318 let feeduserAccountId: number
319 let feeduserFeedToken: string
321 it('Should list no videos for a user with no videos and no subscriptions', async function () {
322 const attr = { username: 'feeduser', password: 'password' }
323 await servers[0].users.create({ username: attr.username, password: attr.password })
324 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
327 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
328 feeduserAccountId = user.account.id
332 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
333 feeduserFeedToken = token.feedToken
337 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
338 expect(body.total).to.equal(0)
340 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
341 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
342 const jsonObj = JSON.parse(json)
343 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
347 it('Should fail with an invalid token', async function () {
348 const query = { accountId: feeduserAccountId, token: 'toto' }
349 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
352 it('Should fail with a token of another user', async function () {
353 const query = { accountId: feeduserAccountId, token: userFeedToken }
354 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
357 it('Should list no videos for a user with videos but no subscriptions', async function () {
358 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
359 expect(body.total).to.equal(0)
361 const query = { accountId: userAccountId, token: userFeedToken }
362 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
363 const jsonObj = JSON.parse(json)
364 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
367 it('Should list self videos for a user with a subscription to themselves', async function () {
370 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
371 await waitJobs(servers)
374 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
375 expect(body.total).to.equal(1)
376 expect(body.data[0].name).to.equal('user video')
378 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
379 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
380 const jsonObj = JSON.parse(json)
381 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
385 it('Should list videos of a user\'s subscription', async function () {
388 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
389 await waitJobs(servers)
392 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
393 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
395 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
396 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
397 const jsonObj = JSON.parse(json)
398 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
402 it('Should renew the token, and so have an invalid old token', async function () {
403 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
405 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
406 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
409 it('Should succeed with the new token', async function () {
410 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
411 userFeedToken = token.feedToken
413 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
414 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
419 after(async function () {
420 await cleanupTests([ ...servers, serverHLSOnly ])