Skip to content

Commit

Permalink
feat: Read GovPay tokens from team_integrations table (#2790)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Mar 14, 2024
1 parent 3b0bdd7 commit 38bab63
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 66 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@ GOV_UK_PAY_TOKEN_GLOUCESTER=👻
GOV_UK_PAY_TOKEN_MEDWAY=👻

## End-to-end test team (borrows Lambeth's details)
GOV_UK_PAY_SECRET_E2E=👻
GOV_UK_PAY_TOKEN_E2E=👻
3 changes: 3 additions & 0 deletions api.planx.uk/modules/pay/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const makePaymentViaProxy: PaymentProxyController = async (
),
},
req,
res,
)(req, res, next);
};

Expand Down Expand Up @@ -101,6 +102,7 @@ export const makeInviteToPayPaymentViaProxy: PaymentRequestProxyController = (
}),
},
req,
res,
)(req, res, next);
};

Expand Down Expand Up @@ -151,6 +153,7 @@ export function fetchPaymentViaProxyWithCallback(
}),
},
req,
res,
)(req, res, next);
};
}
Expand Down
5 changes: 5 additions & 0 deletions api.planx.uk/modules/pay/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ jest.mock("@opensystemslab/planx-core", () => {
session: {
findDetails: jest.fn().mockImplementation(() => ({ lockedAt: null })),
},
team: {
getIntegrations: jest
.fn()
.mockImplementation(() => ({ govPayToken: "abc123" })),
},
})),
};
});
Expand Down
17 changes: 12 additions & 5 deletions api.planx.uk/modules/pay/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import { ServerError } from "../../errors";

/**
* Confirm that this local authority (aka team) has a pay token
* TODO: Check this against a DB value instead of env vars?
*/
export const isTeamUsingGovPay: RequestHandler = (req, _res, next) => {
const isSupported =
process.env[`GOV_UK_PAY_TOKEN_${req.params.localAuthority.toUpperCase()}`];
export const isTeamUsingGovPay: RequestHandler = async (req, res, next) => {
const env =
process.env.APP_ENVIRONMENT === "production" ? "production" : "staging";

if (!isSupported) {
const { govPayToken } = await $api.team.getIntegrations({
env,
slug: req.params.localAuthority,
encryptionKey: process.env.ENCRYPTION_KEY!,
});

if (!govPayToken) {
return next({
status: 400,
message: `GOV.UK Pay is not enabled for this local authority (${req.params.localAuthority})`,
});
}

res.locals.govPayToken = govPayToken;

next();
};

Expand Down
14 changes: 7 additions & 7 deletions api.planx.uk/modules/pay/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Request } from "express";
import { Response, Request } from "express";
import { fixRequestBody, Options } from "http-proxy-middleware";
import { useProxy } from "../../shared/middleware/proxy";

export const usePayProxy = (options: Partial<Options>, req: Request) => {
export const usePayProxy = (
options: Partial<Options>,
req: Request,
res: Response,
) => {
return useProxy({
target: "https://publicapi.payments.service.gov.uk/v1/payments",
onProxyReq: fixRequestBody,
headers: {
...(req.headers as NodeJS.Dict<string | string[]>),
"content-type": "application/json",
Authorization: `Bearer ${
process.env[
`GOV_UK_PAY_TOKEN_${req.params.localAuthority}`.toUpperCase()
]
}`,
Authorization: `Bearer ${res.locals.govPayToken}`,
},
...options,
});
Expand Down
54 changes: 0 additions & 54 deletions api.planx.uk/tests/serverErrorHandler.test.js

This file was deleted.

28 changes: 28 additions & 0 deletions e2e/tests/ui-driven/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export async function setUpTestContext(
publisherId: context.user!.id!,
});
}
await setupGovPaySecret($admin, context);

return context;
}

Expand Down Expand Up @@ -327,3 +329,29 @@ async function deleteTeam(adminGQLClient: GraphQLClient, context: Context) {
}
}
}

async function setupGovPaySecret($admin: CoreDomainClient, context: Context) {
try {
await $admin.client.request(
gql`
mutation SetupGovPaySecret(
$team_id: Int
$staging_govpay_secret: String
) {
update_team_integrations(
where: { team_id: { _eq: $team_id } }
_set: { staging_govpay_secret: $staging_govpay_secret }
) {
affected_rows
}
}
`,
{
team_id: context.team.id,
staging_govpay_secret: process.env.GOV_UK_PAY_SECRET_E2E,
},
);
} catch (error) {
throw Error("Failed to setup GovPay secret for E2E team");
}
}

0 comments on commit 38bab63

Please sign in to comment.