Merge from upstream
[oweals/peertube.git] / server / tests / client.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import * as request from 'supertest'
6 import {
7   flushTests,
8   getCustomConfig,
9   getVideosList,
10   killallServers,
11   makeHTMLRequest,
12   runServer,
13   ServerInfo,
14   serverLogin,
15   updateCustomConfig,
16   updateCustomSubConfig,
17   uploadVideo
18 } from '../../shared/utils'
19
20 const expect = chai.expect
21
22 function checkIndexTags (html: string, title: string, description: string, css: string) {
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
28 describe('Test a client controllers', function () {
29   let server: ServerInfo
30
31   before(async function () {
32     this.timeout(120000)
33
34     await flushTests()
35
36     server = await runServer(1)
37     server.accessToken = await serverLogin(server)
38
39     const videoAttributes = {
40       name: 'my super name for server 1',
41       description: 'my super description for server 1'
42     }
43     await uploadVideo(server.url, server.accessToken, videoAttributes)
44
45     const res = await getVideosList(server.url)
46     const videos = res.body.data
47
48     expect(videos.length).to.equal(1)
49
50     server.video = videos[0]
51   })
52
53   it('Should have valid Open Graph tags on the watch page with video id', async function () {
54     const res = await request(server.url)
55       .get('/videos/watch/' + server.video.id)
56       .set('Accept', 'text/html')
57       .expect(200)
58
59     expect(res.text).to.contain('<meta property="og:title" content="my super name for server 1" />')
60     expect(res.text).to.contain('<meta property="og:description" content="my super description for server 1" />')
61   })
62
63   it('Should have valid Open Graph tags on the watch page with video uuid', async function () {
64     const res = await request(server.url)
65       .get('/videos/watch/' + server.video.uuid)
66       .set('Accept', 'text/html')
67       .expect(200)
68
69     expect(res.text).to.contain('<meta property="og:title" content="my super name for server 1" />')
70     expect(res.text).to.contain('<meta property="og:description" content="my super description for server 1" />')
71   })
72
73   it('Should have valid oEmbed discovery tags', async function () {
74     const path = '/videos/watch/' + server.video.uuid
75     const res = await request(server.url)
76       .get(path)
77       .set('Accept', 'text/html')
78       .expect(200)
79
80     const expectedLink = '<link rel="alternate" type="application/json+oembed" href="http://localhost:9001/services/oembed?' +
81       `url=http%3A%2F%2Flocalhost%3A9001%2Fvideos%2Fwatch%2F${server.video.uuid}" ` +
82       `title="${server.video.name}" />`
83
84     expect(res.text).to.contain(expectedLink)
85   })
86
87   it('Should have valid twitter card', async function () {
88     const res = await request(server.url)
89       .get('/videos/watch/' + server.video.uuid)
90       .set('Accept', 'text/html')
91       .expect(200)
92
93     expect(res.text).to.contain('<meta property="twitter:card" content="summary_large_image" />')
94     expect(res.text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
95   })
96
97   it('Should have valid twitter card if Twitter is whitelisted', async function () {
98     const res1 = await getCustomConfig(server.url, server.accessToken)
99     const config = res1.body
100     config.services.twitter = {
101       username: '@Kuja',
102       whitelisted: true
103     }
104     await updateCustomConfig(server.url, server.accessToken, config)
105
106     const res = await request(server.url)
107       .get('/videos/watch/' + server.video.uuid)
108       .set('Accept', 'text/html')
109       .expect(200)
110
111     expect(res.text).to.contain('<meta property="twitter:card" content="player" />')
112     expect(res.text).to.contain('<meta property="twitter:site" content="@Kuja" />')
113   })
114
115   it('Should have valid index html tags (title, description...)', async function () {
116     const res = await makeHTMLRequest(server.url, '/videos/trending')
117
118     const description = 'PeerTube, a federated (ActivityPub) video streaming platform using P2P (BitTorrent) directly in the web browser ' +
119       'with WebTorrent and Angular.'
120     checkIndexTags(res.text, 'PeerTube', description, '')
121   })
122
123   it('Should update the customized configuration and have the correct index html tags', async function () {
124     await updateCustomSubConfig(server.url, server.accessToken, {
125       instance: {
126         name: 'PeerTube updated',
127         shortDescription: 'my short description',
128         description: 'my super description',
129         terms: 'my super terms',
130         defaultClientRoute: '/videos/recently-added',
131         defaultNSFWPolicy: 'blur',
132         customizations: {
133           javascript: 'alert("coucou")',
134           css: 'body { background-color: red; }'
135         }
136       }
137     })
138
139     const res = await makeHTMLRequest(server.url, '/videos/trending')
140
141     checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }')
142   })
143
144   it('Should have valid index html updated tags (title, description...)', async function () {
145     const res = await makeHTMLRequest(server.url, '/videos/trending')
146
147     checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }')
148   })
149
150   after(async function () {
151     killallServers([ server ])
152   })
153 })