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'
13 flushAndRunMultipleServers,
17 listUserSubscriptionVideos,
18 renewUserScopedTokens,
20 setAccessTokensToServers,
25 } from '@shared/extra-utils'
26 import { User, VideoPrivacy } from '@shared/models'
27 import { ScopedToken } from '@shared/models/users/user-scoped-token'
29 chai.use(require('chai-xml'))
30 chai.use(require('chai-json-schema'))
31 chai.config.includeStack = true
32 const expect = chai.expect
34 describe('Test syndication feeds', () => {
35 let servers: ServerInfo[] = []
36 let serverHLSOnly: ServerInfo
37 let userAccessToken: string
38 let rootAccountId: number
39 let rootChannelId: number
40 let userAccountId: number
41 let userChannelId: number
42 let userFeedToken: string
44 before(async function () {
48 servers = await flushAndRunMultipleServers(2)
49 serverHLSOnly = await flushAndRunServer(3, {
52 webtorrent: { enabled: false },
53 hls: { enabled: true }
57 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
58 await doubleFollow(servers[0], servers[1])
61 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
62 const user: User = res.body
63 rootAccountId = user.account.id
64 rootChannelId = user.videoChannels[0].id
68 const attr = { username: 'john', password: 'password' }
69 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
70 userAccessToken = await userLogin(servers[0], attr)
72 const res = await getMyUserInformation(servers[0].url, userAccessToken)
73 const user: User = res.body
74 userAccountId = user.account.id
75 userChannelId = user.videoChannels[0].id
77 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
78 const token: ScopedToken = res2.body
79 userFeedToken = token.feedToken
83 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
87 const videoAttributes = {
88 name: 'my super name for server 1',
89 description: 'my super description for server 1',
90 fixture: 'video_short.webm'
92 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
93 const videoId = res.body.video.id
95 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
96 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
100 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
101 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
102 const videoId = res.body.video.id
104 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
107 await waitJobs(servers)
110 describe('All feed', function () {
112 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
113 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
114 const rss = await servers[0].feedCommand.getXML({ feed })
115 expect(rss).xml.to.be.valid()
117 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
118 expect(atom).xml.to.be.valid()
122 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
123 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
124 const jsonText = await servers[0].feedCommand.getJSON({ feed })
125 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
130 describe('Videos feed', function () {
132 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
133 for (const server of servers) {
134 const rss = await server.feedCommand.getXML({ feed: 'videos' })
135 expect(xmlParser.validate(rss)).to.be.true
137 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
139 const enclosure = xmlDoc.rss.channel.item[0].enclosure
140 expect(enclosure).to.exist
141 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
142 expect(enclosure['@_length']).to.equal(218910)
143 expect(enclosure['@_url']).to.contain('720.torrent')
147 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
148 for (const server of servers) {
149 const json = await server.feedCommand.getJSON({ feed: 'videos' })
150 const jsonObj = JSON.parse(json)
151 expect(jsonObj.items.length).to.be.equal(2)
152 expect(jsonObj.items[0].attachments).to.exist
153 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
154 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
155 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
156 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
160 it('Should filter by account', async function () {
162 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
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')
166 expect(jsonObj.items[0].author.name).to.equal('root')
170 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
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')
174 expect(jsonObj.items[0].author.name).to.equal('john')
177 for (const server of servers) {
179 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
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')
186 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
187 const jsonObj = JSON.parse(json)
188 expect(jsonObj.items.length).to.be.equal(1)
189 expect(jsonObj.items[0].title).to.equal('user video')
194 it('Should filter by video channel', async function () {
196 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
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')
200 expect(jsonObj.items[0].author.name).to.equal('root')
204 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
205 const jsonObj = JSON.parse(json)
206 expect(jsonObj.items.length).to.be.equal(1)
207 expect(jsonObj.items[0].title).to.equal('user video')
208 expect(jsonObj.items[0].author.name).to.equal('john')
211 for (const server of servers) {
213 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
214 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
215 const jsonObj = JSON.parse(json)
216 expect(jsonObj.items.length).to.be.equal(1)
217 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
221 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
222 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
223 const jsonObj = JSON.parse(json)
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 serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
238 const jsonObj = JSON.parse(json)
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 server.feedCommand.getJSON({ feed: 'video-comments' })
257 const jsonObj = JSON.parse(json)
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 servers[1].blocklistCommand.addToServerBlocklist({ account: remoteHandle })
272 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
273 const jsonObj = JSON.parse(json)
274 expect(jsonObj.items.length).to.be.equal(0)
277 await servers[1].blocklistCommand.removeFromServerBlocklist({ account: 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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
286 const jsonObj = JSON.parse(json)
287 expect(jsonObj.items.length).to.be.equal(3)
290 await servers[1].blocklistCommand.addToMyBlocklist({ account: remoteHandle })
293 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
294 const jsonObj = JSON.parse(json)
295 expect(jsonObj.items.length).to.be.equal(2)
300 describe('Video feed from my subscriptions', function () {
301 let feeduserAccountId: number
302 let feeduserFeedToken: string
304 it('Should list no videos for a user with no videos and no subscriptions', async function () {
305 const attr = { username: 'feeduser', password: 'password' }
306 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
307 const feeduserAccessToken = await userLogin(servers[0], attr)
310 const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)
311 const user: User = res.body
312 feeduserAccountId = user.account.id
316 const res = await getUserScopedTokens(servers[0].url, feeduserAccessToken)
317 const token: ScopedToken = res.body
318 feeduserFeedToken = token.feedToken
322 const res = await listUserSubscriptionVideos(servers[0].url, feeduserAccessToken)
323 expect(res.body.total).to.equal(0)
325 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
326 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
327 const jsonObj = JSON.parse(json)
328 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
332 it('Should fail with an invalid token', async function () {
333 const query = { accountId: feeduserAccountId, token: 'toto' }
334 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
337 it('Should fail with a token of another user', async function () {
338 const query = { accountId: feeduserAccountId, token: userFeedToken }
339 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
342 it('Should list no videos for a user with videos but no subscriptions', async function () {
343 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
344 expect(res.body.total).to.equal(0)
346 const query = { accountId: userAccountId, token: userFeedToken }
347 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
348 const jsonObj = JSON.parse(json)
349 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
352 it('Should list self videos for a user with a subscription to themselves', async function () {
355 await addUserSubscription(servers[0].url, userAccessToken, 'john_channel@localhost:' + servers[0].port)
356 await waitJobs(servers)
359 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
360 expect(res.body.total).to.equal(1)
361 expect(res.body.data[0].name).to.equal('user video')
363 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
364 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
365 const jsonObj = JSON.parse(json)
366 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
370 it('Should list videos of a user\'s subscription', async function () {
373 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port)
374 await waitJobs(servers)
377 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
378 expect(res.body.total).to.equal(2, "there should be 2 videos part of the subscription")
380 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
381 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
382 const jsonObj = JSON.parse(json)
383 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
387 it('Should renew the token, and so have an invalid old token', async function () {
388 await renewUserScopedTokens(servers[0].url, userAccessToken)
390 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
391 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
394 it('Should succeed with the new token', async function () {
395 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
396 const token: ScopedToken = res2.body
397 userFeedToken = token.feedToken
399 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
400 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
405 after(async function () {
406 await cleanupTests([ ...servers, serverHLSOnly ])