1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import * as libxmljs from 'libxmljs'
7 addAccountToAccountBlocklist,
8 addAccountToServerBlocklist,
9 removeAccountFromServerBlocklist
10 } from '@shared/extra-utils/users/blocklist'
11 import { VideoPrivacy } from '@shared/models'
16 flushAndRunMultipleServers,
21 setAccessTokensToServers,
27 } from '../../../shared/extra-utils'
28 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
29 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
30 import { User } from '../../../shared/models/users'
31 import { ScopedToken } from '@shared/models/users/user-scoped-token'
32 import { listUserSubscriptionVideos, addUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
34 chai.use(require('chai-xml'))
35 chai.use(require('chai-json-schema'))
36 chai.config.includeStack = true
37 const expect = chai.expect
39 describe('Test syndication feeds', () => {
40 let servers: ServerInfo[] = []
41 let serverHLSOnly: ServerInfo
42 let userAccessToken: string
43 let rootAccountId: number
44 let rootChannelId: number
45 let userAccountId: number
46 let userChannelId: number
47 let userFeedToken: string
49 before(async function () {
53 servers = await flushAndRunMultipleServers(2)
54 serverHLSOnly = await flushAndRunServer(3, {
57 webtorrent: { enabled: false },
58 hls: { enabled: true }
62 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
63 await doubleFollow(servers[0], servers[1])
66 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
67 const user: User = res.body
68 rootAccountId = user.account.id
69 rootChannelId = user.videoChannels[0].id
73 const attr = { username: 'john', password: 'password' }
74 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
75 userAccessToken = await userLogin(servers[0], attr)
77 const res = await getMyUserInformation(servers[0].url, userAccessToken)
78 const user: User = res.body
79 userAccountId = user.account.id
80 userChannelId = user.videoChannels[0].id
82 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
83 const token: ScopedToken = res2.body
84 userFeedToken = token.feedToken
88 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
92 const videoAttributes = {
93 name: 'my super name for server 1',
94 description: 'my super description for server 1',
95 fixture: 'video_short.webm'
97 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
98 const videoId = res.body.video.id
100 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
101 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
105 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
106 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
107 const videoId = res.body.video.id
109 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
112 await waitJobs(servers)
115 describe('All feed', function () {
117 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
118 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
119 const rss = await getXMLfeed(servers[0].url, feed)
120 expect(rss.text).xml.to.be.valid()
122 const atom = await getXMLfeed(servers[0].url, feed, 'atom')
123 expect(atom.text).xml.to.be.valid()
127 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
128 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
129 const json = await getJSONfeed(servers[0].url, feed)
130 expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
135 describe('Videos feed', function () {
137 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
138 for (const server of servers) {
139 const rss = await getXMLfeed(server.url, 'videos')
140 const xmlDoc = libxmljs.parseXmlString(rss.text)
141 const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
142 expect(xmlEnclosure).to.exist
143 expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
144 expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
145 expect(xmlEnclosure.attr('url').value()).to.contain('720.torrent')
149 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
150 for (const server of servers) {
151 const json = await getJSONfeed(server.url, 'videos')
152 const jsonObj = JSON.parse(json.text)
153 expect(jsonObj.items.length).to.be.equal(2)
154 expect(jsonObj.items[0].attachments).to.exist
155 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
156 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
157 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
158 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
162 it('Should filter by account', async function () {
164 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: rootAccountId })
165 const jsonObj = JSON.parse(json.text)
166 expect(jsonObj.items.length).to.be.equal(1)
167 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
168 expect(jsonObj.items[0].author.name).to.equal('root')
172 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId })
173 const jsonObj = JSON.parse(json.text)
174 expect(jsonObj.items.length).to.be.equal(1)
175 expect(jsonObj.items[0].title).to.equal('user video')
176 expect(jsonObj.items[0].author.name).to.equal('john')
179 for (const server of servers) {
181 const json = await getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
182 const jsonObj = JSON.parse(json.text)
183 expect(jsonObj.items.length).to.be.equal(1)
184 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
188 const json = await getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
189 const jsonObj = JSON.parse(json.text)
190 expect(jsonObj.items.length).to.be.equal(1)
191 expect(jsonObj.items[0].title).to.equal('user video')
196 it('Should filter by video channel', async function () {
198 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
199 const jsonObj = JSON.parse(json.text)
200 expect(jsonObj.items.length).to.be.equal(1)
201 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
202 expect(jsonObj.items[0].author.name).to.equal('root')
206 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
207 const jsonObj = JSON.parse(json.text)
208 expect(jsonObj.items.length).to.be.equal(1)
209 expect(jsonObj.items[0].title).to.equal('user video')
210 expect(jsonObj.items[0].author.name).to.equal('john')
213 for (const server of servers) {
215 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
216 const jsonObj = JSON.parse(json.text)
217 expect(jsonObj.items.length).to.be.equal(1)
218 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
222 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
223 const jsonObj = JSON.parse(json.text)
224 expect(jsonObj.items.length).to.be.equal(1)
225 expect(jsonObj.items[0].title).to.equal('user video')
230 it('Should correctly have videos feed with HLS only', async function () {
233 await uploadVideo(serverHLSOnly.url, serverHLSOnly.accessToken, { name: 'hls only video' })
235 await waitJobs([ serverHLSOnly ])
237 const json = await getJSONfeed(serverHLSOnly.url, 'videos')
238 const jsonObj = JSON.parse(json.text)
239 expect(jsonObj.items.length).to.be.equal(1)
240 expect(jsonObj.items[0].attachments).to.exist
241 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
243 for (let i = 0; i < 4; i++) {
244 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
245 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
246 expect(jsonObj.items[0].attachments[i].url).to.exist
251 describe('Video comments feed', function () {
253 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
254 for (const server of servers) {
255 const json = await getJSONfeed(server.url, 'video-comments')
257 const jsonObj = JSON.parse(json.text)
258 expect(jsonObj.items.length).to.be.equal(2)
259 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
260 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
264 it('Should not list comments from muted accounts or instances', async function () {
267 const remoteHandle = 'root@localhost:' + servers[0].port
269 await addAccountToServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
272 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 2 })
273 const jsonObj = JSON.parse(json.text)
274 expect(jsonObj.items.length).to.be.equal(0)
277 await removeAccountFromServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
280 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
281 await waitJobs(servers)
282 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'super comment')
283 await waitJobs(servers)
285 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 3 })
286 const jsonObj = JSON.parse(json.text)
287 expect(jsonObj.items.length).to.be.equal(3)
290 await addAccountToAccountBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
293 const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 4 })
294 const jsonObj = JSON.parse(json.text)
295 expect(jsonObj.items.length).to.be.equal(2)
300 describe('Video feed from my subscriptions', function () {
302 * use the 'version' query parameter to bust cache between tests
305 it('Should list no videos for a user with no videos and no subscriptions', async function () {
306 let feeduserAccountId: number
307 let feeduserFeedToken: string
309 const attr = { username: 'feeduser', password: 'password' }
310 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
311 const feeduserAccessToken = await userLogin(servers[0], attr)
314 const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)
315 const user: User = res.body
316 feeduserAccountId = user.account.id
320 const res = await getUserScopedTokens(servers[0].url, feeduserAccessToken)
321 const token: ScopedToken = res.body
322 feeduserFeedToken = token.feedToken
326 const res = await listUserSubscriptionVideos(servers[0].url, feeduserAccessToken)
327 expect(res.body.total).to.equal(0)
329 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: feeduserAccountId, token: feeduserFeedToken })
330 const jsonObj = JSON.parse(json.text)
331 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
335 it('Should list no videos for a user with videos but no subscriptions', async function () {
337 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
338 expect(res.body.total).to.equal(0)
340 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId, token: userFeedToken })
341 const jsonObj = JSON.parse(json.text)
342 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
346 it('Should list self videos for a user with a subscription to themselves', async function () {
349 await addUserSubscription(servers[0].url, userAccessToken, 'john_channel@localhost:' + servers[0].port)
350 await waitJobs(servers)
353 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
354 expect(res.body.total).to.equal(1)
355 expect(res.body.data[0].name).to.equal('user video')
357 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId, token: userFeedToken, version: 1 })
358 const jsonObj = JSON.parse(json.text)
359 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
363 it('Should list videos of a user\'s subscription', async function () {
366 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port)
367 await waitJobs(servers)
370 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
371 expect(res.body.total).to.equal(2, "there should be 2 videos part of the subscription")
373 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId, token: userFeedToken, version: 2 })
374 const jsonObj = JSON.parse(json.text)
375 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
381 after(async function () {
382 await cleanupTests([ ...servers, serverHLSOnly ])