Skip to content

Commit

Permalink
chore: move issue tests to independent files (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Jan 24, 2020
1 parent 3e27f3a commit 695435f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 113 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ aliases:

- &test_package
name: test
command: npm run test
command: |
npm run test
npm run test:extras -- --type=issue
- &lint_package
name: lint
Expand Down
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ but it is mostly for emergencies. All automations are wired to the GitHub projec

When a change lands on GitHub, it will be checked by the continuous integration system.

### Issue fixes

All issue fixes should generally be accompanied by corresponding tests on the issues
to prevent future regressions.

Examples of those can be found in `tests/extras/issue-*.test.js`.

## Getting Started

```bash
Expand Down Expand Up @@ -57,6 +64,7 @@ By contributing to `otplib`, you agree that your contributions will be licensed

- is the code tested?
- `npm run test`
- `npm run test:extras`
- is the code linted?
- `npm run lint`
- is the code formatted?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build:site": "./scripts/build-site.sh",
"build": "./scripts/build.sh",
"clean": "rimraf coverage packages/**/builds",
"format": "prettier --write \"{packages,scripts,configs}/**/*.{ts,tsx,js,jsx,json,md,mdx}\"",
"format": "prettier --write \"{packages,scripts,configs,tests}/**/*.{ts,tsx,js,jsx,json,md,mdx}\"",
"lint": "tsc --noEmit",
"release:beta": " npx lerna publish from-package --contents builds --dist-tag next",
"release:stable": "npx lerna publish from-package --contents builds --dist-tag latest",
Expand Down
53 changes: 53 additions & 0 deletions tests/extras/issue-136.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Authenticator as AuthenticatorDefault } from '@otplib/core';
import { createDigest, createRandomBytes } from '@otplib/plugin-crypto';
import { keyDecoder, keyEncoder } from '@otplib/plugin-thirty-two';

const secret = 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD';
const code = '123456';

function runTestIssue136(name, Authenticator) {
const authenticator = new Authenticator();

test(`#136 - [${name}] option is reset if set via options setter`, async () => {
expect.assertions(1);

const instance = authenticator.create();

instance.options = {
createDigest,
createRandomBytes,
keyEncoder,
keyDecoder
};

try {
await instance.check(code, secret);
instance.resetOptions();
await instance.check(code, secret);
} catch (err) {
expect(err).not.toBeUndefined();
}
});

test(`#136 - [${name}] allow setting of persistent options via constructor / create`, async () => {
expect.assertions(0);

// @ts-ignore
const instance = authenticator.create({
createDigest,
createRandomBytes,
keyEncoder,
keyDecoder
});

try {
await instance.check(code, secret);
instance.resetOptions();
await instance.check(code, secret);
} catch (err) {
expect(err).toBeUndefined();
}
});
}

runTestIssue136('Authenticator', AuthenticatorDefault);
22 changes: 8 additions & 14 deletions tests/extras/issue-201.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ import { createRandomBytes } from '@otplib/plugin-crypto';
import * as thirtyTwo from '@otplib/plugin-thirty-two';
import * as base32EncDec from '@otplib/plugin-base32-enc-dec';

[
{
name: 'thirty-two',
mod: thirtyTwo
},
{
name: 'base32-enc-dec',
mod: base32EncDec
}
].forEach(({ name, mod }) => {
function runTestIssue201(name, base32) {
const options = {
createRandomBytes,
encoding: KeyEncodings.HEX,
keyEncoder: mod.keyEncoder,
keyDecoder: mod.keyDecoder
keyEncoder: base32.keyEncoder,
keyDecoder: base32.keyDecoder
};

test(`[${name}] generated secret should have consistent length`, async () => {
test(`#201 - [${name}] generated secret should have consistent length`, async () => {
for (let i = 0; i < 20; i++) {
expect(authenticatorGenerateSecret(10, options)).toHaveLength(16);
}
});
});
}

runTestIssue201('thirty-two', thirtyTwo);
runTestIssue201('base32-enc-dec', base32EncDec);
29 changes: 29 additions & 0 deletions tests/extras/issue-7.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { authenticator as authenticatorDefault } from '@otplib/preset-default';
import { authenticator as authenticatorAsync } from '@otplib/preset-default-async';
import { authenticator as authenticatorBrowser } from '@otplib/preset-browser';

function runTestIssue7(name, authenticator) {
test(`[${name}] sample 1`, async () => {
authenticator.resetOptions();
const secret = 'xbja vgc6 gv4i i4qq h5ct 6stz ytcp ksiz'.replace(/ /g, '');

authenticator.options = { epoch: 1507953809 * 1000 };
const result = await authenticator.generate(secret);

expect(result).toBe('849140');
});

test(`[${name}] sample 2`, async () => {
authenticator.resetOptions();
const secret = 'SVT52XEZE2TWC2MU';

authenticator.options = { epoch: 1507908269 * 1000 };
const result = await authenticator.generate(secret);

expect(result).toBe('334156');
});
}

runTestIssue7('preset-default', authenticatorDefault);
runTestIssue7('preset-default-async', authenticatorAsync);
runTestIssue7('preset-browser', authenticatorBrowser);
95 changes: 0 additions & 95 deletions tests/suite/issues.ts

This file was deleted.

2 changes: 0 additions & 2 deletions tests/suite/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
rfcTestSuiteTOTP,
dataTestSuiteAuthenticator
} from '@tests/suite/rfcs';
import { testSuiteIssues } from '@tests/suite/issues';

interface Presets {
hotp: HOTP | HOTPAsync;
Expand All @@ -17,5 +16,4 @@ export function testSuitePreset(name: string, pkg: Presets): void {
rfcTestSuiteHOTP(name, pkg.hotp);
rfcTestSuiteTOTP(name, pkg.totp);
dataTestSuiteAuthenticator(name, pkg.authenticator);
testSuiteIssues(name, pkg.authenticator);
}

0 comments on commit 695435f

Please sign in to comment.