From 0982968f79506da5c8b47557106b7fc9fd91c830 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Tue, 13 Jul 2021 12:08:58 +0100 Subject: [PATCH] Setup common test data --- .../middleware/passport/tests/google.spec.js | 71 +++++----- .../middleware/passport/tests/oidc.spec.js | 121 +++++++++--------- .../passport/tests/third-party-common.spec.js | 19 +++ .../middleware/passport/tests/utilities.js | 33 +++++ 4 files changed, 141 insertions(+), 103 deletions(-) create mode 100644 packages/auth/src/middleware/passport/tests/third-party-common.spec.js create mode 100644 packages/auth/src/middleware/passport/tests/utilities.js diff --git a/packages/auth/src/middleware/passport/tests/google.spec.js b/packages/auth/src/middleware/passport/tests/google.spec.js index 9c1a2dae04..988487a814 100644 --- a/packages/auth/src/middleware/passport/tests/google.spec.js +++ b/packages/auth/src/middleware/passport/tests/google.spec.js @@ -1,3 +1,23 @@ +// Mock data + +const { data } = require("./utilities") + +const googleConfig = { + callbackURL: "http://somecallbackurl", + clientID: data.clientID, + clientSecret: data.clientSecret, +} + +const profile = { + id: "mockId", + _json: { + email : data.email + }, + provider: "google" +} + +const user = data.buildThirdPartyUser("google", "google", profile) + describe("google", () => { describe("strategyFactory", () => { // mock passport strategy factory @@ -6,22 +26,17 @@ describe("google", () => { 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(googleConfig) + + const expectedOptions = { + clientID: googleConfig.clientID, + clientSecret: googleConfig.clientSecret, + callbackURL: googleConfig.callbackURL, } - - await google.strategyFactory(config) - + expect(mockStrategy).toHaveBeenCalledWith( - { - clientID: config.clientID, - clientSecret: config.clientSecret, - callbackURL: config.callbackURL, - }, + expectedOptions, expect.anything() ) }) @@ -36,43 +51,21 @@ describe("google", () => { 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, + data.accessToken, + data.refreshToken, profile, mockDone ) expect(authenticateThirdParty).toHaveBeenCalledWith( - thirdPartyUser, + user, true, mockDone) }) diff --git a/packages/auth/src/middleware/passport/tests/oidc.spec.js b/packages/auth/src/middleware/passport/tests/oidc.spec.js index 487bd71407..7b19ea2923 100644 --- a/packages/auth/src/middleware/passport/tests/oidc.spec.js +++ b/packages/auth/src/middleware/passport/tests/oidc.spec.js @@ -1,50 +1,69 @@ +// Mock data + +const { data } = require("./utilities") + +const issuer = "mockIssuer" +const sub = "mockSub" +const profile = { + id: "mockId", + _json: { + email : data.email + } +} +let jwtClaims = {} +const idToken = "mockIdToken" +const params = {} + +const callbackUrl = "http://somecallbackurl" + +// response from .well-known/openid-configuration +const oidcConfigUrlResponse = { + issuer: issuer, + authorization_endpoint: "mockAuthorizationEndpoint", + token_endpoint: "mockTokenEndpoint", + userinfo_endpoint: "mockUserInfoEndpoint" +} + +const oidcConfig = { + configUrl: "http://someconfigurl", + clientID: data.clientID, + clientSecret: data.clientSecret, +} + +const user = data.buildThirdPartyUser(issuer, "oidc", profile) + describe("oidc", () => { describe("strategyFactory", () => { // mock passport strategy factory jest.mock("@techpass/passport-openidconnect") const mockStrategy = require("@techpass/passport-openidconnect").Strategy - // mock the response from .well-known/openid-configuration - const configUrlResponse = { - issuer: "mockIssuer", - authorization_endpoint: "mockAuthorizationEndpoint", - token_endpoint: "mockTokenEndpoint", - userinfo_endpoint: "mockUserInfoEndpoint" - } - // mock the request to retrieve the oidc configuration - jest.mock("node-fetch", () => jest.fn(() => ( - { - ok: true, - json: async () => configUrlResponse - } - ))) + jest.mock("node-fetch") const mockFetch = require("node-fetch") + mockFetch.mockReturnValue({ + ok: true, + json: () => oidcConfigUrlResponse + }) it("should create successfully create an oidc strategy", async () => { const oidc = require("../oidc") - - // mock the config supplied to the strategy factory - config = { - configUrl: "http://someconfigurl", - clientID: "clientId", - clientSecret: "clientSecret", + + await oidc.strategyFactory(oidcConfig, callbackUrl) + + expect(mockFetch).toHaveBeenCalledWith(oidcConfig.configUrl) + + const expectedOptions = { + issuer: oidcConfigUrlResponse.issuer, + authorizationURL: oidcConfigUrlResponse.authorization_endpoint, + tokenURL: oidcConfigUrlResponse.token_endpoint, + userInfoURL: oidcConfigUrlResponse.userinfo_endpoint, + clientID: oidcConfig.clientID, + clientSecret: oidcConfig.clientSecret, + callbackURL: callbackUrl, } - callbackUrl = "http://somecallbackurl" - - await oidc.strategyFactory(config, callbackUrl) - - expect(mockFetch).toHaveBeenCalledWith("http://someconfigurl") expect(mockStrategy).toHaveBeenCalledWith( - { - issuer: configUrlResponse.issuer, - authorizationURL: configUrlResponse.authorization_endpoint, - tokenURL: configUrlResponse.token_endpoint, - userInfoURL: configUrlResponse.userinfo_endpoint, - clientID: config.clientID, - clientSecret: config.clientSecret, - callbackURL: callbackUrl, - }, + expectedOptions, expect.anything() ) }) @@ -58,36 +77,10 @@ describe("oidc", () => { // mock third party common authentication jest.mock("../third-party-common") const authenticateThirdParty = require("../third-party-common").authenticateThirdParty - - // parameters - const issuer = "mockIssuer" - const sub = "mockSub" - const profile = { - id: "mockId", - _json: { - email : "mock@budibase.com" - } - } - let jwtClaims = {} - const accessToken = "mockAccessToken" - const refreshToken = "mockRefreshToken" - const idToken = "mockIdToken" - const params = {} + // mock the passport callback const mockDone = jest.fn() - const thirdPartyUser = { - provider: issuer, - providerType: "oidc", - userId: profile.id, - profile: profile, - email: "mock@budibase.com", - oauth2: { - accessToken: accessToken, - refreshToken: refreshToken, - }, - } - async function doAuthenticate() { const oidc = require("../oidc") @@ -96,8 +89,8 @@ describe("oidc", () => { sub, profile, jwtClaims, - accessToken, - refreshToken, + data.accessToken, + data.refreshToken, idToken, params, mockDone @@ -108,7 +101,7 @@ describe("oidc", () => { await doAuthenticate() expect(authenticateThirdParty).toHaveBeenCalledWith( - thirdPartyUser, + user, false, mockDone) } diff --git a/packages/auth/src/middleware/passport/tests/third-party-common.spec.js b/packages/auth/src/middleware/passport/tests/third-party-common.spec.js new file mode 100644 index 0000000000..61d747d68a --- /dev/null +++ b/packages/auth/src/middleware/passport/tests/third-party-common.spec.js @@ -0,0 +1,19 @@ +// Mock data + +const { authenticateThirdParty } = require("../third-party-common") + +describe("third party common", () => { + + describe("authenticateThirdParty", () => { + it("", () => { + + }) + }) + + describe("syncUser", () => { + it("", () => { + + }) + }) +}) + diff --git a/packages/auth/src/middleware/passport/tests/utilities.js b/packages/auth/src/middleware/passport/tests/utilities.js new file mode 100644 index 0000000000..69e8312915 --- /dev/null +++ b/packages/auth/src/middleware/passport/tests/utilities.js @@ -0,0 +1,33 @@ +// Mock Data + +const mockClientID = "mockClientID" +const mockClientSecret = "mockClientSecret" + +const mockEmail = "mock@budibase.com" +const mockAccessToken = "mockAccessToken" +const mockRefreshToken = "mockRefreshToken" + +const buildOauth2 = (accessToken=mockAccessToken, refreshToken=mockRefreshToken) => ( + { + accessToken: accessToken, + refreshToken: refreshToken, + } +) + +const buildThirdPartyUser = (provider, providerType, profile, email=mockEmail, oauth2=buildOauth2()) => ({ + provider: provider, + providerType: providerType, + userId: profile.id, + profile: profile, + email: email, + oauth2: oauth2, +}) + +exports.data = { + clientID: mockClientID, + clientSecret: mockClientSecret, + email: mockEmail, + accessToken: mockAccessToken, + refreshToken: mockRefreshToken, + buildThirdPartyUser +} \ No newline at end of file