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