]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/feeds/feeds.ts
d978123cfc827449b0a22e28ae4da90f74146862
[github/Chocobozzz/PeerTube.git] / server / tests / feeds / feeds.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 cleanupTests,
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 getJSONfeed,
11 getMyUserInformation,
12 getXMLfeed,
13 ServerInfo,
14 setAccessTokensToServers,
15 uploadVideo,
16 userLogin
17 } from '../../../shared/extra-utils'
18 import * as libxmljs from 'libxmljs'
19 import { addVideoCommentThread } from '../../../shared/extra-utils/videos/video-comments'
20 import { waitJobs } from '../../../shared/extra-utils/server/jobs'
21 import { User } from '../../../shared/models/users'
22 import { VideoPrivacy } from '@shared/models'
23
24 chai.use(require('chai-xml'))
25 chai.use(require('chai-json-schema'))
26 chai.config.includeStack = true
27 const expect = chai.expect
28
29 describe('Test syndication feeds', () => {
30 let servers: ServerInfo[] = []
31 let userAccessToken: string
32 let rootAccountId: number
33 let rootChannelId: number
34 let userAccountId: number
35 let userChannelId: number
36
37 before(async function () {
38 this.timeout(120000)
39
40 // Run servers
41 servers = await flushAndRunMultipleServers(2)
42
43 await setAccessTokensToServers(servers)
44 await doubleFollow(servers[0], servers[1])
45
46 {
47 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
48 const user: User = res.body
49 rootAccountId = user.account.id
50 rootChannelId = user.videoChannels[0].id
51 }
52
53 {
54 const attr = { username: 'john', password: 'password' }
55 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: attr.username, password: attr.password })
56 userAccessToken = await userLogin(servers[0], attr)
57
58 const res = await getMyUserInformation(servers[0].url, userAccessToken)
59 const user: User = res.body
60 userAccountId = user.account.id
61 userChannelId = user.videoChannels[0].id
62 }
63
64 {
65 await uploadVideo(servers[0].url, userAccessToken, { name: 'user video' })
66 }
67
68 {
69 const videoAttributes = {
70 name: 'my super name for server 1',
71 description: 'my super description for server 1',
72 fixture: 'video_short.webm'
73 }
74 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
75 const videoId = res.body.video.id
76
77 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
78 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
79 }
80
81 {
82 const videoAttributes = { name: 'unlisted video', privacy: VideoPrivacy.UNLISTED }
83 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
84 const videoId = res.body.video.id
85
86 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'comment on unlisted video')
87 }
88
89 await waitJobs(servers)
90 })
91
92 describe('All feed', function () {
93
94 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
95 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
96 const rss = await getXMLfeed(servers[0].url, feed)
97 expect(rss.text).xml.to.be.valid()
98
99 const atom = await getXMLfeed(servers[0].url, feed, 'atom')
100 expect(atom.text).xml.to.be.valid()
101 }
102 })
103
104 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
105 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
106 const json = await getJSONfeed(servers[0].url, feed)
107 expect(JSON.parse(json.text)).to.be.jsonSchema({ type: 'object' })
108 }
109 })
110 })
111
112 describe('Videos feed', function () {
113 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
114 for (const server of servers) {
115 const rss = await getXMLfeed(server.url, 'videos')
116 const xmlDoc = libxmljs.parseXmlString(rss.text)
117 const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
118 expect(xmlEnclosure).to.exist
119 expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
120 expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
121 expect(xmlEnclosure.attr('url').value()).to.contain('720.torrent')
122 }
123 })
124
125 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
126 for (const server of servers) {
127 const json = await getJSONfeed(server.url, 'videos')
128 const jsonObj = JSON.parse(json.text)
129 expect(jsonObj.items.length).to.be.equal(2)
130 expect(jsonObj.items[0].attachments).to.exist
131 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
132 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
133 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
134 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
135 }
136 })
137
138 it('Should filter by account', async function () {
139 {
140 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: rootAccountId })
141 const jsonObj = JSON.parse(json.text)
142 expect(jsonObj.items.length).to.be.equal(1)
143 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
144 expect(jsonObj.items[0].author.name).to.equal('root')
145 }
146
147 {
148 const json = await getJSONfeed(servers[0].url, 'videos', { accountId: userAccountId })
149 const jsonObj = JSON.parse(json.text)
150 expect(jsonObj.items.length).to.be.equal(1)
151 expect(jsonObj.items[0].title).to.equal('user video')
152 expect(jsonObj.items[0].author.name).to.equal('john')
153 }
154
155 for (const server of servers) {
156 {
157 const json = await getJSONfeed(server.url, 'videos', { accountName: 'root@localhost:' + servers[0].port })
158 const jsonObj = JSON.parse(json.text)
159 expect(jsonObj.items.length).to.be.equal(1)
160 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
161 }
162
163 {
164 const json = await getJSONfeed(server.url, 'videos', { accountName: 'john@localhost:' + servers[0].port })
165 const jsonObj = JSON.parse(json.text)
166 expect(jsonObj.items.length).to.be.equal(1)
167 expect(jsonObj.items[0].title).to.equal('user video')
168 }
169 }
170 })
171
172 it('Should filter by video channel', async function () {
173 {
174 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: rootChannelId })
175 const jsonObj = JSON.parse(json.text)
176 expect(jsonObj.items.length).to.be.equal(1)
177 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
178 expect(jsonObj.items[0].author.name).to.equal('root')
179 }
180
181 {
182 const json = await getJSONfeed(servers[0].url, 'videos', { videoChannelId: userChannelId })
183 const jsonObj = JSON.parse(json.text)
184 expect(jsonObj.items.length).to.be.equal(1)
185 expect(jsonObj.items[0].title).to.equal('user video')
186 expect(jsonObj.items[0].author.name).to.equal('john')
187 }
188
189 for (const server of servers) {
190 {
191 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'root_channel@localhost:' + servers[0].port })
192 const jsonObj = JSON.parse(json.text)
193 expect(jsonObj.items.length).to.be.equal(1)
194 expect(jsonObj.items[0].title).to.equal('my super name for server 1')
195 }
196
197 {
198 const json = await getJSONfeed(server.url, 'videos', { videoChannelName: 'john_channel@localhost:' + servers[0].port })
199 const jsonObj = JSON.parse(json.text)
200 expect(jsonObj.items.length).to.be.equal(1)
201 expect(jsonObj.items[0].title).to.equal('user video')
202 }
203 }
204 })
205 })
206
207 describe('Video comments feed', function () {
208
209 it('Should contain valid comments (covers JSON feed 1.0 endpoint) and not from unlisted videos', async function () {
210 for (const server of servers) {
211 const json = await getJSONfeed(server.url, 'video-comments')
212
213 const jsonObj = JSON.parse(json.text)
214 expect(jsonObj.items.length).to.be.equal(2)
215 expect(jsonObj.items[0].html_content).to.equal('super comment 2')
216 expect(jsonObj.items[1].html_content).to.equal('super comment 1')
217 }
218 })
219 })
220
221 after(async function () {
222 await cleanupTests(servers)
223 })
224 })