]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
Introduce feed command
[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 { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
15 import {
16 cleanupTests,
17 createUser,
18 doubleFollow,
19 flushAndRunMultipleServers,
20 flushAndRunServer,
21 getMyUserInformation,
22 getUserScopedTokens,
23 renewUserScopedTokens,
24 ServerInfo,
25 setAccessTokensToServers,
26 uploadVideo,
27 uploadVideoAndGetId,
28 userLogin
29 } from '../../../shared/extra-utils'
30 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
31 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
32 import { User } from '../../../shared/models/users'
33
34 chai.use(require('chai-xml'))
35 chai.use(require('chai-json-schema'))
36 chai.config.includeStack = true
37 const expect = chai.expect
38
39 describe('Test syndication feeds', () => {
40 let servers: ServerInfo[] = []
41 let serverHLSOnly: ServerInfo
42 let userAccessToken: string
43 let rootAccountId: number
44 let rootChannelId: number
45 let userAccountId: number
46 let userChannelId: number
47 let userFeedToken: string
48
49 before(async function () {
50 this.timeout(120000)
51
52 // Run servers
53 servers = await flushAndRunMultipleServers(2)
54 serverHLSOnly = await flushAndRunServer(3, {
55 transcoding: {
56 enabled: true,
57 webtorrent: { enabled: false },
58 hls: { enabled: true }
59 }
60 })
61
62 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
63 await doubleFollow(servers[0], servers[1])
64
65 {
66 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
67 const user: User = res.body
68 rootAccountId = user.account.id
69 rootChannelId = user.videoChannels[0].id
70 }
71
72 {
73 const attr = { username: 'john', password: 'password' }
74 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
75 userAccessToken = await userLogin(servers[0], attr)
76
77 const res = await getMyUserInformation(servers[0].url, userAccessToken)
78 const user: User = res.body
79 userAccountId = user.account.id
80 userChannelId = user.videoChannels[0].id
81
82 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
83 const token: ScopedToken = res2.body
84 userFeedToken = token.feedToken
85 }
86
87 {
88 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
89 }
90
91 {
92 const videoAttributes = {
93 name: 'my super name for server 1',
94 description: 'my super description for server 1',
95 fixture: 'video_short.webm'
96 }
97 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
98 const videoId = res.body.video.id
99
100 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
101 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
102 }
103
104 {
105 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
106 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
107 const videoId = res.body.video.id
108
109 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
110 }
111
112 await waitJobs(servers)
113 })
114
115 describe('All feed', function () {
116
117 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
118 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
119 const rss = await servers[0].feedCommand.getXML({ feed })
120 expect(rss).xml.to.be.valid()
121
122 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
123 expect(atom).xml.to.be.valid()
124 }
125 })
126
127 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
128 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
129 const jsonText = await servers[0].feedCommand.getJSON({ feed })
130 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
131 }
132 })
133 })
134
135 describe('Videos feed', function () {
136
137 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
138 for (const server of servers) {
139 const rss = await server.feedCommand.getXML({ feed: 'videos' })
140 expect(xmlParser.validate(rss)).to.be.true
141
142 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
143
144 const enclosure = xmlDoc.rss.channel.item[0].enclosure
145 expect(enclosure).to.exist
146 expect(enclosure['@_type']).to.equal('application/x-bittorrent')
147 expect(enclosure['@_length']).to.equal(218910)
148 expect(enclosure['@_url']).to.contain('720.torrent')
149 }
150 })
151
152 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
153 for (const server of servers) {
154 const json = await server.feedCommand.getJSON({ feed: 'videos' })
155 const jsonObj = JSON.parse(json)
156 expect(jsonObj.items.length).to.be.equal(2)
157 expect(jsonObj.items[0].attachments).to.exist
158 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
159 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
160 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
161 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
162 }
163 })
164
165 it('Should filter by account', async function () {
166 {
167 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
168 const jsonObj = JSON.parse(json)
169 expect(jsonObj.items.length).to.be.equal(1)
170 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
171 expect(jsonObj.items[0].author.name).to.equal('root')
172 }
173
174 {
175 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
176 const jsonObj = JSON.parse(json)
177 expect(jsonObj.items.length).to.be.equal(1)
178 expect(jsonObj.items[0].title).to.equal('user video')
179 expect(jsonObj.items[0].author.name).to.equal('john')
180 }
181
182 for (const server of servers) {
183 {
184 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
185 const jsonObj = JSON.parse(json)
186 expect(jsonObj.items.length).to.be.equal(1)
187 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
188 }
189
190 {
191 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
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 }
196 }
197 })
198
199 it('Should filter by video channel', async function () {
200 {
201 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
202 const jsonObj = JSON.parse(json)
203 expect(jsonObj.items.length).to.be.equal(1)
204 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
205 expect(jsonObj.items[0].author.name).to.equal('root')
206 }
207
208 {
209 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
210 const jsonObj = JSON.parse(json)
211 expect(jsonObj.items.length).to.be.equal(1)
212 expect(jsonObj.items[0].title).to.equal('user video')
213 expect(jsonObj.items[0].author.name).to.equal('john')
214 }
215
216 for (const server of servers) {
217 {
218 const query = { videoChannelName: 'root_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('my super name for server 1')
223 }
224
225 {
226 const query = { videoChannelName: 'john_channel@localhost:' + servers[0].port }
227 const json = await server.feedCommand.getJSON({ feed: 'videos', query })
228 const jsonObj = JSON.parse(json)
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 serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
243 const jsonObj = JSON.parse(json)
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 server.feedCommand.getJSON({ feed: 'video-comments' })
261
262 const jsonObj = JSON.parse(json)
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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
278 const jsonObj = JSON.parse(json)
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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
291 const jsonObj = JSON.parse(json)
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 servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
299 const jsonObj = JSON.parse(json)
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 query = { accountId: feeduserAccountId, token: feeduserFeedToken }
331 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
332 const jsonObj = JSON.parse(json)
333 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
334 }
335 })
336
337 it('Should fail with an invalid token', async function () {
338 const query = { accountId: feeduserAccountId, token: 'toto' }
339 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
340 })
341
342 it('Should fail with a token of another user', async function () {
343 const query = { accountId: feeduserAccountId, token: userFeedToken }
344 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
345 })
346
347 it('Should list no videos for a user with videos but no subscriptions', async function () {
348 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
349 expect(res.body.total).to.equal(0)
350
351 const query = { accountId: userAccountId, token: userFeedToken }
352 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
353 const jsonObj = JSON.parse(json)
354 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
355 })
356
357 it('Should list self videos for a user with a subscription to themselves', async function () {
358 this.timeout(30000)
359
360 await addUserSubscription(servers[0].url, userAccessToken, 'john_channel@localhost:' + servers[0].port)
361 await waitJobs(servers)
362
363 {
364 const res = await listUserSubscriptionVideos(servers[0].url, userAccessToken)
365 expect(res.body.total).to.equal(1)
366 expect(res.body.data[0].name).to.equal('user video')
367
368 const query = { accountId: userAccountId, token: userFeedToken, version: 1 }
369 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
370 const jsonObj = JSON.parse(json)
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 query = { accountId: userAccountId, token: userFeedToken, version: 2 }
386 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
387 const jsonObj = JSON.parse(json)
388 expect(jsonObj.items.length).to.be.equal(2) // subscribed to root, it should not list the instance's videos but list root/john's
389 }
390 })
391
392 it('Should renew the token, and so have an invalid old token', async function () {
393 await renewUserScopedTokens(servers[0].url, userAccessToken)
394
395 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
396 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
397 })
398
399 it('Should succeed with the new token', async function () {
400 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
401 const token: ScopedToken = res2.body
402 userFeedToken = token.feedToken
403
404 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
405 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
406 })
407
408 })
409
410 after(async function () {
411 await cleanupTests([ ...servers, serverHLSOnly ])
412 })
413 })