Skip to content

Commit

Permalink
ATO-1237: update consumer test for client registry
Browse files Browse the repository at this point in the history
 - re-write tests so that it calls the relevant handler which does the api call
 - updated the request and response bodys
 - not included error test has handler don't expect specificc errors and handle it generically
  • Loading branch information
isaac-GDS committed Jan 14, 2025
1 parent 465dc2f commit 3acdc19
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
2 changes: 1 addition & 1 deletion backend/api/src/handlers/auth/register-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type RegisterClientPayload = {
contactEmail: string;
};

const public_key =
export const public_key =
"MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAp2mLkQGo24Kz1rut0oZlviMkGomlQCH+iT1pFvegZFXq39NPjRWyatmXp/XIUPqCq9Kk8/+tq4Sgjw+EM5tATJ06j5r+35of58ATGVPniW//IhGizrv6/ebGcGEUJ0Y/ZmlCHYPV+lbewpttQ/IYKM1nr3k/Rl6qepbVYe+MpGubluQvdhgUYel9OzxiOvUk7XI0axPquiXzoEgmNNOai8+WhYTkBqE3/OucAv+XwXdnx4XHmKzMwTv93dYMpUmvTxWcSeEJ/4/SrbiK4PyHWVKU2BozfSUejVNhahAzZeyyDwhYJmhBaZi/3eOOlqGXj9UdkOXbl3vcwBH8wD30O9/4F5ERLKxzOaMnKZ+RpnygWF0qFhf+UeFMy+O06sdgiaFnXaSCsIy/SohspkKiLjNnhvrDNmPLMQbQKQlJdcp6zUzI7Gzys7luEmOxyMpA32lDBQcjL7KNwM15s4ytfrJ46XEPZUXESce2gj6NazcPPsrTa/Q2+oLS9GWupGh7AgMBAAE=";

// Handler is invoked by step function not API Gateway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,15 @@

import {MatchersV3, PactV3} from "@pact-foundation/pact";
import * as path from "path";
import axios from "axios";
import {Agent} from "node:http";
import {public_key, registerClientHandler, RegisterClientPayload} from "../../../src/handlers/auth/register-client";
import {clientRegistryResponse} from "../../../src/handlers/types/client-registry-response";
import {mockLambdaContext} from "../../handlers/utils";
import {updateClientInRegistryHandler, UpdateClientPayload} from "../../../src/handlers/auth/update-client";

beforeAll((): void => {
jest.setTimeout(200000);
});

const client = axios.create({
httpAgent: new Agent()
});

const EXPECTED_BODY =
"{\n" +
'"client_name": "My test service",\n' +
'"public_key": "1234567890",\n' +
'"redirect_uris": ["http://localhost/"],\n' +
'"contacts": ["[email protected]"],\n' +
'"scopes": ["openid", "email", "phone"],\n' +
'"subject_type": "pairwise",\n' +
'"service_type": "MANDATORY",\n' +
'"sector_identifier_uri": "http://gov.uk"\n' +
"}";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const postRequest = async (url, data): string => {
const postUrl = `${url}/connect/register`;
const headers = {headers: {responseType: "json", "Content-Type": "application/json; charset=utf-8"}};
console.log("********** postRequest sending to :" + postUrl);
return await client
.post(postUrl, data, headers)
.then(res => {
console.log("********** postRequest returning : " + res.data.toString());
return res.data.toString();
})
.catch(err => {
console.log("********** postRequest error : " + err.toString());
return "ERROR: " + err.toString();
});
};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const putRequest = async (url, data): string => {
const postUrl = `${url}/connect/register`;
const headers = {headers: {responseType: "json", "Content-Type": "application/json; charset=utf-8"}};
console.log("********** putRequest sending to :" + postUrl);
return await client
.put(postUrl, data, headers)
.then(res => {
console.log("********** putRequest returning : " + res.data.toString());
return res.data.toString();
})
.catch(err => {
console.log("********** putRequest error : " + err.toString());
return "ERROR: " + err.toString();
});
};

describe("ClientRegistryProvider", () => {
const {eachLike} = MatchersV3;

Expand All @@ -73,61 +23,121 @@ describe("ClientRegistryProvider", () => {
});

describe("When a POST request is made to create a client", () => {
test("it should return a message", async () => {
const postEvent: RegisterClientPayload = {
contactEmail: "[email protected]",
service: {
serviceName: "My test service",
id: "service#testRandomId"
}
};

const client_config = {
client_name: postEvent.service.serviceName,
public_key: public_key,
redirect_uris: ["http://localhost/"],
contacts: [postEvent.contactEmail],
scopes: ["openid", "email", "phone"],
subject_type: "pairwise",
service_type: "MANDATORY",
sector_identifier_uri: "http://gov.uk",
client_locs: ["P2"]
};

const {client_locs, ...expected_returned_values} = client_config;

test("it should return an a client registry object when adding a client", async () => {
provider
.uponReceiving("add a Client")
.uponReceiving("configuration to add a client")
.withRequest({
method: "POST",
path: "/connect/register",
contentType: "application/json",
headers: {
"Content-Type": "application/json; charset=utf-8"
"Content-Type": "application/json"
},
body: EXPECTED_BODY
body: JSON.stringify(client_config)
})
.willRespondWith({
status: 200,
body: eachLike(EXPECTED_BODY),
body: eachLike(expected_returned_values),
contentType: "application/json",
headers: {
"Content-Type": "application/json; charset=utf-8"
"Content-Type": "application/json"
}
});

await provider.executeTest(async mockProvider => {
const [results] = await Promise.all([postRequest(mockProvider.url, EXPECTED_BODY)]);
console.log("RESULTS: " + JSON.stringify(results));
expect(results).toContain(EXPECTED_BODY);
process.env.AUTH_REGISTRATION_BASE_URL = mockProvider.url;
const results = await registerClientHandler(postEvent, mockLambdaContext);
expect(JSON.parse(results.body)).toMatchObject(expected_returned_values);
});
});
});

describe("When a PUT request is made to update a client", () => {
test("it should return a message", async () => {
const updatesForClient = {
contacts: ["[email protected]"],
subject_type: "pairwise"
};

const putEvent: UpdateClientPayload = {
clientId: "testClientId",
serviceId: "testServiceId",
selfServiceClientId: "testSelfServiceClientId",
updates: {
...updatesForClient
}
};

const expected_response: clientRegistryResponse = {
client_name: "testClientUpdateResponseName",
public_key: "testClientPublicKey",
public_key_source: "STATIC",
redirect_uris: ["http://testClientUrl"],
contacts: updatesForClient.contacts,
scopes: ["openid", "email"],
subject_type: updatesForClient.subject_type,
service_type: "MANDATORY",
sector_identifier_uri: "http://gov.uk",
client_id: "testClientId",
post_logout_redirect_uris: [],
back_channel_logout_uri: null,
token_endpoint_auth_method: "private_key_jwt",
response_type: "code",
jar_validation_required: false,
claims: [],
client_type: "web",
id_token_signing_algorithm: "ES256",
jwks_uri: null,
channel: "WEB",
max_age_enabled: false
};

test("it should return an a client registry object when updating a client", async () => {
provider
.uponReceiving("update a Client")
.uponReceiving("configuration to update a client")
.withRequest({
method: "PUT",
path: "/connect/register",
path: `/connect/register/${putEvent.clientId}`,
contentType: "application/json",
headers: {
"Content-Type": "application/json; charset=utf-8"
"Content-Type": "application/json"
},
body: EXPECTED_BODY
body: updatesForClient
})
.willRespondWith({
status: 200,
body: eachLike(EXPECTED_BODY),
body: eachLike(expected_response),
contentType: "application/json",
headers: {
"Content-Type": "application/json; charset=utf-8"
"Content-Type": "application/json"
}
});

await provider.executeTest(async mockProvider => {
const [results] = await Promise.all([putRequest(mockProvider.url, EXPECTED_BODY)]);
console.log("RESULTS: " + JSON.stringify(JSON.stringify(results)));
expect(results).toContain(EXPECTED_BODY);
process.env.AUTH_REGISTRATION_BASE_URL = mockProvider.url;
const results = await updateClientInRegistryHandler(putEvent, mockLambdaContext);
expect(JSON.parse(results.body)[0]).toMatchObject(expected_response);
});
});
});
Expand Down

0 comments on commit 3acdc19

Please sign in to comment.