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