Skip to content

Commit

Permalink
INCIDEN-922: Pass access token to delete entries methods [deploy]
Browse files Browse the repository at this point in the history
We're now expecting an access token on the backend, so we
need to start passing it through here not to break any existing
functionality.
  • Loading branch information
Ryan-Andrews99 committed Sep 18, 2024
1 parent 73437b0 commit 9a0a7bc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions express/src/controllers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export const processAddServiceForm: RequestHandler = async (req, res, next) => {

let serviceId = "";
let body;

const accessToken = nonNull(req.session.authenticationResult?.AccessToken);
try {
console.info("Generating Client to Service:" + service.serviceName);
const generatedClient = await s4.generateClient(service, nonNull(req.session.authenticationResult));
Expand All @@ -353,7 +353,7 @@ export const processAddServiceForm: RequestHandler = async (req, res, next) => {
} catch (error) {
console.error("Unable to Register Client to Service - Service Items removed");
console.error(error);
await s4.deleteServiceEntries(uuid);
await s4.deleteServiceEntries(uuid, accessToken);
return res.render("there-is-a-problem.njk");
}

Expand Down
8 changes: 4 additions & 4 deletions express/src/services/lambda-facade/LambdaFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class LambdaFacade implements LambdaFacadeInterface {
}
}

async deleteClientEntries(userID: string, serviceID: string): Promise<void> {
async deleteClientEntries(userID: string, serviceID: string, accessToken: string): Promise<void> {
console.log("In LambdaFacade-deleteClientEntries");

const body = {
Expand All @@ -162,22 +162,22 @@ export default class LambdaFacade implements LambdaFacadeInterface {
};

try {
await this.post("/delete-dynamodb-client-entries/", JSON.stringify(body));
await this.post("/delete-dynamodb-client-entries/", JSON.stringify(body), accessToken);
} catch (error) {
console.error(error as Error);
throw error;
}
}

async deleteServiceEntries(serviceID: string): Promise<void> {
async deleteServiceEntries(serviceID: string, accessToken: string): Promise<void> {
console.log("In LambdaFacade-deleteServiceEntries");

const body = {
serviceId: serviceID
};

try {
await this.post("/delete-dynamodb-service-entries/", JSON.stringify(body));
await this.post("/delete-dynamodb-service-entries/", JSON.stringify(body), accessToken);
} catch (error) {
console.error(error as Error);
throw error;
Expand Down
4 changes: 2 additions & 2 deletions express/src/services/lambda-facade/LambdaFacadeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export default interface LambdaFacadeInterface {

getDynamoDBEntries(email: string): Promise<AxiosResponse>;

deleteClientEntries(oldUserID: string, serviceID: string): Promise<void>;
deleteServiceEntries(serviceID: string): Promise<void>;
deleteClientEntries(oldUserID: string, serviceID: string, accessToken: string): Promise<void>;
deleteServiceEntries(serviceID: string, accessToken: string): Promise<void>;
}
14 changes: 7 additions & 7 deletions express/src/services/self-service-services-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,15 @@ export default class SelfServiceServicesService {

async recreateDynamoDBAccountLinks(authenticationResult: AuthenticationResultType, oldUserID: string) {
console.info("In self-service-services-service:createNewDynamoDBAccountLinks()");
const accessToken = nonNull(authenticationResult.AccessToken);
await this.validateToken(accessToken, "recreateDynamoDBAccountLinks")

Check failure on line 366 in express/src/services/self-service-services-service.ts

View workflow job for this annotation

GitHub Actions / Linting

Insert `;`

const serviceListToReplicate = dynamoServicesToDomainServices(
(await this.lambda.listServices(oldUserID, nonNull(authenticationResult.AccessToken))).data.Items
);
const serviceListToReplicate = dynamoServicesToDomainServices((await this.lambda.listServices(oldUserID, accessToken)).data.Items);

for (const serviceItem of serviceListToReplicate) {
const serviceID: string = serviceItem.id;
const serviceName: string = serviceItem.serviceName;
const currentUserID = AuthenticationResultParser.getCognitoId(authenticationResult);
const accessToken: string = authenticationResult.AccessToken as string;
const emailAddress = AuthenticationResultParser.getEmail(authenticationResult);
const mobileNumber = AuthenticationResultParser.getPhoneNumber(authenticationResult);

Expand All @@ -392,13 +391,14 @@ export default class SelfServiceServicesService {

await this.putUser(dynamoUser, accessToken);
await this.newService(service, currentUserID, authenticationResult);
await this.lambda.deleteClientEntries(oldUserID, serviceID);
await this.lambda.deleteClientEntries(oldUserID, serviceID, accessToken);

Check failure on line 394 in express/src/services/self-service-services-service.ts

View workflow job for this annotation

GitHub Actions / Linting

Insert `····`
}
}

async deleteServiceEntries(serviceID: string) {
async deleteServiceEntries(serviceID: string, accessToken: string) {
console.info("In self-service-services-service:deleteServiceEntries()");
console.log("Service ID => " + serviceID);
await this.lambda.deleteServiceEntries(serviceID);
await this.validateToken(accessToken, "deleteServiceEntries")

Check failure on line 401 in express/src/services/self-service-services-service.ts

View workflow job for this annotation

GitHub Actions / Linting

Insert `;`
await this.lambda.deleteServiceEntries(serviceID, accessToken);
}
}
3 changes: 2 additions & 1 deletion express/tests/controllers/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "../../src/controllers/register";
import {request, response} from "../mocks";
import {
TEST_ACCESS_TOKEN,
TEST_AUTHENTICATION_RESULT,
TEST_COGNITO_ID,
TEST_COGNITO_SESSION_STRING,
Expand Down Expand Up @@ -792,7 +793,7 @@ describe("processAddServiceForm controller tests", () => {
{id: `service#${TEST_UUID}`, serviceName: TEST_SERVICE_NAME},
TEST_AUTHENTICATION_RESULT
);
expect(s4DeleteServiceEntriesSpy).toHaveBeenCalledWith(TEST_UUID);
expect(s4DeleteServiceEntriesSpy).toHaveBeenCalledWith(TEST_UUID, TEST_ACCESS_TOKEN);
expect(mockRes.render).toHaveBeenCalledWith("there-is-a-problem.njk");
});

Expand Down
18 changes: 14 additions & 4 deletions express/tests/services/lambda-facade/LambdaFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,36 @@ describe("Lambda Facade class tests", () => {

it("POSTs the /delete-dynamodb-client-entries when the deleteClientEntries method is called", async () => {
const mockLambdaFacade = new LambdaFacade();
await mockLambdaFacade.deleteClientEntries(TEST_COGNITO_ID, TEST_SERVICE_ID);
await mockLambdaFacade.deleteClientEntries(TEST_COGNITO_ID, TEST_SERVICE_ID, TEST_ACCESS_TOKEN);

expect(mockPost).toHaveBeenCalledWith(
"/delete-dynamodb-client-entries/",
JSON.stringify({
userId: TEST_COGNITO_ID,
serviceId: TEST_SERVICE_ID
})
}),
{
headers: {
Authorization: `Bearer ${TEST_ACCESS_TOKEN}`
}
}
);
});

it("POSTs the /delete-dynamodb-service-entries when the deleteServiceEntries method is called", async () => {
const mockLambdaFacade = new LambdaFacade();
await mockLambdaFacade.deleteServiceEntries(TEST_SERVICE_ID);
await mockLambdaFacade.deleteServiceEntries(TEST_SERVICE_ID, TEST_ACCESS_TOKEN);

expect(mockPost).toHaveBeenCalledWith(
"/delete-dynamodb-service-entries/",
JSON.stringify({
serviceId: TEST_SERVICE_ID
})
}),
{
headers: {
Authorization: `Bearer ${TEST_ACCESS_TOKEN}`
}
}
);
});

Expand Down

0 comments on commit 9a0a7bc

Please sign in to comment.