1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import * as xmlParser from 'fast-xml-parser'
12 setAccessTokensToServers,
14 } from '@shared/extra-utils'
15 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
17 chai.use(require('chai-xml'))
18 chai.use(require('chai-json-schema'))
19 chai.config.includeStack = true
20 const expect = chai.expect
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
32 before(async function () {
36 servers = await createMultipleServers(2)
37 serverHLSOnly = await createSingleServer(3, {
40 webtorrent: { enabled: false },
41 hls: { enabled: true }
45 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
46 await doubleFollow(servers[0], servers[1])
49 const user = await servers[0].users.getMyInfo()
50 rootAccountId = user.account.id
51 rootChannelId = user.videoChannels[0].id
55 const attr = { username: 'john', password: 'password' }
56 await servers[0].users.create({ username: attr.username, password: attr.password })
57 userAccessToken = await servers[0].login.getAccessToken(attr)
59 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
60 userAccountId = user.account.id
61 userChannelId = user.videoChannels[0].id
63 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
64 userFeedToken = token.feedToken
68 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
73 name: 'my super name for server 1',
74 description: 'my super description for server 1',
75 fixture: 'video_short.webm'
77 const { id } = await servers[0].videos.upload({ attributes })
79 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
80 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
84 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
85 const { id } = await servers[0].videos.upload({ attributes })
87 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
90 await waitJobs(servers)
93 describe('All feed', function () {
95 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
96 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
97 const rss = await servers[0].feed.getXML({ feed })
98 expect(rss).xml.to.be.valid()
100 const atom = await servers[0].feed.getXML({ feed, format: 'atom' })
101 expect(atom).xml.to.be.valid()
105 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
106 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
107 const jsonText = await servers[0].feed.getJSON({ feed })
108 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
113 describe('Videos feed', function () {
115 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
116 for (const server of servers) {
117 const rss = await server.feed.getXML({ feed: 'videos' })
118 expect(xmlParser.validate(rss)).to.be.true
120 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
122 const enclosure = xmlDoc.rss.channel.item[0].enclosure
123 expect(enclosure).to.exist
124 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
125 expect(enclosure['@_length']).to.equal(218910)
126 expect(enclosure['@_url']).to.contain('720.torrent')
130 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
131 for (const server of servers) {
132 const json = await server.feed.getJSON({ feed: 'videos' })
133 const jsonObj = JSON.parse(json)
134 expect(jsonObj.items.length).to.be.equal(2)
135 expect(jsonObj.items[0].attachments).to.exist
136 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
137 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
138 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
139 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
143 it('Should filter by account', async function () {
145 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
146 const jsonObj = JSON.parse(json)
147 expect(jsonObj.items.length).to.be.equal(1)
148 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
149 expect(jsonObj.items[0].author.name).to.equal('root')
153 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
154 const jsonObj = JSON.parse(json)
155 expect(jsonObj.items.length).to.be.equal(1)
156 expect(jsonObj.items[0].title).to.equal('user video')
157 expect(jsonObj.items[0].author.name).to.equal('john')
160 for (const server of servers) {
162 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
163 const jsonObj = JSON.parse(json)
164 expect(jsonObj.items.length).to.be.equal(1)
165 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
169 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
170 const jsonObj = JSON.parse(json)
171 expect(jsonObj.items.length).to.be.equal(1)
172 expect(jsonObj.items[0].title).to.equal('user video')
177 it('Should filter by video channel', async function () {
179 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
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: { videoChannelId: userChannelId } })
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 query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
197 const json = await server.feed.getJSON({ feed: 'videos', query })
198 const jsonObj = JSON.parse(json)
199 expect(jsonObj.items.length).to.be.equal(1)
200 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
204 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
205 const json = await server.feed.getJSON({ feed: 'videos', query })
206 const jsonObj = JSON.parse(json)
207 expect(jsonObj.items.length).to.be.equal(1)
208 expect(jsonObj.items[0].title).to.equal('user video')
213 it('Should correctly have videos feed with HLS only', async function () {
216 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
218 await waitJobs([ serverHLSOnly ])
220 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
221 const jsonObj = JSON.parse(json)
222 expect(jsonObj.items.length).to.be.equal(1)
223 expect(jsonObj.items[0].attachments).to.exist
224 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
226 for (let i = 0; i < 4; i++) {
227 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
228 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
229 expect(jsonObj.items[0].attachments[i].url).to.exist
234 describe('Video comments feed', function () {
236 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
237 for (const server of servers) {
238 const json = await server.feed.getJSON({ feed: 'video-comments' })
240 const jsonObj = JSON.parse(json)
241 expect(jsonObj.items.length).to.be.equal(2)
242 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
243 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
247 it('Should not list comments from muted accounts or instances', async function () {
250 const remoteHandle = 'root@localhost:' + servers[0].port
252 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
255 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
256 const jsonObj = JSON.parse(json)
257 expect(jsonObj.items.length).to.be.equal(0)
260 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
263 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
264 await waitJobs(servers)
265 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
266 await waitJobs(servers)
268 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
269 const jsonObj = JSON.parse(json)
270 expect(jsonObj.items.length).to.be.equal(3)
273 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
276 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
277 const jsonObj = JSON.parse(json)
278 expect(jsonObj.items.length).to.be.equal(2)
283 describe('Video feed from my subscriptions', function () {
284 let feeduserAccountId: number
285 let feeduserFeedToken: string
287 it('Should list no videos for a user with no videos and no subscriptions', async function () {
288 const attr = { username: 'feeduser', password: 'password' }
289 await servers[0].users.create({ username: attr.username, password: attr.password })
290 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
293 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
294 feeduserAccountId = user.account.id
298 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
299 feeduserFeedToken = token.feedToken
303 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
304 expect(body.total).to.equal(0)
306 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
307 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
308 const jsonObj = JSON.parse(json)
309 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
313 it('Should fail with an invalid token', async function () {
314 const query = { accountId: feeduserAccountId, token: 'toto' }
315 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
318 it('Should fail with a token of another user', async function () {
319 const query = { accountId: feeduserAccountId, token: userFeedToken }
320 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
323 it('Should list no videos for a user with videos but no subscriptions', async function () {
324 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
325 expect(body.total).to.equal(0)
327 const query = { accountId: userAccountId, token: userFeedToken }
328 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
329 const jsonObj = JSON.parse(json)
330 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
333 it('Should list self videos for a user with a subscription to themselves', async function () {
336 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
337 await waitJobs(servers)
340 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
341 expect(body.total).to.equal(1)
342 expect(body.data[0].name).to.equal('user video')
344 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
345 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
346 const jsonObj = JSON.parse(json)
347 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
351 it('Should list videos of a user\'s subscription', async function () {
354 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
355 await waitJobs(servers)
358 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
359 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
361 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
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(2) // subscribed to root, it should not list the instance's videos but list root/john's
368 it('Should renew the token, and so have an invalid old token', async function () {
369 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
371 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
372 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
375 it('Should succeed with the new token', async function () {
376 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
377 userFeedToken = token.feedToken
379 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
380 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
385 after(async function () {
386 await cleanupTests([ ...servers, serverHLSOnly ])