Skip to content

Commit

Permalink
fix(DTFS2-7223): ensure registration number regex only accepts 8-char…
Browse files Browse the repository at this point in the history
…acter strings (#943)
  • Loading branch information
oscar-richardson-softwire authored Jun 12, 2024
1 parent b4e9683 commit 18ba1c5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/constants/companies.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const COMPANIES = {
},
REGEX: {
// This Companies House registration number regex was copied from the DTFS codebase.
COMPANIES_HOUSE_REGISTRATION_NUMBER: /^(([A-Z]{2}|[A-Z]\d|\d{2})(\d{5,6}|\d{4,5}[A-Z]))$/,
COMPANIES_HOUSE_REGISTRATION_NUMBER: /^(([A-Z]{2}|[A-Z]\d|\d{2})(\d{6}|\d{5}[A-Z]))$/,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export class GetCompanyByRegistrationNumberQuery {
@ApiProperty({
description: 'The Companies House registration number of the company to find.',
example: COMPANIES.EXAMPLES.COMPANIES_HOUSE_REGISTRATION_NUMBER,
minLength: 7,
minLength: 8,
maxLength: 8,
pattern: COMPANIES.REGEX.COMPANIES_HOUSE_REGISTRATION_NUMBER.source,
})
@MinLength(7)
@MinLength(8)
@MaxLength(8)
@Matches(COMPANIES.REGEX.COMPANIES_HOUSE_REGISTRATION_NUMBER)
public registrationNumber: string;
Expand Down
38 changes: 23 additions & 15 deletions test/companies/get-company-by-registration-number.api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,43 @@ describe('GET /companies?registrationNumber=', () => {

it.each([
{
registrationNumber: valueGenerator.stringOfNumericCharacters({ length: 6 }),
validationError: 'registrationNumber must be longer than or equal to 7 characters',
descriptionForTestName: 'too short',
registrationNumber: valueGenerator.stringOfNumericCharacters({ length: 7 }),
validationError: 'registrationNumber must be longer than or equal to 8 characters',
},
{
descriptionForTestName: 'too long',
registrationNumber: valueGenerator.stringOfNumericCharacters({ length: 9 }),
validationError: 'registrationNumber must be shorter than or equal to 8 characters',
},
{
descriptionForTestName: 'in the wrong format',
registrationNumber: '0A000001',
validationError: 'registrationNumber must match /^(([A-Z]{2}|[A-Z]\\d|\\d{2})(\\d{5,6}|\\d{4,5}[A-Z]))$/ regular expression',
validationError: 'registrationNumber must match /^(([A-Z]{2}|[A-Z]\\d|\\d{2})(\\d{6}|\\d{5}[A-Z]))$/ regular expression',
},
{
descriptionForTestName: 'the empty string',
registrationNumber: '',
validationError: 'registrationNumber must be longer than or equal to 7 characters',
validationError: 'registrationNumber must be longer than or equal to 8 characters',
},
{
descriptionForTestName: 'all spaces',
registrationNumber: ' ',
validationError: 'registrationNumber must match /^(([A-Z]{2}|[A-Z]\\d|\\d{2})(\\d{5,6}|\\d{4,5}[A-Z]))$/ regular expression',
validationError: 'registrationNumber must match /^(([A-Z]{2}|[A-Z]\\d|\\d{2})(\\d{6}|\\d{5}[A-Z]))$/ regular expression',
},
])(`returns a 400 response with validation errors if postcode is '$registrationNumber'`, async ({ registrationNumber, validationError }) => {
const { status, body } = await api.get(getMdmPath(registrationNumber));

expect(status).toBe(400);
expect(body).toMatchObject({
error: 'Bad Request',
message: expect.arrayContaining([validationError]),
statusCode: 400,
});
});
])(
'returns a 400 response with the correct validation errors if the registration number is $descriptionForTestName',
async ({ registrationNumber, validationError }) => {
const { status, body } = await api.get(getMdmPath(registrationNumber));

expect(status).toBe(400);
expect(body).toMatchObject({
error: 'Bad Request',
message: expect.arrayContaining([validationError]),
statusCode: 400,
});
},
);

it(`returns a 404 response if the Companies House API returns a 404 response containing the error string 'company-profile-not-found'`, async () => {
requestToGetCompanyByRegistrationNumber(companiesHousePath).reply(404, getCompanyCompaniesHouseNotFoundResponse);
Expand Down

0 comments on commit 18ba1c5

Please sign in to comment.