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'
14 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
19 flushAndRunMultipleServers,
23 renewUserScopedTokens,
25 setAccessTokensToServers,
29 } from '../../../shared/extra-utils'
30 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
31 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
32 import { User } from '../../../shared/models/users'
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 servers[0].feedCommand.getXML({ feed })
120 expect(rss).xml.to.be.valid()
122 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
123 expect(atom).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 jsonText = await servers[0].feedCommand.getJSON({ feed })
130 expect(JSON.parse(jsonText)).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 server.feedCommand.getXML({ feed: 'videos' })
140 expect(xmlParser.validate(rss)).to.be.true
142 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
144 const enclosure = xmlDoc.rss.channel.item[0].enclosure
145 expect(enclosure).to.exist
146 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
147 expect(enclosure['@_length']).to.equal(218910)
148 expect(enclosure['@_url']).to.contain('720.torrent')
152 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
153 for (const server of servers) {
154 const json = await server.feedCommand.getJSON({ feed: 'videos' })
155 const jsonObj = JSON.parse(json)
156 expect(jsonObj.items.length).to.be.equal(2)
157 expect(jsonObj.items[0].attachments).to.exist
158 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
159 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
160 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
161 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
165 it('Should filter by account', async function () {
167 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
168 const jsonObj = JSON.parse(json)
169 expect(jsonObj.items.length).to.be.equal(1)
170 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
171 expect(jsonObj.items[0].author.name).to.equal('root')
175 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
176 const jsonObj = JSON.parse(json)
177 expect(jsonObj.items.length).to.be.equal(1)
178 expect(jsonObj.items[0].title).to.equal('user video')
179 expect(jsonObj.items[0].author.name).to.equal('john')
182 for (const server of servers) {
184 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
185 const jsonObj = JSON.parse(json)
186 expect(jsonObj.items.length).to.be.equal(1)
187 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
191 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
192 const jsonObj = JSON.parse(json)
193 expect(jsonObj.items.length).to.be.equal(1)
194 expect(jsonObj.items[0].title).to.equal('user video')
199 it('Should filter by video channel', async function () {
201 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
202 const jsonObj = JSON.parse(json)
203 expect(jsonObj.items.length).to.be.equal(1)
204 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
205 expect(jsonObj.items[0].author.name).to.equal('root')
209 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
210 const jsonObj = JSON.parse(json)
211 expect(jsonObj.items.length).to.be.equal(1)
212 expect(jsonObj.items[0].title).to.equal('user video')
213 expect(jsonObj.items[0].author.name).to.equal('john')
216 for (const server of servers) {
218 const query = { videoChannelName: 'root_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('my super name for server 1')
226 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
227 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
228 const jsonObj = JSON.parse(json)
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 serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
243 const jsonObj = JSON.parse(json)
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 server.feedCommand.getJSON({ feed: 'video-comments' })
262 const jsonObj = JSON.parse(json)
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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
278 const jsonObj = JSON.parse(json)
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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
291 const jsonObj = JSON.parse(json)
292 expect(jsonObj.items.length).to.be.equal(3)
295 await addAccountToAccountBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
298 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
299 const jsonObj = JSON.parse(json)
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 query = { accountId: feeduserAccountId, token: feeduserFeedToken }
331 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
332 const jsonObj = JSON.parse(json)
333 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
337 it('Should fail with an invalid token', async function () {
338 const query = { accountId: feeduserAccountId, token: 'toto' }
339 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
342 it('Should fail with a token of another user', async function () {
343 const query = { accountId: feeduserAccountId, token: userFeedToken }
344 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
347 it('Should list no videos for a user with videos but no subscriptions', async function () {
348 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
349 expect(res.body.total).to.equal(0)
351 const query = { accountId: userAccountId, token: userFeedToken }
352 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
353 const jsonObj = JSON.parse(json)
354 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
357 it('Should list self videos for a user with a subscription to themselves', async function () {
360 await addUserSubscription(servers[0].url, userAccessToken, 'john_channel@localhost:' + servers[0].port)
361 await waitJobs(servers)
364 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
365 expect(res.body.total).to.equal(1)
366 expect(res.body.data[0].name).to.equal('user video')
368 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
369 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
370 const jsonObj = JSON.parse(json)
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 query = { accountId: userAccountId, token: userFeedToken, version: 2 }
386 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
387 const jsonObj = JSON.parse(json)
388 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
392 it('Should renew the token, and so have an invalid old token', async function () {
393 await renewUserScopedTokens(servers[0].url, userAccessToken)
395 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
396 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
399 it('Should succeed with the new token', async function () {
400 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
401 const token: ScopedToken = res2.body
402 userFeedToken = token.feedToken
404 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
405 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
410 after(async function () {
411 await cleanupTests([ ...servers, serverHLSOnly ])