Skip to content

Commit 5ae39e0

Browse files
committed
More functional tests.
1 parent 9bf3047 commit 5ae39e0

File tree

4 files changed

+129
-4
lines changed

4 files changed

+129
-4
lines changed

src/editor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ function setupEvents() {
368368
// HMAC secret, when changed the encoded token must be updated.
369369
eventManager.addDomEvent(secretInput, 'input', encodeToken);
370370
// Base64 checkbox, when changes the encoded token must be updated.
371-
eventManager.addDomEvent(secretBase64Checkbox, 'input', encodeToken);
371+
eventManager.addDomEvent(secretBase64Checkbox, 'change', encodeToken);
372372
// Private key, when changed the encoded token must be updated.
373373
eventManager.addDomEvent(privateKeyTextArea, 'input', encodeToken);
374374
// Public key, when changed the encoded token must NOT be updated

src/editor/instances.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ const tokenEditorOptions = Object.assign({}, jsonEditorOptions, {
2828
export const headerEditor = new CodeMirror(headerElement, jsonEditorOptions);
2929
export const payloadEditor = new CodeMirror(payloadElement, jsonEditorOptions);
3030
export const tokenEditor = new CodeMirror(editorElement, tokenEditorOptions);
31+
32+
// Expose instances as globals for functional tests.
33+
if(!window.test) {
34+
window.test = {};
35+
}
36+
window.test.headerEditor = headerEditor;
37+
window.test.payloadEditor = payloadEditor;
38+
window.test.tokenEditor = tokenEditor;

test/functional/editor.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const chai = require('chai');
2+
const chaiAsPromised = require('chai-as-promised');
3+
const chaiArrays = require('chai-arrays');
4+
5+
const utils = require('./utils.js');
6+
7+
const isVisible = utils.isVisible;
8+
9+
chai.use(chaiAsPromised);
10+
chai.use(chaiArrays);
11+
const expect = chai.expect;
12+
13+
describe('Editor', function() {
14+
before(utils.launchBrowser);
15+
16+
after(utils.closeBrowser);
17+
18+
it('Displays editor when clicking on navbar', async function() {
19+
await this.page.click('a[href="#debugger-io"]');
20+
// Wait for scroll
21+
await this.page.waitFor(3000);
22+
expect(await this.page.$eval('#debugger-io', isVisible)).to.be.true;
23+
});
24+
25+
it('HS256 should be selected by default', async function() {
26+
const selected = await this.page.$eval('#algorithm-select', select => {
27+
return select.options[select.selectedIndex].value;
28+
});
29+
30+
expect(selected).to.equal('HS256');
31+
});
32+
33+
it('Should display a tooltip with a human readable ' +
34+
'date on claim hover', async function() {
35+
await this.page.mouse.move(0, 0);
36+
37+
expect(await this.page.$eval('#js-payload-tooltip', isVisible)).to.be.false;
38+
39+
const iatPos = await this.page.evaluate(() => {
40+
return window.test.payloadEditor.charCoords({
41+
line: 4,
42+
pos: 3
43+
}, 'window');
44+
});
45+
46+
await this.page.mouse.move(iatPos.left, iatPos.top);
47+
48+
expect(await this.page.$eval('#js-payload-tooltip', isVisible)).to.be.true;
49+
});
50+
51+
it('Displays a valid token by default', async function() {
52+
const valid = await this.page.$eval('.validation-status', status => {
53+
return status.classList.contains('valid-token') &&
54+
status.textContent.indexOf('verified') !== -1;
55+
});
56+
57+
expect(valid).to.be.true;
58+
});
59+
60+
it('Shows invalid token when a valid token is edited ' +
61+
'in the left pane', async function() {
62+
await this.page.evaluate(() => {
63+
let token = window.test.tokenEditor.getValue();
64+
token += 'asdf23';
65+
window.test.tokenEditor.setValue(token);
66+
});
67+
68+
const invalid = await this.page.$eval('.validation-status', status => {
69+
return status.classList.contains('invalid-token') &&
70+
status.textContent.indexOf('invalid') !== -1;
71+
});
72+
73+
expect(invalid).to.be.true;
74+
});
75+
76+
it('Updates the token when the secret changes', async function() {
77+
const oldToken = await this.page.evaluate(() => {
78+
return window.test.tokenEditor.getValue()
79+
});
80+
81+
const secretInput = await this.page.$('input[name="secret"]');
82+
83+
await secretInput.type('asdfasdf');
84+
85+
const newToken = await this.page.evaluate(() => {
86+
return window.test.tokenEditor.getValue()
87+
});
88+
89+
expect(oldToken).to.not.equal(newToken);
90+
});
91+
92+
it('Updates the token when the Base64 checkbox changes', async function() {
93+
const oldToken = await this.page.evaluate(() => {
94+
return window.test.tokenEditor.getValue()
95+
});
96+
97+
await this.page.click('#is-base64-encoded');
98+
99+
let newToken = await this.page.evaluate(() => {
100+
return window.test.tokenEditor.getValue()
101+
});
102+
103+
expect(oldToken).to.not.equal(newToken);
104+
105+
await this.page.click('#is-base64-encoded');
106+
107+
newToken = await this.page.evaluate(() => {
108+
return window.test.tokenEditor.getValue()
109+
});
110+
111+
expect(oldToken).to.equal(newToken);
112+
});
113+
});

test/functional/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const puppeteer = require('puppeteer');
2+
const os = require('os');
23

34
// https://stackoverflow.com/questions/5353934/check-if-element-is-visible-on-screen
45
function isVisible(elm) {
@@ -18,11 +19,14 @@ async function launchBrowser() {
1819
// Initial navigation may take long due to outside requests.
1920
this.timeout(30000);
2021

21-
this.browser = await puppeteer.launch({
22-
//args: ['--no-sandbox'],
22+
const options = os.hostname() === 'i7-4790K-LIN' ? {
2323
headless: false,
2424
executablePath: '/usr/bin/chromium'
25-
});
25+
} : {
26+
args: ['--no-sandbox']
27+
};
28+
29+
this.browser = await puppeteer.launch(options);
2630
this.page = await this.browser.newPage();
2731
await this.page.setViewport({
2832
width: 1920,

0 commit comments

Comments
 (0)