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