From b11b950ef5fdabd08e5a5e205f1c33bbaa08fd0e Mon Sep 17 00:00:00 2001 From: Alec Rippberger Date: Tue, 15 Apr 2025 15:52:49 -0500 Subject: [PATCH] fromJSON should return null if the cache is empty --- ...wo-factor-auth-email-cache.service.spec.ts | 18 ++++++++++++++ .../two-factor-auth-email-cache.service.ts | 7 +++++- .../two-factor-auth-cache.service.spec.ts | 24 +++++++++++++++++++ .../two-factor-auth-cache.service.ts | 7 +++++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.spec.ts b/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.spec.ts index c89fd88e08e..24c6b3aeddd 100644 --- a/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.spec.ts +++ b/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.spec.ts @@ -11,6 +11,24 @@ import { TwoFactorAuthEmailComponentCacheService, } from "./two-factor-auth-email-cache.service"; +describe("TwoFactorAuthEmailCache", () => { + describe("fromJSON", () => { + it("returns null when input is null", () => { + const result = TwoFactorAuthEmailCache.fromJSON(null as any); + expect(result).toBeNull(); + }); + + it("creates a TwoFactorAuthEmailCache instance from valid JSON", () => { + const jsonData = { emailSent: true }; + const result = TwoFactorAuthEmailCache.fromJSON(jsonData); + + expect(result).not.toBeNull(); + expect(result).toBeInstanceOf(TwoFactorAuthEmailCache); + expect(result?.emailSent).toBe(true); + }); + }); +}); + describe("TwoFactorAuthEmailComponentCacheService", () => { let service: TwoFactorAuthEmailComponentCacheService; let mockViewCacheService: MockProxy; diff --git a/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.ts b/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.ts index f382913e83e..0660fe99e56 100644 --- a/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.ts +++ b/libs/auth/src/angular/two-factor-auth/child-components/two-factor-auth-email/two-factor-auth-email-cache.service.ts @@ -13,7 +13,12 @@ const TWO_FACTOR_AUTH_EMAIL_CACHE_KEY = "two-factor-auth-email-cache"; export class TwoFactorAuthEmailCache { emailSent: boolean = false; - static fromJSON(obj: Partial>): TwoFactorAuthEmailCache { + static fromJSON(obj: Partial>): TwoFactorAuthEmailCache | null { + // Return null if the cache is empty + if (obj == null) { + return null; + } + return Object.assign(new TwoFactorAuthEmailCache(), obj); } } diff --git a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts b/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts index 3da9cef0e27..cf8a70a4f3f 100644 --- a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts +++ b/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts @@ -13,6 +13,30 @@ import { TwoFactorAuthData, } from "./two-factor-auth-cache.service"; +describe("TwoFactorAuthCache", () => { + describe("fromJSON", () => { + it("returns null when input is null", () => { + const result = TwoFactorAuthCache.fromJSON(null as any); + expect(result).toBeNull(); + }); + + it("creates a TwoFactorAuthCache instance from valid JSON", () => { + const jsonData = { + token: "123456", + remember: true, + selectedProviderType: TwoFactorProviderType.Email, + }; + const result = TwoFactorAuthCache.fromJSON(jsonData as any); + + expect(result).not.toBeNull(); + expect(result).toBeInstanceOf(TwoFactorAuthCache); + expect(result?.token).toBe("123456"); + expect(result?.remember).toBe(true); + expect(result?.selectedProviderType).toBe(TwoFactorProviderType.Email); + }); + }); +}); + describe("TwoFactorAuthComponentCacheService", () => { let service: TwoFactorAuthComponentCacheService; let mockViewCacheService: MockProxy; diff --git a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts b/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts index 906d86b7288..6e19f745200 100644 --- a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts +++ b/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts @@ -16,7 +16,12 @@ export class TwoFactorAuthCache { remember: boolean | undefined = undefined; selectedProviderType: TwoFactorProviderType | undefined = undefined; - static fromJSON(obj: Partial>): TwoFactorAuthCache { + static fromJSON(obj: Partial>): TwoFactorAuthCache | null { + // Return null if the cache is empty + if (obj == null) { + return null; + } + return Object.assign(new TwoFactorAuthCache(), obj); } }