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