]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/feeds/feeds.ts
Introduce subscriptions command
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
fe3a55b0 2
fe3a55b0 3import 'mocha'
696d83fd 4import * as chai from 'chai'
b70025bf 5import * as xmlParser from 'fast-xml-parser'
5f8bd4cb 6import { HttpStatusCode } from '@shared/core-utils'
696d83fd 7import {
5f8bd4cb 8 addVideoCommentThread,
7c3b7976 9 cleanupTests,
662fb3ab 10 createUser,
fe3a55b0
C
11 doubleFollow,
12 flushAndRunMultipleServers,
18490b07 13 flushAndRunServer,
57cfff78 14 getMyUserInformation,
18490b07 15 getUserScopedTokens,
18490b07 16 renewUserScopedTokens,
fe3a55b0
C
17 ServerInfo,
18 setAccessTokensToServers,
57cfff78 19 uploadVideo,
696d83fd 20 uploadVideoAndGetId,
5f8bd4cb
C
21 userLogin,
22 waitJobs
23} from '@shared/extra-utils'
24import { User, VideoPrivacy } from '@shared/models'
25import { ScopedToken } from '@shared/models/users/user-scoped-token'
fe3a55b0
C
26
27chai.use(require('chai-xml'))
28chai.use(require('chai-json-schema'))
29chai.config.includeStack = true
30const expect = chai.expect
31
32describe('Test syndication feeds', () => {
33 let servers: ServerInfo[] = []
97816649 34 let serverHLSOnly: ServerInfo
662fb3ab 35 let userAccessToken: string
57cfff78
C
36 let rootAccountId: number
37 let rootChannelId: number
38 let userAccountId: number
39 let userChannelId: number
afff310e 40 let userFeedToken: string
fe3a55b0
C
41
42 before(async function () {
43 this.timeout(120000)
44
45 // Run servers
46 servers = await flushAndRunMultipleServers(2)
97816649
C
47 serverHLSOnly = await flushAndRunServer(3, {
48 transcoding: {
49 enabled: true,
50 webtorrent: { enabled: false },
51 hls: { enabled: true }
52 }
53 })
fe3a55b0 54
97816649 55 await setAccessTokensToServers([ ...servers, serverHLSOnly ])
fe3a55b0
C
56 await doubleFollow(servers[0], servers[1])
57
662fb3ab
C
58 {
59 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
60 const user: User = res.body
57cfff78
C
61 rootAccountId = user.account.id
62 rootChannelId = user.videoChannels[0].id
fe3a55b0 63 }
fe3a55b0 64
662fb3ab
C
65 {
66 const attr = { username: 'john', password: 'password' }
a1587156 67 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
662fb3ab
C
68 userAccessToken = await userLogin(servers[0], attr)
69
70 const res = await getMyUserInformation(servers[0].url, userAccessToken)
71 const user: User = res.body
57cfff78
C
72 userAccountId = user.account.id
73 userChannelId = user.videoChannels[0].id
afff310e
RK
74
75 const res2 = await getUserScopedTokens(servers[0].url, userAccessToken)
76 const token: ScopedToken = res2.body
77 userFeedToken = token.feedToken
662fb3ab
C
78 }
79
80 {
a1587156 81 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
662fb3ab
C
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 }
a1587156 90 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
662fb3ab
C
91 const videoId = res.body.video.id
92
a1587156
C
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')
662fb3ab 95 }
fe3a55b0 96
68b6fd21
C
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
3cd0734f 105 await waitJobs(servers)
fe3a55b0
C
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' ]) {
c1bc8ee4
C
112 const rss = await servers[0].feedCommand.getXML({ feed })
113 expect(rss).xml.to.be.valid()
fe3a55b0 114
c1bc8ee4
C
115 const atom = await servers[0].feedCommand.getXML({ feed, format: 'atom' })
116 expect(atom).xml.to.be.valid()
fe3a55b0
C
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' ]) {
c1bc8ee4
C
122 const jsonText = await servers[0].feedCommand.getJSON({ feed })
123 expect(JSON.parse(jsonText)).to.be.jsonSchema({ type: 'object' })
fe3a55b0
C
124 }
125 })
126 })
127
128 describe('Videos feed', function () {
97816649 129
fe3a55b0
C
130 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
131 for (const server of servers) {
c1bc8ee4
C
132 const rss = await server.feedCommand.getXML({ feed: 'videos' })
133 expect(xmlParser.validate(rss)).to.be.true
b70025bf 134
c1bc8ee4 135 const xmlDoc = xmlParser.parse(rss, { parseAttributeValue: true, ignoreAttributes: false })
b70025bf
C
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')
fe3a55b0
C
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) {
c1bc8ee4
C
147 const json = await server.feedCommand.getJSON({ feed: 'videos' })
148 const jsonObj = JSON.parse(json)
662fb3ab 149 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
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')
fe3a55b0
C
155 }
156 })
662fb3ab
C
157
158 it('Should filter by account', async function () {
57cfff78 159 {
c1bc8ee4
C
160 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: rootAccountId } })
161 const jsonObj = JSON.parse(json)
57cfff78 162 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
163 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
164 expect(jsonObj.items[0].author.name).to.equal('root')
57cfff78
C
165 }
166
167 {
c1bc8ee4
C
168 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { accountId: userAccountId } })
169 const jsonObj = JSON.parse(json)
57cfff78 170 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
171 expect(jsonObj.items[0].title).to.equal('user video')
172 expect(jsonObj.items[0].author.name).to.equal('john')
57cfff78
C
173 }
174
662fb3ab
C
175 for (const server of servers) {
176 {
c1bc8ee4
C
177 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'root@localhost:' + servers[0].port } })
178 const jsonObj = JSON.parse(json)
662fb3ab 179 expect(jsonObj.items.length).to.be.equal(1)
a1587156 180 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
181 }
182
183 {
c1bc8ee4
C
184 const json = await server.feedCommand.getJSON({ feed: 'videos', query: { accountName: 'john@localhost:' + servers[0].port } })
185 const jsonObj = JSON.parse(json)
662fb3ab 186 expect(jsonObj.items.length).to.be.equal(1)
a1587156 187 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
188 }
189 }
57cfff78 190 })
662fb3ab 191
57cfff78 192 it('Should filter by video channel', async function () {
662fb3ab 193 {
c1bc8ee4
C
194 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: rootChannelId } })
195 const jsonObj = JSON.parse(json)
662fb3ab 196 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
197 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
198 expect(jsonObj.items[0].author.name).to.equal('root')
662fb3ab
C
199 }
200
201 {
c1bc8ee4
C
202 const json = await servers[0].feedCommand.getJSON({ feed: 'videos', query: { videoChannelId: userChannelId } })
203 const jsonObj = JSON.parse(json)
662fb3ab 204 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
205 expect(jsonObj.items[0].title).to.equal('user video')
206 expect(jsonObj.items[0].author.name).to.equal('john')
662fb3ab 207 }
662fb3ab 208
662fb3ab
C
209 for (const server of servers) {
210 {
c1bc8ee4
C
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)
662fb3ab 214 expect(jsonObj.items.length).to.be.equal(1)
a1587156 215 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
216 }
217
218 {
c1bc8ee4
C
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)
662fb3ab 222 expect(jsonObj.items.length).to.be.equal(1)
a1587156 223 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
224 }
225 }
662fb3ab 226 })
97816649
C
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
c1bc8ee4
C
235 const json = await serverHLSOnly.feedCommand.getJSON({ feed: 'videos' })
236 const jsonObj = JSON.parse(json)
97816649
C
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 })
fe3a55b0
C
247 })
248
249 describe('Video comments feed', function () {
68b6fd21
C
250
251 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
fe3a55b0 252 for (const server of servers) {
c1bc8ee4 253 const json = await server.feedCommand.getJSON({ feed: 'video-comments' })
fe3a55b0 254
c1bc8ee4 255 const jsonObj = JSON.parse(json)
fe3a55b0 256 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
257 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
258 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
fe3a55b0
C
259 }
260 })
1df8a4d7
C
261
262 it('Should not list comments from muted accounts or instances', async function () {
696d83fd
C
263 this.timeout(30000)
264
265 const remoteHandle = 'root@localhost:' + servers[0].port
266
5f8bd4cb 267 await servers[1].blocklistCommand.addToServerBlocklist({ account: remoteHandle })
1df8a4d7
C
268
269 {
c1bc8ee4
C
270 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 2 } })
271 const jsonObj = JSON.parse(json)
1df8a4d7
C
272 expect(jsonObj.items.length).to.be.equal(0)
273 }
274
5f8bd4cb 275 await servers[1].blocklistCommand.removeFromServerBlocklist({ account: remoteHandle })
696d83fd
C
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
c1bc8ee4
C
283 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 3 } })
284 const jsonObj = JSON.parse(json)
696d83fd
C
285 expect(jsonObj.items.length).to.be.equal(3)
286 }
287
5f8bd4cb 288 await servers[1].blocklistCommand.addToMyBlocklist({ account: remoteHandle })
696d83fd
C
289
290 {
c1bc8ee4
C
291 const json = await servers[1].feedCommand.getJSON({ feed: 'video-comments', query: { version: 4 } })
292 const jsonObj = JSON.parse(json)
696d83fd
C
293 expect(jsonObj.items.length).to.be.equal(2)
294 }
1df8a4d7 295 })
fe3a55b0
C
296 })
297
afff310e 298 describe('Video feed from my subscriptions', function () {
18490b07
C
299 let feeduserAccountId: number
300 let feeduserFeedToken: string
afff310e
RK
301
302 it('Should list no videos for a user with no videos and no subscriptions', async function () {
afff310e
RK
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 {
2c27e704
C
320 const body = await servers[0].subscriptionsCommand.listVideos({ token: feeduserAccessToken })
321 expect(body.total).to.equal(0)
afff310e 322
c1bc8ee4
C
323 const query = { accountId: feeduserAccountId, token: feeduserFeedToken }
324 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
325 const jsonObj = JSON.parse(json)
afff310e
RK
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
18490b07 330 it('Should fail with an invalid token', async function () {
c1bc8ee4
C
331 const query = { accountId: feeduserAccountId, token: 'toto' }
332 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
333 })
334
335 it('Should fail with a token of another user', async function () {
c1bc8ee4
C
336 const query = { accountId: feeduserAccountId, token: userFeedToken }
337 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
338 })
339
afff310e 340 it('Should list no videos for a user with videos but no subscriptions', async function () {
2c27e704
C
341 const body = await servers[0].subscriptionsCommand.listVideos({ token: userAccessToken })
342 expect(body.total).to.equal(0)
afff310e 343
c1bc8ee4
C
344 const query = { accountId: userAccountId, token: userFeedToken }
345 const json = await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
346 const jsonObj = JSON.parse(json)
18490b07 347 expect(jsonObj.items.length).to.be.equal(0) // no subscription, it should not list the instance's videos but list 0 videos
afff310e
RK
348 })
349
350 it('Should list self videos for a user with a subscription to themselves', async function () {
351 this.timeout(30000)
352
2c27e704 353 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'john_channel@localhost:' + servers[0].port })
afff310e
RK
354 await waitJobs(servers)
355
356 {
2c27e704
C
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')
afff310e 360
c1bc8ee4
C
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)
afff310e
RK
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
2c27e704 371 await servers[0].subscriptionsCommand.add({ token: userAccessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
afff310e
RK
372 await waitJobs(servers)
373
374 {
2c27e704
C
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")
afff310e 377
c1bc8ee4
C
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)
afff310e
RK
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
18490b07
C
385 it('Should renew the token, and so have an invalid old token', async function () {
386 await renewUserScopedTokens(servers[0].url, userAccessToken)
387
c1bc8ee4
C
388 const query = { accountId: userAccountId, token: userFeedToken, version: 3 }
389 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
18490b07
C
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
c1bc8ee4
C
397 const query = { accountId: userAccountId, token: userFeedToken, version: 4 }
398 await servers[0].feedCommand.getJSON({ feed: 'subscriptions', query })
18490b07
C
399 })
400
afff310e
RK
401 })
402
7c3b7976 403 after(async function () {
97816649 404 await cleanupTests([ ...servers, serverHLSOnly ])
fe3a55b0
C
405 })
406})