]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/feeds/feeds.ts
Inject plugin CSS in embed too
[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
C
4import * as chai from 'chai'
5import * as libxmljs from 'libxmljs'
6import {
7 addAccountToAccountBlocklist,
8 addAccountToServerBlocklist,
9 removeAccountFromServerBlocklist
10} from '@shared/extra-utils/users/blocklist'
11import { VideoPrivacy } from '@shared/models'
fe3a55b0 12import {
7c3b7976 13 cleanupTests,
662fb3ab 14 createUser,
fe3a55b0
C
15 doubleFollow,
16 flushAndRunMultipleServers,
57cfff78
C
17 getJSONfeed,
18 getMyUserInformation,
fe3a55b0 19 getXMLfeed,
fe3a55b0
C
20 ServerInfo,
21 setAccessTokensToServers,
57cfff78 22 uploadVideo,
696d83fd 23 uploadVideoAndGetId,
57cfff78 24 userLogin
94565d52 25} from '../../../shared/extra-utils'
94565d52 26import { waitJobs } from '../../../shared/extra-utils/server/jobs'
696d83fd 27import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
662fb3ab 28import { User } from '../../../shared/models/users'
fe3a55b0
C
29
30chai.use(require('chai-xml'))
31chai.use(require('chai-json-schema'))
32chai.config.includeStack = true
33const expect = chai.expect
34
35describe('Test syndication feeds', () => {
36 let servers: ServerInfo[] = []
662fb3ab 37 let userAccessToken: string
57cfff78
C
38 let rootAccountId: number
39 let rootChannelId: number
40 let userAccountId: number
41 let userChannelId: number
fe3a55b0
C
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
662fb3ab
C
52 {
53 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
54 const user: User = res.body
57cfff78
C
55 rootAccountId = user.account.id
56 rootChannelId = user.videoChannels[0].id
fe3a55b0 57 }
fe3a55b0 58
662fb3ab
C
59 {
60 const attr = { username: 'john', password: 'password' }
a1587156 61 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
662fb3ab
C
62 userAccessToken = await userLogin(servers[0], attr)
63
64 const res = await getMyUserInformation(servers[0].url, userAccessToken)
65 const user: User = res.body
57cfff78
C
66 userAccountId = user.account.id
67 userChannelId = user.videoChannels[0].id
662fb3ab
C
68 }
69
70 {
a1587156 71 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
662fb3ab
C
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 }
a1587156 80 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
662fb3ab
C
81 const videoId = res.body.video.id
82
a1587156
C
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')
662fb3ab 85 }
fe3a55b0 86
68b6fd21
C
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
3cd0734f 95 await waitJobs(servers)
fe3a55b0
C
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' ]) {
a1587156 102 const rss = await getXMLfeed(servers[0].url, feed)
fe3a55b0
C
103 expect(rss.text).xml.to.be.valid()
104
a1587156 105 const atom = await getXMLfeed(servers[0].url, feed, 'atom')
fe3a55b0
C
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' ]) {
a1587156
C
112 const json = await getJSONfeed(servers[0].url, feed)
113 expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
fe3a55b0
C
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)
662fb3ab 135 expect(jsonObj.items.length).to.be.equal(2)
a1587156
C
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')
fe3a55b0
C
141 }
142 })
662fb3ab
C
143
144 it('Should filter by account', async function () {
57cfff78
C
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)
a1587156
C
149 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
150 expect(jsonObj.items[0].author.name).to.equal('root')
57cfff78
C
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)
a1587156
C
157 expect(jsonObj.items[0].title).to.equal('user video')
158 expect(jsonObj.items[0].author.name).to.equal('john')
57cfff78
C
159 }
160
662fb3ab
C
161 for (const server of servers) {
162 {
57cfff78 163 const json = await getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
662fb3ab
C
164 const jsonObj = JSON.parse(json.text)
165 expect(jsonObj.items.length).to.be.equal(1)
a1587156 166 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
167 }
168
169 {
57cfff78 170 const json = await getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
662fb3ab
C
171 const jsonObj = JSON.parse(json.text)
172 expect(jsonObj.items.length).to.be.equal(1)
a1587156 173 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
174 }
175 }
57cfff78 176 })
662fb3ab 177
57cfff78 178 it('Should filter by video channel', async function () {
662fb3ab 179 {
57cfff78 180 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
662fb3ab
C
181 const jsonObj = JSON.parse(json.text)
182 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
183 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
184 expect(jsonObj.items[0].author.name).to.equal('root')
662fb3ab
C
185 }
186
187 {
57cfff78 188 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
662fb3ab
C
189 const jsonObj = JSON.parse(json.text)
190 expect(jsonObj.items.length).to.be.equal(1)
a1587156
C
191 expect(jsonObj.items[0].title).to.equal('user video')
192 expect(jsonObj.items[0].author.name).to.equal('john')
662fb3ab 193 }
662fb3ab 194
662fb3ab
C
195 for (const server of servers) {
196 {
57cfff78 197 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
662fb3ab
C
198 const jsonObj = JSON.parse(json.text)
199 expect(jsonObj.items.length).to.be.equal(1)
a1587156 200 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
662fb3ab
C
201 }
202
203 {
57cfff78 204 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
662fb3ab
C
205 const jsonObj = JSON.parse(json.text)
206 expect(jsonObj.items.length).to.be.equal(1)
a1587156 207 expect(jsonObj.items[0].title).to.equal('user video')
662fb3ab
C
208 }
209 }
662fb3ab 210 })
fe3a55b0
C
211 })
212
213 describe('Video comments feed', function () {
68b6fd21
C
214
215 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
fe3a55b0
C
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)
a1587156
C
221 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
222 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
fe3a55b0
C
223 }
224 })
1df8a4d7
C
225
226 it('Should not list comments from muted accounts or instances', async function () {
696d83fd
C
227 this.timeout(30000)
228
229 const remoteHandle = 'root@localhost:' + servers[0].port
230
231 await addAccountToServerBlocklist(servers[1].url, servers[1].accessToken, remoteHandle)
1df8a4d7
C
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
696d83fd
C
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 }
1df8a4d7 259 })
fe3a55b0
C
260 })
261
7c3b7976
C
262 after(async function () {
263 await cleanupTests(servers)
fe3a55b0
C
264 })
265})