Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Improve and add tests, exclude un-testable code
Browse files Browse the repository at this point in the history
  • Loading branch information
Crecket committed Jun 6, 2019
1 parent d0a8ad0 commit dc03be4
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 20 deletions.
12 changes: 10 additions & 2 deletions jest.config.js
Expand Up @@ -2,13 +2,21 @@ module.exports = {
// limit jest to the following patterns/folders
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
testPathIgnorePatterns: ["/node_modules/", "/dist/", ".html", "/tests/custom-db/*", "/package.json"],
testPathIgnorePatterns: [
"/node_modules/",
"/dist/",
".html",
"/tests/custom-db/*",
"/tests/TestHelpers/*",
"/tests/TestData/*",
"/package.json"
],

// coverage options
collectCoverage: true,
coverageReporters: process.env.DEV_MODE === "true" ? ["json", "lcov"] : ["json", "lcov", "text"],
coverageDirectory: "coverage",
coveragePathIgnorePatterns: ["src/Helpers/FileReaderHelper.ts"],
coveragePathIgnorePatterns: ["src/Helpers/FileReaderHelper.ts", "src/Stores/LocalstorageStore.ts"],

testEnvironment: "node",

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -19,8 +19,8 @@
"build:dev": "tsc -w --newline lf",
"test": "npm run test:prettier && npm run test:jest",
"posttest": "rm -r tests/custom-db/*",
"test:dev": "cross-env ENV_CI=true DEV_MODE=true jest --watch --config jest.config.js",
"test:jest": "cross-env ENV_CI=true jest --verbose --forceExit --config jest.config.js",
"test:dev": "cross-env ENV_CI=true DEV_MODE=true jest --watch --coverage --config jest.config.js",
"test:jest": "cross-env ENV_CI=true jest --verbose --forceExit --coverage --config jest.config.js",
"test:prettier": "prettier-check \"+(tests|src|examples)/**/**.+(js|ts)\" --tab-width 4 --print-width 120",
"prettier": "./node_modules/.bin/prettier --tab-width 4 --print-width 120 --write \"+(tests|src|examples)/**/**.+(js|ts)\"",
"preversion": "rm -rf dist/* && npm run build"
Expand Down
2 changes: 2 additions & 0 deletions src/Api/AttachementContent.ts
Expand Up @@ -38,6 +38,7 @@ export default class AttachementContent implements ApiEndpointInterface {

// return data as base64
if (options.base64 === true) {
/* istanbul ignore else - can't be tested for browser */
if (response instanceof Buffer) {
// buffers are simply encoded as base64
return response.toString("base64");
Expand All @@ -46,6 +47,7 @@ export default class AttachementContent implements ApiEndpointInterface {
return arrayBufferToBase64(response);
}

/* istanbul ignore next line - can't be tested for browser */
throw new Error("No valid Buffer given and FileReader not available");
}

Expand Down
1 change: 1 addition & 0 deletions src/Api/CustomerStatementExportContent.ts
Expand Up @@ -41,6 +41,7 @@ export default class CustomerStatementExportContent implements ApiEndpointInterf
);

// for browsers, wrap in Blob element
/* istanbul ignore next line */
if (typeof Blob === "function") {
return new Blob([response]);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Api/NoteAttachment.ts
Expand Up @@ -22,11 +22,7 @@ export default class NoteAttachment implements ApiEndpointInterface {
* @param {false | number} secondaryEventId
* @returns {string}
*/
private createEndpoint(
eventType: NoteEventType,
eventId: false | number = false,
secondaryEventId: false | number = false
): string {
public createEndpoint(eventType: NoteEventType, eventId: number, secondaryEventId: false | number = false): string {
switch (eventType) {
case "schedule":
return `schedule/${eventId}/schedule-instance/${secondaryEventId}`;
Expand Down
6 changes: 1 addition & 5 deletions src/Api/NoteText.ts
Expand Up @@ -22,11 +22,7 @@ export default class NoteText implements ApiEndpointInterface {
* @param {false | number} secondaryEventId
* @returns {string}
*/
private createEndpoint(
eventType: NoteEventType,
eventId: false | number = false,
secondaryEventId: false | number = false
): string {
public createEndpoint(eventType: NoteEventType, eventId: number, secondaryEventId: false | number = false): string {
switch (eventType) {
case "schedule":
return `schedule/${eventId}/schedule-instance/${secondaryEventId}`;
Expand Down
1 change: 1 addition & 0 deletions src/Crypto/Pbkdf2.ts
Expand Up @@ -17,6 +17,7 @@ export const derivePasswordKey = async (
const derivedKey = await new Promise((resolve, reject) => {
// derive a 32-byte key from the password
forge.pkcs5.pbkdf2(password, salt, iterations, 16, (errorMessage, derivedKey) => {
/* istanbul ignore if - can't manually trigger an error with this lib */
if (errorMessage) {
reject(errorMessage);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/HTTP/EncryptRequestHandler.ts
Expand Up @@ -42,7 +42,8 @@ export default class EncryptRequestHandler {
request.setData(Buffer.from(encryptedBody, "binary"));

// disable request transform
request.setOptions("transformRequest", (data: any, headers: any) => {
/* istanbul ignore next line - can't be tested since Moxios bypasses it */
request.setOptions("transformRequest", (data: any) => {
return data;
});

Expand Down
4 changes: 3 additions & 1 deletion src/HTTP/SignRequestHandler.ts
Expand Up @@ -34,6 +34,7 @@ export default class SignRequestHandler {
}

// manually include the user agent
/* istanbul ignore else - can't be tested for browser */
if (typeof navigator === "undefined") {
const nodeUserAgent = `Node-${process.version}-bunqJSClient`;
request.setHeader("User-Agent", nodeUserAgent);
Expand All @@ -43,7 +44,7 @@ export default class SignRequestHandler {

if (options.isEncrypted || options.includesFile) {
// overwrite transformRequest
request.setOption("transformRequest", (data: any, headers: any) => {
request.setOption("transformRequest", (data: any) => {
return data;
});
}
Expand Down Expand Up @@ -93,6 +94,7 @@ ${data}`;
// sign the template with our private key
const signature = await signString(template, this.Session.privateKey, dataEncoding);

/* istanbul ignore next line - can't be tested for react native */
if (typeof navigator !== "undefined" && navigator.product !== "ReactNative") {
// remove the user agent again if we're in a browser env where we aren't allowed to
request.removeHeader("User-Agent");
Expand Down
11 changes: 10 additions & 1 deletion tests/src/Api/Card.test.ts
Expand Up @@ -75,11 +75,20 @@ describe("API", () => {
monetary_account_id: 1234
}
],
null
2
);
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});
it("#UPDATE - no options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.card.update(1, 2);
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});
});
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Api/CardDebit.test.ts
Expand Up @@ -35,5 +35,15 @@ describe("API", () => {

expect(response).not.toBeNull();
});

it("#POST - default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.cardDebit.post(1, "cardname", "description");
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});
});
});
10 changes: 10 additions & 0 deletions tests/src/Api/MonetaryAccount.test.ts
Expand Up @@ -43,5 +43,15 @@ describe("API", () => {

expect(response).not.toBeNull();
});

it("#LIST - with default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.monetaryAccount.list(1, {});
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});
});
});
27 changes: 27 additions & 0 deletions tests/src/Api/NoteAttachment.test.ts
Expand Up @@ -10,6 +10,13 @@ describe("API", () => {
afterEach(() => moxios.uninstall());

describe("NoteAttachment", () => {
it("#createEndpoint()", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const endpointUrl = bunqApp.api.noteAttachment.createEndpoint("payment", 2);
expect(endpointUrl).toBe("payment/2");
});

it("#GET", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down Expand Up @@ -84,6 +91,16 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#POST - no description", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.noteAttachment.post("payment", 1, 2, 3, 4);
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#PUT", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand All @@ -104,6 +121,16 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#PUT - no description", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.noteAttachment.put("payment", 1, 2, 3, 4, 5);
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#DELETE", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down
7 changes: 7 additions & 0 deletions tests/src/Api/NoteText.test.ts
Expand Up @@ -10,6 +10,13 @@ describe("API", () => {
afterEach(() => moxios.uninstall());

describe("NoteText", () => {
it("#createEndpoint()", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const endpointUrl = bunqApp.api.noteText.createEndpoint("payment", 2);
expect(endpointUrl).toBe("payment/2");
});

it("#GET", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down
10 changes: 10 additions & 0 deletions tests/src/Api/Payment.test.ts
Expand Up @@ -44,6 +44,16 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#LIST - with default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.payment.list(1, 2, {});
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#POST", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down
10 changes: 10 additions & 0 deletions tests/src/Api/Schedule.test.ts
Expand Up @@ -33,5 +33,15 @@ describe("API", () => {

expect(response).not.toBeNull();
});

it("#LIST - default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.schedule.list(1, 2, {});
await sessionRegistration(moxios);
const response = await request;

expect(response).not.toBeNull();
});
});
});
10 changes: 10 additions & 0 deletions tests/src/Api/SchedulePayment.test.ts
Expand Up @@ -44,6 +44,16 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#LIST - with default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.schedulePayment.list(1, 2, {});
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#POST", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down
39 changes: 39 additions & 0 deletions tests/src/Api/ShareInviteBankInquiry.test.ts
Expand Up @@ -54,6 +54,16 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#LIST - with default options", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.shareInviteBankInquiry.list(1, 2, {});
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#POST", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down Expand Up @@ -200,6 +210,35 @@ describe("API", () => {
expect(response).not.toBeNull();
});

it("#PUT - no default share_type", async () => {
const bunqApp: BunqJSClient = await SetupApp();

const request = bunqApp.api.shareInviteBankInquiry.put(
1,
2,
3,
{
type: "EMAIL",
value: "mail@mail.com"
},
{
ShareDetailPayment: {
make_payments: true,
make_draft_payments: true,
view_balance: true,
view_old_events: true,
view_new_events: true
}
},
"PENDING",
{}
);
await defaultResponse(moxios);
const response = await request;

expect(response).not.toBeNull();
});

it("#PUT - putStatus", async () => {
const bunqApp: BunqJSClient = await SetupApp();

Expand Down
28 changes: 25 additions & 3 deletions tests/src/Crypto/Aes.test.ts
@@ -1,7 +1,8 @@
import { validateKey } from "../../../src/Crypto/Aes";
import { validateKey, encryptString, decryptString } from "../../../src/Crypto/Aes";
import Prepare from "../../TestHelpers/Prepare";

const INPUT_STRING = "c9f23cf4aadd693992965686278125ffe9e49acaf3e3a84d9f531212fc93f5f9";
const INPUT_KEY = "c9f23cf4aadd693992965686278125ffe9e49acaf3e3a84d9f531212fc93f5f9";
const INPUT_KEY2 = "d9f23cf4aadd693992965686278125ffe9e49acaf3e3a84d9f531212fc92f4f8";
const INVALID_INPUT_STRING = "Super secure but invalid aes key :(";

describe("Aes", () => {
Expand All @@ -12,7 +13,7 @@ describe("Aes", () => {

describe("#validateKey()", () => {
it("should return true", async () => {
const hash = await validateKey(INPUT_STRING);
const hash = await validateKey(INPUT_KEY);

expect(hash).toBeTruthy();
});
Expand All @@ -23,4 +24,25 @@ describe("Aes", () => {
expect(hash).toBeFalsy();
});
});

describe("#decryptString()", () => {
it("should decrypt the value", async () => {
// encrypt a string
const { iv, key, encryptedString } = await encryptString("INPUT_STRING", INPUT_KEY);

// attempt to decrypt it again
const decryptedString = await decryptString(encryptedString, key, iv);

expect(decryptedString).toBe("INPUT_STRING");
});

it("should have no result on invalid key the value", async () => {
// encrypt a string
const { iv, encryptedString } = await encryptString("INPUT_STRING", INPUT_KEY);

return decryptString(encryptedString, INPUT_KEY2, iv)
.then(() => expect(true).toBe("SHOULD FAIL"))
.catch(() => expect(true).toBeTruthy());
});
});
});

0 comments on commit dc03be4

Please sign in to comment.