Skip to content

Commit

Permalink
chore: add andapply prettier to files
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Mar 3, 2018
1 parent 86cb74e commit 70bfc33
Show file tree
Hide file tree
Showing 78 changed files with 498 additions and 467 deletions.
15 changes: 5 additions & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn"
},
"plugins": [
"prettier"
],
"extends": [
"eslint:recommended"
"eslint:recommended",
"plugin:prettier/recommended"
]
}
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"coveralls": "cat ./coverage/lcov.info | coveralls",
"deploy-gh-pages": "./scripts/build-site.sh",
"deploy-package": "./scripts/build-publish.sh",
"lint": "eslint --ext js src",
"lint": "eslint \"packages/**/**\"",
"lint:format": "prettier --write \"{packages,scripts}/**/**.js\"",
"test": "jest --coverage",
"test:watch": "jest --coverage --watch"
},
Expand Down Expand Up @@ -44,7 +45,6 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.1",
"babel-loader": "^6.4.1",
"babel-plugin-module-resolver": "^2.7.1",
"babel-plugin-transform-class-properties": "^6.23.0",
Expand All @@ -53,9 +53,12 @@
"coveralls": "^2.13.0",
"create-hmac": "^1.1.4",
"eslint": "^3.19.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"jest": "^20.0.4",
"jsdoc": "^3.4.3",
"minami": "^1.1.1",
"prettier": "1.11.1",
"rimraf": "^2.6.1",
"rollup": "^0.49.2",
"rollup-plugin-cleanup": "^1.0.1",
Expand Down
9 changes: 4 additions & 5 deletions packages/otplib-authenticator/Authenticator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import totp from 'otplib-totp';
import {secretKey} from 'otplib-utils';
import { secretKey } from 'otplib-utils';
import check from './check';
import decodeKey from './decodeKey';
import encodeKey from './encodeKey';
Expand Down Expand Up @@ -35,7 +35,6 @@ const TOTP = totp.TOTP;
* @since 3.0.0
*/
class Authenticator extends TOTP {

constructor() {
super();
}
Expand All @@ -49,8 +48,8 @@ class Authenticator extends TOTP {
return {
encoding: 'hex',
epoch: null,
step: 30,
}
step: 30
};
}

/**
Expand Down Expand Up @@ -122,5 +121,5 @@ Authenticator.prototype.utils = {
encodeKey,
keyuri,
token
}
};
export default Authenticator;
94 changes: 42 additions & 52 deletions packages/otplib-authenticator/Authenticator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ jest.mock('./encodeKey', () => jest.fn());
jest.mock('./keyuri', () => jest.fn());
jest.mock('./token', () => jest.fn());

describe('Authenticator', function () {
describe('Authenticator', () => {
let lib;
const testValue = 'test';

beforeEach(() => {
lib = new Authenticator();
});

it('exposes the class as a prototype', function () {
it('exposes the class as a prototype', () => {
expect(lib.Authenticator).toEqual(Authenticator);
});

it('exposes authenticator functions as utils', function () {
it('exposes authenticator functions as utils', () => {
expect(Object.keys(lib.utils)).toEqual([
'check',
'decodeKey',
Expand All @@ -34,40 +34,34 @@ describe('Authenticator', function () {
]);
});

it('should have expected default options', function () {
it('should have expected default options', () => {
const options = lib.options;
expect(options).toEqual({
encoding: 'hex',
epoch: null,
step: 30,
step: 30
});
});

it('method: encode => encodeKey', function () {
methodExpectation('encode', encodeKey, [
'123'
]);
it('method: encode => encodeKey', () => {
methodExpectation('encode', encodeKey, ['123']);
});

it('method: decode => decodeKey', function () {
methodExpectation('decode', decodeKey, [
'123'
]);
it('method: decode => decodeKey', () => {
methodExpectation('decode', decodeKey, ['123']);
});

it('method: keyuri => keyuri', function () {
methodExpectation('keyuri', keyuri, [
'123'
]);
it('method: keyuri => keyuri', () => {
methodExpectation('keyuri', keyuri, ['123']);
});

it('method: generateSecret returns empty string on falsy len params', function () {
it('method: generateSecret returns empty string on falsy len params', () => {
expect(lib.generateSecret(0)).toBe('');
});

it('method: generateSecret should return an encoded secret', function () {
it('method: generateSecret should return an encoded secret', () => {
const mocks = mockGenerateSecret();
lib.options = { epoch: 1519995424045 }
lib.options = { epoch: 1519995424045 };
const result = lib.generateSecret(10);

expect(mocks.secretKey).toHaveBeenCalledTimes(1);
Expand All @@ -79,52 +73,42 @@ describe('Authenticator', function () {
expect(result).toBe(testValue);
});

it('method: generateSecret should return empty string on null parms', function () {
it('method: generateSecret should return empty string on null parms', () => {
const mocks = mockGenerateSecret();
const result = lib.generateSecret(null);
expect(result).toBe('');
expect(mocks.secretKey).toHaveBeenCalledTimes(0);
});

it('method: generateSecret should use default params on undefined params', function () {
it('method: generateSecret should use default params on undefined params', () => {
const mocks = mockGenerateSecret();
const result = lib.generateSecret();
expect(mocks.secretKey).toHaveBeenCalledTimes(1);
expect(encodeKey).toHaveBeenCalledTimes(1);
expect(result).toBe(testValue);
});

it('method: generate => token', function () {
methodExpectationWithOptions('generate', token, [
'secret'
]);
it('method: generate => token', () => {
methodExpectationWithOptions('generate', token, ['secret']);
});

it('method: generate => token (fallback to secret in options)', function () {
lib.options = { secret: 'option-secret' }
methodExpectationWithOptions('generate', token, [
null
], [
'option-secret'
]);
it('method: generate => token (fallback to secret in options)', () => {
lib.options = { secret: 'option-secret' };
methodExpectationWithOptions('generate', token, [null], ['option-secret']);
});

it('method: check => check', function () {
methodExpectationWithOptions('check', check, [
'token',
'secret'
]);
it('method: check => check', () => {
methodExpectationWithOptions('check', check, ['token', 'secret']);
});

it('method: check => check (fallback to secret in options)', function () {
lib.options = { secret: 'option-secret' }
methodExpectationWithOptions('check', check, [
'token',
null
], [
'token',
'option-secret'
]);
it('method: check => check (fallback to secret in options)', () => {
lib.options = { secret: 'option-secret' };
methodExpectationWithOptions(
'check',
check,
['token', null],
['token', 'option-secret']
);
});

function methodExpectation(methodName, mockFn, args) {
Expand All @@ -137,28 +121,34 @@ describe('Authenticator', function () {
expect(mockFn).toHaveBeenCalledWith(...args);
}

function methodExpectationWithOptions(methodName, mockFn, args, modifiedArgs) {
function methodExpectationWithOptions(
methodName,
mockFn,
args,
modifiedArgs
) {
mockFn.mockImplementation(() => testValue);
lib.options = { epoch: 1519995424045 }
lib.options = { epoch: 1519995424045 };

const result = lib[methodName](...args);
const calledArgs = modifiedArgs || args;

expect(result).toBe(testValue);
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(...calledArgs, lib.optionsAll)
expect(mockFn).toHaveBeenCalledWith(...calledArgs, lib.optionsAll);
}

function mockGenerateSecret() {
const secret = '1234567890';
const secretKey = jest.spyOn(utils, 'secretKey')
const secretKey = jest
.spyOn(utils, 'secretKey')
.mockImplementation(() => secret);

encodeKey.mockImplementation(() => testValue);

return {
secret,
secretKey
}
};
}
});
2 changes: 1 addition & 1 deletion packages/otplib-authenticator/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {totpCheck} from 'otplib-core';
import { totpCheck } from 'otplib-core';
import decodeKey from './decodeKey';

/**
Expand Down
11 changes: 6 additions & 5 deletions packages/otplib-authenticator/check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import decodeKey from './decodeKey';

jest.mock('./decodeKey', () => jest.fn());

describe('check', function () {
it('should call and return value from totpToken', function () {
describe('check', () => {
it('should call and return value from totpToken', () => {
const token = '123456';
const secret = 'GEZDGNBVGY3TQOJQGEZDG';
const options = { test: 'test' }
const options = { test: 'test' };

const spy = jest.spyOn(core, 'totpCheck')
const spy = jest
.spyOn(core, 'totpCheck')
.mockImplementation(() => jest.fn());

decodeKey.mockImplementation(() => 'decode');

check(token, secret, options)
check(token, secret, options);

expect(decodeKey).toHaveBeenCalledTimes(1);
expect(decodeKey).toHaveBeenCalledWith('GEZDGNBVGY3TQOJQGEZDG');
Expand Down
3 changes: 1 addition & 2 deletions packages/otplib-authenticator/decodeKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import base32 from 'thirty-two';
* @return {string} A hex decoded string.
*/
function decodeKey(encodedKey) {
return base32.decode(encodedKey)
.toString('hex');
return base32.decode(encodedKey).toString('hex');
}

export default decodeKey;
4 changes: 2 additions & 2 deletions packages/otplib-authenticator/decodeKey.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import decodeKey from './decodeKey';

describe('decodeKey', function () {
it('should return expected decoded key', function () {
describe('decodeKey', () => {
it('should return expected decoded key', () => {
const result = decodeKey('GEZDGNBVGY3TQOJQGEZDG');
expect(result).toBe('31323334353637383930313233');
});
Expand Down
3 changes: 2 additions & 1 deletion packages/otplib-authenticator/encodeKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import base32 from 'thirty-two';
* @return {string} Base32 string
*/
function encodeKey(secret) {
return base32.encode(secret)
return base32
.encode(secret)
.toString()
.replace(/=/g, '');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/otplib-authenticator/encodeKey.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import encodeKey from './encodeKey';

describe('encodeKey', function () {
it('should return expected encoded key', function () {
describe('encodeKey', () => {
it('should return expected encoded key', () => {
const result = encodeKey('1234567890');
expect(result).toBe('GEZDGNBVGY3TQOJQ');
});

it('should not have equal sign in the result', function () {
it('should not have equal sign in the result', () => {
const result = encodeKey('1234567890123');
const expected = 'GEZDGNBVGY3TQOJQGEZDG===';

Expand Down
6 changes: 3 additions & 3 deletions packages/otplib-authenticator/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import authenticator from './index';
import Authenticator from './Authenticator';

describe('index', function () {
it('should expose authenticator class', function () {
describe('index', () => {
it('should expose authenticator class', () => {
expect(authenticator.Authenticator).toEqual(Authenticator);
});

it('should expose an instance of Authenticator', function () {
it('should expose an instance of Authenticator', () => {
expect(authenticator).toBeInstanceOf(Authenticator);
});
});
3 changes: 2 additions & 1 deletion packages/otplib-authenticator/keyuri.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const data = '{service}:{user}?secret={secret}&issuer={service}';
*/
function keyuri(user = 'user', service = 'service', secret = '') {
const protocol = 'otpauth://totp/';
const value = data.replace('{user}', user)
const value = data
.replace('{user}', user)
.replace('{secret}', secret)
.replace(/{service}/g, service);

Expand Down
Loading

0 comments on commit 70bfc33

Please sign in to comment.