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