]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/feeds/feeds.ts
Lazy load piscina
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
fe3a55b0 2
fe3a55b0 3import 'mocha'
696d83fd 4import * as chai from 'chai'
2d28b0c2 5import { XMLParser, XMLValidator } from 'fast-xml-parser'
d0800f76 6import { HttpStatusCode, VideoPrivacy } from '@shared/models'
696d83fd 7import {
7c3b7976 8 cleanupTests,
254d3579
C
9 createMultipleServers,
10 createSingleServer,
4c7e60bc 11 doubleFollow,
20bafcb6 12 makeGetRequest,
254d3579 13 PeerTubeServer,
fe3a55b0 14 setAccessTokensToServers,
d0800f76 15 setDefaultChannelAvatar,
7a5c3d77 16 stopFfmpeg,
5f8bd4cb 17 waitJobs
bf54587a 18} from '@shared/server-commands'
fe3a55b0
C
19
20chai.use(require('chai-xml'))
21chai.use(require('chai-json-schema'))
22chai.config.includeStack = true
23const expect = chai.expect
24
25describe('Test syndication feeds', () => {
254d3579
C
26 let servers: PeerTubeServer[] = []
27 let serverHLSOnly: PeerTubeServer
662fb3ab 28 let userAccessToken: string
57cfff78
C
29 let rootAccountId: number
30 let rootChannelId: number
31 let userAccountId: number
32 let userChannelId: number
afff310e 33 let userFeedToken: string
7a5c3d77 34 let liveId: string
fe3a55b0
C
35
36 before(async function () {
37 this.timeout(120000)
38
39 // Run servers
254d3579
C
40 servers = await createMultipleServers(2)
41 serverHLSOnly = await createSingleServer(3, {
97816649
C
42 transcoding: {
43 enabled: true,
44 webtorrent: { enabled: false },
45 hls: { enabled: true }
46 }
47 })
fe3a55b0 48
97816649 49 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
d0800f76 50 await setDefaultChannelAvatar(servers[0])
fe3a55b0
C
51 await doubleFollow(servers[0], servers[1])
52
7a5c3d77
C
53 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
54
662fb3ab 55 {
89d241a7 56 const user = await servers[0].users.getMyInfo()
57cfff78
C
57 rootAccountId = user.account.id
58 rootChannelId = user.videoChannels[0].id
fe3a55b0 59 }
fe3a55b0 60
662fb3ab 61 {
20bafcb6 62 userAccessToken = await servers[0].users.generateUserAndToken('john')
662fb3ab 63
89d241a7 64 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
57cfff78
C
65 userAccountId = user.account.id
66 userChannelId = user.videoChannels[0].id
afff310e 67
89d241a7 68 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
afff310e 69 userFeedToken = token.feedToken
662fb3ab
C
70 }
71
72 {
89d241a7 73 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
662fb3ab
C
74 }
75
76 {
d23dd9fb 77 const attributes = {
662fb3ab
C
78 name: 'my super name for server 1',
79 description: 'my super description for server 1',
80 fixture: 'video_short.webm'
81 }
89d241a7 82 const { id } = await servers[0].videos.upload({ attributes })
662fb3ab 83
89d241a7
C
84 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
85 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
662fb3ab 86 }
fe3a55b0 87
68b6fd21 88 {
d23dd9fb 89 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
89d241a7 90 const { id } = await servers[0].videos.upload({ attributes })
68b6fd21 91
89d241a7 92 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
68b6fd21
C
93 }
94
3cd0734f 95 await waitJobs(servers)
fe3a55b0
C
96 })
97
98 describe('All feed', function () {
99
100 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
101 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
41e74ec9 102 const rss = await servers[0].feed.getXML({ feed, ignoreCache: true })
c1bc8ee4 103 expect(rss).xml.to.be.valid()
fe3a55b0 104
41e74ec9 105 const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true })
c1bc8ee4 106 expect(atom).xml.to.be.valid()
fe3a55b0
C
107 }
108 })
109
110 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
111 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
41e74ec9 112 const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true })
c1bc8ee4 113 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
fe3a55b0
C
114 }
115 })
20bafcb6
C
116
117 it('Should serve the endpoint with a classic request', async function () {
118 await makeGetRequest({
119 url: servers[0].url,
120 path: '/feeds/videos.xml',
121 accept: 'application/xml',
122 expectedStatus: HttpStatusCode.OK_200
123 })
124 })
125
126 it('Should serve the endpoint as a cached request', async function () {
127 const res = await makeGetRequest({
128 url: servers[0].url,
129 path: '/feeds/videos.xml',
130 accept: 'application/xml',
131 expectedStatus: HttpStatusCode.OK_200
132 })
133
134 expect(res.headers['x-api-cache-cached']).to.equal('true')
135 })
136
137 it('Should not serve the endpoint as a cached request', async function () {
138 const res = await makeGetRequest({
139 url: servers[0].url,
140 path: '/feeds/videos.xml?v=186',
141 accept: 'application/xml',
142 expectedStatus: HttpStatusCode.OK_200
143 })
144
145 expect(res.headers['x-api-cache-cached']).to.not.exist
146 })
147
148 it('Should refuse to serve the endpoint without accept header', async function () {
149 await makeGetRequest({ url: servers[0].url, path: '/feeds/videos.xml', expectedStatus: HttpStatusCode.NOT_ACCEPTABLE_406 })
150 })
fe3a55b0
C
151 })
152
153 describe('Videos feed', function () {
97816649 154
fe3a55b0
C
155 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
156 for (const server of servers) {
41e74ec9 157 const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true })
2d28b0c2 158 expect(XMLValidator.validate(rss)).to.be.true
b70025bf 159
2d28b0c2
C
160 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
161 const xmlDoc = parser.parse(rss)
b70025bf
C
162
163 const enclosure = xmlDoc.rss.channel.item[0].enclosure
164 expect(enclosure).to.exist
4393b255
C
165
166 expect(enclosure['@_type']).to.equal('video/webm')
b70025bf 167 expect(enclosure['@_length']).to.equal(218910)
4393b255 168 expect(enclosure['@_url']).to.contain('-720.webm')
fe3a55b0
C
169 }
170 })
171
172 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
173 for (const server of servers) {
41e74ec9 174 const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true })
c1bc8ee4 175 const jsonObj = JSON.parse(json)
662fb3ab 176 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
177 expect(jsonObj.items[0].attachments).to.exist
178 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
179 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
180 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
181 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
fe3a55b0
C
182 }
183 })
662fb3ab
C
184
185 it('Should filter by account', async function () {
57cfff78 186 {
41e74ec9 187 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true })
c1bc8ee4 188 const jsonObj = JSON.parse(json)
57cfff78 189 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
190 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
191 expect(jsonObj.items[0].author.name).to.equal('root')
57cfff78
C
192 }
193
194 {
41e74ec9 195 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true })
c1bc8ee4 196 const jsonObj = JSON.parse(json)
57cfff78 197 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
198 expect(jsonObj.items[0].title).to.equal('user video')
199 expect(jsonObj.items[0].author.name).to.equal('john')
57cfff78
C
200 }
201
662fb3ab
C
202 for (const server of servers) {
203 {
41e74ec9 204 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true })
c1bc8ee4 205 const jsonObj = JSON.parse(json)
662fb3ab 206 expect(jsonObj.items.length).to.be.equal(1)
a1587156 207 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
208 }
209
210 {
41e74ec9 211 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true })
c1bc8ee4 212 const jsonObj = JSON.parse(json)
662fb3ab 213 expect(jsonObj.items.length).to.be.equal(1)
a1587156 214 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
215 }
216 }
57cfff78 217 })
662fb3ab 218
57cfff78 219 it('Should filter by video channel', async function () {
662fb3ab 220 {
41e74ec9 221 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true })
c1bc8ee4 222 const jsonObj = JSON.parse(json)
662fb3ab 223 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
224 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
225 expect(jsonObj.items[0].author.name).to.equal('root')
662fb3ab
C
226 }
227
228 {
41e74ec9 229 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true })
c1bc8ee4 230 const jsonObj = JSON.parse(json)
662fb3ab 231 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
232 expect(jsonObj.items[0].title).to.equal('user video')
233 expect(jsonObj.items[0].author.name).to.equal('john')
662fb3ab 234 }
662fb3ab 235
662fb3ab
C
236 for (const server of servers) {
237 {
41e74ec9
C
238 const query = { videoChannelName: 'root_channel@' + servers[0].host }
239 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
c1bc8ee4 240 const jsonObj = JSON.parse(json)
662fb3ab 241 expect(jsonObj.items.length).to.be.equal(1)
a1587156 242 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
243 }
244
245 {
41e74ec9
C
246 const query = { videoChannelName: 'john_channel@' + servers[0].host }
247 const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true })
c1bc8ee4 248 const jsonObj = JSON.parse(json)
662fb3ab 249 expect(jsonObj.items.length).to.be.equal(1)
a1587156 250 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
251 }
252 }
662fb3ab 253 })
97816649
C
254
255 it('Should correctly have videos feed with HLS only', async function () {
256 this.timeout(120000)
257
89d241a7 258 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
97816649
C
259
260 await waitJobs([ serverHLSOnly ])
261
41e74ec9 262 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true })
c1bc8ee4 263 const jsonObj = JSON.parse(json)
97816649
C
264 expect(jsonObj.items.length).to.be.equal(1)
265 expect(jsonObj.items[0].attachments).to.exist
266 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
267
268 for (let i = 0; i < 4; i++) {
269 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
270 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
271 expect(jsonObj.items[0].attachments[i].url).to.exist
272 }
273 })
7a5c3d77
C
274
275 it('Should not display waiting live videos', async function () {
276 const { uuid } = await servers[0].live.create({
277 fields: {
278 name: 'live',
279 privacy: VideoPrivacy.PUBLIC,
280 channelId: rootChannelId
281 }
282 })
283 liveId = uuid
284
41e74ec9 285 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
7a5c3d77
C
286
287 const jsonObj = JSON.parse(json)
288 expect(jsonObj.items.length).to.be.equal(2)
289 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
290 expect(jsonObj.items[1].title).to.equal('user video')
291 })
292
41e74ec9 293 it('Should display published live videos', async function () {
7a5c3d77
C
294 this.timeout(120000)
295
296 const ffmpeg = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveId, copyCodecs: true, fixtureName: 'video_short.mp4' })
297 await servers[0].live.waitUntilPublished({ videoId: liveId })
298
41e74ec9 299 const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true })
7a5c3d77
C
300
301 const jsonObj = JSON.parse(json)
302 expect(jsonObj.items.length).to.be.equal(3)
41e74ec9
C
303 expect(jsonObj.items[0].title).to.equal('live')
304 expect(jsonObj.items[1].title).to.equal('my super name for server 1')
305 expect(jsonObj.items[2].title).to.equal('user video')
7a5c3d77
C
306
307 await stopFfmpeg(ffmpeg)
308 })
fe3a55b0
C
309 })
310
311 describe('Video comments feed', function () {
68b6fd21
C
312
313 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
fe3a55b0 314 for (const server of servers) {
41e74ec9 315 const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true })
fe3a55b0 316
c1bc8ee4 317 const jsonObj = JSON.parse(json)
fe3a55b0 318 expect(jsonObj.items.length).to.be.equal(2)
4393b255
C
319 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
320 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
fe3a55b0
C
321 }
322 })
1df8a4d7
C
323
324 it('Should not list comments from muted accounts or instances', async function () {
696d83fd
C
325 this.timeout(30000)
326
41e74ec9 327 const remoteHandle = 'root@' + servers[0].host
696d83fd 328
89d241a7 329 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
1df8a4d7
C
330
331 {
41e74ec9 332 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
c1bc8ee4 333 const jsonObj = JSON.parse(json)
1df8a4d7
C
334 expect(jsonObj.items.length).to.be.equal(0)
335 }
336
89d241a7 337 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
696d83fd
C
338
339 {
89d241a7 340 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
696d83fd 341 await waitJobs(servers)
89d241a7 342 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
696d83fd
C
343 await waitJobs(servers)
344
41e74ec9 345 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
c1bc8ee4 346 const jsonObj = JSON.parse(json)
696d83fd
C
347 expect(jsonObj.items.length).to.be.equal(3)
348 }
349
89d241a7 350 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
696d83fd
C
351
352 {
41e74ec9 353 const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true })
c1bc8ee4 354 const jsonObj = JSON.parse(json)
696d83fd
C
355 expect(jsonObj.items.length).to.be.equal(2)
356 }
1df8a4d7 357 })
fe3a55b0
C
358 })
359
afff310e 360 describe('Video feed from my subscriptions', function () {
18490b07
C
361 let feeduserAccountId: number
362 let feeduserFeedToken: string
afff310e
RK
363
364 it('Should list no videos for a user with no videos and no subscriptions', async function () {
afff310e 365 const attr = { username: 'feeduser', password: 'password' }
89d241a7
C
366 await servers[0].users.create({ username: attr.username, password: attr.password })
367 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
afff310e
RK
368
369 {
89d241a7 370 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
afff310e
RK
371 feeduserAccountId = user.account.id
372 }
373
374 {
89d241a7 375 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
afff310e
RK
376 feeduserFeedToken = token.feedToken
377 }
378
379 {
89d241a7 380 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
2c27e704 381 expect(body.total).to.equal(0)
afff310e 382
c1bc8ee4 383 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
41e74ec9 384 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
c1bc8ee4 385 const jsonObj = JSON.parse(json)
afff310e
RK
386 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
387 }
388 })
389
18490b07 390 it('Should fail with an invalid token', async function () {
c1bc8ee4 391 const query = { accountId: feeduserAccountId, token: 'toto' }
41e74ec9 392 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
18490b07
C
393 })
394
395 it('Should fail with a token of another user', async function () {
c1bc8ee4 396 const query = { accountId: feeduserAccountId, token: userFeedToken }
41e74ec9 397 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
18490b07
C
398 })
399
afff310e 400 it('Should list no videos for a user with videos but no subscriptions', async function () {
89d241a7 401 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 402 expect(body.total).to.equal(0)
afff310e 403
c1bc8ee4 404 const query = { accountId: userAccountId, token: userFeedToken }
41e74ec9 405 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
c1bc8ee4 406 const jsonObj = JSON.parse(json)
18490b07 407 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
afff310e
RK
408 })
409
410 it('Should list self videos for a user with a subscription to themselves', async function () {
411 this.timeout(30000)
412
41e74ec9 413 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host })
afff310e
RK
414 await waitJobs(servers)
415
416 {
89d241a7 417 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704
C
418 expect(body.total).to.equal(1)
419 expect(body.data[0].name).to.equal('user video')
afff310e 420
41e74ec9
C
421 const query = { accountId: userAccountId, token: userFeedToken }
422 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
c1bc8ee4 423 const jsonObj = JSON.parse(json)
afff310e
RK
424 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
425 }
426 })
427
428 it('Should list videos of a user\'s subscription', async function () {
429 this.timeout(30000)
430
41e74ec9 431 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host })
afff310e
RK
432 await waitJobs(servers)
433
434 {
89d241a7 435 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 436 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
afff310e 437
41e74ec9
C
438 const query = { accountId: userAccountId, token: userFeedToken }
439 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
c1bc8ee4 440 const jsonObj = JSON.parse(json)
afff310e
RK
441 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
442 }
443 })
444
18490b07 445 it('Should renew the token, and so have an invalid old token', async function () {
89d241a7 446 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
18490b07 447
41e74ec9
C
448 const query = { accountId: userAccountId, token: userFeedToken }
449 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true })
18490b07
C
450 })
451
452 it('Should succeed with the new token', async function () {
89d241a7 453 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
18490b07
C
454 userFeedToken = token.feedToken
455
41e74ec9
C
456 const query = { accountId: userAccountId, token: userFeedToken }
457 await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true })
18490b07
C
458 })
459
afff310e
RK
460 })
461
7c3b7976 462 after(async function () {
97816649 463 await cleanupTests([ ...servers, serverHLSOnly ])
fe3a55b0
C
464 })
465})