Skip to content

Commit 9664827

Browse files
committed
Some tests (and now logging can be disabled).
1 parent 5ddc70f commit 9664827

File tree

17 files changed

+364
-76
lines changed

17 files changed

+364
-76
lines changed

Gruntfile.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = grunt => {
55
grunt.loadNpmTasks('grunt-webpack');
66
grunt.loadNpmTasks('grunt-contrib-watch');
77
grunt.loadNpmTasks('grunt-contrib-clean');
8+
grunt.loadNpmTasks('grunt-mocha-test');
89

910
grunt.initConfig({
1011
clean: {
@@ -99,6 +100,7 @@ module.exports = grunt => {
99100
websiteDev: require('./webpack.website-dev.js'),
100101
extensionProd: require('./webpack.extension-prod.js'),
101102
extensionDev: require('./webpack.extension-dev.js'),
103+
test: require('./webpack.website-tests.js')
102104
},
103105

104106
watch: {
@@ -150,6 +152,13 @@ module.exports = grunt => {
150152
],
151153
tasks: ['build-extension-views']
152154
}
155+
},
156+
157+
mochaTest: {
158+
all: {
159+
options: {},
160+
src: ['dist/test/*.js']
161+
}
153162
}
154163
});
155164

@@ -197,6 +206,8 @@ module.exports = grunt => {
197206
'build-website-dev',
198207
'build-extension-dev'
199208
]);
209+
210+
grunt.registerTask('test', ['webpack:test', 'mochaTest']);
200211

201212
grunt.registerTask('default', ['build-dev', 'watch']);
202213
};

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"@babel/preset-env": "^7.0.0-beta.36",
1313
"babel-loader": "^8.0.0-beta.0",
1414
"bootstrap": "^3.3.7",
15+
"chai": "^4.1.2",
16+
"chai-as-promised": "^7.1.1",
1517
"codemirror": "^5.33.0",
1618
"flipclock": "^0.7.8",
1719
"grunt": "^1.0.1",
@@ -21,17 +23,22 @@
2123
"grunt-contrib-pug": "^1.0.0",
2224
"grunt-contrib-stylus": "^1.2.0",
2325
"grunt-contrib-watch": "^1.0.0",
26+
"grunt-mocha-test": "^0.13.3",
2427
"grunt-webpack": "^3.0.2",
2528
"highlight.js": "^9.12.0",
2629
"isotope-layout": "^3.0.5",
2730
"jquery": "^3.2.1",
2831
"jsrsasign": "^8.0.4",
2932
"jstransformer-markdown-it": "^2.0.0",
33+
"loglevel": "^1.6.1",
34+
"mocha": "^5.0.0",
3035
"pug": "^2.0.0-rc.4",
36+
"source-map-support": "^0.5.3",
3137
"stylus": "^0.54.5",
3238
"uglifyjs-webpack-plugin": "^1.1.6",
3339
"webpack": "^3.10.0",
34-
"webpack-merge": "^4.1.1"
40+
"webpack-merge": "^4.1.1",
41+
"xhr-mock": "^2.0.3"
3542
},
3643
"scripts": {
3744
"test": "echo UNIMPLEMENTED",

src/editor/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
decodedTabElement
3737
} from '../dom-elements.js';
3838

39+
import * as log from 'loglevel';
40+
3941
// The event manager lets us enable/disable events as needed without
4042
// manually tracking them. Events that need to be disabled should be
4143
// passed to the event manager.
@@ -97,7 +99,7 @@ function selectAlgorithm(algorithm) {
9799
algorithmSelect.querySelector(`option[value="${algorithm}"]`);
98100

99101
if(!selected) {
100-
console.log(`Invalid algorithm ${algorithm}, ignoring...`);
102+
log.info(`Invalid algorithm ${algorithm}, ignoring...`);
101103
return;
102104
}
103105

@@ -148,7 +150,7 @@ function setAlgorithmInHeader(algorithm) {
148150
// errors.
149151
if(!(e instanceof SyntaxError)) {
150152
// If it's not a SyntaxError, log the error.
151-
console.error('Failed to encode token: ', e);
153+
log.warn('Failed to encode token: ', e);
152154
}
153155
}
154156

@@ -254,7 +256,7 @@ function encodeToken() {
254256

255257
saveAsLastToken();
256258
} catch(e) {
257-
console.error('Failed to sign/encode token: ', e);
259+
log.warn('Failed to sign/encode token: ', e);
258260
markAsInvalid();
259261
tokenEditor.setValue('');
260262
}
@@ -291,7 +293,7 @@ function decodeToken() {
291293
verifyToken();
292294
}
293295
} catch(e) {
294-
console.error('Failed to decode token: ', e);
296+
log.warn('Failed to decode token: ', e);
295297
}
296298
});
297299
}

src/editor/jwt.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isValidBase64String } from '../utils.js';
2+
13
import {
24
jws,
35
KEYUTIL,
@@ -6,6 +8,8 @@ import {
68
utf8tohex
79
} from 'jsrsasign';
810

11+
import * as log from 'loglevel';
12+
913
export function sign(header,
1014
payload,
1115
secretOrPrivateKeyString,
@@ -25,6 +29,10 @@ export function sign(header,
2529
}
2630

2731
export function verify(jwt, secretOrPublicKeyString, base64Secret = false) {
32+
if(!isToken(jwt)) {
33+
return false;
34+
}
35+
2836
const decoded = decode(jwt);
2937

3038
if(!decoded.header.alg) {
@@ -41,7 +49,7 @@ export function verify(jwt, secretOrPublicKeyString, base64Secret = false) {
4149
return jws.JWS.verify(jwt, secretOrPublicKeyString);
4250
}
4351
} catch(e) {
44-
console.error('Could not verify token, ' +
52+
log.warn('Could not verify token, ' +
4553
'probably due to bad data in it or the keys: ', e);
4654
return false;
4755
}
@@ -78,7 +86,20 @@ export function decode(jwt) {
7886
return result;
7987
}
8088

81-
export function isToken(jwt) {
89+
export function isToken(jwt, checkTypClaim = false) {
8290
const decoded = decode(jwt);
83-
return !decoded.errors && decoded.header.typ === 'JWT';
91+
92+
if(decoded.errors) {
93+
return false;
94+
}
95+
96+
if(checkTypClaim && decoded.header.typ !== 'JWT') {
97+
return false;
98+
}
99+
100+
const split = jwt.split('.');
101+
let valid = true;
102+
split.forEach(s => valid = valid && isValidBase64String(s, true));
103+
104+
return valid;
84105
}

src/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { KEYUTIL } from 'jsrsasign';
2+
import * as log from 'loglevel';
23

34
export function httpGet(url, cache = true) {
45
return new Promise((resolve, reject) => {
@@ -154,7 +155,7 @@ export function copyTextToClipboard(text) {
154155
try {
155156
document.execCommand('copy');
156157
} catch (err) {
157-
console.error(err);
158+
log.warn(err);
158159
}
159160

160161
document.body.removeChild(textArea);
@@ -197,7 +198,7 @@ export function safeLocalStorageSetItem(key, value) {
197198
try {
198199
localStorage.setItem(key, value);
199200
} catch (e) {
200-
console.log('Cannot save token to Local Storage ' +
201+
log.info('Cannot save token to Local Storage ' +
201202
'(private browsing enabled?), ignoring...', e);
202203
// Safari when in private browsing doesn't allow it
203204
}

src/website/counter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { httpGet } from '../utils.js';
55
import $ from 'jquery';
66
import jQuery from 'jquery';
77
import 'flipclock/compiled/flipclock.js';
8+
import * as log from 'loglevel';
89

910
const initialCount = 80482701;
1011
const pollIntervalWhenVisible = 5000;
@@ -24,7 +25,7 @@ function updateCounterFromWebtask() {
2425
const parsed = JSON.parse(data);
2526
flipCounter.setTime(parsed.logins);
2627
}).catch(e => {
27-
console.error('Failed to set count from Webtask: ', e);
28+
log.warn('Failed to set count from Webtask: ', e);
2829
});
2930
});
3031
}

src/website/libraries.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './dom-elements.js';
88

99
import Isotope from 'isotope-layout';
10+
import * as log from 'loglevel';
1011

1112
const librariesGrid = new Isotope(librariesElement, {
1213
layoutMode: 'fitRows',
@@ -58,7 +59,7 @@ function getStarsForGitHubRepos() {
5859
}
5960
} catch (e) {
6061
// Ignore bad data
61-
console.error('Bad data in stored stars count, ignoring...', e);
62+
log.warn('Bad data in stored stars count, ignoring...', e);
6263
}
6364

6465
const url = `https://api.github.com/repos/${repo}`;
@@ -70,7 +71,7 @@ function getStarsForGitHubRepos() {
7071

7172
insertStarCount(element, count);
7273
}).catch(e => {
73-
console.error('Failed to get GitHub stars count for repository, ' +
74+
log.warn('Failed to get GitHub stars count for repository, ' +
7475
'is the repository URL OK? ', e);
7576
});
7677
});

src/website/smooth-scrolling.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { menuScrollableLinks, navbarElement } from './dom-elements.js';
22
import { isWideScreen } from '../utils.js';
33

44
import $ from 'jquery';
5+
import * as log from 'loglevel';
56

67
// This is the only function that really requires jQuery, other than some
78
// of the dependencies. Consider this when adding code that depends on
@@ -30,7 +31,7 @@ export function setupSmoothScrolling() {
3031

3132
const start = scrollable.href.indexOf('#');
3233
if (start === -1) {
33-
console.error('<a> element with .scrollto set and bad link: ',
34+
log.warn('<a> element with .scrollto set and bad link: ',
3435
scrollable.href);
3536
return;
3637
}

test/functional/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'source-map-support/register';

test/test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)