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