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