Skip to content

Commit

Permalink
Storage change bug fix, Tests and some log changes (#446)
Browse files Browse the repository at this point in the history
* storage change bug fix, tests and some log changes

* added av param to requests

* changed av place

* hc lookut removed
  • Loading branch information
turtledreams committed Oct 20, 2023
1 parent a600cbf commit 653428f
Show file tree
Hide file tree
Showing 22 changed files with 466 additions and 6,158 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 23.6.2
- Adding app version information to every request

- Mitigated an issue where if a long numerical device ID was changed while SDK was running at multiple tabs, it was parsed

## 23.6.1
- Mitigated an issue where numerical device IDs were parsed at the initialization

Expand Down
53 changes: 27 additions & 26 deletions cypress/fixtures/click_test.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
<html>
<head>
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='text/javascript' src='../support/integration_helper.js'></script>
<script type='text/javascript'>
// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
for (const cookie of cookies) {
const eqPos = cookie.indexOf("=");
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='module'>
import { queryExtractor } from '../support/integration_helper.js';

// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
for (const cookie of cookies) {
const eqPos = cookie.indexOf("=");
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
}
window.localStorage.clear();
deleteAllCookies();
console.error("cleared the storage");
var domEl = queryExtractor(window.location.search).dom;
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
debug:true,
test_mode: true
})
Countly.track_sessions();
Countly.track_pageview();
window.onload = function() {
Countly.track_clicks(document.getElementById(domEl));
};
window.localStorage.clear();
deleteAllCookies();
console.error("cleared the storage");
var domEl = queryExtractor(window.location.search).dom;
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
debug: true,
test_mode: true
})
Countly.track_sessions();
Countly.track_pageview();
window.onload = function () {
Countly.track_clicks(document.getElementById(domEl));
};
</script>
</head>
<body>
Expand Down
6 changes: 4 additions & 2 deletions cypress/fixtures/multi_instance.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<html>
<head>
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='text/javascript' src='../support/integration_helper.js'></script>
<script type='text/javascript'>
<script type='module'>
import { triggerStorageChange } from '../support/integration_helper.js';
// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
Expand All @@ -27,6 +27,8 @@
})
Countly.remove_consent();
Countly.disable_offline_mode();
triggerStorageChange("YOUR_APP_KEY1/cly_id", "id1");
triggerStorageChange("YOUR_APP_KEY3/cly_id", "id3");
Countly.add_event({ key: "test", count: 1, sum: 1, dur: 1, segmentation: { test: "test" } });
Countly.start_event("test");
Countly.cancel_event("gobbledygook");
Expand Down
4 changes: 2 additions & 2 deletions cypress/fixtures/session_test_auto.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<!--Countly script-->
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='text/javascript' src='../support/integration_helper.js'></script>
<script type='text/javascript'>
<script type='module'>
import { queryExtractor } from '../support/integration_helper.js';
// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
Expand Down
4 changes: 2 additions & 2 deletions cypress/fixtures/session_test_manual_1.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<!--Countly script-->
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='text/javascript' src='../support/integration_helper.js'></script>
<script type='text/javascript'>
<script type='module'>
import { queryExtractor } from '../support/integration_helper.js';
// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
Expand Down
4 changes: 2 additions & 2 deletions cypress/fixtures/session_test_manual_2.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<!--Countly script-->
<script type='text/javascript' src='../../lib/countly.js'></script>
<script type='text/javascript' src='../support/integration_helper.js'></script>
<script type='text/javascript'>
<script type='module'>
import { queryExtractor } from '../support/integration_helper.js';
// here we implement a cookie and localStorage clearing logic
const deleteAllCookies = () => {
const cookies = document.cookie.split(";");
Expand Down
7 changes: 7 additions & 0 deletions cypress/integration/device_id.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,11 @@ describe("Device ID init tests for request state", ()=>{
});
});
});
it("Start with a long numerical device ID", () => {
hp.haltAndClearStorage(() => {
localStorage.setItem("YOUR_APP_KEY/cly_id", "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
initMain(undefined, false, undefined, false, true, true);
expect(Countly.get_device_id()).to.equal("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
});
});
});
6 changes: 5 additions & 1 deletion cypress/integration/remaining_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ function initMain(shouldStopRequests) {
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
app_version: "1.0",
// would prevent requests from being sent to the server if true
test_mode: shouldStopRequests
});
}

const av = "1.0";
describe("Remaining requests tests ", () => {
it("Checks the requests for rr", () => {
hp.haltAndClearStorage(() => {
Expand All @@ -27,16 +28,19 @@ describe("Remaining requests tests ", () => {
hp.interceptAndCheckRequests(undefined, undefined, undefined, "?begin_session=*", "begin_session", (requestParams) => {
expect(requestParams.get("begin_session")).to.equal("1");
expect(requestParams.get("rr")).to.equal("3");
expect(requestParams.get("av")).to.equal(av);
});
// End the session
Countly.end_session(undefined, true);
hp.interceptAndCheckRequests(undefined, undefined, undefined, undefined, "end_session", (requestParams) => {
expect(requestParams.get("end_session")).to.equal("1");
expect(requestParams.get("rr")).to.equal("2");
expect(requestParams.get("av")).to.equal(av);
});
hp.interceptAndCheckRequests(undefined, undefined, undefined, undefined, "orientation", (requestParams) => {
expect(JSON.parse(requestParams.get("events"))[0].key).to.equal("[CLY]_orientation");
expect(requestParams.get("rr")).to.equal("1");
expect(requestParams.get("av")).to.equal(av);
});
cy.wait(100).then(() => {
cy.fetch_local_request_queue().then((rq) => {
Expand Down
1 change: 1 addition & 0 deletions cypress/integration/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,5 @@ for (let i = 0; i < 5; i++) {
});
});
});
document.cookie = ""; // clear cookies
}
129 changes: 129 additions & 0 deletions cypress/integration/storage_change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* eslint-disable require-jsdoc */
var Countly = require("../../lib/countly");
var hp = require("../support/helper");
import { triggerStorageChange } from "../support/integration_helper";

function initMain(val) {
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
device_id: 0, // provide number
test_mode_eq: true,
test_mode: true,
debug: true,
storage: val
});
}

const userDetailObj = hp.userDetailObj;

describe("Multi tab storage change test", () => {
// onStorageChange is a ram only operation
it("Check device ID changes at a different tab are reflected correctly", () => {
hp.haltAndClearStorage(() => {
initMain("localstorage");

// numerical device ID provided at init is converted to string
var id = Countly.get_device_id();
expect(id).to.equal("0");

// Check short string id is set correctly
triggerStorageChange("YOUR_APP_KEY/cly_id", "123");
id = Countly.get_device_id();
expect(id).to.equal("123");

// Check empty string id is set correctly
triggerStorageChange("YOUR_APP_KEY/cly_id", "");
id = Countly.get_device_id();
expect(id).to.equal("");

// Check null id is set correctly
triggerStorageChange("YOUR_APP_KEY/cly_id", null);
id = Countly.get_device_id();
expect(id).to.equal(null);

// Check long string numerical id is set correctly
triggerStorageChange("YOUR_APP_KEY/cly_id", "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
id = Countly.get_device_id();
expect(id).to.equal("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
Countly.user_details(userDetailObj);
cy.fetch_local_request_queue().then((rq) => {
expect(rq.length).to.equal(1);
cy.check_user_details(rq[0], userDetailObj);
expect(rq[0].device_id).to.equal("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
});

// Check numerical conversion to string (positive)
triggerStorageChange("YOUR_APP_KEY/cly_id", 123);
id = Countly.get_device_id();
expect(id).to.equal("123");

// Check numerical conversion to string (negative)
triggerStorageChange("YOUR_APP_KEY/cly_id", -123);
id = Countly.get_device_id();
expect(id).to.equal("-123");

// Check numerical conversion to string (0)
triggerStorageChange("YOUR_APP_KEY/cly_id", 0);
id = Countly.get_device_id();
expect(id).to.equal("0");

// Check numerical conversion to string (0)
triggerStorageChange("YOUR_APP_KEY/cly_id", "{}");
id = Countly.get_device_id();
expect(id).to.equal("{}");
});
});
it("Check in multi instance scenarions device ID assigned correctly", () => {
hp.haltAndClearStorage(() => {
let firstIns = Countly.init({
app_key: "YOUR_APP_KEY1",
url: "https://try.count.ly",
device_id: 0,
test_mode_eq: true,
test_mode: true,
debug: true,
storage: "localstorage"
});
let secondIns = Countly.init({
app_key: "YOUR_APP_KEY2",
url: "https://try.count.ly",
device_id: -1,
test_mode_eq: true,
test_mode: true,
debug: false,
storage: "localstorage"
});

// numerical device ID provided at init is converted to string
var id1 = firstIns.get_device_id();
var id2 = secondIns.get_device_id();
expect(id1).to.equal("0");
expect(id2).to.equal("-1");

// Check long string numerical id is set correctly
triggerStorageChange("YOUR_APP_KEY1/cly_id", "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
id1 = firstIns.get_device_id();
id2 = secondIns.get_device_id();
expect(id1).to.equal("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
expect(id2).to.equal("-1");

// Check ID in request queue is set correctly for the correct instance
firstIns.user_details(userDetailObj);
cy.fetch_local_request_queue("YOUR_APP_KEY1").then((rq) => {
expect(rq.length).to.equal(1);
cy.check_user_details(rq[0], userDetailObj, undefined, "YOUR_APP_KEY1");
expect(rq[0].device_id).to.equal("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
});
secondIns.user_details(userDetailObj);
cy.fetch_local_request_queue("YOUR_APP_KEY2").then((rq) => {
expect(rq.length).to.equal(1);
cy.check_user_details(rq[0], userDetailObj, undefined, "YOUR_APP_KEY2");
expect(rq[0].device_id).to.equal("-1");
});

firstIns = null;
secondIns = null;
});
});
});
15 changes: 1 addition & 14 deletions cypress/integration/user_details.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,7 @@ function initMain() {
});
}

const userDetailObj = {
name: "Barturiana Sosinsiava",
username: "bar2rawwen",
email: "test@test.com",
organization: "Dukely",
phone: "+123456789",
picture: "https://ps.timg.com/profile_images/52237/011_n_400x400.jpg",
gender: "Non-binary",
byear: 1987, // birth year
custom: {
"key1 segment": "value1 segment",
"key2 segment": "value2 segment"
}
};
const userDetailObj = hp.userDetailObj;

// an event object to use
const eventObj = {
Expand Down
4 changes: 2 additions & 2 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ Cypress.Commands.add("check_scroll_event", (queueObject) => {
* @param {Object} limits - optional, if internal limits are going to be checked this should be provided as an object like this (values can change):
* {key: 8, value: 8, segment: 3, breadcrumb: 2, line_thread: 3, line_length: 10};
*/
Cypress.Commands.add("check_user_details", (detailsObject, userDetails, limits) => {
Cypress.Commands.add("check_user_details", (detailsObject, userDetails, limits, appKey) => {
const obj = detailsObject;
cy.check_commons(obj);
cy.check_request_commons(obj);
cy.check_request_commons(obj, appKey);
const queue = JSON.parse(obj.user_details);
if (limits !== undefined) {
expect(queue.name).to.equal(userDetails.name.substring(0, limits.value));
Expand Down
22 changes: 21 additions & 1 deletion cypress/support/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ function haltAndClearStorage(callback) {
});
}

/**
* user details object for user details tests (used in user_details.js and storage_change.js)
* @type {Object}
*/
const userDetailObj = {
name: "Barturiana Sosinsiava",
username: "bar2rawwen",
email: "test@test.com",
organization: "Dukely",
phone: "+123456789",
picture: "https://ps.timg.com/profile_images/52237/011_n_400x400.jpg",
gender: "Non-binary",
byear: 1987,
custom: {
"key1 segment": "value1 segment",
"key2 segment": "value2 segment"
}
};

/**
* get timestamp
* @returns {number} -timestamp
Expand Down Expand Up @@ -262,5 +281,6 @@ module.exports = {
eventArray,
testNormalFlow,
interceptAndCheckRequests,
validateDefaultUtmTags
validateDefaultUtmTags,
userDetailObj
};
17 changes: 17 additions & 0 deletions cypress/support/integration_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@ function queryExtractor(query) {
}
return returnVal;
}

/**
* Sets a value to local storage and triggers a storage event
* @param {*} key - storage key
* @param {*} value - storage value
*/
function triggerStorageChange(key, value) {
localStorage.setItem(key, value);
const storageEvent = new StorageEvent("storage", {
key: key,
newValue: value
});

window.dispatchEvent(storageEvent);
}

export { queryExtractor, triggerStorageChange };
Loading

0 comments on commit 653428f

Please sign in to comment.