From f93106b365f5e41d35bec0f3d8c9b97828b640f4 Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Tue, 16 Apr 2024 14:17:37 +0200 Subject: [PATCH 1/7] Support fnr validation in other environments than prod --- .../NationalIdentityNumber.test.ts | 14 ++++++++ .../NationalIdentityNumber.ts | 35 ++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts index 935a3f707..f797e6f2b 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts @@ -49,5 +49,19 @@ describe('Fodselsnummer', () => { it('fails validation for tnr', () => { expect(fnrComp.validateFnrNew(VALID_TNR)).toEqual(TEXTS.validering.fodselsnummerDNummer); }); + + it.only('succeeds validation for tnr if env is delingslenke', () => { + fnrComp.options = { + appConfig: { config: { isDelingslenke: true } }, + }; + expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); + }); + + it.only('succeeds validation for tnr if env is development', () => { + fnrComp.options = { + appConfig: { config: { isDevelopment: true } }, + }; + expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); + }); }); }); diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts index b604d0074..1b4e97beb 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts @@ -1,12 +1,16 @@ import { idnr } from '@navikt/fnrvalidator'; -import { TEXTS } from '@navikt/skjemadigitalisering-shared-domain'; +import { ConfigType, TEXTS } from '@navikt/skjemadigitalisering-shared-domain'; import TextFieldComponent from 'formiojs/components/textfield/TextField'; import nationalIdentityNumberBuilder from './NationalIdentityNumber.builder'; import nationalIdentityNumberForm from './NationalIdentityNumber.form'; const ALLOWED_TYPES = ['fnr', 'dnr']; +const ALLOWED_TEST_TYPES = ['fnr', 'dnr', 'hnr', 'tnr', 'dnr-and-hnr']; export default class NationalIdentityNumber extends TextFieldComponent { + options: any; + t: any; + static schema() { return TextFieldComponent.schema({ label: 'Fødselsnummer eller d-nummer', @@ -35,18 +39,31 @@ export default class NationalIdentityNumber extends TextFieldComponent { return true; } + const appConfig = this.options?.appConfig?.config as ConfigType; + const shouldAcceptTestFnr = appConfig?.isDelingslenke || appConfig?.isDevelopment; + const inputValueNoSpace = inputValue.replace(' ', ''); + const result = idnr(inputValueNoSpace); - // @ts-ignore - const { status, type } = idnr(inputValueNoSpace); - if (!ALLOWED_TYPES.includes(type) || status !== 'valid') { - //translate based on key in validering file. - // @ts-ignore - return this.t('fodselsnummerDNummer') === 'fodselsnummerDNummer' + const errorMessage: string = + this.t('fodselsnummerDNummer') === 'fodselsnummerDNummer' ? TEXTS.validering.fodselsnummerDNummer - : // @ts-ignore - this.t('fodselsnummerDNummer'); + : this.t('fodselsnummerDNummer'); + + if (result.status === 'invalid') { + return errorMessage; } + + if (result.status === 'valid') { + if (ALLOWED_TYPES.includes(result.type)) { + return true; + } else if (shouldAcceptTestFnr) { + return ALLOWED_TEST_TYPES.includes(result.type); + } else { + return errorMessage; + } + } + return true; } } From fb478722bfbe44757846caa0d9d89424e051bafb Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Tue, 16 Apr 2024 14:18:30 +0200 Subject: [PATCH 2/7] Remove .only --- .../national-identity-number/NationalIdentityNumber.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts index f797e6f2b..2f509daef 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts @@ -50,14 +50,14 @@ describe('Fodselsnummer', () => { expect(fnrComp.validateFnrNew(VALID_TNR)).toEqual(TEXTS.validering.fodselsnummerDNummer); }); - it.only('succeeds validation for tnr if env is delingslenke', () => { + it('succeeds validation for tnr if env is delingslenke', () => { fnrComp.options = { appConfig: { config: { isDelingslenke: true } }, }; expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); }); - it.only('succeeds validation for tnr if env is development', () => { + it('succeeds validation for tnr if env is development', () => { fnrComp.options = { appConfig: { config: { isDevelopment: true } }, }; From b237afb7a2aec161e0ff5adce737fa3758c5f5dc Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Tue, 16 Apr 2024 14:35:30 +0200 Subject: [PATCH 3/7] Use isProduction --- packages/fyllut-backend/src/routers/api/config.ts | 12 ++++++++++-- .../NationalIdentityNumber.test.ts | 3 +++ .../NationalIdentityNumber.ts | 3 +-- packages/shared-domain/src/config/types.d.ts | 1 + 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/fyllut-backend/src/routers/api/config.ts b/packages/fyllut-backend/src/routers/api/config.ts index 8e4348124..cd7a54fd3 100644 --- a/packages/fyllut-backend/src/routers/api/config.ts +++ b/packages/fyllut-backend/src/routers/api/config.ts @@ -2,8 +2,15 @@ import { ConfigType } from '@navikt/skjemadigitalisering-shared-domain'; import { config as configObject } from '../../config/config'; import { getIsLoggedIn } from '../../security/tokenHelper'; -const { naisClusterName, featureToggles, isDelingslenke, isDevelopment, amplitude, frontendLoggerConfig } = - configObject; +const { + naisClusterName, + featureToggles, + isDelingslenke, + isDevelopment, + amplitude, + frontendLoggerConfig, + isProduction, +} = configObject; const config = { get: async (req, res): Promise => { @@ -12,6 +19,7 @@ const config = { return res.json({ NAIS_CLUSTER_NAME: naisClusterName, FEATURE_TOGGLES: featureToggles, + isProduction, isDelingslenke, isDevelopment, amplitudeApiEndpoint: amplitude.apiEndpoint, diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts index 2f509daef..78fe1bb96 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts @@ -10,6 +10,9 @@ describe('Fodselsnummer', () => { beforeEach(() => { fnrComp = new NationalIdenityNumber(); + fnrComp.options = { + appConfig: { config: { isProduction: true } }, + }; // @ts-ignore vi.spyOn(NationalIdenityNumber.prototype, 't').mockImplementation((text) => text); }); diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts index 1b4e97beb..dbeb5848f 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts @@ -40,7 +40,6 @@ export default class NationalIdentityNumber extends TextFieldComponent { } const appConfig = this.options?.appConfig?.config as ConfigType; - const shouldAcceptTestFnr = appConfig?.isDelingslenke || appConfig?.isDevelopment; const inputValueNoSpace = inputValue.replace(' ', ''); const result = idnr(inputValueNoSpace); @@ -57,7 +56,7 @@ export default class NationalIdentityNumber extends TextFieldComponent { if (result.status === 'valid') { if (ALLOWED_TYPES.includes(result.type)) { return true; - } else if (shouldAcceptTestFnr) { + } else if (!appConfig?.isProduction) { return ALLOWED_TEST_TYPES.includes(result.type); } else { return errorMessage; diff --git a/packages/shared-domain/src/config/types.d.ts b/packages/shared-domain/src/config/types.d.ts index b3dc70f56..82bb45f3c 100644 --- a/packages/shared-domain/src/config/types.d.ts +++ b/packages/shared-domain/src/config/types.d.ts @@ -3,6 +3,7 @@ export interface ConfigType { NAIS_CLUSTER_NAME: string; isDelingslenke: boolean; isDevelopment: boolean; + isProduction: boolean; isLoggedIn: boolean; amplitudeApiEndpoint: string; loggerConfig: { From 15df2be272d3d611a4d5d92f6be2830c853247c8 Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Tue, 16 Apr 2024 14:51:38 +0200 Subject: [PATCH 4/7] Use cluster name in isProduction check --- packages/fyllut-backend/src/config/config.ts | 2 +- .../national-identity-number/NationalIdentityNumber.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/fyllut-backend/src/config/config.ts b/packages/fyllut-backend/src/config/config.ts index 667113f82..02537efe9 100644 --- a/packages/fyllut-backend/src/config/config.ts +++ b/packages/fyllut-backend/src/config/config.ts @@ -113,7 +113,7 @@ const config: ConfigType = { featureToggles: featureUtils.toFeatureToggles(process.env.ENABLED_FEATURES ?? defaultEnabledFeatures), isDevelopment: process.env.NODE_ENV === 'development', isTest: process.env.NODE_ENV === 'test', - isProduction: process.env.NODE_ENV === 'production', + isProduction: process.env.NODE_ENV === 'production' && process.env.NAIS_CLUSTER_NAME === 'prod-gcp', isDelingslenke: process.env.NAIS_APP_NAME === 'skjemautfylling-delingslenke', pdlTokenScopeCluster: process.env.PDL_TOKEN_SCOPE_CLUSTER!, backendLogLevel: process.env.FYLLUT_BACKEND_LOGLEVEL || (process.env.NODE_ENV === 'test' ? 'warning' : 'info'), diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts index dbeb5848f..553611b8a 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts @@ -41,6 +41,8 @@ export default class NationalIdentityNumber extends TextFieldComponent { const appConfig = this.options?.appConfig?.config as ConfigType; + console.log('appConfig', appConfig); + const inputValueNoSpace = inputValue.replace(' ', ''); const result = idnr(inputValueNoSpace); From 002c9fc4c1dba1dc38b41bc480ad77836899e1eb Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Tue, 16 Apr 2024 15:06:57 +0200 Subject: [PATCH 5/7] Remove console.log --- .../national-identity-number/NationalIdentityNumber.test.ts | 4 ++-- .../national-identity-number/NationalIdentityNumber.ts | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts index 78fe1bb96..7074df2ba 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts @@ -55,14 +55,14 @@ describe('Fodselsnummer', () => { it('succeeds validation for tnr if env is delingslenke', () => { fnrComp.options = { - appConfig: { config: { isDelingslenke: true } }, + appConfig: { config: { isDelingslenke: true, isProduction: false } }, }; expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); }); it('succeeds validation for tnr if env is development', () => { fnrComp.options = { - appConfig: { config: { isDevelopment: true } }, + appConfig: { config: { isDevelopment: true, isProduction: false } }, }; expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); }); diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts index 553611b8a..dbeb5848f 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts @@ -41,8 +41,6 @@ export default class NationalIdentityNumber extends TextFieldComponent { const appConfig = this.options?.appConfig?.config as ConfigType; - console.log('appConfig', appConfig); - const inputValueNoSpace = inputValue.replace(' ', ''); const result = idnr(inputValueNoSpace); From 4b7cd9e12ff997662334a902d48e413e0950b5b3 Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Wed, 17 Apr 2024 10:24:12 +0200 Subject: [PATCH 6/7] Use nais cluster name --- packages/fyllut-backend/src/config/config.ts | 2 +- .../NationalIdentityNumber.test.ts | 6 +++--- .../national-identity-number/NationalIdentityNumber.ts | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/fyllut-backend/src/config/config.ts b/packages/fyllut-backend/src/config/config.ts index 02537efe9..667113f82 100644 --- a/packages/fyllut-backend/src/config/config.ts +++ b/packages/fyllut-backend/src/config/config.ts @@ -113,7 +113,7 @@ const config: ConfigType = { featureToggles: featureUtils.toFeatureToggles(process.env.ENABLED_FEATURES ?? defaultEnabledFeatures), isDevelopment: process.env.NODE_ENV === 'development', isTest: process.env.NODE_ENV === 'test', - isProduction: process.env.NODE_ENV === 'production' && process.env.NAIS_CLUSTER_NAME === 'prod-gcp', + isProduction: process.env.NODE_ENV === 'production', isDelingslenke: process.env.NAIS_APP_NAME === 'skjemautfylling-delingslenke', pdlTokenScopeCluster: process.env.PDL_TOKEN_SCOPE_CLUSTER!, backendLogLevel: process.env.FYLLUT_BACKEND_LOGLEVEL || (process.env.NODE_ENV === 'test' ? 'warning' : 'info'), diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts index 7074df2ba..b0a95face 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.test.ts @@ -11,7 +11,7 @@ describe('Fodselsnummer', () => { beforeEach(() => { fnrComp = new NationalIdenityNumber(); fnrComp.options = { - appConfig: { config: { isProduction: true } }, + appConfig: { config: { NAIS_CLUSTER_NAME: 'prod-gcp' } }, }; // @ts-ignore vi.spyOn(NationalIdenityNumber.prototype, 't').mockImplementation((text) => text); @@ -55,14 +55,14 @@ describe('Fodselsnummer', () => { it('succeeds validation for tnr if env is delingslenke', () => { fnrComp.options = { - appConfig: { config: { isDelingslenke: true, isProduction: false } }, + appConfig: { config: { isDelingslenke: true, NAIS_CLUSTER_NAME: 'dev-gcp' } }, }; expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); }); it('succeeds validation for tnr if env is development', () => { fnrComp.options = { - appConfig: { config: { isDevelopment: true, isProduction: false } }, + appConfig: { config: { isDevelopment: true, NAIS_CLUSTER_NAME: 'dev-gcp' } }, }; expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true); }); diff --git a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts index dbeb5848f..b3c834436 100644 --- a/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts +++ b/packages/shared-components/src/formio/components/extensions/national-identity-number/NationalIdentityNumber.ts @@ -54,13 +54,17 @@ export default class NationalIdentityNumber extends TextFieldComponent { } if (result.status === 'valid') { + // Allow only fnr and dnr in production if (ALLOWED_TYPES.includes(result.type)) { return true; - } else if (!appConfig?.isProduction) { + } + + // Allow all types in test environments + if (appConfig?.NAIS_CLUSTER_NAME !== 'prod-gcp') { return ALLOWED_TEST_TYPES.includes(result.type); - } else { - return errorMessage; } + + return errorMessage; } return true; From 7cbae81197918646821d9bcd5951c8fbc76bcfc6 Mon Sep 17 00:00:00 2001 From: Morten Stulen Date: Wed, 17 Apr 2024 10:32:45 +0200 Subject: [PATCH 7/7] Add types --- packages/fyllut-backend/src/routers/api/config.ts | 12 ++---------- packages/shared-domain/src/config/types.d.ts | 3 +-- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/fyllut-backend/src/routers/api/config.ts b/packages/fyllut-backend/src/routers/api/config.ts index cd7a54fd3..8e4348124 100644 --- a/packages/fyllut-backend/src/routers/api/config.ts +++ b/packages/fyllut-backend/src/routers/api/config.ts @@ -2,15 +2,8 @@ import { ConfigType } from '@navikt/skjemadigitalisering-shared-domain'; import { config as configObject } from '../../config/config'; import { getIsLoggedIn } from '../../security/tokenHelper'; -const { - naisClusterName, - featureToggles, - isDelingslenke, - isDevelopment, - amplitude, - frontendLoggerConfig, - isProduction, -} = configObject; +const { naisClusterName, featureToggles, isDelingslenke, isDevelopment, amplitude, frontendLoggerConfig } = + configObject; const config = { get: async (req, res): Promise => { @@ -19,7 +12,6 @@ const config = { return res.json({ NAIS_CLUSTER_NAME: naisClusterName, FEATURE_TOGGLES: featureToggles, - isProduction, isDelingslenke, isDevelopment, amplitudeApiEndpoint: amplitude.apiEndpoint, diff --git a/packages/shared-domain/src/config/types.d.ts b/packages/shared-domain/src/config/types.d.ts index 82bb45f3c..9fd4fa413 100644 --- a/packages/shared-domain/src/config/types.d.ts +++ b/packages/shared-domain/src/config/types.d.ts @@ -1,9 +1,8 @@ export interface ConfigType { FEATURE_TOGGLES: string; - NAIS_CLUSTER_NAME: string; + NAIS_CLUSTER_NAME?: 'dev-gcp' | 'prod-gcp'; isDelingslenke: boolean; isDevelopment: boolean; - isProduction: boolean; isLoggedIn: boolean; amplitudeApiEndpoint: string; loggerConfig: {