Skip to content

Commit

Permalink
Merge pull request #2369 from govuk-one-login/BAU/remove-chai-as-prom…
Browse files Browse the repository at this point in the history
…ised

BAU: Remove Chai As Promised Dependency
  • Loading branch information
BeckaL authored Dec 2, 2024
2 parents 7ef84f0 + e4803ba commit e276140
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 136 deletions.
10 changes: 7 additions & 3 deletions dev-check-environment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
chai.config.truncateThreshold = 0;
const { expect } = chai;

Expand Down Expand Up @@ -40,7 +38,13 @@ describe("environment", function () {

it("should be importable by jose", async function () {
if (key === undefined) return this.skip();
await expect(jose.importSPKI(key, "ES256"), "jose could not import the PEM. Other failing tests may help you identify why").to.not.be.rejected;
try {
await jose.importSPKI(key, "ES256");
} catch (error) {
expect.fail(
`jose could not import the PEM. Other failing tests may help you identify why. Message: ${error.message}`
);
}
});

it("should not contain '\\n'", function () {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
},
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/cheerio": "^0.22.31",
"@types/cookie-parser": "^1.4.2",
"@types/csurf": "^1.11.2",
Expand All @@ -128,7 +127,6 @@
"@typescript-eslint/eslint-plugin": "^8.13.0",
"@typescript-eslint/parser": "^8.13.0",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"chai-http": "^5.1.1",
"cheerio": "^1.0.0-rc.10",
"concurrently": "^9.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sendEmailOtp } from "../send-email-otp-middleware";
import { SendNotificationServiceInterface } from "../../../common/send-notification/types";
import { BadRequestError } from "../../../../utils/error";
import { ERROR_CODES } from "../../../common/constants";
import { strict as assert } from "assert";

describe("sendEmailOTPMiddleware", () => {
let req: Partial<Request>;
Expand Down Expand Up @@ -79,13 +80,16 @@ describe("sendEmailOTPMiddleware", () => {
}),
} as unknown as SendNotificationServiceInterface;

await expect(
sendEmailOtp(fakeNotificationService)(
req as Request,
res as Response,
next as NextFunction
)
).to.be.rejectedWith(BadRequestError, "999999999999999:test error message");
await assert.rejects(
async () =>
sendEmailOtp(fakeNotificationService)(
req as Request,
res as Response,
next as NextFunction
),
BadRequestError,
"999999999999999:test error message"
);

expect(next).to.not.be.called;
});
Expand Down
126 changes: 74 additions & 52 deletions src/components/authorize/tests/authorize-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,17 @@ describe("authorize controller", () => {
}),
} as unknown as AuthorizeServiceInterface;

await expect(
authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response)
)
.to.eventually.be.rejectedWith("1000:Session-Id is missing or invalid")
.and.be.an.instanceOf(BadRequestError)
.and.have.property("level", "Info");
await assertBadRequestErrorThrownWithErrorMessage(
async () =>
await authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response),
"1000:Session-Id is missing or invalid",
"Info"
);
});

it("should throw an error without level property if the authorize service returns a code 1001 Request is missing parameters", async () => {
Expand All @@ -392,17 +392,16 @@ describe("authorize controller", () => {
}),
} as unknown as AuthorizeServiceInterface;

await expect(
authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response)
)
.to.eventually.be.rejectedWith("1001:Request is missing parameters")
.and.be.an.instanceOf(BadRequestError)
.and.not.to.have.property("level");
await assertBadRequestErrorThrownWithErrorMessage(
async () =>
await authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response),
"1001:Request is missing parameters"
);
});

it("should set session fields from jwt claims", async () => {
Expand Down Expand Up @@ -515,46 +514,46 @@ describe("authorize controller", () => {
it("should throw an error if response_type does not exist in the query params", async () => {
delete req.query.response_type;

await expect(
authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response)
)
.to.eventually.be.rejectedWith("Response type is not set")
.and.be.an.instanceOf(BadRequestError);
await assertBadRequestErrorThrownWithErrorMessage(
async () =>
await authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response),
"Response type is not set"
);
});

it("should throw an error if response_type is null in the query params", async () => {
req.query.response_type = null as unknown as string;

await expect(
authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response)
)
.to.eventually.be.rejectedWith("Response type is not set")
.and.be.an.instanceOf(BadRequestError);
await assertBadRequestErrorThrownWithErrorMessage(
async () =>
await authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response),
"Response type is not set"
);
});

it("should throw an error if client_id value is incorrect in the query params", async () => {
req.query.client_id = "wrong_client id";

await expect(
authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response)
)
.to.eventually.be.rejectedWith("Client ID value is incorrect")
.and.be.an.instanceOf(BadRequestError);
await assertBadRequestErrorThrownWithErrorMessage(
async () =>
await authorizeGet(
fakeAuthorizeService,
fakeCookieConsentService,
fakeKmsDecryptionService,
fakeJwtService
)(req as Request, res as Response),
"Client ID value is incorrect"
);
});

it("should set session channel session field from jwt claims when claim is present", async () => {
Expand Down Expand Up @@ -612,4 +611,27 @@ describe("authorize controller", () => {
success: true,
};
}

async function assertBadRequestErrorThrownWithErrorMessage(
fn: () => Promise<any>,
errorMessage: string,
level?: string
) {
let caughtError;

try {
await fn();
} catch (error) {
caughtError = error;
}

expect(caughtError).to.exist; // Ensure an error was thrown
expect(caughtError).to.be.an.instanceOf(BadRequestError);
if (level) {
expect(caughtError).to.have.property("level", level);
} else {
expect(caughtError).not.to.have.property("level");
}
expect(caughtError.message).to.equal(errorMessage);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CreatePasswordServiceInterface } from "../types";
import { PATH_NAMES } from "../../../app.constants";
import { mockResponse, RequestOutput, ResponseOutput } from "mock-req-res";
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";
import { strict as assert } from "assert";

describe("create-password controller", () => {
let req: RequestOutput;
Expand Down Expand Up @@ -58,9 +59,9 @@ describe("create-password controller", () => {
req.body.password = "password1";
req.session.user = undefined;

await expect(
createPasswordPost(fakeService)(req as Request, res as Response)
).to.be.rejectedWith(
await assert.rejects(
async () =>
createPasswordPost(fakeService)(req as Request, res as Response),
TypeError,
"Cannot read properties of undefined (reading 'email')"
);
Expand All @@ -75,9 +76,9 @@ describe("create-password controller", () => {
req.body = undefined;
req.session.user.email = "[email protected]";

await expect(
createPasswordPost(fakeService)(req as Request, res as Response)
).to.be.rejectedWith(
await assert.rejects(
async () =>
createPasswordPost(fakeService)(req as Request, res as Response),
TypeError,
"Cannot read properties of undefined (reading 'password')"
);
Expand All @@ -93,9 +94,12 @@ describe("create-password controller", () => {
req.body.password = "password1";
req.session.user.email = "[email protected]";

await expect(
createPasswordPost(fakeService)(req as Request, res as Response)
).to.be.rejectedWith(Error, "Internal server error");
await assert.rejects(
async () =>
createPasswordPost(fakeService)(req as Request, res as Response),
Error,
"Internal server error"
);
expect(fakeService.signUpUser).to.have.been.called;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mockResponse, RequestOutput, ResponseOutput } from "mock-req-res";
import { PATH_NAMES } from "../../../app.constants";
import { docCheckingAppGet } from "../doc-checking-app-controller";
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";
import { strict as assert } from "assert";

describe("doc checking app controller", () => {
let req: RequestOutput;
Expand Down Expand Up @@ -51,9 +52,12 @@ describe("doc checking app controller", () => {
}),
} as unknown as DocCheckingAppInterface;

await expect(
docCheckingAppGet(fakeService)(req as Request, res as Response)
).to.be.rejectedWith("1222:Error occurred");
await assert.rejects(
async () =>
docCheckingAppGet(fakeService)(req as Request, res as Response),
Error,
"1222:Error occurred"
);
});
});
});
16 changes: 10 additions & 6 deletions src/components/enter-email/tests/enter-email-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { mockResponse, RequestOutput, ResponseOutput } from "mock-req-res";
import { CheckReauthServiceInterface } from "../../check-reauth-users/types";
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";
import { commonVariables } from "../../../../test/helpers/common-test-variables";
import { strict as assert } from "assert";

describe("enter email controller", () => {
let req: RequestOutput;
Expand Down Expand Up @@ -206,9 +207,12 @@ describe("enter email controller", () => {
userExists: sinon.fake.throws(error),
};

await expect(
enterEmailPost(fakeService)(req as Request, res as Response)
).to.be.rejectedWith(Error, "Internal server error");
await assert.rejects(
async () =>
enterEmailPost(fakeService)(req as Request, res as Response),
Error,
"Internal server error"
);
expect(fakeService.userExists).to.have.been.calledOnce;
});

Expand All @@ -219,9 +223,9 @@ describe("enter email controller", () => {

req.session.user = undefined;

await expect(
enterEmailPost(fakeService)(req as Request, res as Response)
).to.be.rejectedWith(
await assert.rejects(
async () =>
enterEmailPost(fakeService)(req as Request, res as Response),
TypeError,
"Cannot set properties of undefined (setting 'email')"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { accountInterventionsFakeHelper } from "../../../../test/helpers/account
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";
import { commonVariables } from "../../../../test/helpers/common-test-variables";
import { ReauthJourneyError } from "../../../utils/error";
import { strict as assert } from "assert";

describe("enter password controller", () => {
let req: RequestOutput;
Expand Down Expand Up @@ -410,13 +411,16 @@ describe("enter password controller", () => {
sendMfaCode: sinon.fake(),
};

await expect(
enterPasswordPost(
false,
fakeService,
fakeMfaService
)(req as Request, res as Response)
).to.be.rejectedWith(Error, "Internal server error");
await assert.rejects(
async () =>
enterPasswordPost(
false,
fakeService,
fakeMfaService
)(req as Request, res as Response),
Error,
"Internal server error"
);
expect(fakeService.loginUser).to.have.been.calledOnce;
});

Expand Down
Loading

0 comments on commit e276140

Please sign in to comment.