From c24f6e2c2ce8eb71a7f8b7251d633131875947c6 Mon Sep 17 00:00:00 2001 From: Sarah Scott Date: Thu, 29 Jul 2021 14:17:36 -0700 Subject: [PATCH] trying to mock passport --- api.planx.uk/gis/helpers.js | 1 - .../gis/local_authorities/southwark.js | 2 - api.planx.uk/package.json | 1 + api.planx.uk/publish.test.js | 27 +++++++ api.planx.uk/server.js | 71 +++++++++++-------- api.planx.uk/server.test.js | 2 +- api.planx.uk/yarn.lock | 30 ++++++++ .../FlowEditor/components/PreviewBrowser.tsx | 2 +- 8 files changed, 103 insertions(+), 33 deletions(-) create mode 100644 api.planx.uk/publish.test.js diff --git a/api.planx.uk/gis/helpers.js b/api.planx.uk/gis/helpers.js index 189576d848..cd1bbfeea0 100644 --- a/api.planx.uk/gis/helpers.js +++ b/api.planx.uk/gis/helpers.js @@ -31,7 +31,6 @@ const makeEsriUrl = (domain, id, serverIndex = 0, overrideParams = {}) => { .map((key) => key + "=" + escape(params[key])) .join("&"), ].join("?"); - console.log({ url }); return url; }; diff --git a/api.planx.uk/gis/local_authorities/southwark.js b/api.planx.uk/gis/local_authorities/southwark.js index f83c13c7af..5698c99974 100644 --- a/api.planx.uk/gis/local_authorities/southwark.js +++ b/api.planx.uk/gis/local_authorities/southwark.js @@ -127,8 +127,6 @@ async function locationSearch(x, y, extras) { : false, }; - console.log(responses); - responses .filter(([_key, result]) => result instanceof Error) .forEach(([key, _result]) => { diff --git a/api.planx.uk/package.json b/api.planx.uk/package.json index 16aa89a5e9..e0ea503d2a 100644 --- a/api.planx.uk/package.json +++ b/api.planx.uk/package.json @@ -27,6 +27,7 @@ "nanoid": "^3.1.12", "passport": "^0.4.1", "passport-google-oauth20": "^2.0.0", + "passport-mocked": "^1.4.0", "pino-noir": "^2.2.1" }, "scripts": { diff --git a/api.planx.uk/publish.test.js b/api.planx.uk/publish.test.js new file mode 100644 index 0000000000..3066a1dde1 --- /dev/null +++ b/api.planx.uk/publish.test.js @@ -0,0 +1,27 @@ +const nock = require("nock"); +const supertest = require("supertest"); +const passport = require("passport"); + +const app = require("./server"); + +it("works", async () => { + const strategy = passport._strategies; + strategy._token_response = { + access_token: "at-1234", + expires_in: 100000, + }; + + strategy._profile = { + id: 1234, + provider: "google", + displayName: "Hi", + emails: [{ value: "fjdklajfds@example.com" }], + }; + + await supertest(app).get("/auth/google").expect(302); + + await supertest(app) + .post("/flows/1/publish") + .expect(200) + .then((res) => console.log(res)); +}); diff --git a/api.planx.uk/server.js b/api.planx.uk/server.js index d154c0d5ff..844a33916e 100644 --- a/api.planx.uk/server.js +++ b/api.planx.uk/server.js @@ -14,6 +14,7 @@ const { Server } = require("http"); const passport = require("passport"); const { sign } = require("jsonwebtoken"); const GoogleStrategy = require("passport-google-oauth20").Strategy; +const MockStrategy = require("passport-mocked").Strategy; const { createProxyMiddleware, responseInterceptor, @@ -168,19 +169,36 @@ const buildJWT = async (profile, done) => { } }; -passport.use( - "google", - new GoogleStrategy( - { - clientID: process.env.GOOGLE_CLIENT_ID, - clientSecret: process.env.GOOGLE_CLIENT_SECRET, - callbackURL: `${process.env.API_URL_EXT}/auth/google/callback`, - }, - async function (_accessToken, _refreshToken, profile, done) { - await buildJWT(profile, done); - } - ) -); +function strategyForEnv() { + const strategy = + process.env.NODE_ENV === "test" + ? new MockStrategy( + { + name: "google", + clientID: process.env.GOOGLE_CLIENT_ID, + clientSecret: process.env.GOOGLE_CLIENT_SECRET, + callbackURL: `${process.env.API_URL_EXT}/auth/google/callback`, + }, + async (_accessToken, _refreshToken, profile, done) => { + await strategyCallback(_accessToken, _refreshToken, profile, done); + } + ) + : new GoogleStrategy( + { + clientID: process.env.GOOGLE_CLIENT_ID, + clientSecret: process.env.GOOGLE_CLIENT_SECRET, + callbackURL: `${process.env.API_URL_EXT}/auth/google/callback`, + }, + strategyCallback + ); + return strategy; +} + +async function strategyCallback(_accessToken, _refreshToken, profile, done) { + await buildJWT(profile, done); +} + +passport.use("google", strategyForEnv()); passport.serializeUser(function (user, cb) { cb(null, user); @@ -333,18 +351,16 @@ app.get("/pay/:localAuthority/:paymentId", (req, res) => { { pathRewrite: () => `/${req.params.paymentId}`, selfHandleResponse: true, - onProxyRes: responseInterceptor( - async (responseBuffer) => { - const govUkResponse = JSON.parse(responseBuffer.toString("utf8")); - - // only return payment status, filter out PII - return JSON.stringify({ - payment_id: govUkResponse.payment_id, - amount: govUkResponse.amount, - state: govUkResponse.state, - }); - } - ) + onProxyRes: responseInterceptor(async (responseBuffer) => { + const govUkResponse = JSON.parse(responseBuffer.toString("utf8")); + + // only return payment status, filter out PII + return JSON.stringify({ + payment_id: govUkResponse.payment_id, + amount: govUkResponse.amount, + state: govUkResponse.state, + }); + }), }, req )(req, res); @@ -451,10 +467,9 @@ app.get("/flows/:flowId/download-schema", async (req, res) => { }); } else { // build a CSV and stream it - stringify(schema.get_flow_schema, { header: true }) - .pipe(res); + stringify(schema.get_flow_schema, { header: true }).pipe(res); - res.header('Content-type', 'text/csv'); + res.header("Content-type", "text/csv"); res.attachment(`${req.params.flowId}.csv`); } } catch (error) { diff --git a/api.planx.uk/server.test.js b/api.planx.uk/server.test.js index ddf52046c4..6b5b438382 100644 --- a/api.planx.uk/server.test.js +++ b/api.planx.uk/server.test.js @@ -104,7 +104,7 @@ describe("sending a payment to GOV.UK Pay", () => { }); }); -describe.only("fetching status of a GOV.UK payment", () => { +describe("fetching status of a GOV.UK payment", () => { // https://docs.payments.service.gov.uk/reporting/#get-information-about-a-single-payment const govUKResponse = { created_date: "2019-07-11T10:36:26.988Z", diff --git a/api.planx.uk/yarn.lock b/api.planx.uk/yarn.lock index 2b90767563..f0501be6c5 100644 --- a/api.planx.uk/yarn.lock +++ b/api.planx.uk/yarn.lock @@ -877,6 +877,11 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" +bluebird@^3.5.5: + version "3.7.2" + resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f" + integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg== + body-parser@1.19.0, body-parser@^1.19.0: version "1.19.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" @@ -3538,6 +3543,15 @@ passport-google-oauth20@^2.0.0: dependencies: passport-oauth2 "1.x.x" +passport-mocked@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/passport-mocked/-/passport-mocked-1.4.0.tgz#10f84b931b7ac8469a82472ab8c5ed08029ba508" + integrity sha512-49NUkg08n0+Wj3hMCSnd8ScmtyNhIeodLutp3qjP/8YXaNGNUrKKImXosvP9ktA2nCjm9gYcv5QVyD4IqQ1JJQ== + dependencies: + bluebird "^3.5.5" + passport "0.3.0" + url "^0.11.0" + passport-oauth2@1.x.x: version "1.5.0" resolved "https://registry.yarnpkg.com/passport-oauth2/-/passport-oauth2-1.5.0.tgz#64babbb54ac46a4dcab35e7f266ed5294e3c4108" @@ -3554,6 +3568,14 @@ passport-strategy@1.x.x: resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4" integrity sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ= +passport@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/passport/-/passport-0.3.0.tgz#14c151b0eb6795aa9335239827f548d5f94c7046" + integrity sha1-FMFRsOtnlaqTNSOYJ/VI1flMcEY= + dependencies: + passport-strategy "1.x.x" + pause "0.0.1" + passport@^0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.1.tgz#941446a21cb92fc688d97a0861c38ce9f738f270" @@ -4587,6 +4609,14 @@ url@0.10.3: punycode "1.3.2" querystring "0.2.0" +url@^0.11.0: + version "0.11.0" + resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1" + integrity sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE= + dependencies: + punycode "1.3.2" + querystring "0.2.0" + use@^3.1.0: version "3.1.1" resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" diff --git a/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx b/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx index a838a44541..252b9b2a84 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx @@ -149,7 +149,7 @@ const PreviewBrowser: React.FC<{ url: string }> = React.memo((props) => { setLastPublishedTitle( publishedFlow?.data.alteredNodes ? `Successfully published changes to ${publishedFlow.data.alteredNodes.length} node(s)` - : "No new changes to publish!" + : "No new changes to publish" ); }} >