]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/client.ts
Fix server run
[github/Chocobozzz/PeerTube.git] / server / tests / client.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e1dc3e7
C
2
3import 'mocha'
4import * as chai from 'chai'
012580d9 5import { omit } from 'lodash'
012580d9 6import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
65e6e260 7import { Account, HTMLServerConfig, ServerConfig, VideoPlaylistCreateResult, VideoPlaylistPrivacy } from '@shared/models'
0e1dc3e7 8import {
7c3b7976 9 cleanupTests,
a59f210f 10 doubleFollow,
254d3579 11 createMultipleServers,
012580d9 12 makeGetRequest,
590fb506 13 makeHTMLRequest,
254d3579 14 PeerTubeServer,
a59f210f 15 setAccessTokensToServers,
39433fa5 16 setDefaultVideoChannel,
a59f210f 17 waitJobs
94565d52 18} from '../../shared/extra-utils'
590fb506
C
19
20const expect = chai.expect
5bcfd029 21
aea0b0e7 22function checkIndexTags (html: string, title: string, description: string, css: string, config: ServerConfig) {
5bcfd029
C
23 expect(html).to.contain('<title>' + title + '</title>')
24 expect(html).to.contain('<meta name="description" content="' + description + '" />')
25 expect(html).to.contain('<style class="custom-css-style">' + css + '</style>')
aea0b0e7
C
26
27 const htmlConfig: HTMLServerConfig = omit(config, 'signup')
28 expect(html).to.contain(`<script type="application/javascript">window.PeerTubeServerConfig = '${JSON.stringify(htmlConfig)}'</script>`)
5bcfd029 29}
0e1dc3e7
C
30
31describe('Test a client controllers', function () {
254d3579 32 let servers: PeerTubeServer[] = []
39433fa5
C
33 let account: Account
34
35 const videoName = 'my super name for server 1'
a073c912
RK
36 const videoDescription = 'my<br> super __description__ for *server* 1<p></p>'
37 const videoDescriptionPlainText = 'my super description for server 1'
39433fa5
C
38
39 const playlistName = 'super playlist name'
40 const playlistDescription = 'super playlist description'
d4a8e7a6 41 let playlist: VideoPlaylistCreateResult
39433fa5
C
42
43 const channelDescription = 'my super channel description'
0e1dc3e7 44
a1eda903
C
45 const watchVideoBasePaths = [ '/videos/watch/', '/w/' ]
46 const watchPlaylistBasePaths = [ '/videos/watch/playlist/', '/w/p/' ]
47
d4a8e7a6
C
48 let videoIds: (string | number)[] = []
49 let playlistIds: (string | number)[] = []
50
0e1dc3e7
C
51 before(async function () {
52 this.timeout(120000)
53
254d3579 54 servers = await createMultipleServers(2)
e5024f51
TC
55
56 await setAccessTokensToServers(servers)
57
e5024f51
TC
58 await doubleFollow(servers[0], servers[1])
59
a59f210f 60 await setDefaultVideoChannel(servers)
0e1dc3e7 61
89d241a7
C
62 await servers[0].channels.update({
63 channelName: servers[0].store.channel.name,
a5461888
C
64 attributes: { description: channelDescription }
65 })
8d987ec6 66
39433fa5 67 // Video
8d987ec6 68
d23dd9fb
C
69 {
70 const attributes = { name: videoName, description: videoDescription }
89d241a7 71 await servers[0].videos.upload({ attributes })
0e1dc3e7 72
89d241a7 73 const { data } = await servers[0].videos.list()
d23dd9fb 74 expect(data.length).to.equal(1)
0e1dc3e7 75
d23dd9fb 76 const video = data[0]
89d241a7 77 servers[0].store.video = video
d23dd9fb
C
78 videoIds = [ video.id, video.uuid, video.shortUUID ]
79 }
8d987ec6
K
80
81 // Playlist
82
d23dd9fb
C
83 {
84 const attributes = {
85 displayName: playlistName,
86 description: playlistDescription,
87 privacy: VideoPlaylistPrivacy.PUBLIC,
89d241a7 88 videoChannelId: servers[0].store.channel.id
d23dd9fb 89 }
8d987ec6 90
89d241a7 91 playlist = await servers[0].playlists.create({ attributes })
d23dd9fb 92 playlistIds = [ playlist.id, playlist.shortUUID, playlist.uuid ]
8d987ec6 93
89d241a7 94 await servers[0].playlists.addElement({ playlistId: playlist.shortUUID, attributes: { videoId: servers[0].store.video.id } })
d23dd9fb 95 }
8d987ec6
K
96
97 // Account
98
d23dd9fb 99 {
89d241a7 100 await servers[0].users.updateMe({ description: 'my account description' })
8d987ec6 101
89d241a7 102 account = await servers[0].accounts.get({ accountName: `${servers[0].store.user.username}@${servers[0].host}` })
d23dd9fb 103 }
a59f210f
C
104
105 await waitJobs(servers)
0e1dc3e7
C
106 })
107
6fad8e51 108 describe('oEmbed', function () {
a1eda903 109
6fad8e51 110 it('Should have valid oEmbed discovery tags for videos', async function () {
a1eda903 111 for (const basePath of watchVideoBasePaths) {
d4a8e7a6
C
112 for (const id of videoIds) {
113 const res = await makeGetRequest({
114 url: servers[0].url,
115 path: basePath + id,
116 accept: 'text/html',
117 statusCodeExpected: HttpStatusCode.OK_200
118 })
0e1dc3e7 119
d4a8e7a6 120 const port = servers[0].port
0e1dc3e7 121
d4a8e7a6 122 const expectedLink = '<link rel="alternate" type="application/json+oembed" href="http://localhost:' + port + '/services/oembed?' +
89d241a7
C
123 `url=http%3A%2F%2Flocalhost%3A${port}%2Fw%2F${servers[0].store.video.uuid}" ` +
124 `title="${servers[0].store.video.name}" />`
8d987ec6 125
d4a8e7a6
C
126 expect(res.text).to.contain(expectedLink)
127 }
a1eda903 128 }
6fad8e51 129 })
8d987ec6 130
6fad8e51 131 it('Should have valid oEmbed discovery tags for a playlist', async function () {
a1eda903 132 for (const basePath of watchPlaylistBasePaths) {
d4a8e7a6
C
133 for (const id of playlistIds) {
134 const res = await makeGetRequest({
135 url: servers[0].url,
136 path: basePath + id,
137 accept: 'text/html',
138 statusCodeExpected: HttpStatusCode.OK_200
139 })
6fad8e51 140
d4a8e7a6 141 const port = servers[0].port
8d987ec6 142
d4a8e7a6
C
143 const expectedLink = '<link rel="alternate" type="application/json+oembed" href="http://localhost:' + port + '/services/oembed?' +
144 `url=http%3A%2F%2Flocalhost%3A${port}%2Fw%2Fp%2F${playlist.uuid}" ` +
145 `title="${playlistName}" />`
8d987ec6 146
d4a8e7a6
C
147 expect(res.text).to.contain(expectedLink)
148 }
a1eda903 149 }
6fad8e51 150 })
8d987ec6
K
151 })
152
6fad8e51 153 describe('Open Graph', function () {
8d987ec6 154
012580d9
C
155 async function accountPageTest (path: string) {
156 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
157 const text = res.text
9a911038 158
012580d9
C
159 expect(text).to.contain(`<meta property="og:title" content="${account.displayName}" />`)
160 expect(text).to.contain(`<meta property="og:description" content="${account.description}" />`)
161 expect(text).to.contain('<meta property="og:type" content="website" />')
89d241a7 162 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/accounts/${servers[0].store.user.username}" />`)
012580d9 163 }
9a911038 164
012580d9
C
165 async function channelPageTest (path: string) {
166 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
167 const text = res.text
0e1dc3e7 168
89d241a7 169 expect(text).to.contain(`<meta property="og:title" content="${servers[0].store.channel.displayName}" />`)
012580d9
C
170 expect(text).to.contain(`<meta property="og:description" content="${channelDescription}" />`)
171 expect(text).to.contain('<meta property="og:type" content="website" />')
89d241a7 172 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/video-channels/${servers[0].store.channel.name}" />`)
012580d9
C
173 }
174
a1eda903
C
175 async function watchVideoPageTest (path: string) {
176 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
177 const text = res.text
178
179 expect(text).to.contain(`<meta property="og:title" content="${videoName}" />`)
180 expect(text).to.contain(`<meta property="og:description" content="${videoDescriptionPlainText}" />`)
181 expect(text).to.contain('<meta property="og:type" content="video" />')
89d241a7 182 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/w/${servers[0].store.video.uuid}" />`)
a1eda903
C
183 }
184
185 async function watchPlaylistPageTest (path: string) {
186 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
187 const text = res.text
188
189 expect(text).to.contain(`<meta property="og:title" content="${playlistName}" />`)
190 expect(text).to.contain(`<meta property="og:description" content="${playlistDescription}" />`)
191 expect(text).to.contain('<meta property="og:type" content="video" />')
d4a8e7a6 192 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/w/p/${playlist.uuid}" />`)
a1eda903
C
193 }
194
012580d9 195 it('Should have valid Open Graph tags on the account page', async function () {
89d241a7
C
196 await accountPageTest('/accounts/' + servers[0].store.user.username)
197 await accountPageTest('/a/' + servers[0].store.user.username)
198 await accountPageTest('/@' + servers[0].store.user.username)
6fad8e51 199 })
d8755eed 200
6fad8e51 201 it('Should have valid Open Graph tags on the channel page', async function () {
89d241a7
C
202 await channelPageTest('/video-channels/' + servers[0].store.channel.name)
203 await channelPageTest('/c/' + servers[0].store.channel.name)
204 await channelPageTest('/@' + servers[0].store.channel.name)
6fad8e51 205 })
d8755eed 206
a1eda903 207 it('Should have valid Open Graph tags on the watch page', async function () {
d4a8e7a6
C
208 for (const path of watchVideoBasePaths) {
209 for (const id of videoIds) {
210 await watchVideoPageTest(path + id)
211 }
212 }
6fad8e51 213 })
8d987ec6 214
6fad8e51 215 it('Should have valid Open Graph tags on the watch playlist page', async function () {
d4a8e7a6
C
216 for (const path of watchPlaylistBasePaths) {
217 for (const id of playlistIds) {
218 await watchPlaylistPageTest(path + id)
219 }
220 }
6fad8e51 221 })
8d987ec6
K
222 })
223
6fad8e51 224 describe('Twitter card', async function () {
8d987ec6 225
012580d9 226 describe('Not whitelisted', function () {
8d987ec6 227
012580d9
C
228 async function accountPageTest (path: string) {
229 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
230 const text = res.text
8d987ec6 231
012580d9
C
232 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
233 expect(text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
234 expect(text).to.contain(`<meta property="twitter:title" content="${account.name}" />`)
235 expect(text).to.contain(`<meta property="twitter:description" content="${account.description}" />`)
236 }
8be1afa1 237
012580d9
C
238 async function channelPageTest (path: string) {
239 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
240 const text = res.text
8be1afa1 241
012580d9
C
242 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
243 expect(text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
89d241a7 244 expect(text).to.contain(`<meta property="twitter:title" content="${servers[0].store.channel.displayName}" />`)
012580d9 245 expect(text).to.contain(`<meta property="twitter:description" content="${channelDescription}" />`)
9a911038
K
246 }
247
a1eda903
C
248 async function watchVideoPageTest (path: string) {
249 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
250 const text = res.text
8be1afa1 251
a1eda903
C
252 expect(text).to.contain('<meta property="twitter:card" content="summary_large_image" />')
253 expect(text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
254 expect(text).to.contain(`<meta property="twitter:title" content="${videoName}" />`)
255 expect(text).to.contain(`<meta property="twitter:description" content="${videoDescriptionPlainText}" />`)
256 }
257
258 async function watchPlaylistPageTest (path: string) {
259 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
260 const text = res.text
261
262 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
263 expect(text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
264 expect(text).to.contain(`<meta property="twitter:title" content="${playlistName}" />`)
265 expect(text).to.contain(`<meta property="twitter:description" content="${playlistDescription}" />`)
266 }
267
268 it('Should have valid twitter card on the watch video page', async function () {
d4a8e7a6
C
269 for (const path of watchVideoBasePaths) {
270 for (const id of videoIds) {
271 await watchVideoPageTest(path + id)
272 }
273 }
012580d9 274 })
9a911038 275
012580d9 276 it('Should have valid twitter card on the watch playlist page', async function () {
d4a8e7a6
C
277 for (const path of watchPlaylistBasePaths) {
278 for (const id of playlistIds) {
279 await watchPlaylistPageTest(path + id)
280 }
281 }
012580d9
C
282 })
283
284 it('Should have valid twitter card on the account page', async function () {
285 await accountPageTest('/accounts/' + account.name)
286 await accountPageTest('/a/' + account.name)
287 await accountPageTest('/@' + account.name)
288 })
289
290 it('Should have valid twitter card on the channel page', async function () {
89d241a7
C
291 await channelPageTest('/video-channels/' + servers[0].store.channel.name)
292 await channelPageTest('/c/' + servers[0].store.channel.name)
293 await channelPageTest('/@' + servers[0].store.channel.name)
012580d9 294 })
6fad8e51 295 })
8d987ec6 296
012580d9 297 describe('Whitelisted', function () {
8d987ec6 298
012580d9 299 before(async function () {
89d241a7 300 const config = await servers[0].config.getCustomConfig()
012580d9
C
301 config.services.twitter = {
302 username: '@Kuja',
303 whitelisted: true
304 }
8d987ec6 305
89d241a7 306 await servers[0].config.updateCustomConfig({ newCustomConfig: config })
012580d9 307 })
8d987ec6 308
012580d9
C
309 async function accountPageTest (path: string) {
310 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
311 const text = res.text
8be1afa1 312
012580d9
C
313 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
314 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
9a911038
K
315 }
316
012580d9
C
317 async function channelPageTest (path: string) {
318 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
319 const text = res.text
9a911038 320
012580d9
C
321 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
322 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
323 }
9a911038 324
a1eda903
C
325 async function watchVideoPageTest (path: string) {
326 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
327 const text = res.text
6fad8e51 328
a1eda903
C
329 expect(text).to.contain('<meta property="twitter:card" content="player" />')
330 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
331 }
332
333 async function watchPlaylistPageTest (path: string) {
334 const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 })
335 const text = res.text
336
337 expect(text).to.contain('<meta property="twitter:card" content="player" />')
338 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
339 }
340
341 it('Should have valid twitter card on the watch video page', async function () {
d4a8e7a6
C
342 for (const path of watchVideoBasePaths) {
343 for (const id of videoIds) {
344 await watchVideoPageTest(path + id)
345 }
346 }
012580d9 347 })
9a911038 348
012580d9 349 it('Should have valid twitter card on the watch playlist page', async function () {
d4a8e7a6
C
350 for (const path of watchPlaylistBasePaths) {
351 for (const id of playlistIds) {
352 await watchPlaylistPageTest(path + id)
353 }
354 }
012580d9
C
355 })
356
357 it('Should have valid twitter card on the account page', async function () {
358 await accountPageTest('/accounts/' + account.name)
359 await accountPageTest('/a/' + account.name)
360 await accountPageTest('/@' + account.name)
361 })
362
363 it('Should have valid twitter card on the channel page', async function () {
89d241a7
C
364 await channelPageTest('/video-channels/' + servers[0].store.channel.name)
365 await channelPageTest('/c/' + servers[0].store.channel.name)
366 await channelPageTest('/@' + servers[0].store.channel.name)
012580d9 367 })
6fad8e51 368 })
5bcfd029
C
369 })
370
6fad8e51
C
371 describe('Index HTML', function () {
372
373 it('Should have valid index html tags (title, description...)', async function () {
89d241a7 374 const config = await servers[0].config.getConfig()
a59f210f 375 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
6fad8e51
C
376
377 const description = 'PeerTube, an ActivityPub-federated video streaming platform using P2P directly in your web browser.'
65e6e260 378 checkIndexTags(res.text, 'PeerTube', description, '', config)
590fb506 379 })
5bcfd029 380
6fad8e51 381 it('Should update the customized configuration and have the correct index html tags', async function () {
89d241a7 382 await servers[0].config.updateCustomSubConfig({
65e6e260
C
383 newConfig: {
384 instance: {
385 name: 'PeerTube updated',
386 shortDescription: 'my short description',
387 description: 'my super description',
388 terms: 'my super terms',
389 defaultNSFWPolicy: 'blur',
390 defaultClientRoute: '/videos/recently-added',
391 customizations: {
392 javascript: 'alert("coucou")',
393 css: 'body { background-color: red; }'
394 }
6fad8e51
C
395 }
396 }
397 })
5bcfd029 398
89d241a7 399 const config = await servers[0].config.getConfig()
a59f210f 400 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
6fad8e51 401
65e6e260 402 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
6fad8e51 403 })
5bcfd029 404
6fad8e51 405 it('Should have valid index html updated tags (title, description...)', async function () {
89d241a7 406 const config = await servers[0].config.getConfig()
a59f210f 407 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
5bcfd029 408
65e6e260 409 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
6fad8e51 410 })
e5024f51
TC
411
412 it('Should use the original video URL for the canonical tag', async function () {
a1eda903 413 for (const basePath of watchVideoBasePaths) {
d4a8e7a6
C
414 for (const id of videoIds) {
415 const res = await makeHTMLRequest(servers[1].url, basePath + id)
89d241a7 416 expect(res.text).to.contain(`<link rel="canonical" href="${servers[0].url}/videos/watch/${servers[0].store.video.uuid}" />`)
d4a8e7a6 417 }
a1eda903 418 }
a59f210f
C
419 })
420
421 it('Should use the original account URL for the canonical tag', async function () {
65e6e260 422 const accountURLtest = res => {
9a911038
K
423 expect(res.text).to.contain(`<link rel="canonical" href="${servers[0].url}/accounts/root" />`)
424 }
425
426 accountURLtest(await makeHTMLRequest(servers[1].url, '/accounts/root@' + servers[0].host))
427 accountURLtest(await makeHTMLRequest(servers[1].url, '/a/root@' + servers[0].host))
428 accountURLtest(await makeHTMLRequest(servers[1].url, '/@root@' + servers[0].host))
a59f210f
C
429 })
430
431 it('Should use the original channel URL for the canonical tag', async function () {
65e6e260 432 const channelURLtests = res => {
9a911038
K
433 expect(res.text).to.contain(`<link rel="canonical" href="${servers[0].url}/video-channels/root_channel" />`)
434 }
435
436 channelURLtests(await makeHTMLRequest(servers[1].url, '/video-channels/root_channel@' + servers[0].host))
437 channelURLtests(await makeHTMLRequest(servers[1].url, '/c/root_channel@' + servers[0].host))
438 channelURLtests(await makeHTMLRequest(servers[1].url, '/@root_channel@' + servers[0].host))
a59f210f
C
439 })
440
441 it('Should use the original playlist URL for the canonical tag', async function () {
a1eda903 442 for (const basePath of watchPlaylistBasePaths) {
d4a8e7a6
C
443 for (const id of playlistIds) {
444 const res = await makeHTMLRequest(servers[1].url, basePath + id)
445 expect(res.text).to.contain(`<link rel="canonical" href="${servers[0].url}/video-playlists/${playlist.uuid}" />`)
446 }
a1eda903 447 }
e5024f51 448 })
5bcfd029
C
449 })
450
aea0b0e7
C
451 describe('Embed HTML', function () {
452
453 it('Should have the correct embed html tags', async function () {
89d241a7
C
454 const config = await servers[0].config.getConfig()
455 const res = await makeHTMLRequest(servers[0].url, servers[0].store.video.embedPath)
aea0b0e7 456
65e6e260 457 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
aea0b0e7
C
458 })
459 })
460
7c3b7976 461 after(async function () {
a59f210f 462 await cleanupTests(servers)
0e1dc3e7
C
463 })
464})