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