From fecd5a48f9a29c2c9ce8d8841b33d99505ad803a Mon Sep 17 00:00:00 2001 From: Basile Spaenlehauer Date: Fri, 22 Nov 2024 13:49:24 +0100 Subject: [PATCH] fix: remove duplicated regex from member username (#712) * fix: remove duplicated regex fro member username * fix: improve tests * fix: remove warnings --- eslint.config.mjs | 1 - src/cookie/cookie.test.ts | 2 -- src/member/constants.test.ts | 46 ++++++---------------------------- src/member/constants.ts | 2 -- src/validation/isEmail.test.ts | 1 - 5 files changed, 8 insertions(+), 44 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index f8d748e6..11788c7a 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -24,7 +24,6 @@ export default [ rules: { semi: ['error', 'always'], - quotes: ['error', 'single'], '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/no-explicit-any': 'error', diff --git a/src/cookie/cookie.test.ts b/src/cookie/cookie.test.ts index ffeaa08c..34d2a97e 100644 --- a/src/cookie/cookie.test.ts +++ b/src/cookie/cookie.test.ts @@ -106,7 +106,6 @@ describe('Cookie Util Tests', () => { }); describe('getLangCookie', () => { - // eslint-disable-next-line quotes it("get user's lang in cookie", () => { const getCookieSpy = vi.spyOn(Cookies, 'get'); Cookies.set(CookieKeys.Lang, MOCK_LANG); @@ -116,7 +115,6 @@ describe('Cookie Util Tests', () => { }); describe('setLangCookie', () => { - // eslint-disable-next-line quotes it("save user's lang in cookie", () => { const mock = vi.spyOn(Cookies, 'set'); setLangCookie(MOCK_LANG, MOCK_DOMAIN); diff --git a/src/member/constants.test.ts b/src/member/constants.test.ts index 69301a77..e3e4157c 100644 --- a/src/member/constants.test.ts +++ b/src/member/constants.test.ts @@ -3,44 +3,14 @@ import { describe, expect, it } from 'vitest'; import { MemberConstants } from './constants.js'; describe('Member constants', () => { - const invalidUsername = '
'; - // prettier-ignore - const invalidUsername1 = 'user1234%66457657\''; - const invalidUsername2 = ' user\u{001A}'; - const invalidUsername3 = '\u{008F}USER'; - const validUsername = 'Johnny'; - // prettier-ignore - const validUsername1 = 'Jéàn de l\'Avoiné'; - - it('username forbidden characters regex', () => { - expect(invalidUsername).toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); - expect(invalidUsername1).toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); - expect(invalidUsername2).toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); - expect(invalidUsername3).toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); - - expect(validUsername).not.toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); - expect(validUsername1).not.toMatch( - MemberConstants.USERNAME_FORBIDDEN_CHARS_REGEX, - ); + it.each(['Johnny', "Jéàn de l'Avoiné"])('valid username', (username) => { + expect(username).toMatch(MemberConstants.USERNAME_FORMAT_REGEX); }); - it('username format regex', () => { - expect(invalidUsername).not.toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - expect(invalidUsername1).not.toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - expect(invalidUsername2).not.toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - expect(invalidUsername3).not.toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - - expect(validUsername).toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - expect(validUsername1).toMatch(MemberConstants.USERNAME_FORMAT_REGEX); - }); + it.each(['
', "user1234%66457657'", ' user\u{001A}', '\u{008F}USER'])( + 'invalid username', + (username) => { + expect(username).not.toMatch(MemberConstants.USERNAME_FORMAT_REGEX); + }, + ); }); diff --git a/src/member/constants.ts b/src/member/constants.ts index d976de35..19187831 100644 --- a/src/member/constants.ts +++ b/src/member/constants.ts @@ -1,5 +1,3 @@ export const MemberConstants = { - USERNAME_FORBIDDEN_CHARS_REGEX: - /["<>^%\\\u{0000}-\u{001F}\u{007F}-\u{009F}]+/u, USERNAME_FORMAT_REGEX: /^[^"<>^%\\\u{0000}-\u{001F}\u{007F}-\u{009F}]+$/u, }; diff --git a/src/validation/isEmail.test.ts b/src/validation/isEmail.test.ts index 5fcd4e03..a68ef437 100644 --- a/src/validation/isEmail.test.ts +++ b/src/validation/isEmail.test.ts @@ -158,7 +158,6 @@ describe('isEmail', () => { 'Some Name ', 'Some Name ', 'Some Name ', - // eslint-disable-next-line quotes "'Foo Bar, Esq'", 'Some Name ', 'Some Middle Name ',