Skip to content

Commit

Permalink
Merge branch 'web-test-runner'
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Jan 31, 2023
2 parents 2145bb1 + e67e3b0 commit 2b67bdb
Show file tree
Hide file tree
Showing 8 changed files with 4,556 additions and 5,356 deletions.
9,642 changes: 4,426 additions & 5,216 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
}
},
"devDependencies": {
"@types/jest": "^29.2.1",
"@open-wc/testing": "^3.1.7",
"@types/node": "^18.11.9",
"barely-a-dev-server": "^0.3.6",
"esbuild": "^0.15.13",
"jest": "^29.2.2",
"jest-environment-jsdom": "^29.2.2",
"rome": "^10.0.1",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
},
"scripts": {
Expand All @@ -47,7 +44,7 @@
"build-pages": "node script/build-pages.js",
"dev": "node script/dev.js",
"clean": "rm -rf ./.temp ./dist",
"test": "npx jest && npm run lint",
"test": "npx web-test-runner && npm run lint",
"lint": "npx rome check src",
"format": "npx rome format --write src",
"prepack": "npm run clean && npm run build"
Expand Down Expand Up @@ -76,5 +73,9 @@
"/browser-ponyfill/**/*",
"/extended/**/*",
"/src/webauthn-json/**/*"
]
],
"dependencies": {
"@web/dev-server-esbuild": "^0.3.3",
"@web/test-runner": "^0.15.0"
}
}
40 changes: 0 additions & 40 deletions src/webauthn-json/arraybuffer.jest.ts

This file was deleted.

38 changes: 20 additions & 18 deletions src/webauthn-json/base64url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
/**
* @jest-environment jsdom
*/
import { expect } from "@open-wc/testing";

import { base64urlToBuffer, bufferToBase64url } from "./base64url";
import "./arraybuffer.jest";

describe("base64url", () => {
suite("base64url", () => {
test("should convert simple base64url values to `ArrayBuffer`", () => {
expect(base64urlToBuffer("")).toEqualBuffer(new Uint8Array([]));
expect(base64urlToBuffer("AA")).toEqualBuffer(new Uint8Array([0]));
expect(base64urlToBuffer("TEST")).toEqualBuffer(
new Uint8Array([76, 68, 147]),
// TODO: avoid round-trip while testing
// https://www.chaijs.com/plugins/chai-bytes/ doesn't seem to work, due to compatibility issues with the default importt.
expect(bufferToBase64url(base64urlToBuffer(""))).to.equal(
bufferToBase64url(new Uint8Array([])),
);
expect(base64urlToBuffer("BAMCAQ")).toEqualBuffer(
new Uint8Array([4, 3, 2, 1]),
expect(bufferToBase64url(base64urlToBuffer("AA"))).to.equal(
bufferToBase64url(new Uint8Array([0])),
);
expect(base64urlToBuffer("A-B-C-")).toEqualBuffer(
new Uint8Array([3, 224, 126, 11]),
expect(bufferToBase64url(base64urlToBuffer("TEST"))).to.equal(
bufferToBase64url(new Uint8Array([76, 68, 147])),
);
expect(bufferToBase64url(base64urlToBuffer("BAMCAQ"))).to.equal(
bufferToBase64url(new Uint8Array([4, 3, 2, 1])),
);
expect(bufferToBase64url(base64urlToBuffer("A-B-C-"))).to.equal(
bufferToBase64url(new Uint8Array([3, 224, 126, 11])),
);
});

test("should convert simple `ArrayBuffer` values to base64url", () => {
expect(bufferToBase64url(new Uint8Array([]))).toBe("");
expect(bufferToBase64url(new Uint8Array([0]))).toBe("AA");
expect(bufferToBase64url(new Uint8Array([4, 3, 2, 1]))).toBe("BAMCAQ");
expect(bufferToBase64url(new Uint8Array([]))).to.equal("");
expect(bufferToBase64url(new Uint8Array([0]))).to.equal("AA");
expect(bufferToBase64url(new Uint8Array([4, 3, 2, 1]))).to.equal("BAMCAQ");
});

test("should round-trip through `ArrayBuffer`", () => {
// Fun fact: multiple base64url encodings can represent the same value. We expect
// the output value to be different than the input in this case.
expect(bufferToBase64url(base64urlToBuffer("ABCDEF")))
.toBe("ABCDEA");
expect(bufferToBase64url(base64urlToBuffer("ABCDEF"))).to.equal("ABCDEA");
});
});
29 changes: 15 additions & 14 deletions src/webauthn-json/extended.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @jest-environment jsdom
*/
import { expect } from "@open-wc/testing";

import { base64urlToBuffer } from "./base64url";
import { base64urlToBuffer, bufferToBase64url } from "./base64url";
import { CredentialCreationOptionsExtendedJSON } from "./extended/json";
import { PublicKeyCredentialWithClientExtensionResults } from "./basic/json";
import { credentialCreationOptionsExtended } from "./extended/schema";
Expand All @@ -11,13 +9,12 @@ import {
getExtendedResponseToJSON,
} from "./extended/api";
import { convert } from "./convert";
import "./arraybuffer.jest";

const unimplemented: any = () => {
throw new Error("unimplemented");
};

describe("extended schema", () => {
suite("extended schema", () => {
test("converts CredentialCreationOptionsExtendedJSON", () => {
const cco: CredentialCreationOptionsExtendedJSON = {
publicKey: {
Expand Down Expand Up @@ -50,12 +47,16 @@ describe("extended schema", () => {
credentialCreationOptionsExtended,
cco,
);
expect(converted.publicKey!.extensions!.largeBlob!.write).toEqualBuffer(
new Uint8Array([
0x2c, 0x04, 0x46, 0x13, 0xe0, 0x4b, 0x38, 0x1f, 0x8b, 0x01, 0x11, 0x84,
0xf8, 0x12, 0xce, 0x07, 0xe2, 0xc0, 0x44, 0x61, 0x3e, 0x04, 0xb3, 0x81,
0xf8, 0xb0, 0x11, 0x18, 0x4f, 0x81, 0x2c, 0xe0,
]),
expect(
bufferToBase64url(converted.publicKey!.extensions!.largeBlob!.write),
).to.equal(
bufferToBase64url(
new Uint8Array([
0x2c, 0x04, 0x46, 0x13, 0xe0, 0x4b, 0x38, 0x1f, 0x8b, 0x01, 0x11,
0x84, 0xf8, 0x12, 0xce, 0x07, 0xe2, 0xc0, 0x44, 0x61, 0x3e, 0x04,
0xb3, 0x81, 0xf8, 0xb0, 0x11, 0x18, 0x4f, 0x81, 0x2c, 0xe0,
]),
),
);
});

Expand All @@ -81,7 +82,7 @@ describe("extended schema", () => {
}) as AuthenticationExtensionsClientOutputs,
};
const converted = createExtendedResponseToJSON(pkcwa);
expect(converted).toEqual({
expect(converted).to.deep.equal({
type: "public-key",
id: "URL_SAFE_BASE_64_CREDENTIAL_ID-URL_SAFE_BASE_64_CREDENTIAL_ID-URL_SAFE_BASE_64_CREDENT",
rawId: "AQIDBA",
Expand Down Expand Up @@ -118,7 +119,7 @@ describe("extended schema", () => {
}) as AuthenticationExtensionsClientOutputs,
};
const converted = getExtendedResponseToJSON(pkcwa);
expect(converted).toEqual({
expect(converted).to.deep.equal({
type: "public-key",
id: "URL_SAFE_BASE_64_CREDENTIAL_ID-URL_SAFE_BASE_64_CREDENTIAL_ID-URL_SAFE_BASE_64_CREDENT",
rawId: "AQIDBA",
Expand Down
87 changes: 47 additions & 40 deletions src/webauthn-json/schema-format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @jest-environment jsdom
*/
import { expect } from "@open-wc/testing";

import { base64urlToBuffer } from "./base64url";
import { base64urlToBuffer, bufferToBase64url } from "./base64url";
import { Schema } from "./schema-format";
import {
convert,
Expand All @@ -11,44 +9,45 @@ import {
optional,
required,
} from "./convert";
import "./arraybuffer.jest";

describe("conversion", () => {
suite("conversion", () => {
test("can handle empty schema", () => {
const schema: Schema = {};
expect(() => convert(base64urlToBuffer, schema, {})).not.toThrow();
expect(() => convert(base64urlToBuffer, schema, {})).not.to.throw();
});

test("copies a required `copy` value", () => {
const schema: Schema = { number: { required: true, schema: "copy" } };
const converted = convert(base64urlToBuffer, schema, { number: 4 });
expect(converted.number).toBe(4);
expect(converted.number).to.equal(4);
});

test("errors when a required `copy` value is missing", () => {
const schema: Schema = { number: { required: true, schema: "copy" } };
expect(() => convert(base64urlToBuffer, schema, {})).toThrowError(
expect(() => convert(base64urlToBuffer, schema, {})).to.throw(
/Missing key/,
);
});

test("copies a required `convert` value", () => {
const schema: Schema = { number: { required: true, schema: "convert" } };
const converted = convert(base64urlToBuffer, schema, { number: "AA==" });
expect(converted.number).toEqualBuffer(new Uint8Array([0]));
expect(bufferToBase64url(converted.number)).to.equal(
bufferToBase64url(new Uint8Array([0])),
);
});

test("errors when a required `convert` value is missing", () => {
const schema: Schema = { number: { required: true, schema: "convert" } };
expect(() => convert(base64urlToBuffer, schema, {})).toThrowError(
expect(() => convert(base64urlToBuffer, schema, {})).to.throw(
/Missing key/,
);
});

test("allows a missing optional value", () => {
const schema: Schema = { number: { required: false, schema: "copy" } };
const converted = convert(base64urlToBuffer, schema, {});
expect(converted).not.toHaveProperty("number");
expect(converted).not.to.haveOwnProperty("number");
});

test("ignores unknown properties", () => {
Expand All @@ -57,8 +56,8 @@ describe("conversion", () => {
number: 6,
extra: "hi",
});
expect(converted.number).toBe(6);
expect(converted).not.toHaveProperty("extra");
expect(converted.number).to.equal(6);
expect(converted).not.haveOwnProperty("extra");
});

test("converts object", () => {
Expand All @@ -74,9 +73,9 @@ describe("conversion", () => {
const converted = convert(base64urlToBuffer, schema, {
nestedObject: { number: 7, convertField: "BB==" },
});
expect(converted.nestedObject.number).toBe(7);
expect(converted.nestedObject.convertField).toEqualBuffer(
new Uint8Array([4]),
expect(converted.nestedObject.number).to.equal(7);
expect(bufferToBase64url(converted.nestedObject.convertField)).to.equal(
bufferToBase64url(new Uint8Array([4])),
);
});

Expand All @@ -100,31 +99,35 @@ describe("conversion", () => {
],
});

expect(converted.nestedObjectList).toHaveLength(2);
expect(converted.nestedObjectList).to.have.length(2);

expect(converted.nestedObjectList[0].number).toBe(8);
expect(converted.nestedObjectList[0].string).toBe("hi");
expect(converted.nestedObjectList[0].convertField).toEqualBuffer(
new Uint8Array([8]),
);
expect(converted.nestedObjectList[0].number).to.equal(8);
expect(converted.nestedObjectList[0].string).to.equal("hi");
expect(
bufferToBase64url(converted.nestedObjectList[0].convertField),
).to.equal(bufferToBase64url(new Uint8Array([8])));

expect(converted.nestedObjectList[1].number).toBe(9);
expect(converted.nestedObjectList[1]).not.toHaveProperty("string");
expect(converted.nestedObjectList[1].convertField).toEqualBuffer(
new Uint8Array([12]),
);
expect(converted.nestedObjectList[1].number).to.equal(9);
expect(converted.nestedObjectList[1]).not.to.haveOwnProperty("string");
expect(
bufferToBase64url(converted.nestedObjectList[1].convertField),
).to.equal(bufferToBase64url(new Uint8Array([12])));
});
});

test("converts leaf lists", () => {
const schema: Schema = ["convert"];
const converted = convert(base64urlToBuffer, schema, ["EE", "FF"]);
expect(converted).toHaveLength(2);
expect(converted[0]).toEqualBuffer(new Uint8Array([16]));
expect(converted[1]).toEqualBuffer(new Uint8Array([20]));
expect(converted).to.have.length(2);
expect(bufferToBase64url(converted[0])).to.equal(
bufferToBase64url(new Uint8Array([16])),
);
expect(bufferToBase64url(converted[1])).to.equal(
bufferToBase64url(new Uint8Array([20])),
);
});

describe("convenience functions", () => {
suite("convenience functions", () => {
test("can be called", () => {
const schema: Schema = {
a: required(copyValue),
Expand All @@ -138,27 +141,31 @@ describe("convenience functions", () => {
c: 7,
d: "FF",
});
expect(converted.a).toBe(5);
expect(converted.b).toEqualBuffer(new Uint8Array([16]));
expect(converted.c).toBe(7);
expect(converted.d).toEqualBuffer(new Uint8Array([20]));
expect(converted.a).to.equal(5);
expect(bufferToBase64url(converted.b)).to.equal(
bufferToBase64url(new Uint8Array([16])),
);
expect(converted.c).to.equal(7);
expect(bufferToBase64url(converted.d)).to.equal(
bufferToBase64url(new Uint8Array([20])),
);
});

test("enforce required value", () => {
const schema: Schema = { number: required(copyValue) };
expect(() => convert(base64urlToBuffer, schema, {})).toThrowError(
expect(() => convert(base64urlToBuffer, schema, {})).to.throw(
/Missing key/,
);
});

test("allow leaving out optional value", () => {
const schema: Schema = { number: { required: false, schema: "copy" } };
const converted = convert(base64urlToBuffer, schema, {});
expect(converted).not.toHaveProperty("number");
expect(converted).not.to.haveOwnProperty("number");
});
});

describe("conversion function", () => {
suite("conversion function", () => {
function double(s: string): string {
return s + s;
}
Expand All @@ -168,6 +175,6 @@ describe("conversion function", () => {
convertField: { required: true, schema: "convert" },
};
const converted = convert(double, schema, { convertField: "hi" });
expect(converted.convertField).toBe("hihi");
expect(converted.convertField).to.equal("hihi");
});
});
Loading

0 comments on commit 2b67bdb

Please sign in to comment.