From 68f9d2330e67ac13f84e5df428f7b10eb2b0d506 Mon Sep 17 00:00:00 2001 From: Alex S <49695018+alexs-mparticle@users.noreply.github.com> Date: Thu, 25 Apr 2024 10:02:20 -0400 Subject: [PATCH] refactor: Add test cases for setting isLoggedIn value via persistence (#872) --- src/persistence.interfaces.ts | 4 +++ test/src/tests-persistence.ts | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/persistence.interfaces.ts b/src/persistence.interfaces.ts index a43b0f066..1f062f8ad 100644 --- a/src/persistence.interfaces.ts +++ b/src/persistence.interfaces.ts @@ -44,6 +44,10 @@ export interface IGlobalStoreV2MinifiedKeys { export interface IPersistenceMinified extends Dictionary { cu: MPID; // Current User MPID gs: IGlobalStoreV2MinifiedKeys; + + // Stored as 0 or 1 in device persistence but returned as a + // boolean when decoding from device persistence via + // _Persistence.getPersistence and _Persistence.decodePersistence l: boolean; // IsLoggedIn // Persistence Minified can also store optional dictionaries with diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index c8ea6e7c5..996a56c17 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -20,6 +20,7 @@ import { IPersistenceMinified, } from '../../src/persistence.interfaces'; import { ConsentState } from '@mparticle/web-sdk'; +import { MParticleUser } from '../../src/sdkRuntimeModels'; const { findCookie, @@ -1483,6 +1484,72 @@ describe('persistence', () => { done(); }); + it('get/set isLoggedIn for localStorage', done => { + mParticle._resetForTests(MPConfig); + + mockServer.respondWith(urls.login, [ + 200, + {}, + JSON.stringify({ mpid: 'mpid1', is_logged_in: true }), + ]); + + + mParticle.init(apiKey, mParticle.config); + let user: MParticleUser = mParticle + .getInstance() + .Identity.getCurrentUser() + expect(user).to.be.ok; + expect(user.isLoggedIn()).to.be.false; + + let localStorageData = mParticle.getInstance()._Persistence.getPersistence(); + + // The `l` property of Persistence is a boolean, but when saved + // to local storage, Persistence encodes this as a 0 or 1. + // It is then re-encoded as a boolean when retrieved from local storage. + expect(localStorageData.l).to.equal(false); + + mParticle.Identity.login(); + + localStorageData = mParticle.getInstance()._Persistence.getPersistence(); + expect(localStorageData.l).to.equal(true); + + done(); + }); + + + it('get/set isLoggedIn for cookies', done => { + mParticle._resetForTests(MPConfig); + mParticle.config.useCookieStorage = true; + + mockServer.respondWith(urls.login, [ + 200, + {}, + JSON.stringify({ mpid: 'mpid1', is_logged_in: true }), + ]); + + mParticle.init(apiKey, mParticle.config); + + let user: MParticleUser = mParticle + .getInstance() + .Identity.getCurrentUser() + expect(user).to.be.ok; + expect(user.isLoggedIn()).to.be.false; + + let cookieData = findCookie(); + + // The `l` property of Persistence is a boolean, but when saved + // to cookie storage, Persistence encodes this as a 0 or 1. + // It is then re-encoded as a boolean when retrieved from cookies storage + cookieData.l.should.equal(false); + + mParticle.Identity.login(); + + cookieData = findCookie(); + cookieData.l.should.equal(true); + + done(); + }); + it('get/set consent state for single user', done => { mParticle._resetForTests(MPConfig);