]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/feeds/feeds.ts
Use an object to represent a server
[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'
b70025bf 5import * as xmlParser from 'fast-xml-parser'
5f8bd4cb 6import { HttpStatusCode } from '@shared/core-utils'
696d83fd 7import {
7c3b7976 8 cleanupTests,
fe3a55b0 9 doubleFollow,
254d3579
C
10 createMultipleServers,
11 createSingleServer,
12 PeerTubeServer,
fe3a55b0 13 setAccessTokensToServers,
5f8bd4cb
C
14 waitJobs
15} from '@shared/extra-utils'
7926c5f9 16import { 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
C
55 {
56 const attr = { username: 'john', password: 'password' }
89d241a7
C
57 await servers[0].users.create({ username: attr.username, password: attr.password })
58 userAccessToken = await servers[0].login.getAccessToken(attr)
662fb3ab 59
89d241a7 60 const user = await servers[0].users.getMyInfo({ token: userAccessToken })
57cfff78
C
61 userAccountId = user.account.id
62 userChannelId = user.videoChannels[0].id
afff310e 63
89d241a7 64 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
afff310e 65 userFeedToken = token.feedToken
662fb3ab
C
66 }
67
68 {
89d241a7 69 await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'user video' } })
662fb3ab
C
70 }
71
72 {
d23dd9fb 73 const attributes = {
662fb3ab
C
74 name: 'my super name for server 1',
75 description: 'my super description for server 1',
76 fixture: 'video_short.webm'
77 }
89d241a7 78 const { id } = await servers[0].videos.upload({ attributes })
662fb3ab 79
89d241a7
C
80 await servers[0].comments.createThread({ videoId: id, text: 'super comment 1' })
81 await servers[0].comments.createThread({ videoId: id, text: 'super comment 2' })
662fb3ab 82 }
fe3a55b0 83
68b6fd21 84 {
d23dd9fb 85 const attributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
89d241a7 86 const { id } = await servers[0].videos.upload({ attributes })
68b6fd21 87
89d241a7 88 await servers[0].comments.createThread({ videoId: id, text: 'comment on unlisted video' })
68b6fd21
C
89 }
90
3cd0734f 91 await waitJobs(servers)
fe3a55b0
C
92 })
93
94 describe('All feed', function () {
95
96 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
97 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
89d241a7 98 const rss = await servers[0].feed.getXML({ feed })
c1bc8ee4 99 expect(rss).xml.to.be.valid()
fe3a55b0 100
89d241a7 101 const atom = await servers[0].feed.getXML({ feed, format: 'atom' })
c1bc8ee4 102 expect(atom).xml.to.be.valid()
fe3a55b0
C
103 }
104 })
105
106 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
107 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
89d241a7 108 const jsonText = await servers[0].feed.getJSON({ feed })
c1bc8ee4 109 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
fe3a55b0
C
110 }
111 })
112 })
113
114 describe('Videos feed', function () {
97816649 115
fe3a55b0
C
116 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
117 for (const server of servers) {
89d241a7 118 const rss = await server.feed.getXML({ feed: 'videos' })
c1bc8ee4 119 expect(xmlParser.validate(rss)).to.be.true
b70025bf 120
c1bc8ee4 121 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
b70025bf
C
122
123 const enclosure = xmlDoc.rss.channel.item[0].enclosure
124 expect(enclosure).to.exist
125 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
126 expect(enclosure['@_length']).to.equal(218910)
127 expect(enclosure['@_url']).to.contain('720.torrent')
fe3a55b0
C
128 }
129 })
130
131 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
132 for (const server of servers) {
89d241a7 133 const json = await server.feed.getJSON({ feed: 'videos' })
c1bc8ee4 134 const jsonObj = JSON.parse(json)
662fb3ab 135 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
136 expect(jsonObj.items[0].attachments).to.exist
137 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
138 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
139 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
140 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
fe3a55b0
C
141 }
142 })
662fb3ab
C
143
144 it('Should filter by account', async function () {
57cfff78 145 {
89d241a7 146 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
c1bc8ee4 147 const jsonObj = JSON.parse(json)
57cfff78 148 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
149 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
150 expect(jsonObj.items[0].author.name).to.equal('root')
57cfff78
C
151 }
152
153 {
89d241a7 154 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
c1bc8ee4 155 const jsonObj = JSON.parse(json)
57cfff78 156 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
157 expect(jsonObj.items[0].title).to.equal('user video')
158 expect(jsonObj.items[0].author.name).to.equal('john')
57cfff78
C
159 }
160
662fb3ab
C
161 for (const server of servers) {
162 {
89d241a7 163 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
c1bc8ee4 164 const jsonObj = JSON.parse(json)
662fb3ab 165 expect(jsonObj.items.length).to.be.equal(1)
a1587156 166 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
167 }
168
169 {
89d241a7 170 const json = await server.feed.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
c1bc8ee4 171 const jsonObj = JSON.parse(json)
662fb3ab 172 expect(jsonObj.items.length).to.be.equal(1)
a1587156 173 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
174 }
175 }
57cfff78 176 })
662fb3ab 177
57cfff78 178 it('Should filter by video channel', async function () {
662fb3ab 179 {
89d241a7 180 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
c1bc8ee4 181 const jsonObj = JSON.parse(json)
662fb3ab 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')
662fb3ab
C
185 }
186
187 {
89d241a7 188 const json = await servers[0].feed.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
c1bc8ee4 189 const jsonObj = JSON.parse(json)
662fb3ab 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')
662fb3ab 193 }
662fb3ab 194
662fb3ab
C
195 for (const server of servers) {
196 {
c1bc8ee4 197 const query = { videoChannelName: 'root_channel@localhost:' + servers[0].port }
89d241a7 198 const json = await server.feed.getJSON({ feed: 'videos', query })
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 {
c1bc8ee4 205 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
89d241a7 206 const json = await server.feed.getJSON({ feed: 'videos', query })
c1bc8ee4 207 const jsonObj = JSON.parse(json)
662fb3ab 208 expect(jsonObj.items.length).to.be.equal(1)
a1587156 209 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
210 }
211 }
662fb3ab 212 })
97816649
C
213
214 it('Should correctly have videos feed with HLS only', async function () {
215 this.timeout(120000)
216
89d241a7 217 await serverHLSOnly.videos.upload({ attributes: { name: 'hls only video' } })
97816649
C
218
219 await waitJobs([ serverHLSOnly ])
220
89d241a7 221 const json = await serverHLSOnly.feed.getJSON({ feed: 'videos' })
c1bc8ee4 222 const jsonObj = JSON.parse(json)
97816649
C
223 expect(jsonObj.items.length).to.be.equal(1)
224 expect(jsonObj.items[0].attachments).to.exist
225 expect(jsonObj.items[0].attachments.length).to.be.eq(4)
226
227 for (let i = 0; i < 4; i++) {
228 expect(jsonObj.items[0].attachments[i].mime_type).to.be.eq('application/x-bittorrent')
229 expect(jsonObj.items[0].attachments[i].size_in_bytes).to.be.greaterThan(0)
230 expect(jsonObj.items[0].attachments[i].url).to.exist
231 }
232 })
fe3a55b0
C
233 })
234
235 describe('Video comments feed', function () {
68b6fd21
C
236
237 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
fe3a55b0 238 for (const server of servers) {
89d241a7 239 const json = await server.feed.getJSON({ feed: 'video-comments' })
fe3a55b0 240
c1bc8ee4 241 const jsonObj = JSON.parse(json)
fe3a55b0 242 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
243 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
244 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
fe3a55b0
C
245 }
246 })
1df8a4d7
C
247
248 it('Should not list comments from muted accounts or instances', async function () {
696d83fd
C
249 this.timeout(30000)
250
251 const remoteHandle = 'root@localhost:' + servers[0].port
252
89d241a7 253 await servers[1].blocklist.addToServerBlocklist({ account: remoteHandle })
1df8a4d7
C
254
255 {
89d241a7 256 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 2 } })
c1bc8ee4 257 const jsonObj = JSON.parse(json)
1df8a4d7
C
258 expect(jsonObj.items.length).to.be.equal(0)
259 }
260
89d241a7 261 await servers[1].blocklist.removeFromServerBlocklist({ account: remoteHandle })
696d83fd
C
262
263 {
89d241a7 264 const videoUUID = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid
696d83fd 265 await waitJobs(servers)
89d241a7 266 await servers[0].comments.createThread({ videoId: videoUUID, text: 'super comment' })
696d83fd
C
267 await waitJobs(servers)
268
89d241a7 269 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 3 } })
c1bc8ee4 270 const jsonObj = JSON.parse(json)
696d83fd
C
271 expect(jsonObj.items.length).to.be.equal(3)
272 }
273
89d241a7 274 await servers[1].blocklist.addToMyBlocklist({ account: remoteHandle })
696d83fd
C
275
276 {
89d241a7 277 const json = await servers[1].feed.getJSON({ feed: 'video-comments', query: { version: 4 } })
c1bc8ee4 278 const jsonObj = JSON.parse(json)
696d83fd
C
279 expect(jsonObj.items.length).to.be.equal(2)
280 }
1df8a4d7 281 })
fe3a55b0
C
282 })
283
afff310e 284 describe('Video feed from my subscriptions', function () {
18490b07
C
285 let feeduserAccountId: number
286 let feeduserFeedToken: string
afff310e
RK
287
288 it('Should list no videos for a user with no videos and no subscriptions', async function () {
afff310e 289 const attr = { username: 'feeduser', password: 'password' }
89d241a7
C
290 await servers[0].users.create({ username: attr.username, password: attr.password })
291 const feeduserAccessToken = await servers[0].login.getAccessToken(attr)
afff310e
RK
292
293 {
89d241a7 294 const user = await servers[0].users.getMyInfo({ token: feeduserAccessToken })
afff310e
RK
295 feeduserAccountId = user.account.id
296 }
297
298 {
89d241a7 299 const token = await servers[0].users.getMyScopedTokens({ token: feeduserAccessToken })
afff310e
RK
300 feeduserFeedToken = token.feedToken
301 }
302
303 {
89d241a7 304 const body = await servers[0].subscriptions.listVideos({ token: feeduserAccessToken })
2c27e704 305 expect(body.total).to.equal(0)
afff310e 306
c1bc8ee4 307 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
89d241a7 308 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 309 const jsonObj = JSON.parse(json)
afff310e
RK
310 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
311 }
312 })
313
18490b07 314 it('Should fail with an invalid token', async function () {
c1bc8ee4 315 const query = { accountId: feeduserAccountId, token: 'toto' }
89d241a7 316 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
317 })
318
319 it('Should fail with a token of another user', async function () {
c1bc8ee4 320 const query = { accountId: feeduserAccountId, token: userFeedToken }
89d241a7 321 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
322 })
323
afff310e 324 it('Should list no videos for a user with videos but no subscriptions', async function () {
89d241a7 325 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 326 expect(body.total).to.equal(0)
afff310e 327
c1bc8ee4 328 const query = { accountId: userAccountId, token: userFeedToken }
89d241a7 329 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 330 const jsonObj = JSON.parse(json)
18490b07 331 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
afff310e
RK
332 })
333
334 it('Should list self videos for a user with a subscription to themselves', async function () {
335 this.timeout(30000)
336
89d241a7 337 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
afff310e
RK
338 await waitJobs(servers)
339
340 {
89d241a7 341 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704
C
342 expect(body.total).to.equal(1)
343 expect(body.data[0].name).to.equal('user video')
afff310e 344
c1bc8ee4 345 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
89d241a7 346 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 347 const jsonObj = JSON.parse(json)
afff310e
RK
348 expect(jsonObj.items.length).to.be.equal(1) // subscribed to self, it should not list the instance's videos but list john's
349 }
350 })
351
352 it('Should list videos of a user\'s subscription', async function () {
353 this.timeout(30000)
354
89d241a7 355 await servers[0].subscriptions.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
afff310e
RK
356 await waitJobs(servers)
357
358 {
89d241a7 359 const body = await servers[0].subscriptions.listVideos({ token: userAccessToken })
2c27e704 360 expect(body.total).to.equal(2, "there should be 2 videos part of the subscription")
afff310e 361
c1bc8ee4 362 const query = { accountId: userAccountId, token: userFeedToken, version: 2 }
89d241a7 363 const json = await servers[0].feed.getJSON({ feed: 'subscriptions', query })
c1bc8ee4 364 const jsonObj = JSON.parse(json)
afff310e
RK
365 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
366 }
367 })
368
18490b07 369 it('Should renew the token, and so have an invalid old token', async function () {
89d241a7 370 await servers[0].users.renewMyScopedTokens({ token: userAccessToken })
18490b07 371
c1bc8ee4 372 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
89d241a7 373 await servers[0].feed.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
374 })
375
376 it('Should succeed with the new token', async function () {
89d241a7 377 const token = await servers[0].users.getMyScopedTokens({ token: userAccessToken })
18490b07
C
378 userFeedToken = token.feedToken
379
c1bc8ee4 380 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
89d241a7 381 await servers[0].feed.getJSON({ feed: 'subscriptions', query })
18490b07
C
382 })
383
afff310e
RK
384 })
385
7c3b7976 386 after(async function () {
97816649 387 await cleanupTests([ ...servers, serverHLSOnly ])
fe3a55b0
C
388 })
389})