Skip to content

Commit

Permalink
test(iam): add error handling to google provider
Browse files Browse the repository at this point in the history
re [#15]
  • Loading branch information
shavinac authored and gdixon committed Apr 12, 2022
1 parent 1079fa9 commit e5b6449
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
92 changes: 71 additions & 21 deletions iam/__tests__/google.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,84 @@

// ---- Test subject
import { RequestPayload } from "@dpopp/types";
import { GoogleProvider } from "../src/providers/google";

jest.mock('google-auth-library');
jest.mock("google-auth-library");

import { OAuth2Client } from "google-auth-library";

const OAuth2ClientMock = jest
.spyOn(OAuth2Client.prototype, 'verifyIdToken')
.mockImplementation(() => {
const MOCK_EMAIL = "testEmail";
const MOCK_EMAIL_VERIFIED = true;
const MOCK_TOKEN_ID = "testToken";

return {
getPayload: jest.fn(() => ({
"email": "test",
"email_verified": "test"
}))
}
});
const OAuth2ClientMock = jest.spyOn(OAuth2Client.prototype, "verifyIdToken").mockImplementation(() => {
return {
getPayload: jest.fn(() => ({
email: MOCK_EMAIL,
email_verified: MOCK_EMAIL_VERIFIED,
})),
};
});

describe("Attempt verification", function () {
it("handles valid verification attempt", async () => {
const google = new GoogleProvider();
it("handles valid verification attempt", async () => {
const google = new GoogleProvider();

const verifiedPayload = await google.verify({
proofs: {
tokenId: MOCK_TOKEN_ID,
},
} as unknown as RequestPayload);

expect(OAuth2ClientMock).toBeCalledWith({ idToken: MOCK_TOKEN_ID, audience: process.env.GOOGLE_CLIENT_ID });
expect(verifiedPayload).toEqual({
valid: true,
record: {
email: MOCK_EMAIL,
},
});
});

it("should return invalid payload when email is not verified", async () => {
jest.spyOn(OAuth2Client.prototype, "verifyIdToken").mockImplementation(() => {
return {
getPayload: jest.fn(() => ({
email: MOCK_EMAIL,
email_verified: false,
})),
};
});

const google = new GoogleProvider();

const verifiedPayload = await google.verify({
proofs: {
tokenId: MOCK_TOKEN_ID,
},
} as unknown as RequestPayload);

expect(verifiedPayload).toEqual({
valid: false,
record: {
email: MOCK_EMAIL,
},
});
});

it("should return invalid payload when verifyIdToken throws exception", async () => {
jest.spyOn(OAuth2Client.prototype, "verifyIdToken").mockImplementation(() => {
throw Error("bad >:(");
});

const google = new GoogleProvider();

google.verify({
proofs: {
tokenId: "test"
}
} as unknown as RequestPayload)
const verifiedPayload = await google.verify({
proofs: {
tokenId: MOCK_TOKEN_ID,
},
} as unknown as RequestPayload);

expect(OAuth2ClientMock).toBeCalledWith({idToken: "test", audience: process.env.GOOGLE_CLIENT_ID})
expect(verifiedPayload).toEqual({
valid: false,
});
});
});
});
4 changes: 3 additions & 1 deletion iam/src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ export class GoogleProvider implements Provider {

try {
verifiedPayload = await verifyGoogle(payload.proofs.tokenId);
} catch (e) {
return { valid: false };
} finally {
valid = verifiedPayload && verifiedPayload.emailVerified ? true : false;
}

return {
valid: valid,
record: {
email: verifiedPayload.email || "",
email: verifiedPayload.email,
},
};
}
Expand Down

0 comments on commit e5b6449

Please sign in to comment.