Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Apr 8, 2017
1 parent 452ff9e commit 5827189
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/core/hotpCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import hotpToken from './hotpToken';
*/
function hotpCheck(token, secret, counter = 0, options = {}) {
const systemToken = hotpToken(secret, counter, options);
if (systemToken.length < 1) {
return false;
}
return isSameToken(token, systemToken);
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/hotpToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import hotpOptions from './hotpOptions';
* @param {string} secret - your secret that is used to generate the token
* @param {number} counter - the OTP counter (usually it's an incremental count)
* @param {object} options - allowed options as specified in hotpOptions()
* @return {number} OTP Code
* @return {string} OTP Code
*/
function hotpToken(secret, counter, options = {}) {
if (counter == null) {
return '';
}

const opt = hotpOptions(options);

// Convert secret to encoding for hmacSecret
Expand Down
3 changes: 3 additions & 0 deletions src/core/totpCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import totpToken from './totpToken';
*/
function totpCheck(token, secret, options = {}){
const systemToken = totpToken(secret, options);
if (systemToken.length < 1) {
return false;
}
return isSameToken(token, systemToken);
}

Expand Down
8 changes: 6 additions & 2 deletions src/core/totpToken.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import hotpToken from './hotpToken';
import totpOptions from './totpOptions';

function floor(value) {
return Math.floor(value);
}

/**
* Generates the OTP code
*
* @module core/totpToken
* @param {string} secret - your secret that is used to generate the token
* @param {object} options - allowed options as specified in totpOptions()
* @return {number} OTP Code
* @return {string} OTP Code
*/
function totpToken(secret, options = {}) {
const opt = totpOptions(options);
const timeCounter = Math.floor(opt.epoch / (opt.step * 1000.0));
const timeCounter = floor(opt.epoch / (opt.step * 1000.0));
return hotpToken(secret, timeCounter, opt.digits);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/specs/core/hotpCheck.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {expect} from 'chai';
import hotpCheck from 'src/core/hotpCheck';

describe('core/hotpCheck', function () {
it('should return true', function () {
[
['i6im0gc96j0mn00c', 3, '229021'],
['i6im0gc96j0mn00c', 47412420, '196182'],
['65jh84eo38k32edm', 47412423, '963234'],
['f4515l6ob3gkganp', 47412433, '415572'],
['2o9989k76ij7eh9c', 47412435, '343659']
].forEach(([secret, counter, expected]) => {
expect(hotpCheck(expected, secret, counter)).to.be.true;
});
});

it('shoudl default counter to 0 when undefined', function () {
expect(hotpCheck('847856', 'i6im0gc96j0mn00c')).to.be.true;
});

it('should return false when invalid counter', function () {
expect(hotpCheck('229021', 'i6im0gc96j0mn00c', null)).to.be.false;
});
});
6 changes: 6 additions & 0 deletions tests/specs/core/hotpToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import hotpToken from 'src/core/hotpToken';

describe('core/hotpToken', function () {

it('should return empty string when counter is null or undefined', function () {
expect(hotpToken('i6im0gc96j0mn00c')).to.equal('');
expect(hotpToken('i6im0gc96j0mn00c', void 0)).to.equal('');
expect(hotpToken('i6im0gc96j0mn00c', null)).to.equal('');
});

it('should return correct tokens', function () {
[
['i6im0gc96j0mn00c', 3, '229021'],
Expand Down
51 changes: 51 additions & 0 deletions tests/specs/core/totpToken.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {expect} from 'chai';
import {stub} from 'sinon';
import totpToken from 'src/core/totpToken';

describe('core/totpToken', function () {
it('should return floored number', function () {
const floor = totpToken.__get__('floor');
expect(floor(1.9)).to.equal(1);
});

it('should return correct tokens', function () {
const stubbed = rewire();
const token = totpToken('i6im0gc96j0mn00c', {
epoch: 60000
});

reset();

expect(stubbed.floor.calledWith(2)).to.be.true;
expect(token).to.equal('229021');
});

it('should return result even if options is null', function () {
rewire();
const token = totpToken('i6im0gc96j0mn00c', null);
reset();

expect(token).to.equal('229021');
});

it('should return result even if options is void 0', function () {
rewire();
const token = totpToken('i6im0gc96j0mn00c', void 0);
reset();

expect(token).to.equal('229021');
});

function rewire() {
const floor = stub().returns(3);
totpToken.__Rewire__('floor', floor);

return {
floor
}
}

function reset() {
totpToken.__ResetDependency__('floor');
}
});

0 comments on commit 5827189

Please sign in to comment.