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'
11 flushAndRunMultipleServers,
15 renewUserScopedTokens,
17 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { User, VideoPrivacy } from '@shared/models'
23 import { ScopedToken } from '@shared/models/users/user-scoped-token'
25 chai.use(require('chai-xml'))
26 chai.use(require('chai-json-schema'))
27 chai.config.includeStack = true
28 const expect = chai.expect
30 describe('Test syndication feeds', () => {
31 let servers: ServerInfo[] = []
32 let serverHLSOnly: ServerInfo
33 let userAccessToken: string
34 let rootAccountId: number
35 let rootChannelId: number
36 let userAccountId: number
37 let userChannelId: number
38 let userFeedToken: string
40 before(async function () {
44 servers = await flushAndRunMultipleServers(2)
45 serverHLSOnly = await flushAndRunServer(3, {
48 webtorrent: { enabled: false },
49 hls: { enabled: true }
53 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
54 await doubleFollow(servers[0], servers[1])
57 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
58 const user: User = res.body
59 rootAccountId = user.account.id
60 rootChannelId = user.videoChannels[0].id
64 const attr = { username: 'john', password: 'password' }
65 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
66 userAccessToken = await servers[0].loginCommand.getAccessToken(attr)
68 const res = await getMyUserInformation(servers[0].url, userAccessToken)
69 const user: User = res.body
70 userAccountId = user.account.id
71 userChannelId = user.videoChannels[0].id
73 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
74 const token: ScopedToken = res2.body
75 userFeedToken = token.feedToken
79 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
83 const videoAttributes = {
84 name: 'my super name for server 1',
85 description: 'my super description for server 1',
86 fixture: 'video_short.webm'
88 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
89 const videoId = res.body.video.id
91 await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 1' })
92 await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 2' })
96 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
97 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
98 const videoId = res.body.video.id
100 await servers[0].commentsCommand.createThread({ videoId, text: 'comment on unlisted video' })
103 await waitJobs(servers)
106 describe('All feed', function () {
108 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
109 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
110 const rss = await servers[0].feedCommand.getXML({ feed })
111 expect(rss).xml.to.be.valid()
113 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
114 expect(atom).xml.to.be.valid()
118 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
119 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
120 const jsonText = await servers[0].feedCommand.getJSON({ feed })
121 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
126 describe('Videos feed', function () {
128 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
129 for (const server of servers) {
130 const rss = await server.feedCommand.getXML({ feed: 'videos' })
131 expect(xmlParser.validate(rss)).to.be.true
133 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
135 const enclosure = xmlDoc.rss.channel.item[0].enclosure
136 expect(enclosure).to.exist
137 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
138 expect(enclosure['@_length']).to.equal(218910)
139 expect(enclosure['@_url']).to.contain('720.torrent')
143 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
144 for (const server of servers) {
145 const json = await server.feedCommand.getJSON({ feed: 'videos' })
146 const jsonObj = JSON.parse(json)
147 expect(jsonObj.items.length).to.be.equal(2)
148 expect(jsonObj.items[0].attachments).to.exist
149 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
150 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
151 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
152 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
156 it('Should filter by account', async function () {
158 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
159 const jsonObj = JSON.parse(json)
160 expect(jsonObj.items.length).to.be.equal(1)
161 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
162 expect(jsonObj.items[0].author.name).to.equal('root')
166 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
167 const jsonObj = JSON.parse(json)
168 expect(jsonObj.items.length).to.be.equal(1)
169 expect(jsonObj.items[0].title).to.equal('user video')
170 expect(jsonObj.items[0].author.name).to.equal('john')
173 for (const server of servers) {
175 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
176 const jsonObj = JSON.parse(json)
177 expect(jsonObj.items.length).to.be.equal(1)
178 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
182 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
183 const jsonObj = JSON.parse(json)
184 expect(jsonObj.items.length).to.be.equal(1)
185 expect(jsonObj.items[0].title).to.equal('user video')
190 it('Should filter by video channel', async function () {
192 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
193 const jsonObj = JSON.parse(json)
194 expect(jsonObj.items.length).to.be.equal(1)
195 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
196 expect(jsonObj.items[0].author.name).to.equal('root')
200 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
201 const jsonObj = JSON.parse(json)
202 expect(jsonObj.items.length).to.be.equal(1)
203 expect(jsonObj.items[0].title).to.equal('user video')
204 expect(jsonObj.items[0].author.name).to.equal('john')
207 for (const server of servers) {
209 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
210 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
211 const jsonObj = JSON.parse(json)
212 expect(jsonObj.items.length).to.be.equal(1)
213 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
217 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
218 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
219 const jsonObj = JSON.parse(json)
220 expect(jsonObj.items.length).to.be.equal(1)
221 expect(jsonObj.items[0].title).to.equal('user video')
226 it('Should correctly have videos feed with HLS only', async function () {
229 await uploadVideo(serverHLSOnly.url, serverHLSOnly.accessToken, { name: 'hls only video' })
231 await waitJobs([ serverHLSOnly ])
233 const json = await serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
234 const jsonObj = JSON.parse(json)
235 expect(jsonObj.items.length).to.be.equal(1)
236 expect(jsonObj.items[0].attachments).to.exist
237 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
239 for (let i = 0; i < 4; i++) {
240 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
241 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
242 expect(jsonObj.items[0].attachments[i].url).to.exist
247 describe('Video comments feed', function () {
249 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
250 for (const server of servers) {
251 const json = await server.feedCommand.getJSON({ feed: 'video-comments' })
253 const jsonObj = JSON.parse(json)
254 expect(jsonObj.items.length).to.be.equal(2)
255 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
256 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
260 it('Should not list comments from muted accounts or instances', async function () {
263 const remoteHandle = 'root@localhost:' + servers[0].port
265 await servers[1].blocklistCommand.addToServerBlocklist({ account: remoteHandle })
268 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
269 const jsonObj = JSON.parse(json)
270 expect(jsonObj.items.length).to.be.equal(0)
273 await servers[1].blocklistCommand.removeFromServerBlocklist({ account: remoteHandle })
276 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
277 await waitJobs(servers)
278 await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'super comment' })
279 await waitJobs(servers)
281 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
282 const jsonObj = JSON.parse(json)
283 expect(jsonObj.items.length).to.be.equal(3)
286 await servers[1].blocklistCommand.addToMyBlocklist({ account: remoteHandle })
289 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
290 const jsonObj = JSON.parse(json)
291 expect(jsonObj.items.length).to.be.equal(2)
296 describe('Video feed from my subscriptions', function () {
297 let feeduserAccountId: number
298 let feeduserFeedToken: string
300 it('Should list no videos for a user with no videos and no subscriptions', async function () {
301 const attr = { username: 'feeduser', password: 'password' }
302 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
303 const feeduserAccessToken = await servers[0].loginCommand.getAccessToken(attr)
306 const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)
307 const user: User = res.body
308 feeduserAccountId = user.account.id
312 const res = await getUserScopedTokens(servers[0].url, feeduserAccessToken)
313 const token: ScopedToken = res.body
314 feeduserFeedToken = token.feedToken
318 const body = await servers[0].subscriptionsCommand.listVideos({ token: feeduserAccessToken })
319 expect(body.total).to.equal(0)
321 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
322 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
323 const jsonObj = JSON.parse(json)
324 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
328 it('Should fail with an invalid token', async function () {
329 const query = { accountId: feeduserAccountId, token: 'toto' }
330 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
333 it('Should fail with a token of another user', async function () {
334 const query = { accountId: feeduserAccountId, token: userFeedToken }
335 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
338 it('Should list no videos for a user with videos but no subscriptions', async function () {
339 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
340 expect(body.total).to.equal(0)
342 const query = { accountId: userAccountId, token: userFeedToken }
343 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
344 const jsonObj = JSON.parse(json)
345 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
348 it('Should list self videos for a user with a subscription to themselves', async function () {
351 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
352 await waitJobs(servers)
355 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
356 expect(body.total).to.equal(1)
357 expect(body.data[0].name).to.equal('user video')
359 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
360 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
361 const jsonObj = JSON.parse(json)
362 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
366 it('Should list videos of a user\'s subscription', async function () {
369 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
370 await waitJobs(servers)
373 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
374 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
376 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
377 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
378 const jsonObj = JSON.parse(json)
379 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
383 it('Should renew the token, and so have an invalid old token', async function () {
384 await renewUserScopedTokens(servers[0].url, userAccessToken)
386 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
387 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
390 it('Should succeed with the new token', async function () {
391 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
392 const token: ScopedToken = res2.body
393 userFeedToken = token.feedToken
395 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
396 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
401 after(async function () {
402 await cleanupTests([ ...servers, serverHLSOnly ])