Skip to content

Commit

Permalink
feat(iam): test ProviderContext acts as accumulator between provider …
Browse files Browse the repository at this point in the history
…calls
  • Loading branch information
nutrina committed Aug 19, 2022
1 parent bc0220a commit 2cf474c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
104 changes: 102 additions & 2 deletions iam/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import request from "supertest";

// ---- Test subject
import { app, config } from "../src/index";
import { app, config, providers } from "../src/index";

// ---- Types
import { ErrorResponseBody, ValidResponseBody } from "@gitcoin/passport-types";
import {
ErrorResponseBody,
ProviderContext,
RequestPayload,
ValidResponseBody,
VerifiedPayload,
} from "@gitcoin/passport-types";

import * as identityMock from "@gitcoin/passport-identity/dist/commonjs/src/credentials";

Expand Down Expand Up @@ -210,6 +216,100 @@ describe("POST /verify", function () {
expect((response.body[1] as ValidResponseBody).credential.credentialSubject.id).toEqual(expectedId);
});

it("handles valid challenge requests with multiple types, and acumulates values between provider calls", async () => {
// challenge received from the challenge endpoint
const challenge = {
issuer: config.issuer,
credentialSubject: {
id: "did:pkh:eip155:1:0x0",
provider: "challenge-GitcoinContributorStatistics#numGrantsContributeToGte#10",
address: "0x0",
challenge: "123456789ABDEFGHIJKLMNOPQRSTUVWXYZ",
},
};
// payload containing a signature of the challenge in the challenge credential
const payload = {
type: "GitcoinContributorStatistics#numGrantsContributeToGte#10",
types: [
"GitcoinContributorStatistics#numGrantsContributeToGte#10",
"GitcoinContributorStatistics#numGrantsContributeToGte#25",
"GitcoinContributorStatistics#numGrantsContributeToGte#100",
],
address: "0x0",
proofs: {
code: "SECRET_CODE",
},
};

// resolve the verification
jest.spyOn(identityMock, "verifyCredential").mockResolvedValue(true);

// spy on the providers
jest
.spyOn(providers._providers["GitcoinContributorStatistics#numGrantsContributeToGte#10"], "verify")
.mockImplementation(async (payload: RequestPayload, context: ProviderContext): Promise<VerifiedPayload> => {
context["update_1"] = true;
return {
valid: true,
record: {
numGrantsContributeToGte: "10",
},
};
});
jest
.spyOn(providers._providers["GitcoinContributorStatistics#numGrantsContributeToGte#25"], "verify")
.mockImplementation(async (payload: RequestPayload, context: ProviderContext): Promise<VerifiedPayload> => {
context["update_2"] = true;

return {
valid: true,
record: {
numGrantsContributeToGte: "25",
},
};
});
const gitcoinGte100 = jest
.spyOn(providers._providers["GitcoinContributorStatistics#numGrantsContributeToGte#100"], "verify")
.mockImplementation(async (payload: RequestPayload, context: ProviderContext): Promise<VerifiedPayload> => {
return {
valid: true,
record: {
numGrantsContributeToGte: "100",
},
};
});

// check that ID matches the payload (this has been mocked)
const expectedId = "did:pkh:eip155:1:0x0";

// create a req against the express app
const response = await request(app)
.post("/api/v0.0.0/verify")
.send({ challenge, payload })
.set("Accept", "application/json")
.expect(200)
.expect("Content-Type", /json/);

// check for an id match on the mocked credential
expect((response.body[0] as ValidResponseBody).credential.credentialSubject.id).toEqual(expectedId);
expect((response.body[1] as ValidResponseBody).credential.credentialSubject.id).toEqual(expectedId);

expect(gitcoinGte100).toBeCalledWith(
{
type: "GitcoinContributorStatistics#numGrantsContributeToGte#10",
types: [
"GitcoinContributorStatistics#numGrantsContributeToGte#10",
"GitcoinContributorStatistics#numGrantsContributeToGte#25",
"GitcoinContributorStatistics#numGrantsContributeToGte#100",
],
address: "0x0",
proofs: { code: "SECRET_CODE" },
},
// The expected acumulated value in the context
{ update_1: true, update_2: true }
);
});

it("handles invalid challenge requests where credential.issuer is unknown", async () => {
// challenge received from the challenge endpoint
const challenge = {
Expand Down
2 changes: 1 addition & 1 deletion iam/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const config: {
};

// Initiate providers - new Providers should be registered in this array...
const providers = new Providers([
export const providers = new Providers([
// Example provider which verifies the payload when `payload.proofs.valid === "true"`
new SimpleProvider(),
new GoogleProvider(),
Expand Down

0 comments on commit 2cf474c

Please sign in to comment.