Skip to content

Commit

Permalink
feat: permit more names in frontend NAMES_REGEX, update tests
Browse files Browse the repository at this point in the history
refs YJDH-661
  • Loading branch information
karisal-anders committed Feb 13, 2024
1 parent a21bdab commit 5c068b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
17 changes: 9 additions & 8 deletions frontend/kesaseteli/youth/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ describe('frontend/kesaseteli/youth/src/pages/index.tsx', () => {
const indexPageApi = getIndexPageApi();
await indexPageApi.expectations.pageIsLoaded();

await indexPageApi.actions.typeInput('first_name', '!#$%&()*+/:;<=>?@');
await indexPageApi.actions.typeInput('last_name', '~¡¿÷ˆ]+$');
await indexPageApi.actions.typeInput('first_name', ' leading whitespace');
await indexPageApi.actions.typeInput('last_name', '\tleading whitespace');
// Note! 170915-915L is a fake ssn. See more info (in finnish only):
// https://www.tuomas.salste.net/doc/tunnus/henkilotunnus.html#keinotunnus
await indexPageApi.actions.typeInput(
Expand Down Expand Up @@ -184,10 +184,7 @@ describe('frontend/kesaseteli/youth/src/pages/index.tsx', () => {
'maxLength'
);

await indexPageApi.actions.typeInput(
'unlistedSchool',
'!#$%&()*+/:;<=>?@'
);
await indexPageApi.actions.typeInput('unlistedSchool', ' ');
await indexPageApi.expectations.textInputHasError(
'unlistedSchool',
'pattern'
Expand Down Expand Up @@ -306,7 +303,9 @@ describe('frontend/kesaseteli/youth/src/pages/index.tsx', () => {
),
});
await waitFor(() =>
expect(spyPush).toHaveBeenCalledWith(`${DEFAULT_LANGUAGE}/${errorType}`)
expect(spyPush).toHaveBeenCalledWith(
`${DEFAULT_LANGUAGE}/${errorType}`
)
);
});
}
Expand Down Expand Up @@ -442,7 +441,9 @@ describe('frontend/kesaseteli/youth/src/pages/index.tsx', () => {
),
});
await waitFor(() =>
expect(spyPush).toHaveBeenCalledWith(`${DEFAULT_LANGUAGE}/${errorType}`)
expect(spyPush).toHaveBeenCalledWith(
`${DEFAULT_LANGUAGE}/${errorType}`
)
);
});
}
Expand Down
67 changes: 34 additions & 33 deletions frontend/shared/src/__tests__/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,49 @@ import {
describe('constants', () => {
describe('regex', () => {
describe('NAMES_REGEX', () => {
it('should match Finnish first names, last names and full names', () => {
const firstName = 'Helinä';
const lastName = 'Aalto';
const fullName = 'Kalle Väyrynen';
const fullName2 = 'Janne Ö';

expect(firstName).toMatch(NAMES_REGEX);
expect(lastName).toMatch(NAMES_REGEX);
expect(fullName).toMatch(NAMES_REGEX);
expect(fullName2).toMatch(NAMES_REGEX);
});

it('should match Swedish first names, last names and full names', () => {
const firstName = 'Gun-Britt';
const lastName = 'Lindén';
const fullName = 'Ögge Ekström';

expect(firstName).toMatch(NAMES_REGEX);
expect(lastName).toMatch(NAMES_REGEX);
expect(fullName).toMatch(NAMES_REGEX);
});

it('should match English first names, last names and full names', () => {
const firstName = 'Eric';
const lastName = 'Bradtke';
const fullName = "Daniela O'Brian";
it('should match names in many different languages', () => {
const names = [
"Daniela O'Brian",
'Aalto',
'Bradtke',
'Eric',
'Gun-Britt',
'Helinä',
'Ingólfur Álfheiður',
'Janne Ö',
'Kalle Väyrynen',
'Lindén',
'María Kristín Þorkelsdóttir',
'Matti Meikäläinen',
'Peña',
'Strauß Jünemann',
'Åse-Marie Öllegård',
'Õras',
'Ögge Ekström',
'Ümit',
'Мельник',
'אברהם',
'حَسَّان',
'อาทิตย์',
'慧芬',
'王',
];

expect(firstName).toMatch(NAMES_REGEX);
expect(lastName).toMatch(NAMES_REGEX);
expect(fullName).toMatch(NAMES_REGEX);
names.forEach((name) => {
expect(name).toMatch(NAMES_REGEX);
});
});

it('should fail to match invalid characters', () => {
it('should match special characters', () => {
const invalidCharacters = '!@#$%^&*()_+-=[]{}|;\':",./<>?';

expect(invalidCharacters).not.toMatch(NAMES_REGEX);
expect(invalidCharacters).toMatch(NAMES_REGEX);
});

it('should fail to match digits', () => {
it('should match digits', () => {
const digits = '1234567890';

expect(digits).not.toMatch(NAMES_REGEX);
expect(digits).toMatch(NAMES_REGEX);
});
});

Expand Down
3 changes: 1 addition & 2 deletions frontend/shared/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export const PHONE_NUMBER_REGEX =
// eslint-disable-next-line security/detect-unsafe-regex
/^((\+358[ -]*)+|(\\(\d{2,3}\\)[ -]*)|(\d{2,4})[ -]*)*?\d{3,4}?[ -]*\d{3,4}?$/;
export const POSTAL_CODE_REGEX = /^\d{5}$/;
export const NAMES_REGEX =
/^[\w',.ÄÅÖäåö-][^\d!#$%&()*+/:;<=>?@[\\\]_{|}~¡¿÷ˆ]+$/;
export const NAMES_REGEX = /^\S.*$/;
export const CITY_REGEX = /^[ A-Za-zÄÅÖäåö-]+$/;

export const EMAIL_REGEX =
Expand Down

0 comments on commit 5c068b8

Please sign in to comment.