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'
7 addAccountToAccountBlocklist,
8 addAccountToServerBlocklist,
9 removeAccountFromServerBlocklist
10 } from '@shared/extra-utils/users/blocklist'
11 import { addUserSubscription, listUserSubscriptionVideos } from '@shared/extra-utils/users/user-subscriptions'
12 import { VideoPrivacy } from '@shared/models'
13 import { ScopedToken } from '@shared/models/users/user-scoped-token'
18 flushAndRunMultipleServers,
24 renewUserScopedTokens,
26 setAccessTokensToServers,
30 } from '../../../shared/extra-utils'
31 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
32 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
33 import { User } from '../../../shared/models/users'
34 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
36 chai.use(require('chai-xml'))
37 chai.use(require('chai-json-schema'))
38 chai.config.includeStack = true
39 const expect = chai.expect
41 describe('Test syndication feeds', () => {
42 let servers: ServerInfo[] = []
43 let serverHLSOnly: ServerInfo
44 let userAccessToken: string
45 let rootAccountId: number
46 let rootChannelId: number
47 let userAccountId: number
48 let userChannelId: number
49 let userFeedToken: string
51 before(async function () {
55 servers = await flushAndRunMultipleServers(2)
56 serverHLSOnly = await flushAndRunServer(3, {
59 webtorrent: { enabled: false },
60 hls: { enabled: true }
64 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
65 await doubleFollow(servers[0], servers[1])
68 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
69 const user: User = res.body
70 rootAccountId = user.account.id
71 rootChannelId = user.videoChannels[0].id
75 const attr = { username: 'john', password: 'password' }
76 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
77 userAccessToken = await userLogin(servers[0], attr)
79 const res = await getMyUserInformation(servers[0].url, userAccessToken)
80 const user: User = res.body
81 userAccountId = user.account.id
82 userChannelId = user.videoChannels[0].id
84 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
85 const token: ScopedToken = res2.body
86 userFeedToken = token.feedToken
90 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
94 const videoAttributes = {
95 name: 'my super name for server 1',
96 description: 'my super description for server 1',
97 fixture: 'video_short.webm'
99 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
100 const videoId = res.body.video.id
102 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
103 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
107 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
108 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
109 const videoId = res.body.video.id
111 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
114 await waitJobs(servers)
117 describe('All feed', function () {
119 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
120 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
121 const rss = await getXMLfeed(servers[0].url, feed)
122 expect(rss.text).xml.to.be.valid()
124 const atom = await getXMLfeed(servers[0].url, feed, 'atom')
125 expect(atom.text).xml.to.be.valid()
129 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
130 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
131 const json = await getJSONfeed(servers[0].url, feed)
132 expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
137 describe('Videos feed', function () {
139 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
140 for (const server of servers) {
141 const rss = await getXMLfeed(server.url, 'videos')
142 expect(xmlParser.validate(rss.text)).to.be.true
144 const xmlDoc = xmlParser.parse(rss.text, { parseAttributeValue: true, ignoreAttributes: false })
146 const enclosure = xmlDoc.rss.channel.item[0].enclosure
147 expect(enclosure).to.exist
148 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
149 expect(enclosure['@_length']).to.equal(218910)
150 expect(enclosure['@_url']).to.contain('720.torrent')
154 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
155 for (const server of servers) {
156 const json = await getJSONfeed(server.url, 'videos')
157 const jsonObj = JSON.parse(json.text)
158 expect(jsonObj.items.length).to.be.equal(2)
159 expect(jsonObj.items[0].attachments).to.exist
160 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
161 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
162 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
163 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
167 it('Should filter by account', async function () {
169 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: rootAccountId })
170 const jsonObj = JSON.parse(json.text)
171 expect(jsonObj.items.length).to.be.equal(1)
172 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
173 expect(jsonObj.items[0].author.name).to.equal('root')
177 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId })
178 const jsonObj = JSON.parse(json.text)
179 expect(jsonObj.items.length).to.be.equal(1)
180 expect(jsonObj.items[0].title).to.equal('user video')
181 expect(jsonObj.items[0].author.name).to.equal('john')
184 for (const server of servers) {
186 const json = await getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
187 const jsonObj = JSON.parse(json.text)
188 expect(jsonObj.items.length).to.be.equal(1)
189 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
193 const json = await getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
194 const jsonObj = JSON.parse(json.text)
195 expect(jsonObj.items.length).to.be.equal(1)
196 expect(jsonObj.items[0].title).to.equal('user video')
201 it('Should filter by video channel', async function () {
203 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
204 const jsonObj = JSON.parse(json.text)
205 expect(jsonObj.items.length).to.be.equal(1)
206 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
207 expect(jsonObj.items[0].author.name).to.equal('root')
211 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
212 const jsonObj = JSON.parse(json.text)
213 expect(jsonObj.items.length).to.be.equal(1)
214 expect(jsonObj.items[0].title).to.equal('user video')
215 expect(jsonObj.items[0].author.name).to.equal('john')
218 for (const server of servers) {
220 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
221 const jsonObj = JSON.parse(json.text)
222 expect(jsonObj.items.length).to.be.equal(1)
223 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
227 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
228 const jsonObj = JSON.parse(json.text)
229 expect(jsonObj.items.length).to.be.equal(1)
230 expect(jsonObj.items[0].title).to.equal('user video')
235 it('Should correctly have videos feed with HLS only', async function () {
238 await uploadVideo(serverHLSOnly.url, serverHLSOnly.accessToken, { name: 'hls only video' })
240 await waitJobs([ serverHLSOnly ])
242 const json = await getJSONfeed(serverHLSOnly.url, 'videos')
243 const jsonObj = JSON.parse(json.text)
244 expect(jsonObj.items.length).to.be.equal(1)
245 expect(jsonObj.items[0].attachments).to.exist
246 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
248 for (let i = 0; i < 4; i++) {
249 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
250 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
251 expect(jsonObj.items[0].attachments[i].url).to.exist
256 describe('Video comments feed', function () {
258 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
259 for (const server of servers) {
260 const json = await getJSONfeed(server.url, 'video-comments')
262 const jsonObj = JSON.parse(json.text)
263 expect(jsonObj.items.length).to.be.equal(2)
264 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
265 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
269 it('Should not list comments from muted accounts or instances', async function () {
272 const remoteHandle = 'root@localhost:' + servers[0].port
274 await addAccountToServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
277 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 2 })
278 const jsonObj = JSON.parse(json.text)
279 expect(jsonObj.items.length).to.be.equal(0)
282 await removeAccountFromServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
285 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
286 await waitJobs(servers)
287 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'super comment')
288 await waitJobs(servers)
290 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 3 })
291 const jsonObj = JSON.parse(json.text)
292 expect(jsonObj.items.length).to.be.equal(3)
295 await addAccountToAccountBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
298 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 4 })
299 const jsonObj = JSON.parse(json.text)
300 expect(jsonObj.items.length).to.be.equal(2)
305 describe('Video feed from my subscriptions', function () {
306 let feeduserAccountId: number
307 let feeduserFeedToken: string
309 it('Should list no videos for a user with no videos and no subscriptions', async function () {
310 const attr = { username: 'feeduser', password: 'password' }
311 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
312 const feeduserAccessToken = await userLogin(servers[0], attr)
315 const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)
316 const user: User = res.body
317 feeduserAccountId = user.account.id
321 const res = await getUserScopedTokens(servers[0].url, feeduserAccessToken)
322 const token: ScopedToken = res.body
323 feeduserFeedToken = token.feedToken
327 const res = await listUserSubscriptionVideos(servers[0].url, feeduserAccessToken)
328 expect(res.body.total).to.equal(0)
330 const json = await getJSONfeed(servers[0].url, 'subscriptions', { accountId: feeduserAccountId, token: feeduserFeedToken })
331 const jsonObj = JSON.parse(json.text)
332 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
336 it('Should fail with an invalid token', async function () {
337 await getJSONfeed(servers[0].url, 'subscriptions', { accountId: feeduserAccountId, token: 'toto' }, HttpStatusCode.FORBIDDEN_403)
340 it('Should fail with a token of another user', async function () {
344 { accountId: feeduserAccountId, token: userFeedToken },
345 HttpStatusCode.FORBIDDEN_403
349 it('Should list no videos for a user with videos but no subscriptions', async function () {
350 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
351 expect(res.body.total).to.equal(0)
353 const json = await getJSONfeed(servers[0].url, 'subscriptions', { accountId: userAccountId, token: userFeedToken })
354 const jsonObj = JSON.parse(json.text)
355 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
358 it('Should list self videos for a user with a subscription to themselves', async function () {
361 await addUserSubscription(servers[0].url, userAccessToken, 'john_channel@localhost:' + servers[0].port)
362 await waitJobs(servers)
365 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
366 expect(res.body.total).to.equal(1)
367 expect(res.body.data[0].name).to.equal('user video')
369 const json = await getJSONfeed(servers[0].url, 'subscriptions', { accountId: userAccountId, token: userFeedToken, version: 1 })
370 const jsonObj = JSON.parse(json.text)
371 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
375 it('Should list videos of a user\'s subscription', async function () {
378 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port)
379 await waitJobs(servers)
382 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
383 expect(res.body.total).to.equal(2, "there should be 2 videos part of the subscription")
385 const json = await getJSONfeed(servers[0].url, 'subscriptions', { accountId: userAccountId, token: userFeedToken, version: 2 })
386 const jsonObj = JSON.parse(json.text)
387 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
391 it('Should renew the token, and so have an invalid old token', async function () {
392 await renewUserScopedTokens(servers[0].url, userAccessToken)
397 { accountId: userAccountId, token: userFeedToken, version: 3 },
398 HttpStatusCode.FORBIDDEN_403
402 it('Should succeed with the new token', async function () {
403 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
404 const token: ScopedToken = res2.body
405 userFeedToken = token.feedToken
407 await getJSONfeed(servers[0].url, 'subscriptions', { accountId: userAccountId, token: userFeedToken, version: 4 })
412 after(async function () {
413 await cleanupTests([ ...servers, serverHLSOnly ])