From 92fcb6477fb806aad543f3d691deb5703042f708 Mon Sep 17 00:00:00 2001 From: slimkrazy Date: Mon, 13 Jan 2025 17:07:26 +0000 Subject: [PATCH] Add tests for override properties --- modules/nodalsAiRtdProvider.js | 6 +- test/spec/modules/nodalsAiRtdProvider_spec.js | 142 +++++++++++++----- 2 files changed, 110 insertions(+), 38 deletions(-) diff --git a/modules/nodalsAiRtdProvider.js b/modules/nodalsAiRtdProvider.js index 8c9a5bff942..bd6fe486879 100644 --- a/modules/nodalsAiRtdProvider.js +++ b/modules/nodalsAiRtdProvider.js @@ -101,7 +101,9 @@ class NodalsAiRtdProvider { // Private methods #setOverrides(config) { - this.#overrides.storageTTL = config?.storage?.ttl; + if (config?.storage?.ttl && typeof config.storage.ttl === "number") { + this.#overrides.storageTTL = config.storage.ttl * 1000; + } this.#overrides.storageKey = config?.storage?.key; this.#overrides.endpointOrigin = config?.endpoint?.origin; } @@ -197,7 +199,7 @@ class NodalsAiRtdProvider { #dataIsStale(storedData) { const currentTime = Date.now(); const dataTime = storedData.createdAt || 0; - const staleThreshold = STORAGE_TTL; + const staleThreshold = this.#overrides?.storageTTL ?? STORAGE_TTL; return currentTime - dataTime >= staleThreshold; } diff --git a/test/spec/modules/nodalsAiRtdProvider_spec.js b/test/spec/modules/nodalsAiRtdProvider_spec.js index 71579540f40..94580515e77 100644 --- a/test/spec/modules/nodalsAiRtdProvider_spec.js +++ b/test/spec/modules/nodalsAiRtdProvider_spec.js @@ -5,41 +5,6 @@ import { server } from "test/mocks/xhr.js"; import { nodalsAiRtdSubmodule } from "modules/nodalsAiRtdProvider.js"; -const generateGdprConsent = (consent = {}) => { - const defaults = { - gdprApplies: true, - purpose1Consent: true, - nodalsConsent: true, - }; - const mergedConsent = Object.assign({}, defaults, consent); - return { - gdpr: { - gdprApplies: mergedConsent.gdprApplies, - consentString: mergedConsent.consentString, - vendorData: { - purpose: { - consents: { - 1: mergedConsent.purpose1Consent, - 3: true, - 4: true, - 5: true, - 6: true, - 9: true, - }, - }, - specialFeatureOptins: { - 1: true, - }, - vendor: { - consents: { - 1360: mergedConsent.nodalsConsent, - }, - }, - }, - }, - }; -}; - const jsonResponseHeaders = { "Content-Type": "application/json", }; @@ -59,7 +24,7 @@ const successPubEndpointResponse = { weighting: 1, kvs: [ { - k: "nodalsSky12", + k: "nodals", v: "1", }, ], @@ -90,6 +55,41 @@ const engineGetTargetingDataReturnValue = { }, }; +const generateGdprConsent = (consent = {}) => { + const defaults = { + gdprApplies: true, + purpose1Consent: true, + nodalsConsent: true, + }; + const mergedConsent = Object.assign({}, defaults, consent); + return { + gdpr: { + gdprApplies: mergedConsent.gdprApplies, + consentString: mergedConsent.consentString, + vendorData: { + purpose: { + consents: { + 1: mergedConsent.purpose1Consent, + 3: true, + 4: true, + 5: true, + 6: true, + 9: true, + }, + }, + specialFeatureOptins: { + 1: true, + }, + vendor: { + consents: { + 1360: mergedConsent.nodalsConsent, + }, + }, + }, + }, + }; +}; + const setDataInLocalStorage = (data) => { const storageData = { ...data }; nodalsAiRtdSubmodule.storage.setDataInLocalStorage( @@ -188,6 +188,41 @@ describe("NodalsAI RTD Provider", () => { expect(result).to.be.true; expect(server.requests.length).to.equal(1); }); + + it("should detect stale data if override TTL is exceeded", function () { + const fiveMinutesAgoMs = Date.now() - 5 * 60 * 1000; + setDataInLocalStorage({ + data: { foo: "bar" }, + createdAt: fiveMinutesAgoMs, + }); + const config = Object.assign({}, validConfig); + config.storage = { ttl: 4 * 60 }; + const result = nodalsAiRtdSubmodule.init(config, {}); + expect(result).to.be.true; + expect(server.requests.length).to.equal(1); + }); + + it("should NOT detect stale data if override TTL is not exceeded", function () { + const fiveMinutesAgoMs = Date.now() - 5 * 60 * 1000; + setDataInLocalStorage({ + data: { foo: "bar" }, + createdAt: fiveMinutesAgoMs, + }); + const config = Object.assign({}, validConfig); + config.storage = { ttl: 6 * 60 }; + const result = nodalsAiRtdSubmodule.init(config, {}); + expect(result).to.be.true; + expect(server.requests.length).to.equal(0); + }); + + it("should return true and make a remote request when data stored under default key, but override key specified", () => { + setDataInLocalStorage({ data: { foo: "bar" }, createdAt: Date.now() }); + const config = Object.assign({}, validConfig); + config.storage = { key: "_foobarbaz_" }; + const result = nodalsAiRtdSubmodule.init(config, {}); + expect(result).to.be.true; + expect(server.requests.length).to.equal(1); + }); }); describe("when performing requests to the publisher endpoint", () => { @@ -203,6 +238,20 @@ describe("NodalsAI RTD Provider", () => { expect(requestUrl.origin).to.equal("https://nodals.io"); }); + it("should construct the URL to the overridden origin when specified in the config", () => { + const config = Object.assign({}, validConfig); + config.endpoint = { origin: "http://localhost:8000" }; + const userConsent = generateGdprConsent(); + nodalsAiRtdSubmodule.init(config, userConsent); + + let request = server.requests[0]; + + expect(request.method).to.equal("GET"); + expect(request.withCredentials).to.be.false; + const requestUrl = new URL(request.url); + expect(requestUrl.origin).to.equal("http://localhost:8000"); + }); + it("should construct the correct URL with the correct path", () => { const userConsent = generateGdprConsent(); nodalsAiRtdSubmodule.init(validConfig, userConsent); @@ -253,6 +302,27 @@ describe("NodalsAI RTD Provider", () => { expect(storedData.data).to.deep.equal(successPubEndpointResponse); }); + it("should store successful response data in local storage under the override key", () => { + const userConsent = generateGdprConsent(); + const config = Object.assign({}, validConfig); + config.storage = { key: "_foobarbaz_" }; + nodalsAiRtdSubmodule.init(config, userConsent); + + let request = server.requests[0]; + request.respond( + 200, + jsonResponseHeaders, + JSON.stringify(successPubEndpointResponse) + ); + + const storedData = JSON.parse( + nodalsAiRtdSubmodule.storage.getDataFromLocalStorage("_foobarbaz_") + ); + expect(request.method).to.equal("GET"); + expect(storedData).to.have.property("createdAt"); + expect(storedData.data).to.deep.equal(successPubEndpointResponse); + }); + it("should attempt to load the referenced script libraries contained in the response payload", () => { const userConsent = generateGdprConsent(); nodalsAiRtdSubmodule.init(validConfig, userConsent);