forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-va-portal.js
61 lines (55 loc) · 1.75 KB
/
mock-va-portal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* To test the VA integration, we need to mock the VA portal.
* This is if you don't have access to the VA portal in a staging environment.
* And you can't use the VA portal in production, because that one is
* hardened to only use https://docs.github.com, not your localhost:4000.
*
* So, to test this locally, you need to:
*
* 1. Add `SUPPORT_PORTAL_URL=http://localhost:4000` into your `.env` file
* 2. `npm run dev`
* 3. Navigate to a page whose path is mentioned in the
* `PagePathToVaFlowMapping` object in the `ArticleContext`.
*
* This mocking is not secure, but it's only for local development.
*/
const HTML = `<!doctype html>
<html>
<head>
<script>
const iframeOrigin = '*';
window.onload = (function() {
window.parent.postMessage({ type: 'open' }, iframeOrigin)
})
function triggerStart() {
window.parent.postMessage({ type: 'start' }, iframeOrigin)
}
function triggerStop() {
window.parent.postMessage({ type: 'stop' }, iframeOrigin)
}
</script>
<style>
body, h1 { margin: 0; padding: 10px; }
#chat { margin-top: 20px; padding: 20px; border: 2px solid #efefef; min-height: 500px; }
body { border: 2px dashed orange; }
</style>
</head>
<body>
<h1>Mock Virtual Assistant Portal</h1>
<button type="button" onclick="triggerStart()">START VA</button>
<div id="chat">
<h2>This is the Virtual Assistant Portal</h2>
</div>
<button type="button" onclick="triggerStop()">STOP VA</button>
</body>
</html>`
export default function mockVaPortal(req, res, next) {
if (process.env.NODE_ENV !== 'development') {
throw new Error('Should not have been enabled')
}
if (req.url.startsWith('/iframe/docs_va')) {
res.removeHeader('content-security-policy')
return res.status(200).type('text/html').send(HTML)
}
next()
}