Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DTFS2-7223): ensure registration number regex only accepts 8-character strings #943

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading