allow limiting video-comments rss feeds to an account or video channel
[oweals/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 libxmljs from 'libxmljs'
6 import {
7   addAccountToAccountBlocklist,
8   addAccountToServerBlocklist,
9   removeAccountFromServerBlocklist
10 } from '@shared/extra-utils/users/blocklist'
11 import { VideoPrivacy } from '@shared/models'
12 import {
13   cleanupTests,
14   createUser,
15   doubleFollow,
16   flushAndRunMultipleServers,
17   getJSONfeed,
18   getMyUserInformation,
19   getXMLfeed,
20   ServerInfo,
21   setAccessTokensToServers,
22   uploadVideo,
23   uploadVideoAndGetId,
24   userLogin
25 } from '../../../shared/extra-utils'
26 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
27 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
28 import { User } from '../../../shared/models/users'
29
30 chai.use(require('chai-xml'))
31 chai.use(require('chai-json-schema'))
32 chai.config.includeStack = true
33 const expect = chai.expect
34
35 describe('Test syndication feeds', () => {
36   let servers: ServerInfo[] = []
37   let userAccessToken: string
38   let rootAccountId: number
39   let rootChannelId: number
40   let userAccountId: number
41   let userChannelId: number
42
43   before(async function () {
44     this.timeout(120000)
45
46     // Run servers
47     servers = await flushAndRunMultipleServers(2)
48
49     await setAccessTokensToServers(servers)
50     await doubleFollow(servers[0], servers[1])
51
52     {
53       const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
54       const user: User = res.body
55       rootAccountId = user.account.id
56       rootChannelId = user.videoChannels[0].id
57     }
58
59     {
60       const attr = { username: 'john', password: 'password' }
61       await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
62       userAccessToken = await userLogin(servers[0], attr)
63
64       const res = await getMyUserInformation(servers[0].url, userAccessToken)
65       const user: User = res.body
66       userAccountId = user.account.id
67       userChannelId = user.videoChannels[0].id
68     }
69
70     {
71       await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
72     }
73
74     {
75       const videoAttributes = {
76         name: 'my super name for server 1',
77         description: 'my super description for server 1',
78         fixture: 'video_short.webm'
79       }
80       const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
81       const videoId = res.body.video.id
82
83       await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
84       await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
85     }
86
87     {
88       const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
89       const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
90       const videoId = res.body.video.id
91
92       await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
93     }
94
95     await waitJobs(servers)
96   })
97
98   describe('All feed', function () {
99
100     it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
101       for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
102         const rss = await getXMLfeed(servers[0].url, feed)
103         expect(rss.text).xml.to.be.valid()
104
105         const atom = await getXMLfeed(servers[0].url, feed, 'atom')
106         expect(atom.text).xml.to.be.valid()
107       }
108     })
109
110     it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
111       for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
112         const json = await getJSONfeed(servers[0].url, feed)
113         expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
114       }
115     })
116   })
117
118   describe('Videos feed', function () {
119     it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
120       for (const server of servers) {
121         const rss = await getXMLfeed(server.url, 'videos')
122         const xmlDoc = libxmljs.parseXmlString(rss.text)
123         const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
124         expect(xmlEnclosure).to.exist
125         expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
126         expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
127         expect(xmlEnclosure.attr('url').value()).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 getJSONfeed(server.url, 'videos')
134         const jsonObj = JSON.parse(json.text)
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 getJSONfeed(servers[0].url, 'videos', { accountId: rootAccountId })
147         const jsonObj = JSON.parse(json.text)
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 getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId })
155         const jsonObj = JSON.parse(json.text)
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 getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
164           const jsonObj = JSON.parse(json.text)
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 getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
171           const jsonObj = JSON.parse(json.text)
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 getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
181         const jsonObj = JSON.parse(json.text)
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 getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
189         const jsonObj = JSON.parse(json.text)
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 json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
198           const jsonObj = JSON.parse(json.text)
199           expect(jsonObj.items.length).to.be.equal(1)
200           expect(jsonObj.items[0].title).to.equal('my super name for server 1')
201         }
202
203         {
204           const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
205           const jsonObj = JSON.parse(json.text)
206           expect(jsonObj.items.length).to.be.equal(1)
207           expect(jsonObj.items[0].title).to.equal('user video')
208         }
209       }
210     })
211   })
212
213   describe('Video comments feed', function () {
214
215     it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
216       for (const server of servers) {
217         const json = await getJSONfeed(server.url, 'video-comments')
218
219         const jsonObj = JSON.parse(json.text)
220         expect(jsonObj.items.length).to.be.equal(2)
221         expect(jsonObj.items[0].html_content).to.equal('super comment 2')
222         expect(jsonObj.items[1].html_content).to.equal('super comment 1')
223       }
224     })
225
226     it('Should not list comments from muted accounts or instances', async function () {
227       this.timeout(30000)
228
229       const remoteHandle = 'root@localhost:' + servers[0].port
230
231       await addAccountToServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
232
233       {
234         const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 2 })
235         const jsonObj = JSON.parse(json.text)
236         expect(jsonObj.items.length).to.be.equal(0)
237       }
238
239       await removeAccountFromServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
240
241       {
242         const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
243         await waitJobs(servers)
244         await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'super comment')
245         await waitJobs(servers)
246
247         const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 3 })
248         const jsonObj = JSON.parse(json.text)
249         expect(jsonObj.items.length).to.be.equal(3)
250       }
251
252       await addAccountToAccountBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
253
254       {
255         const json = await getJSONfeed(servers[1].url, 'video-comments', { version: 4 })
256         const jsonObj = JSON.parse(json.text)
257         expect(jsonObj.items.length).to.be.equal(2)
258       }
259     })
260   })
261
262   after(async function () {
263     await cleanupTests(servers)
264   })
265 })