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