Skip to content

Commit

Permalink
test(verification): improve test coverage for `verifyRequestByKeyId()…
Browse files Browse the repository at this point in the history
…` and `fetchVerificationKeys()` (#101)

### What are you trying to accomplish?
Improve test coverage for `verification.js` file. Concretely for
`verifyRequestByKeyId()` and `fetchVerificationKeys()` methods.

relates to
#87
  • Loading branch information
oscard0m authored Sep 18, 2024
1 parent 14bd0e7 commit 7914799
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion test/verification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test("verifyRequestByKeyId()", async (t) => {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
},
}
);
const testRequest = defaultRequest.defaults({
request: { fetch: fetchMock },
Expand All @@ -70,6 +70,55 @@ test("verifyRequestByKeyId()", async (t) => {
t.deepEqual(result, true);
});

test("verifyRequestByKeyId() - throws if keyId not present in verification keys list", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
opts.dispatcher = mockAgent;
return fetch(url, opts);
}

mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.github.com");
mockPool
.intercept({
method: "get",
path: `/meta/public_keys/copilot_api`,
})
.reply(
200,
{
public_keys: [
{
key: CURRENT_PUBLIC_KEY,
key_identifier: KEY_ID,
is_current: true,
},
],
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);
const testRequest = defaultRequest.defaults({
request: { fetch: fetchMock },
});

await t.throwsAsync(
verifyRequestByKeyId(RAW_BODY, SIGNATURE, "wrong_key", {
request: testRequest,
}),
{
name: "Error",
message:
"[@copilot-extensions/preview-sdk] No public key found matching key identifier",
}
);
});

test("verifyRequestByKeyId() - invalid arguments", async (t) => {
t.throwsAsync(verifyRequestByKeyId(RAW_BODY, SIGNATURE), {
name: "Error",
Expand Down Expand Up @@ -170,13 +219,69 @@ test("fetchVerificationKeys()", async (t) => {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);
const testRequest = defaultRequest.defaults({
request: { fetch: fetchMock },
});

const result = await fetchVerificationKeys({
request: testRequest,
});

t.deepEqual(result, publicKeys);
});

test("fetchVerificationKeys() - with token", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
opts.dispatcher = mockAgent;
return fetch(url, opts);
}

const publicKeys = [
{
key: "<key 1>",
key_identifier: "<key-id 1>",
is_current: true,
},
{
key: "<key 2>",
key_identifier: "<key-id 2>",
is_current: true,
},
];

mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.github.com");
const token = "secr3t";
mockPool
.intercept({
method: "get",
path: `/meta/public_keys/copilot_api`,
headers: {
Authorization: `token ${token}`,
},
})
.reply(
200,
{
public_keys: publicKeys,
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);
const testRequest = defaultRequest.defaults({
request: { fetch: fetchMock },
});

const result = await fetchVerificationKeys({
token,
request: testRequest,
});

Expand Down

0 comments on commit 7914799

Please sign in to comment.