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'
6 import { HttpStatusCode } from '@shared/core-utils'
10 flushAndRunMultipleServers,
13 setAccessTokensToServers,
15 } from '@shared/extra-utils'
16 import { 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: ServerInfo[] = []
25 let serverHLSOnly: ServerInfo
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 flushAndRunMultipleServers(2)
38 serverHLSOnly = await flushAndRunServer(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 const attr = { username: 'john', password: 'password' }
57 await servers[0].users.create({ username: attr.username, password: attr.password })
58 userAccessToken = await servers[0].login.getAccessToken(attr)
60 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
61 userAccountId = user.account.id
62 userChannelId = user.videoChannels[0].id
64 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
65 userFeedToken = token.feedToken
69 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
74 name: 'my super name for server 1',
75 description: 'my super description for server 1',
76 fixture: 'video_short.webm'
78 const { id } = await servers[0].videos.upload({ attributes })
80 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
81 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
85 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
86 const { id } = await servers[0].videos.upload({ attributes })
88 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
91 await waitJobs(servers)
94 describe('All feed', function () {
96 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
97 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
98 const rss = await servers[0].feed.getXML({ feed })
99 expect(rss).xml.to.be.valid()
101 const atom = await servers[0].feed.getXML({ feed, format: 'atom' })
102 expect(atom).xml.to.be.valid()
106 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
107 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
108 const jsonText = await servers[0].feed.getJSON({ feed })
109 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
114 describe('Videos feed', function () {
116 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
117 for (const server of servers) {
118 const rss = await server.feed.getXML({ feed: 'videos' })
119 expect(xmlParser.validate(rss)).to.be.true
121 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
123 const enclosure = xmlDoc.rss.channel.item[0].enclosure
124 expect(enclosure).to.exist
125 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
126 expect(enclosure['@_length']).to.equal(218910)
127 expect(enclosure['@_url']).to.contain('720.torrent')
131 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
132 for (const server of servers) {
133 const json = await server.feed.getJSON({ feed: 'videos' })
134 const jsonObj = JSON.parse(json)
135 expect(jsonObj.items.length).to.be.equal(2)
136 expect(jsonObj.items[0].attachments).to.exist
137 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
138 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
139 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
140 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
144 it('Should filter by account', async function () {
146 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
147 const jsonObj = JSON.parse(json)
148 expect(jsonObj.items.length).to.be.equal(1)
149 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
150 expect(jsonObj.items[0].author.name).to.equal('root')
154 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
155 const jsonObj = JSON.parse(json)
156 expect(jsonObj.items.length).to.be.equal(1)
157 expect(jsonObj.items[0].title).to.equal('user video')
158 expect(jsonObj.items[0].author.name).to.equal('john')
161 for (const server of servers) {
163 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
164 const jsonObj = JSON.parse(json)
165 expect(jsonObj.items.length).to.be.equal(1)
166 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
170 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
171 const jsonObj = JSON.parse(json)
172 expect(jsonObj.items.length).to.be.equal(1)
173 expect(jsonObj.items[0].title).to.equal('user video')
178 it('Should filter by video channel', async function () {
180 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
181 const jsonObj = JSON.parse(json)
182 expect(jsonObj.items.length).to.be.equal(1)
183 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
184 expect(jsonObj.items[0].author.name).to.equal('root')
188 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
189 const jsonObj = JSON.parse(json)
190 expect(jsonObj.items.length).to.be.equal(1)
191 expect(jsonObj.items[0].title).to.equal('user video')
192 expect(jsonObj.items[0].author.name).to.equal('john')
195 for (const server of servers) {
197 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
198 const json = await server.feed.getJSON({ feed: 'videos', query })
199 const jsonObj = JSON.parse(json)
200 expect(jsonObj.items.length).to.be.equal(1)
201 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
205 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
206 const json = await server.feed.getJSON({ feed: 'videos', query })
207 const jsonObj = JSON.parse(json)
208 expect(jsonObj.items.length).to.be.equal(1)
209 expect(jsonObj.items[0].title).to.equal('user video')
214 it('Should correctly have videos feed with HLS only', async function () {
217 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
219 await waitJobs([ serverHLSOnly ])
221 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
222 const jsonObj = JSON.parse(json)
223 expect(jsonObj.items.length).to.be.equal(1)
224 expect(jsonObj.items[0].attachments).to.exist
225 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
227 for (let i = 0; i < 4; i++) {
228 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
229 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
230 expect(jsonObj.items[0].attachments[i].url).to.exist
235 describe('Video comments feed', function () {
237 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
238 for (const server of servers) {
239 const json = await server.feed.getJSON({ feed: 'video-comments' })
241 const jsonObj = JSON.parse(json)
242 expect(jsonObj.items.length).to.be.equal(2)
243 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
244 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
248 it('Should not list comments from muted accounts or instances', async function () {
251 const remoteHandle = 'root@localhost:' + servers[0].port
253 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
256 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
257 const jsonObj = JSON.parse(json)
258 expect(jsonObj.items.length).to.be.equal(0)
261 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
264 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
265 await waitJobs(servers)
266 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
267 await waitJobs(servers)
269 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
270 const jsonObj = JSON.parse(json)
271 expect(jsonObj.items.length).to.be.equal(3)
274 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
277 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
278 const jsonObj = JSON.parse(json)
279 expect(jsonObj.items.length).to.be.equal(2)
284 describe('Video feed from my subscriptions', function () {
285 let feeduserAccountId: number
286 let feeduserFeedToken: string
288 it('Should list no videos for a user with no videos and no subscriptions', async function () {
289 const attr = { username: 'feeduser', password: 'password' }
290 await servers[0].users.create({ username: attr.username, password: attr.password })
291 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
294 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
295 feeduserAccountId = user.account.id
299 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
300 feeduserFeedToken = token.feedToken
304 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
305 expect(body.total).to.equal(0)
307 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
308 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
309 const jsonObj = JSON.parse(json)
310 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
314 it('Should fail with an invalid token', async function () {
315 const query = { accountId: feeduserAccountId, token: 'toto' }
316 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
319 it('Should fail with a token of another user', async function () {
320 const query = { accountId: feeduserAccountId, token: userFeedToken }
321 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
324 it('Should list no videos for a user with videos but no subscriptions', async function () {
325 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
326 expect(body.total).to.equal(0)
328 const query = { accountId: userAccountId, token: userFeedToken }
329 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
330 const jsonObj = JSON.parse(json)
331 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
334 it('Should list self videos for a user with a subscription to themselves', async function () {
337 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
338 await waitJobs(servers)
341 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
342 expect(body.total).to.equal(1)
343 expect(body.data[0].name).to.equal('user video')
345 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
346 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
347 const jsonObj = JSON.parse(json)
348 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
352 it('Should list videos of a user\'s subscription', async function () {
355 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
356 await waitJobs(servers)
359 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
360 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
362 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
363 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
364 const jsonObj = JSON.parse(json)
365 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
369 it('Should renew the token, and so have an invalid old token', async function () {
370 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
372 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
373 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
376 it('Should succeed with the new token', async function () {
377 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
378 userFeedToken = token.feedToken
380 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
381 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
386 after(async function () {
387 await cleanupTests([ ...servers, serverHLSOnly ])