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