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