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