Skip to content

Commit cb6acab

Browse files
Fix/url analytics property name (#670)
* fix: add support snake case casing of analytics options
1 parent a634c5f commit cb6acab

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

lib/utils/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,20 +894,25 @@ function url(public_id, options = {}) {
894894
resultUrl += `?${token}`;
895895
}
896896

897-
let urlAnalytics = ensureOption(options, 'urlAnalytics', true);
897+
const urlAnalytics = ensureOption(options, 'urlAnalytics', ensureOption(options, 'analytics', true));
898898

899899
if (urlAnalytics === true) {
900900
let {
901-
sdkCode,
902-
sdkSemver,
903-
techVersion,
904-
product
901+
sdkCode: sdkCodeDefault,
902+
sdkSemver: sdkSemverDefault,
903+
techVersion: techVersionDefault,
904+
product: productDefault
905905
} = getSDKVersions();
906+
const sdkCode = ensureOption(options, 'sdkCode', ensureOption(options, 'sdk_code', sdkCodeDefault));
907+
const sdkSemver = ensureOption(options, 'sdkSemver', ensureOption(options, 'sdk_semver', sdkSemverDefault));
908+
const techVersion = ensureOption(options, 'techVersion', ensureOption(options, 'tech_version', techVersionDefault));
909+
const product = ensureOption(options, 'product', productDefault);
910+
906911
let sdkVersions = {
907-
sdkCode: ensureOption(options, 'sdkCode', sdkCode),
908-
sdkSemver: ensureOption(options, 'sdkSemver', sdkSemver),
909-
techVersion: ensureOption(options, 'techVersion', techVersion),
910-
product: ensureOption(options, 'product', product),
912+
sdkCode: sdkCode,
913+
sdkSemver: sdkSemver,
914+
techVersion: techVersion,
915+
product: product,
911916
urlAnalytics
912917
};
913918

test/testUtils/createTestConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const defaultConfigOptions = {
2-
urlAnalytics: false
2+
urlAnalytics: false,
3+
analytics: false
34
};
45

56
/**
67
* @description Creates a default config for testing, add properties to defaultConfigOptions to
78
* make them global across all tests
89
* @param {{}} [confOptions] Cloudinary's config options
9-
* @return {{} & {urlAnalytics: true} & any}
10+
* @return {{} & {analytics: true} & any}
1011
*/
1112
function createTestConfig(confOptions = {}) {
1213
return Object.assign({}, defaultConfigOptions, confOptions);
Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,105 @@
11
const path = require('path');
22
const mock = require('mock-fs');
3-
const getSDKVersions = require('../../../lib/utils/analytics/getSDKVersions');
43
const cloudinary = require('../../../cloudinary');
4+
const assert = require("assert");
55
const TEST_CLOUD_NAME = require('../../testUtils/testConstants').TEST_CLOUD_NAME;
66

7-
describe('Tests for sdk analytics through image tag', function () {
7+
describe('SDK url analytics', () => {
88
let processVersions = {};
99

10-
beforeEach(() => {
10+
before(() => {
1111
cloudinary.config(true); // reset
1212

1313
processVersions = process.versions;
1414
delete process.versions;
1515

16-
let file = path.join(__dirname, '../../../package.json');
16+
const file = path.join(__dirname, '../../../package.json');
1717

1818
mock({
1919
[file]: '{"version":"1.24.0"}'
2020
});
2121
});
2222

23-
afterEach(function () {
23+
after(() => {
2424
mock.restore();
2525
process.versions = processVersions;
2626
});
2727

28-
it('Can be turned off via options', () => {
28+
it('can be turned off via options', () => {
2929
process.versions = {
3030
node: '12.0.0'
3131
};
3232

33-
let imgStr = cloudinary.image("hello", {
33+
const imgStr = cloudinary.image("hello", {
3434
format: "png",
35-
urlAnalytics: false
35+
analytics: false
3636
});
3737

38-
expect(imgStr).not.to.contain(`MAlhAM0`);
38+
assert.ok(!imgStr.includes('MAlhAM0'))
3939
});
4040

41-
it('Defaults to true even if analytics is not passed as an option', () => {
41+
it('defaults to true even if analytics is not passed as an option', () => {
4242
process.versions = {
4343
node: '12.0.0'
4444
};
4545

46-
let imgStr = cloudinary.image("hello", {
46+
const imgStr = cloudinary.image("hello", {
4747
format: "png"
4848
});
4949

50-
expect(imgStr).to.contain(`MAlhAM0`);
50+
assert.ok(imgStr.includes('MAlhAM0'))
5151
});
5252

53-
it('Reads from process.versions and package.json (Mocked)', () => {
53+
it('reads from process.versions and package.json (Mocked)', () => {
5454
process.versions = {
5555
node: '12.0.0'
5656
};
5757

58-
let imgStr = cloudinary.image("hello", {
58+
const imgStr = cloudinary.image("hello", {
5959
format: "png",
60-
urlAnalytics: true
60+
analytics: true
6161
});
6262

63-
expect(imgStr).to.contain(`src='https://res.cloudinary.com/${TEST_CLOUD_NAME}/image/upload/hello.png?_a=BAMAlhAM0`);
63+
assert.ok(imgStr.includes('?_a=BAMAlhAM0'));
6464
});
6565

66-
it('Reads from process.versions and package.json (Mocked) - Responsive', () => {
66+
it('reads from process.versions and package.json (Mocked) - Responsive', () => {
6767
process.versions = {
6868
node: '12.0.0'
6969
};
7070

71-
let imgStr = cloudinary.image("hello", {
71+
const imgStr = cloudinary.image("hello", {
7272
format: "png",
7373
responsive: true,
74-
urlAnalytics: true
74+
analytics: true
7575
});
7676

77-
expect(imgStr).to.contain(`src='https://res.cloudinary.com/${TEST_CLOUD_NAME}/image/upload/hello.png?_a=BAMAlhAMA`);
77+
assert.ok(imgStr.includes('?_a=BAMAlhAMA'));
7878
});
7979

80-
it('Reads from tracked analytics configuration', () => {
80+
it('reads from tracked analytics configuration', () => {
8181
process.versions = {
8282
node: '12.0.0'
8383
};
8484

85-
let imgStr = cloudinary.image("hello", {
85+
const imgStr = cloudinary.image("hello", {
86+
format: "png",
87+
analytics: true,
88+
sdk_code: "X",
89+
sdk_semver: "7.3.0",
90+
tech_version: "3.4.7",
91+
product: 'B'
92+
});
93+
94+
assert.ok(imgStr.includes('?_a=BBXAEzGT0'));
95+
});
96+
97+
it('should still accept analytics param passed as camel case', () => {
98+
process.versions = {
99+
node: '12.0.0'
100+
};
101+
102+
const imgStr = cloudinary.image("hello", {
86103
format: "png",
87104
urlAnalytics: true,
88105
sdkCode: "X",
@@ -91,6 +108,36 @@ describe('Tests for sdk analytics through image tag', function () {
91108
product: 'B'
92109
});
93110

94-
expect(imgStr).to.contain(`src='https://res.cloudinary.com/${TEST_CLOUD_NAME}/image/upload/hello.png?_a=BBXAEzGT0`);
111+
assert.ok(imgStr.includes('?_a=BBXAEzGT0'));
112+
});
113+
114+
describe('with two different casings', () => {
115+
it('should treat camel case analytics param as more important than snake case', () => {
116+
process.versions = {
117+
node: '12.0.0'
118+
};
119+
120+
const imgStr1 = cloudinary.image("hello", {
121+
format: "png",
122+
urlAnalytics: true,
123+
analytics: false,
124+
sdkCode: "X",
125+
sdkSemver: "7.3.0",
126+
techVersion: "3.4.7",
127+
product: 'B'
128+
});
129+
assert.ok(imgStr1.includes('?_a=BBXAEzGT0'));
130+
131+
const imgStr2 = cloudinary.image("hello", {
132+
format: "png",
133+
analytics: true,
134+
sdkCode: "X",
135+
sdkSemver: "7.3.0",
136+
techVersion: "3.4.7",
137+
tech_version: "1.2.3",
138+
product: 'B'
139+
});
140+
assert.ok(imgStr2.includes('?_a=BBXAEzGT0'));
141+
});
95142
});
96143
});

0 commit comments

Comments
 (0)