Test google auth strategy

This commit is contained in:
Rory Powell 2021-07-13 10:17:14 +01:00
parent 4f397740e0
commit d25d9edda1
2 changed files with 83 additions and 0 deletions

View File

@ -50,3 +50,5 @@ exports.strategyFactory = async function (config) {
throw new Error("Error constructing google authentication strategy", err)
}
}
// expose for testing
exports.authenticate = authenticate

View File

@ -0,0 +1,81 @@
describe("google", () => {
describe("strategyFactory", () => {
// mock passport strategy factory
jest.mock("passport-google-oauth")
const mockStrategy = require("passport-google-oauth").OAuth2Strategy
it("should create successfully create a google strategy", async () => {
const google = require("../google")
// mock the config supplied to the strategy factory
config = {
callbackURL: "http://somecallbackurl",
clientID: "clientId",
clientSecret: "clientSecret",
}
await google.strategyFactory(config)
expect(mockStrategy).toHaveBeenCalledWith(
{
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
},
expect.anything()
)
})
})
describe("authenticate", () => {
afterEach(() => {
jest.clearAllMocks();
});
// mock third party common authentication
jest.mock("../third-party-common")
const authenticateThirdParty = require("../third-party-common").authenticateThirdParty
// parameters
const profile = {
id: "mockId",
_json: {
email : "mock@budibase.com"
},
provider: "google"
}
const accessToken = "mockAccessToken"
const refreshToken = "mockRefreshToken"
// mock the passport callback
const mockDone = jest.fn()
const thirdPartyUser = {
provider: "google",
providerType: "google",
userId: profile.id,
profile: profile,
email: "mock@budibase.com",
oauth2: {
accessToken: accessToken,
refreshToken: refreshToken,
},
}
it("delegates authentication to third party common", async () => {
const google = require("../google")
await google.authenticate(
accessToken,
refreshToken,
profile,
mockDone
)
expect(authenticateThirdParty).toHaveBeenCalledWith(
thirdPartyUser,
true,
mockDone)
})
})
})