Skip to content

Commit

Permalink
trying to mock passport
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahscott committed Jul 29, 2021
1 parent c88cd57 commit c24f6e2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 33 deletions.
1 change: 0 additions & 1 deletion api.planx.uk/gis/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const makeEsriUrl = (domain, id, serverIndex = 0, overrideParams = {}) => {
.map((key) => key + "=" + escape(params[key]))
.join("&"),
].join("?");
console.log({ url });

return url;
};
Expand Down
2 changes: 0 additions & 2 deletions api.planx.uk/gis/local_authorities/southwark.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ async function locationSearch(x, y, extras) {
: false,
};

console.log(responses);

responses
.filter(([_key, result]) => result instanceof Error)
.forEach(([key, _result]) => {
Expand Down
1 change: 1 addition & 0 deletions api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
27 changes: 27 additions & 0 deletions api.planx.uk/publish.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
71 changes: 43 additions & 28 deletions api.planx.uk/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions api.planx.uk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}}
>
Expand Down

0 comments on commit c24f6e2

Please sign in to comment.