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