]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
Merge branch 'release/4.1.0' into develop
[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
160 expect(enclosure['@_type']).to.equal('video/webm')
161 expect(enclosure['@_length']).to.equal(218910)
162 expect(enclosure['@_url']).to.contain('-720.webm')
163 }
164 })
165
166 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
167 for (const server of servers) {
168 const json = await server.feed.getJSON({ feed: 'videos' })
169 const jsonObj = JSON.parse(json)
170 expect(jsonObj.items.length).to.be.equal(2)
171 expect(jsonObj.items[0].attachments).to.exist
172 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
173 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
174 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
175 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
176 }
177 })
178
179 it('Should filter by account', async function () {
180 {
181 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
182 const jsonObj = JSON.parse(json)
183 expect(jsonObj.items.length).to.be.equal(1)
184 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
185 expect(jsonObj.items[0].author.name).to.equal('root')
186 }
187
188 {
189 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
190 const jsonObj = JSON.parse(json)
191 expect(jsonObj.items.length).to.be.equal(1)
192 expect(jsonObj.items[0].title).to.equal('user video')
193 expect(jsonObj.items[0].author.name).to.equal('john')
194 }
195
196 for (const server of servers) {
197 {
198 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
199 const jsonObj = JSON.parse(json)
200 expect(jsonObj.items.length).to.be.equal(1)
201 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
202 }
203
204 {
205 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
206 const jsonObj = JSON.parse(json)
207 expect(jsonObj.items.length).to.be.equal(1)
208 expect(jsonObj.items[0].title).to.equal('user video')
209 }
210 }
211 })
212
213 it('Should filter by video channel', async function () {
214 {
215 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
216 const jsonObj = JSON.parse(json)
217 expect(jsonObj.items.length).to.be.equal(1)
218 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
219 expect(jsonObj.items[0].author.name).to.equal('root')
220 }
221
222 {
223 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
224 const jsonObj = JSON.parse(json)
225 expect(jsonObj.items.length).to.be.equal(1)
226 expect(jsonObj.items[0].title).to.equal('user video')
227 expect(jsonObj.items[0].author.name).to.equal('john')
228 }
229
230 for (const server of servers) {
231 {
232 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
233 const json = await server.feed.getJSON({ feed: 'videos', query })
234 const jsonObj = JSON.parse(json)
235 expect(jsonObj.items.length).to.be.equal(1)
236 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
237 }
238
239 {
240 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
241 const json = await server.feed.getJSON({ feed: 'videos', query })
242 const jsonObj = JSON.parse(json)
243 expect(jsonObj.items.length).to.be.equal(1)
244 expect(jsonObj.items[0].title).to.equal('user video')
245 }
246 }
247 })
248
249 it('Should correctly have videos feed with HLS only', async function () {
250 this.timeout(120000)
251
252 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
253
254 await waitJobs([ serverHLSOnly ])
255
256 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
257 const jsonObj = JSON.parse(json)
258 expect(jsonObj.items.length).to.be.equal(1)
259 expect(jsonObj.items[0].attachments).to.exist
260 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
261
262 for (let i = 0; i < 4; i++) {
263 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
264 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
265 expect(jsonObj.items[0].attachments[i].url).to.exist
266 }
267 })
268 })
269
270 describe('Video comments feed', function () {
271
272 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
273 for (const server of servers) {
274 const json = await server.feed.getJSON({ feed: 'video-comments' })
275
276 const jsonObj = JSON.parse(json)
277 expect(jsonObj.items.length).to.be.equal(2)
278 expect(jsonObj.items[0].content_html).to.contain('<p>super comment 2</p>')
279 expect(jsonObj.items[1].content_html).to.contain('<p>super comment 1</p>')
280 }
281 })
282
283 it('Should not list comments from muted accounts or instances', async function () {
284 this.timeout(30000)
285
286 const remoteHandle = 'root@localhost:' + servers[0].port
287
288 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
289
290 {
291 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
292 const jsonObj = JSON.parse(json)
293 expect(jsonObj.items.length).to.be.equal(0)
294 }
295
296 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
297
298 {
299 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
300 await waitJobs(servers)
301 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
302 await waitJobs(servers)
303
304 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
305 const jsonObj = JSON.parse(json)
306 expect(jsonObj.items.length).to.be.equal(3)
307 }
308
309 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
310
311 {
312 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
313 const jsonObj = JSON.parse(json)
314 expect(jsonObj.items.length).to.be.equal(2)
315 }
316 })
317 })
318
319 describe('Video feed from my subscriptions', function () {
320 let feeduserAccountId: number
321 let feeduserFeedToken: string
322
323 it('Should list no videos for a user with no videos and no subscriptions', async function () {
324 const attr = { username: 'feeduser', password: 'password' }
325 await servers[0].users.create({ username: attr.username, password: attr.password })
326 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
327
328 {
329 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
330 feeduserAccountId = user.account.id
331 }
332
333 {
334 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
335 feeduserFeedToken = token.feedToken
336 }
337
338 {
339 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
340 expect(body.total).to.equal(0)
341
342 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
343 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
344 const jsonObj = JSON.parse(json)
345 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
346 }
347 })
348
349 it('Should fail with an invalid token', async function () {
350 const query = { accountId: feeduserAccountId, token: 'toto' }
351 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
352 })
353
354 it('Should fail with a token of another user', async function () {
355 const query = { accountId: feeduserAccountId, token: userFeedToken }
356 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
357 })
358
359 it('Should list no videos for a user with videos but no subscriptions', async function () {
360 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
361 expect(body.total).to.equal(0)
362
363 const query = { accountId: userAccountId, token: userFeedToken }
364 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
365 const jsonObj = JSON.parse(json)
366 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
367 })
368
369 it('Should list self videos for a user with a subscription to themselves', async function () {
370 this.timeout(30000)
371
372 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
373 await waitJobs(servers)
374
375 {
376 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
377 expect(body.total).to.equal(1)
378 expect(body.data[0].name).to.equal('user video')
379
380 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
381 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
382 const jsonObj = JSON.parse(json)
383 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
384 }
385 })
386
387 it('Should list videos of a user\'s subscription', async function () {
388 this.timeout(30000)
389
390 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
391 await waitJobs(servers)
392
393 {
394 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
395 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
396
397 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
398 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
399 const jsonObj = JSON.parse(json)
400 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
401 }
402 })
403
404 it('Should renew the token, and so have an invalid old token', async function () {
405 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
406
407 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
408 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
409 })
410
411 it('Should succeed with the new token', async function () {
412 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
413 userFeedToken = token.feedToken
414
415 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
416 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
417 })
418
419 })
420
421 after(async function () {
422 await cleanupTests([ ...servers, serverHLSOnly ])
423 })
424 })