From 5c068b8656917a5da5c12bdf3877a868717179ba Mon Sep 17 00:00:00 2001 From: Kari Salminen Date: Fri, 9 Feb 2024 15:58:35 +0200 Subject: [PATCH] feat: permit more names in frontend NAMES_REGEX, update tests refs YJDH-661 --- .../youth/src/__tests__/index.test.tsx | 17 ++--- .../shared/src/__tests__/constants.test.ts | 67 ++++++++++--------- frontend/shared/src/constants.ts | 3 +- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/frontend/kesaseteli/youth/src/__tests__/index.test.tsx b/frontend/kesaseteli/youth/src/__tests__/index.test.tsx index f8c4f0f689..5c1f23740c 100644 --- a/frontend/kesaseteli/youth/src/__tests__/index.test.tsx +++ b/frontend/kesaseteli/youth/src/__tests__/index.test.tsx @@ -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( @@ -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' @@ -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}` + ) ); }); } @@ -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}` + ) ); }); } diff --git a/frontend/shared/src/__tests__/constants.test.ts b/frontend/shared/src/__tests__/constants.test.ts index 24ec09240c..03bf225a82 100644 --- a/frontend/shared/src/__tests__/constants.test.ts +++ b/frontend/shared/src/__tests__/constants.test.ts @@ -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); }); }); diff --git a/frontend/shared/src/constants.ts b/frontend/shared/src/constants.ts index da33bec812..07bfeb260b 100644 --- a/frontend/shared/src/constants.ts +++ b/frontend/shared/src/constants.ts @@ -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 =