]>
Commit | Line | Data |
---|---|---|
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 { HttpStatusCode, VideoPrivacy } from '@shared/models' | |
7 | import { | |
8 | cleanupTests, | |
9 | createMultipleServers, | |
10 | createSingleServer, | |
11 | doubleFollow, | |
12 | makeGetRequest, | |
13 | PeerTubeServer, | |
14 | setAccessTokensToServers, | |
15 | setDefaultChannelAvatar, | |
16 | stopFfmpeg, | |
17 | waitJobs | |
18 | } from '@shared/server-commands' | |
19 | ||
20 | chai.use(require('chai-xml')) | |
21 | chai.use(require('chai-json-schema')) | |
22 | chai.config.includeStack = true | |
23 | const expect = chai.expect | |
24 | ||
25 | describe('Test syndication feeds', () => { | |
26 | let servers: PeerTubeServer[] = [] | |
27 | let serverHLSOnly: PeerTubeServer | |
28 | let userAccessToken: string | |
29 | let rootAccountId: number | |
30 | let rootChannelId: number | |
31 | let userAccountId: number | |
32 | let userChannelId: number | |
33 | let userFeedToken: string | |
34 | let liveId: string | |
35 | ||
36 | before(async function () { | |
37 | this.timeout(120000) | |
38 | ||
39 | // Run servers | |
40 | servers = await createMultipleServers(2) | |
41 | serverHLSOnly = await createSingleServer(3, { | |
42 | transcoding: { | |
43 | enabled: true, | |
44 | webtorrent: { enabled: false }, | |
45 | hls: { enabled: true } | |
46 | } | |
47 | }) | |
48 | ||
49 | await setAccessTokensToServers([ ...servers, serverHLSOnly ]) | |
50 | await setDefaultChannelAvatar(servers[0]) | |
51 | await doubleFollow(servers[0], servers[1]) | |
52 | ||
53 | await servers[0].config.enableLive({ allowReplay: false, transcoding: false }) | |
54 | ||
55 | { | |
56 | const user = await servers[0].users.getMyInfo() | |
57 | rootAccountId = user.account.id | |
58 | rootChannelId = user.videoChannels[0].id | |
59 | } | |
60 | ||
61 | { | |
62 | userAccessToken = await servers[0].users.generateUserAndToken('john') | |
63 | ||
64 | const user = await servers[0].users.getMyInfo({ token: userAccessToken }) | |
65 | userAccountId = user.account.id | |
66 | userChannelId = user.videoChannels[0].id | |
67 | ||
68 | const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken }) | |
69 | userFeedToken = token.feedToken | |
70 | } | |
71 | ||
72 | { | |
73 | await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } }) | |
74 | } | |
75 | ||
76 | { | |
77 | const attributes = { | |
78 | name: 'my super name for server 1', | |
79 | description: 'my super description for server 1', | |
80 | fixture: 'video_short.webm' | |
81 | } | |
82 | const { id } = await servers[0].videos.upload({ attributes }) | |
83 | ||
84 | await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' }) | |
85 | await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' }) | |
86 | } | |
87 | ||
88 | { | |
89 | const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED } | |
90 | const { id } = await servers[0].videos.upload({ attributes }) | |
91 | ||
92 | await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' }) | |
93 | } | |
94 | ||
95 | await waitJobs(servers) | |
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' ]) { | |
102 | const rss = await servers[0].feed.getXML({ feed, ignoreCache: true }) | |
103 | expect(rss).xml.to.be.valid() | |
104 | ||
105 | const atom = await servers[0].feed.getXML({ feed, format: 'atom', ignoreCache: true }) | |
106 | expect(atom).xml.to.be.valid() | |
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' ]) { | |
112 | const jsonText = await servers[0].feed.getJSON({ feed, ignoreCache: true }) | |
113 | expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' }) | |
114 | } | |
115 | }) | |
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 | }) | |
151 | }) | |
152 | ||
153 | describe('Videos feed', function () { | |
154 | ||
155 | it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () { | |
156 | for (const server of servers) { | |
157 | const rss = await server.feed.getXML({ feed: 'videos', ignoreCache: true }) | |
158 | expect(XMLValidator.validate(rss)).to.be.true | |
159 | ||
160 | const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false }) | |
161 | const xmlDoc = parser.parse(rss) | |
162 | ||
163 | const enclosure = xmlDoc.rss.channel.item[0].enclosure | |
164 | expect(enclosure).to.exist | |
165 | ||
166 | expect(enclosure['@_type']).to.equal('video/webm') | |
167 | expect(enclosure['@_length']).to.equal(218910) | |
168 | expect(enclosure['@_url']).to.contain('-720.webm') | |
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) { | |
174 | const json = await server.feed.getJSON({ feed: 'videos', ignoreCache: true }) | |
175 | const jsonObj = JSON.parse(json) | |
176 | expect(jsonObj.items.length).to.be.equal(2) | |
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') | |
182 | } | |
183 | }) | |
184 | ||
185 | it('Should filter by account', async function () { | |
186 | { | |
187 | const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId }, ignoreCache: true }) | |
188 | const jsonObj = JSON.parse(json) | |
189 | expect(jsonObj.items.length).to.be.equal(1) | |
190 | expect(jsonObj.items[0].title).to.equal('my super name for server 1') | |
191 | expect(jsonObj.items[0].author.name).to.equal('root') | |
192 | } | |
193 | ||
194 | { | |
195 | const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId }, ignoreCache: true }) | |
196 | const jsonObj = JSON.parse(json) | |
197 | expect(jsonObj.items.length).to.be.equal(1) | |
198 | expect(jsonObj.items[0].title).to.equal('user video') | |
199 | expect(jsonObj.items[0].author.name).to.equal('john') | |
200 | } | |
201 | ||
202 | for (const server of servers) { | |
203 | { | |
204 | const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@' + servers[0].host }, ignoreCache: true }) | |
205 | const jsonObj = JSON.parse(json) | |
206 | expect(jsonObj.items.length).to.be.equal(1) | |
207 | expect(jsonObj.items[0].title).to.equal('my super name for server 1') | |
208 | } | |
209 | ||
210 | { | |
211 | const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@' + servers[0].host }, ignoreCache: true }) | |
212 | const jsonObj = JSON.parse(json) | |
213 | expect(jsonObj.items.length).to.be.equal(1) | |
214 | expect(jsonObj.items[0].title).to.equal('user video') | |
215 | } | |
216 | } | |
217 | }) | |
218 | ||
219 | it('Should filter by video channel', async function () { | |
220 | { | |
221 | const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId }, ignoreCache: true }) | |
222 | const jsonObj = JSON.parse(json) | |
223 | expect(jsonObj.items.length).to.be.equal(1) | |
224 | expect(jsonObj.items[0].title).to.equal('my super name for server 1') | |
225 | expect(jsonObj.items[0].author.name).to.equal('root') | |
226 | } | |
227 | ||
228 | { | |
229 | const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId }, ignoreCache: true }) | |
230 | const jsonObj = JSON.parse(json) | |
231 | expect(jsonObj.items.length).to.be.equal(1) | |
232 | expect(jsonObj.items[0].title).to.equal('user video') | |
233 | expect(jsonObj.items[0].author.name).to.equal('john') | |
234 | } | |
235 | ||
236 | for (const server of servers) { | |
237 | { | |
238 | const query = { videoChannelName: 'root_channel@' + servers[0].host } | |
239 | const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true }) | |
240 | const jsonObj = JSON.parse(json) | |
241 | expect(jsonObj.items.length).to.be.equal(1) | |
242 | expect(jsonObj.items[0].title).to.equal('my super name for server 1') | |
243 | } | |
244 | ||
245 | { | |
246 | const query = { videoChannelName: 'john_channel@' + servers[0].host } | |
247 | const json = await server.feed.getJSON({ feed: 'videos', query, ignoreCache: true }) | |
248 | const jsonObj = JSON.parse(json) | |
249 | expect(jsonObj.items.length).to.be.equal(1) | |
250 | expect(jsonObj.items[0].title).to.equal('user video') | |
251 | } | |
252 | } | |
253 | }) | |
254 | ||
255 | it('Should correctly have videos feed with HLS only', async function () { | |
256 | this.timeout(120000) | |
257 | ||
258 | await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } }) | |
259 | ||
260 | await waitJobs([ serverHLSOnly ]) | |
261 | ||
262 | const json = await serverHLSOnly.feed.getJSON({ feed: 'videos', ignoreCache: true }) | |
263 | const jsonObj = JSON.parse(json) | |
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 | }) | |
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 | ||
285 | const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true }) | |
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 | ||
293 | it('Should display published live videos', async function () { | |
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 | ||
299 | const json = await servers[0].feed.getJSON({ feed: 'videos', ignoreCache: true }) | |
300 | ||
301 | const jsonObj = JSON.parse(json) | |
302 | expect(jsonObj.items.length).to.be.equal(3) | |
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') | |
306 | ||
307 | await stopFfmpeg(ffmpeg) | |
308 | }) | |
309 | }) | |
310 | ||
311 | describe('Video comments feed', function () { | |
312 | ||
313 | it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () { | |
314 | for (const server of servers) { | |
315 | const json = await server.feed.getJSON({ feed: 'video-comments', ignoreCache: true }) | |
316 | ||
317 | const jsonObj = JSON.parse(json) | |
318 | expect(jsonObj.items.length).to.be.equal(2) | |
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>') | |
321 | } | |
322 | }) | |
323 | ||
324 | it('Should not list comments from muted accounts or instances', async function () { | |
325 | this.timeout(30000) | |
326 | ||
327 | const remoteHandle = 'root@' + servers[0].host | |
328 | ||
329 | await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle }) | |
330 | ||
331 | { | |
332 | const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true }) | |
333 | const jsonObj = JSON.parse(json) | |
334 | expect(jsonObj.items.length).to.be.equal(0) | |
335 | } | |
336 | ||
337 | await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle }) | |
338 | ||
339 | { | |
340 | const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid | |
341 | await waitJobs(servers) | |
342 | await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' }) | |
343 | await waitJobs(servers) | |
344 | ||
345 | const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true }) | |
346 | const jsonObj = JSON.parse(json) | |
347 | expect(jsonObj.items.length).to.be.equal(3) | |
348 | } | |
349 | ||
350 | await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle }) | |
351 | ||
352 | { | |
353 | const json = await servers[1].feed.getJSON({ feed: 'video-comments', ignoreCache: true }) | |
354 | const jsonObj = JSON.parse(json) | |
355 | expect(jsonObj.items.length).to.be.equal(2) | |
356 | } | |
357 | }) | |
358 | }) | |
359 | ||
360 | describe('Video feed from my subscriptions', function () { | |
361 | let feeduserAccountId: number | |
362 | let feeduserFeedToken: string | |
363 | ||
364 | it('Should list no videos for a user with no videos and no subscriptions', async function () { | |
365 | const attr = { username: 'feeduser', password: 'password' } | |
366 | await servers[0].users.create({ username: attr.username, password: attr.password }) | |
367 | const feeduserAccessToken = await servers[0].login.getAccessToken(attr) | |
368 | ||
369 | { | |
370 | const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken }) | |
371 | feeduserAccountId = user.account.id | |
372 | } | |
373 | ||
374 | { | |
375 | const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken }) | |
376 | feeduserFeedToken = token.feedToken | |
377 | } | |
378 | ||
379 | { | |
380 | const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken }) | |
381 | expect(body.total).to.equal(0) | |
382 | ||
383 | const query = { accountId: feeduserAccountId, token: feeduserFeedToken } | |
384 | const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true }) | |
385 | const jsonObj = JSON.parse(json) | |
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 | ||
390 | it('Should fail with an invalid token', async function () { | |
391 | const query = { accountId: feeduserAccountId, token: 'toto' } | |
392 | await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true }) | |
393 | }) | |
394 | ||
395 | it('Should fail with a token of another user', async function () { | |
396 | const query = { accountId: feeduserAccountId, token: userFeedToken } | |
397 | await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true }) | |
398 | }) | |
399 | ||
400 | it('Should list no videos for a user with videos but no subscriptions', async function () { | |
401 | const body = await servers[0].subscriptions.listVideos({ token: userAccessToken }) | |
402 | expect(body.total).to.equal(0) | |
403 | ||
404 | const query = { accountId: userAccountId, token: userFeedToken } | |
405 | const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true }) | |
406 | const jsonObj = JSON.parse(json) | |
407 | expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos | |
408 | }) | |
409 | ||
410 | it('Should list self videos for a user with a subscription to themselves', async function () { | |
411 | this.timeout(30000) | |
412 | ||
413 | await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@' + servers[0].host }) | |
414 | await waitJobs(servers) | |
415 | ||
416 | { | |
417 | const body = await servers[0].subscriptions.listVideos({ token: userAccessToken }) | |
418 | expect(body.total).to.equal(1) | |
419 | expect(body.data[0].name).to.equal('user video') | |
420 | ||
421 | const query = { accountId: userAccountId, token: userFeedToken } | |
422 | const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true }) | |
423 | const jsonObj = JSON.parse(json) | |
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 | ||
431 | await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@' + servers[0].host }) | |
432 | await waitJobs(servers) | |
433 | ||
434 | { | |
435 | const body = await servers[0].subscriptions.listVideos({ token: userAccessToken }) | |
436 | expect(body.total).to.equal(2, "there should be 2 videos part of the subscription") | |
437 | ||
438 | const query = { accountId: userAccountId, token: userFeedToken } | |
439 | const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true }) | |
440 | const jsonObj = JSON.parse(json) | |
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 | ||
445 | it('Should renew the token, and so have an invalid old token', async function () { | |
446 | await servers[0].users.renewMyScopedTokens({ token: userAccessToken }) | |
447 | ||
448 | const query = { accountId: userAccountId, token: userFeedToken } | |
449 | await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403, ignoreCache: true }) | |
450 | }) | |
451 | ||
452 | it('Should succeed with the new token', async function () { | |
453 | const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken }) | |
454 | userFeedToken = token.feedToken | |
455 | ||
456 | const query = { accountId: userAccountId, token: userFeedToken } | |
457 | await servers[0].feed.getJSON({ feed: 'subscriptions', query, ignoreCache: true }) | |
458 | }) | |
459 | ||
460 | }) | |
461 | ||
462 | after(async function () { | |
463 | await cleanupTests([ ...servers, serverHLSOnly ]) | |
464 | }) | |
465 | }) |