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