]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/client.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tests / client.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 { omit } from 'lodash'
6 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
7 import { Account, HTMLServerConfig, ServerConfig, VideoPlaylistCreateResult, VideoPlaylistPrivacy } from '@shared/models'
8 import {
9 cleanupTests,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 makeGetRequest,
13 makeHTMLRequest,
14 ServerInfo,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
17 waitJobs
18 } from '../../shared/extra-utils'
19
20 const expect = chai.expect
21
22 function checkIndexTags (html: string, title: string, description: string, css: string, config: ServerConfig) {
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>')
26
27 const htmlConfig: HTMLServerConfig = omit(config, 'signup')
28 expect(html).to.contain(`<script type="application/javascript">window.PeerTubeServerConfig = '${JSON.stringify(htmlConfig)}'</script>`)
29 }
30
31 describe('Test a client controllers', function () {
32 let servers: ServerInfo[] = []
33 let account: Account
34
35 const videoName = 'my super name for server 1'
36 const videoDescription = 'my<br> super __description__ for *server* 1<p></p>'
37 const videoDescriptionPlainText = 'my super description for server 1'
38
39 const playlistName = 'super playlist name'
40 const playlistDescription = 'super playlist description'
41 let playlist: VideoPlaylistCreateResult
42
43 const channelDescription = 'my super channel description'
44
45 const watchVideoBasePaths = [ '/videos/watch/', '/w/' ]
46 const watchPlaylistBasePaths = [ '/videos/watch/playlist/', '/w/p/' ]
47
48 let videoIds: (string | number)[] = []
49 let playlistIds: (string | number)[] = []
50
51 before(async function () {
52 this.timeout(120000)
53
54 servers = await flushAndRunMultipleServers(2)
55
56 await setAccessTokensToServers(servers)
57
58 await doubleFollow(servers[0], servers[1])
59
60 await setDefaultVideoChannel(servers)
61
62 await servers[0].channels.update({
63 channelName: servers[0].store.channel.name,
64 attributes: { description: channelDescription }
65 })
66
67 // Video
68
69 {
70 const attributes = { name: videoName, description: videoDescription }
71 await servers[0].videos.upload({ attributes })
72
73 const { data } = await servers[0].videos.list()
74 expect(data.length).to.equal(1)
75
76 const video = data[0]
77 servers[0].store.video = video
78 videoIds = [ video.id, video.uuid, video.shortUUID ]
79 }
80
81 // Playlist
82
83 {
84 const attributes = {
85 displayName: playlistName,
86 description: playlistDescription,
87 privacy: VideoPlaylistPrivacy.PUBLIC,
88 videoChannelId: servers[0].store.channel.id
89 }
90
91 playlist = await servers[0].playlists.create({ attributes })
92 playlistIds = [ playlist.id, playlist.shortUUID, playlist.uuid ]
93
94 await servers[0].playlists.addElement({ playlistId: playlist.shortUUID, attributes: { videoId: servers[0].store.video.id } })
95 }
96
97 // Account
98
99 {
100 await servers[0].users.updateMe({ description: 'my account description' })
101
102 account = await servers[0].accounts.get({ accountName: `${servers[0].store.user.username}@${servers[0].host}` })
103 }
104
105 await waitJobs(servers)
106 })
107
108 describe('oEmbed', function () {
109
110 it('Should have valid oEmbed discovery tags for videos', async function () {
111 for (const basePath of watchVideoBasePaths) {
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 })
119
120 const port = servers[0].port
121
122 const expectedLink = '<link rel="alternate" type="application/json+oembed" href="http://localhost:' + port + '/services/oembed?' +
123 `url=http%3A%2F%2Flocalhost%3A${port}%2Fw%2F${servers[0].store.video.uuid}" ` +
124 `title="${servers[0].store.video.name}" />`
125
126 expect(res.text).to.contain(expectedLink)
127 }
128 }
129 })
130
131 it('Should have valid oEmbed discovery tags for a playlist', async function () {
132 for (const basePath of watchPlaylistBasePaths) {
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 })
140
141 const port = servers[0].port
142
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}" />`
146
147 expect(res.text).to.contain(expectedLink)
148 }
149 }
150 })
151 })
152
153 describe('Open Graph', function () {
154
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
158
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" />')
162 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/accounts/${servers[0].store.user.username}" />`)
163 }
164
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
168
169 expect(text).to.contain(`<meta property="og:title" content="${servers[0].store.channel.displayName}" />`)
170 expect(text).to.contain(`<meta property="og:description" content="${channelDescription}" />`)
171 expect(text).to.contain('<meta property="og:type" content="website" />')
172 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/video-channels/${servers[0].store.channel.name}" />`)
173 }
174
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" />')
182 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/w/${servers[0].store.video.uuid}" />`)
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" />')
192 expect(text).to.contain(`<meta property="og:url" content="${servers[0].url}/w/p/${playlist.uuid}" />`)
193 }
194
195 it('Should have valid Open Graph tags on the account page', async function () {
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)
199 })
200
201 it('Should have valid Open Graph tags on the channel page', async function () {
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)
205 })
206
207 it('Should have valid Open Graph tags on the watch page', async function () {
208 for (const path of watchVideoBasePaths) {
209 for (const id of videoIds) {
210 await watchVideoPageTest(path + id)
211 }
212 }
213 })
214
215 it('Should have valid Open Graph tags on the watch playlist page', async function () {
216 for (const path of watchPlaylistBasePaths) {
217 for (const id of playlistIds) {
218 await watchPlaylistPageTest(path + id)
219 }
220 }
221 })
222 })
223
224 describe('Twitter card', async function () {
225
226 describe('Not whitelisted', function () {
227
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
231
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 }
237
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
241
242 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
243 expect(text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
244 expect(text).to.contain(`<meta property="twitter:title" content="${servers[0].store.channel.displayName}" />`)
245 expect(text).to.contain(`<meta property="twitter:description" content="${channelDescription}" />`)
246 }
247
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
251
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 () {
269 for (const path of watchVideoBasePaths) {
270 for (const id of videoIds) {
271 await watchVideoPageTest(path + id)
272 }
273 }
274 })
275
276 it('Should have valid twitter card on the watch playlist page', async function () {
277 for (const path of watchPlaylistBasePaths) {
278 for (const id of playlistIds) {
279 await watchPlaylistPageTest(path + id)
280 }
281 }
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 () {
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)
294 })
295 })
296
297 describe('Whitelisted', function () {
298
299 before(async function () {
300 const config = await servers[0].config.getCustomConfig()
301 config.services.twitter = {
302 username: '@Kuja',
303 whitelisted: true
304 }
305
306 await servers[0].config.updateCustomConfig({ newCustomConfig: config })
307 })
308
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
312
313 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
314 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
315 }
316
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
320
321 expect(text).to.contain('<meta property="twitter:card" content="summary" />')
322 expect(text).to.contain('<meta property="twitter:site" content="@Kuja" />')
323 }
324
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
328
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 () {
342 for (const path of watchVideoBasePaths) {
343 for (const id of videoIds) {
344 await watchVideoPageTest(path + id)
345 }
346 }
347 })
348
349 it('Should have valid twitter card on the watch playlist page', async function () {
350 for (const path of watchPlaylistBasePaths) {
351 for (const id of playlistIds) {
352 await watchPlaylistPageTest(path + id)
353 }
354 }
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 () {
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)
367 })
368 })
369 })
370
371 describe('Index HTML', function () {
372
373 it('Should have valid index html tags (title, description...)', async function () {
374 const config = await servers[0].config.getConfig()
375 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
376
377 const description = 'PeerTube, an ActivityPub-federated video streaming platform using P2P directly in your web browser.'
378 checkIndexTags(res.text, 'PeerTube', description, '', config)
379 })
380
381 it('Should update the customized configuration and have the correct index html tags', async function () {
382 await servers[0].config.updateCustomSubConfig({
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 }
395 }
396 }
397 })
398
399 const config = await servers[0].config.getConfig()
400 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
401
402 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
403 })
404
405 it('Should have valid index html updated tags (title, description...)', async function () {
406 const config = await servers[0].config.getConfig()
407 const res = await makeHTMLRequest(servers[0].url, '/videos/trending')
408
409 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
410 })
411
412 it('Should use the original video URL for the canonical tag', async function () {
413 for (const basePath of watchVideoBasePaths) {
414 for (const id of videoIds) {
415 const res = await makeHTMLRequest(servers[1].url, basePath + id)
416 expect(res.text).to.contain(`<link rel="canonical" href="${servers[0].url}/videos/watch/${servers[0].store.video.uuid}" />`)
417 }
418 }
419 })
420
421 it('Should use the original account URL for the canonical tag', async function () {
422 const accountURLtest = res => {
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))
429 })
430
431 it('Should use the original channel URL for the canonical tag', async function () {
432 const channelURLtests = res => {
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))
439 })
440
441 it('Should use the original playlist URL for the canonical tag', async function () {
442 for (const basePath of watchPlaylistBasePaths) {
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 }
447 }
448 })
449 })
450
451 describe('Embed HTML', function () {
452
453 it('Should have the correct embed html tags', async function () {
454 const config = await servers[0].config.getConfig()
455 const res = await makeHTMLRequest(servers[0].url, servers[0].store.video.embedPath)
456
457 checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }', config)
458 })
459 })
460
461 after(async function () {
462 await cleanupTests(servers)
463 })
464 })