diff --git a/packages/auth/src/middleware/passport/google.js b/packages/auth/src/middleware/passport/google.js index 4c21063a1e..68fe885512 100644 --- a/packages/auth/src/middleware/passport/google.js +++ b/packages/auth/src/middleware/passport/google.js @@ -50,3 +50,5 @@ exports.strategyFactory = async function (config) { throw new Error("Error constructing google authentication strategy", err) } } +// expose for testing +exports.authenticate = authenticate diff --git a/packages/auth/src/middleware/passport/tests/google.spec.js b/packages/auth/src/middleware/passport/tests/google.spec.js new file mode 100644 index 0000000000..9c1a2dae04 --- /dev/null +++ b/packages/auth/src/middleware/passport/tests/google.spec.js @@ -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) + }) + }) +}) +