]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/feeds/feeds.ts
Translated using Weblate (French (France) (fr_FR))
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
fe3a55b0 2
fe3a55b0 3import 'mocha'
696d83fd 4import * as chai from 'chai'
2d28b0c2 5import { XMLParser, XMLValidator } from 'fast-xml-parser'
696d83fd 6import {
7c3b7976 7 cleanupTests,
254d3579
C
8 createMultipleServers,
9 createSingleServer,
4c7e60bc 10 doubleFollow,
20bafcb6 11 makeGetRequest,
254d3579 12 PeerTubeServer,
fe3a55b0 13 setAccessTokensToServers,
5f8bd4cb 14 waitJobs
bf54587a 15} from '@shared/server-commands'
4c7e60bc 16import { HttpStatusCode, VideoPrivacy } from '@shared/models'
fe3a55b0
C
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', () => {
254d3579
C
24 let servers: PeerTubeServer[] = []
25 let serverHLSOnly: PeerTubeServer
662fb3ab 26 let userAccessToken: string
57cfff78
C
27 let rootAccountId: number
28 let rootChannelId: number
29 let userAccountId: number
30 let userChannelId: number
afff310e 31 let userFeedToken: string
fe3a55b0
C
32
33 before(async function () {
34 this.timeout(120000)
35
36 // Run servers
254d3579
C
37 servers = await createMultipleServers(2)
38 serverHLSOnly = await createSingleServer(3, {
97816649
C
39 transcoding: {
40 enabled: true,
41 webtorrent: { enabled: false },
42 hls: { enabled: true }
43 }
44 })
fe3a55b0 45
97816649 46 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
fe3a55b0
C
47 await doubleFollow(servers[0], servers[1])
48
662fb3ab 49 {
89d241a7 50 const user = await servers[0].users.getMyInfo()
57cfff78
C
51 rootAccountId = user.account.id
52 rootChannelId = user.videoChannels[0].id
fe3a55b0 53 }
fe3a55b0 54
662fb3ab 55 {
20bafcb6 56 userAccessToken = await servers[0].users.generateUserAndToken('john')
662fb3ab 57
89d241a7 58 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
57cfff78
C
59 userAccountId = user.account.id
60 userChannelId = user.videoChannels[0].id
afff310e 61
89d241a7 62 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
afff310e 63 userFeedToken = token.feedToken
662fb3ab
C
64 }
65
66 {
89d241a7 67 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
662fb3ab
C
68 }
69
70 {
d23dd9fb 71 const attributes = {
662fb3ab
C
72 name: 'my super name for server 1',
73 description: 'my super description for server 1',
74 fixture: 'video_short.webm'
75 }
89d241a7 76 const { id } = await servers[0].videos.upload({ attributes })
662fb3ab 77
89d241a7
C
78 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
79 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
662fb3ab 80 }
fe3a55b0 81
68b6fd21 82 {
d23dd9fb 83 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
89d241a7 84 const { id } = await servers[0].videos.upload({ attributes })
68b6fd21 85
89d241a7 86 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
68b6fd21
C
87 }
88
3cd0734f 89 await waitJobs(servers)
fe3a55b0
C
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' ]) {
89d241a7 96 const rss = await servers[0].feed.getXML({ feed })
c1bc8ee4 97 expect(rss).xml.to.be.valid()
fe3a55b0 98
89d241a7 99 const atom = await servers[0].feed.getXML({ feed, format: 'atom' })
c1bc8ee4 100 expect(atom).xml.to.be.valid()
fe3a55b0
C
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' ]) {
89d241a7 106 const jsonText = await servers[0].feed.getJSON({ feed })
c1bc8ee4 107 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
fe3a55b0
C
108 }
109 })
20bafcb6
C
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 })
fe3a55b0
C
145 })
146
147 describe('Videos feed', function () {
97816649 148
fe3a55b0
C
149 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
150 for (const server of servers) {
89d241a7 151 const rss = await server.feed.getXML({ feed: 'videos' })
2d28b0c2 152 expect(XMLValidator.validate(rss)).to.be.true
b70025bf 153
2d28b0c2
C
154 const parser = new XMLParser({ parseAttributeValue: true, ignoreAttributes: false })
155 const xmlDoc = parser.parse(rss)
b70025bf
C
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')
fe3a55b0
C
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) {
89d241a7 167 const json = await server.feed.getJSON({ feed: 'videos' })
c1bc8ee4 168 const jsonObj = JSON.parse(json)
662fb3ab 169 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
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')
fe3a55b0
C
175 }
176 })
662fb3ab
C
177
178 it('Should filter by account', async function () {
57cfff78 179 {
89d241a7 180 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
c1bc8ee4 181 const jsonObj = JSON.parse(json)
57cfff78 182 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
183 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
184 expect(jsonObj.items[0].author.name).to.equal('root')
57cfff78
C
185 }
186
187 {
89d241a7 188 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
c1bc8ee4 189 const jsonObj = JSON.parse(json)
57cfff78 190 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
191 expect(jsonObj.items[0].title).to.equal('user video')
192 expect(jsonObj.items[0].author.name).to.equal('john')
57cfff78
C
193 }
194
662fb3ab
C
195 for (const server of servers) {
196 {
89d241a7 197 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
c1bc8ee4 198 const jsonObj = JSON.parse(json)
662fb3ab 199 expect(jsonObj.items.length).to.be.equal(1)
a1587156 200 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
201 }
202
203 {
89d241a7 204 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
c1bc8ee4 205 const jsonObj = JSON.parse(json)
662fb3ab 206 expect(jsonObj.items.length).to.be.equal(1)
a1587156 207 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
208 }
209 }
57cfff78 210 })
662fb3ab 211
57cfff78 212 it('Should filter by video channel', async function () {
662fb3ab 213 {
89d241a7 214 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
c1bc8ee4 215 const jsonObj = JSON.parse(json)
662fb3ab 216 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
217 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
218 expect(jsonObj.items[0].author.name).to.equal('root')
662fb3ab
C
219 }
220
221 {
89d241a7 222 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
c1bc8ee4 223 const jsonObj = JSON.parse(json)
662fb3ab 224 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
225 expect(jsonObj.items[0].title).to.equal('user video')
226 expect(jsonObj.items[0].author.name).to.equal('john')
662fb3ab 227 }
662fb3ab 228
662fb3ab
C
229 for (const server of servers) {
230 {
c1bc8ee4 231 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
89d241a7 232 const json = await server.feed.getJSON({ feed: 'videos', query })
c1bc8ee4 233 const jsonObj = JSON.parse(json)
662fb3ab 234 expect(jsonObj.items.length).to.be.equal(1)
a1587156 235 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
236 }
237
238 {
c1bc8ee4 239 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
89d241a7 240 const json = await server.feed.getJSON({ feed: 'videos', query })
c1bc8ee4 241 const jsonObj = JSON.parse(json)
662fb3ab 242 expect(jsonObj.items.length).to.be.equal(1)
a1587156 243 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
244 }
245 }
662fb3ab 246 })
97816649
C
247
248 it('Should correctly have videos feed with HLS only', async function () {
249 this.timeout(120000)
250
89d241a7 251 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
97816649
C
252
253 await waitJobs([ serverHLSOnly ])
254
89d241a7 255 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
c1bc8ee4 256 const jsonObj = JSON.parse(json)
97816649
C
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 })
fe3a55b0
C
267 })
268
269 describe('Video comments feed', function () {
68b6fd21
C
270
271 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
fe3a55b0 272 for (const server of servers) {
89d241a7 273 const json = await server.feed.getJSON({ feed: 'video-comments' })
fe3a55b0 274
c1bc8ee4 275 const jsonObj = JSON.parse(json)
fe3a55b0 276 expect(jsonObj.items.length).to.be.equal(2)
228d8e8e
C
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>')
fe3a55b0
C
279 }
280 })
1df8a4d7
C
281
282 it('Should not list comments from muted accounts or instances', async function () {
696d83fd
C
283 this.timeout(30000)
284
285 const remoteHandle = 'root@localhost:' + servers[0].port
286
89d241a7 287 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
1df8a4d7
C
288
289 {
89d241a7 290 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
c1bc8ee4 291 const jsonObj = JSON.parse(json)
1df8a4d7
C
292 expect(jsonObj.items.length).to.be.equal(0)
293 }
294
89d241a7 295 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
696d83fd
C
296
297 {
89d241a7 298 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
696d83fd 299 await waitJobs(servers)
89d241a7 300 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
696d83fd
C
301 await waitJobs(servers)
302
89d241a7 303 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
c1bc8ee4 304 const jsonObj = JSON.parse(json)
696d83fd
C
305 expect(jsonObj.items.length).to.be.equal(3)
306 }
307
89d241a7 308 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
696d83fd
C
309
310 {
89d241a7 311 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
c1bc8ee4 312 const jsonObj = JSON.parse(json)
696d83fd
C
313 expect(jsonObj.items.length).to.be.equal(2)
314 }
1df8a4d7 315 })
fe3a55b0
C
316 })
317
afff310e 318 describe('Video feed from my subscriptions', function () {
18490b07
C
319 let feeduserAccountId: number
320 let feeduserFeedToken: string
afff310e
RK
321
322 it('Should list no videos for a user with no videos and no subscriptions', async function () {
afff310e 323 const attr = { username: 'feeduser', password: 'password' }
89d241a7
C
324 await servers[0].users.create({ username: attr.username, password: attr.password })
325 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
afff310e
RK
326
327 {
89d241a7 328 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
afff310e
RK
329 feeduserAccountId = user.account.id
330 }
331
332 {
89d241a7 333 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
afff310e
RK
334 feeduserFeedToken = token.feedToken
335 }
336
337 {
89d241a7 338 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
2c27e704 339 expect(body.total).to.equal(0)
afff310e 340
c1bc8ee4 341 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
89d241a7 342 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 343 const jsonObj = JSON.parse(json)
afff310e
RK
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
18490b07 348 it('Should fail with an invalid token', async function () {
c1bc8ee4 349 const query = { accountId: feeduserAccountId, token: 'toto' }
89d241a7 350 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
351 })
352
353 it('Should fail with a token of another user', async function () {
c1bc8ee4 354 const query = { accountId: feeduserAccountId, token: userFeedToken }
89d241a7 355 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
356 })
357
afff310e 358 it('Should list no videos for a user with videos but no subscriptions', async function () {
89d241a7 359 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 360 expect(body.total).to.equal(0)
afff310e 361
c1bc8ee4 362 const query = { accountId: userAccountId, token: userFeedToken }
89d241a7 363 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 364 const jsonObj = JSON.parse(json)
18490b07 365 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
afff310e
RK
366 })
367
368 it('Should list self videos for a user with a subscription to themselves', async function () {
369 this.timeout(30000)
370
89d241a7 371 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
afff310e
RK
372 await waitJobs(servers)
373
374 {
89d241a7 375 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704
C
376 expect(body.total).to.equal(1)
377 expect(body.data[0].name).to.equal('user video')
afff310e 378
c1bc8ee4 379 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
89d241a7 380 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 381 const jsonObj = JSON.parse(json)
afff310e
RK
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
89d241a7 389 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
afff310e
RK
390 await waitJobs(servers)
391
392 {
89d241a7 393 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 394 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
afff310e 395
c1bc8ee4 396 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
89d241a7 397 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 398 const jsonObj = JSON.parse(json)
afff310e
RK
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
18490b07 403 it('Should renew the token, and so have an invalid old token', async function () {
89d241a7 404 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
18490b07 405
c1bc8ee4 406 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
89d241a7 407 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
408 })
409
410 it('Should succeed with the new token', async function () {
89d241a7 411 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
18490b07
C
412 userFeedToken = token.feedToken
413
c1bc8ee4 414 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
89d241a7 415 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
18490b07
C
416 })
417
afff310e
RK
418 })
419
7c3b7976 420 after(async function () {
97816649 421 await cleanupTests([ ...servers, serverHLSOnly ])
fe3a55b0
C
422 })
423})