diff --git a/packages/consent/consent-tools/src/domain/consent-stamping.ts b/packages/consent/consent-tools/src/domain/consent-stamping.ts index ddaa00359..9d31b38c3 100644 --- a/packages/consent/consent-tools/src/domain/consent-stamping.ts +++ b/packages/consent/consent-tools/src/domain/consent-stamping.ts @@ -17,7 +17,7 @@ export const createConsentStampingMiddleware: CreateConsentMw = } payload.obj.context.consent = { ...payload.obj.context.consent, - categoryPreferences: await getCategories(), + categoryPreferences: categories, } next(payload) } diff --git a/packages/consent/consent-tools/src/domain/validation/__tests__/options-validators.test.ts b/packages/consent/consent-tools/src/domain/validation/__tests__/options-validators.test.ts new file mode 100644 index 000000000..187fbebd3 --- /dev/null +++ b/packages/consent/consent-tools/src/domain/validation/__tests__/options-validators.test.ts @@ -0,0 +1,63 @@ +import { validateCategories, validateOptions } from '../options-validators' +import { ValidationError } from '../validation-error' + +describe(validateOptions, () => { + it('should throw if options is not a plain object', () => { + expect(() => validateOptions(null as any)).toThrow() + expect(() => validateOptions(undefined as any)).toThrow() + expect(() => validateOptions('hello' as any)).toThrow() + }) + + it('should throw an instance of ValidationError', () => { + expect(() => validateOptions(null as any)).toThrowError(Error) + expect(() => validateOptions(null as any)).toThrowError(ValidationError) + }) + + it('should throw with the expected error', () => { + expect(() => + validateOptions(null as any) + ).toThrowErrorMatchingInlineSnapshot( + `"[Validation] Options should be an object (Received: null)"` + ) + }) + + it('should throw if required property(s) are not included', () => { + expect(() => validateOptions({} as any)).toThrow() + }) + + it('should throw if getCategories() is not a function', () => { + expect(() => + validateOptions({ + getCategories: {}, + }) + ).toThrow() + }) +}) + +describe(validateCategories, () => { + it('should throw if categories is not a plain object', () => { + expect(() => validateCategories(null)).toThrow() + expect(() => validateCategories(undefined)).toThrow() + expect(() => validateCategories('hello')).toThrow() + }) + + it('should throw an instance of ValidationError', () => { + expect(() => validateCategories(null)).toThrowError(Error) + expect(() => validateCategories(null)).toThrowError(ValidationError) + }) + + it('should throw with the expected error', () => { + expect(() => validateCategories(null)).toThrowErrorMatchingInlineSnapshot( + `"[Validation] Consent Categories should be {[categoryName: string]: boolean} (Received: null)"` + ) + }) + + it('should throw if categories does not match {categoryName: boolean}', () => { + expect(() => validateCategories({})).not.toThrow() // if getCategories is empty object, it is the same as 'consent to all categories' if false + expect(() => validateCategories({ a: true })).not.toThrow() + expect(() => validateCategories({ a: 'foo' })).toThrow() + expect(() => validateCategories({ a: 'true' })).toThrow() + expect(() => validateCategories({ a: true, b: 'foo' })).toThrow() + expect(() => validateCategories({ a: true, b: 'foo', c: true })).toThrow() + }) +}) diff --git a/packages/consent/consent-tools/src/domain/validation/options-validators.ts b/packages/consent/consent-tools/src/domain/validation/options-validators.ts index a33548460..572ef58fb 100644 --- a/packages/consent/consent-tools/src/domain/validation/options-validators.ts +++ b/packages/consent/consent-tools/src/domain/validation/options-validators.ts @@ -5,17 +5,22 @@ import { ValidationError } from './validation-error' export function validateCategories( ctgs: unknown ): asserts ctgs is NonNullable { + let hasError = true if (ctgs && typeof ctgs === 'object' && !Array.isArray(ctgs)) { + hasError = false for (const k in ctgs) { - if (typeof (ctgs as any)[k] === 'boolean') { - return + if (typeof (ctgs as any)[k] !== 'boolean') { + hasError = true + break } } } - throw new ValidationError( - `Consent Categories should be {[categoryName: string]: boolean}`, - ctgs - ) + if (hasError) { + throw new ValidationError( + `Consent Categories should be {[categoryName: string]: boolean}`, + ctgs + ) + } } export function validateOptions(options: {