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