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,
22 } from '@shared/extra-utils'
23 import { User, VideoPrivacy } from '@shared/models'
24 import { ScopedToken } from '@shared/models/users/user-scoped-token'
26 chai.use(require('chai-xml'))
27 chai.use(require('chai-json-schema'))
28 chai.config.includeStack = true
29 const expect = chai.expect
31 describe('Test syndication feeds', () => {
32 let servers: ServerInfo[] = []
33 let serverHLSOnly: ServerInfo
34 let userAccessToken: string
35 let rootAccountId: number
36 let rootChannelId: number
37 let userAccountId: number
38 let userChannelId: number
39 let userFeedToken: string
41 before(async function () {
45 servers = await flushAndRunMultipleServers(2)
46 serverHLSOnly = await flushAndRunServer(3, {
49 webtorrent: { enabled: false },
50 hls: { enabled: true }
54 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
55 await doubleFollow(servers[0], servers[1])
58 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
59 const user: User = res.body
60 rootAccountId = user.account.id
61 rootChannelId = user.videoChannels[0].id
65 const attr = { username: 'john', password: 'password' }
66 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
67 userAccessToken = await userLogin(servers[0], attr)
69 const res = await getMyUserInformation(servers[0].url, userAccessToken)
70 const user: User = res.body
71 userAccountId = user.account.id
72 userChannelId = user.videoChannels[0].id
74 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
75 const token: ScopedToken = res2.body
76 userFeedToken = token.feedToken
80 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
84 const videoAttributes = {
85 name: 'my super name for server 1',
86 description: 'my super description for server 1',
87 fixture: 'video_short.webm'
89 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
90 const videoId = res.body.video.id
92 await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 1' })
93 await servers[0].commentsCommand.createThread({ videoId, text: 'super comment 2' })
97 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
98 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
99 const videoId = res.body.video.id
101 await servers[0].commentsCommand.createThread({ videoId, text: 'comment on unlisted video' })
104 await waitJobs(servers)
107 describe('All feed', function () {
109 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
110 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
111 const rss = await servers[0].feedCommand.getXML({ feed })
112 expect(rss).xml.to.be.valid()
114 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
115 expect(atom).xml.to.be.valid()
119 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
120 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
121 const jsonText = await servers[0].feedCommand.getJSON({ feed })
122 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
127 describe('Videos feed', function () {
129 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
130 for (const server of servers) {
131 const rss = await server.feedCommand.getXML({ feed: 'videos' })
132 expect(xmlParser.validate(rss)).to.be.true
134 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
136 const enclosure = xmlDoc.rss.channel.item[0].enclosure
137 expect(enclosure).to.exist
138 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
139 expect(enclosure['@_length']).to.equal(218910)
140 expect(enclosure['@_url']).to.contain('720.torrent')
144 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
145 for (const server of servers) {
146 const json = await server.feedCommand.getJSON({ feed: 'videos' })
147 const jsonObj = JSON.parse(json)
148 expect(jsonObj.items.length).to.be.equal(2)
149 expect(jsonObj.items[0].attachments).to.exist
150 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
151 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
152 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
153 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
157 it('Should filter by account', async function () {
159 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
160 const jsonObj = JSON.parse(json)
161 expect(jsonObj.items.length).to.be.equal(1)
162 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
163 expect(jsonObj.items[0].author.name).to.equal('root')
167 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
168 const jsonObj = JSON.parse(json)
169 expect(jsonObj.items.length).to.be.equal(1)
170 expect(jsonObj.items[0].title).to.equal('user video')
171 expect(jsonObj.items[0].author.name).to.equal('john')
174 for (const server of servers) {
176 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
177 const jsonObj = JSON.parse(json)
178 expect(jsonObj.items.length).to.be.equal(1)
179 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
183 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
184 const jsonObj = JSON.parse(json)
185 expect(jsonObj.items.length).to.be.equal(1)
186 expect(jsonObj.items[0].title).to.equal('user video')
191 it('Should filter by video channel', async function () {
193 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
194 const jsonObj = JSON.parse(json)
195 expect(jsonObj.items.length).to.be.equal(1)
196 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
197 expect(jsonObj.items[0].author.name).to.equal('root')
201 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
202 const jsonObj = JSON.parse(json)
203 expect(jsonObj.items.length).to.be.equal(1)
204 expect(jsonObj.items[0].title).to.equal('user video')
205 expect(jsonObj.items[0].author.name).to.equal('john')
208 for (const server of servers) {
210 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
211 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
212 const jsonObj = JSON.parse(json)
213 expect(jsonObj.items.length).to.be.equal(1)
214 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
218 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
219 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
220 const jsonObj = JSON.parse(json)
221 expect(jsonObj.items.length).to.be.equal(1)
222 expect(jsonObj.items[0].title).to.equal('user video')
227 it('Should correctly have videos feed with HLS only', async function () {
230 await uploadVideo(serverHLSOnly.url, serverHLSOnly.accessToken, { name: 'hls only video' })
232 await waitJobs([ serverHLSOnly ])
234 const json = await serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
235 const jsonObj = JSON.parse(json)
236 expect(jsonObj.items.length).to.be.equal(1)
237 expect(jsonObj.items[0].attachments).to.exist
238 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
240 for (let i = 0; i < 4; i++) {
241 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
242 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
243 expect(jsonObj.items[0].attachments[i].url).to.exist
248 describe('Video comments feed', function () {
250 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
251 for (const server of servers) {
252 const json = await server.feedCommand.getJSON({ feed: 'video-comments' })
254 const jsonObj = JSON.parse(json)
255 expect(jsonObj.items.length).to.be.equal(2)
256 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
257 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
261 it('Should not list comments from muted accounts or instances', async function () {
264 const remoteHandle = 'root@localhost:' + servers[0].port
266 await servers[1].blocklistCommand.addToServerBlocklist({ account: remoteHandle })
269 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
270 const jsonObj = JSON.parse(json)
271 expect(jsonObj.items.length).to.be.equal(0)
274 await servers[1].blocklistCommand.removeFromServerBlocklist({ account: remoteHandle })
277 const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
278 await waitJobs(servers)
279 await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'super comment' })
280 await waitJobs(servers)
282 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
283 const jsonObj = JSON.parse(json)
284 expect(jsonObj.items.length).to.be.equal(3)
287 await servers[1].blocklistCommand.addToMyBlocklist({ account: remoteHandle })
290 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
291 const jsonObj = JSON.parse(json)
292 expect(jsonObj.items.length).to.be.equal(2)
297 describe('Video feed from my subscriptions', function () {
298 let feeduserAccountId: number
299 let feeduserFeedToken: string
301 it('Should list no videos for a user with no videos and no subscriptions', async function () {
302 const attr = { username: 'feeduser', password: 'password' }
303 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
304 const feeduserAccessToken = await userLogin(servers[0], attr)
307 const res = await getMyUserInformation(servers[0].url, feeduserAccessToken)
308 const user: User = res.body
309 feeduserAccountId = user.account.id
313 const res = await getUserScopedTokens(servers[0].url, feeduserAccessToken)
314 const token: ScopedToken = res.body
315 feeduserFeedToken = token.feedToken
319 const body = await servers[0].subscriptionsCommand.listVideos({ token: feeduserAccessToken })
320 expect(body.total).to.equal(0)
322 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
323 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
324 const jsonObj = JSON.parse(json)
325 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
329 it('Should fail with an invalid token', async function () {
330 const query = { accountId: feeduserAccountId, token: 'toto' }
331 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
334 it('Should fail with a token of another user', async function () {
335 const query = { accountId: feeduserAccountId, token: userFeedToken }
336 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
339 it('Should list no videos for a user with videos but no subscriptions', async function () {
340 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
341 expect(body.total).to.equal(0)
343 const query = { accountId: userAccountId, token: userFeedToken }
344 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
345 const jsonObj = JSON.parse(json)
346 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
349 it('Should list self videos for a user with a subscription to themselves', async function () {
352 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
353 await waitJobs(servers)
356 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
357 expect(body.total).to.equal(1)
358 expect(body.data[0].name).to.equal('user video')
360 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
361 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
362 const jsonObj = JSON.parse(json)
363 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
367 it('Should list videos of a user\'s subscription', async function () {
370 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
371 await waitJobs(servers)
374 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
375 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
377 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
378 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
379 const jsonObj = JSON.parse(json)
380 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
384 it('Should renew the token, and so have an invalid old token', async function () {
385 await renewUserScopedTokens(servers[0].url, userAccessToken)
387 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
388 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
391 it('Should succeed with the new token', async function () {
392 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
393 const token: ScopedToken = res2.body
394 userFeedToken = token.feedToken
396 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
397 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
402 after(async function () {
403 await cleanupTests([ ...servers, serverHLSOnly ])