-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(iam): add error handling to google provider
re [#15]
- Loading branch information
Showing
2 changed files
with
74 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters