@adobe/acc-js-sdk 1.0.2 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/util.test.js CHANGED
@@ -17,7 +17,8 @@ governing permissions and limitations under the License.
17
17
  *
18
18
  *********************************************************************************/
19
19
 
20
- const { Util } = require('../src/util.js');
20
+ const { Util } = require('../src/util.js');
21
+ const { SafeStorage, Cache } = require('../src/cache.js');
21
22
 
22
23
 
23
24
  describe('Util', function() {
@@ -80,6 +81,155 @@ describe('Util', function() {
80
81
  expect(Util.trim({hello:'Lead<sessiontoken xsi:type="xsd:string">Stuff</sessiontoken>Trail'})).toStrictEqual({hello:'Lead<sessiontoken xsi:type="xsd:string">***</sessiontoken>Trail'});
81
82
  })
82
83
 
84
+ it("Should remove password", () => {
85
+ expect(Util.trim({hello:`<sessiontoken xsi:type="xsd:string"/><login xsi:type="xsd:string">admin</login><password xsi:type="xsd:string">password</password><parameters xsi:type="ns:Element" SOAP-ENV:encodingStyle="http://xml.apache.org/xml-soap/literalxml"/>`})).toStrictEqual({hello:`<sessiontoken xsi:type="xsd:string"/><login xsi:type="xsd:string">admin</login><password xsi:type="xsd:string">***</password><parameters xsi:type="ns:Element" SOAP-ENV:encodingStyle="http://xml.apache.org/xml-soap/literalxml"/>`});
86
+ })
87
+
88
+ it("Should hide X-Security-Token properties", () => {
89
+ expect(Util.trim({"x-security-token": "Hello"})).toMatchObject({"x-security-token": "***"});
90
+ expect(Util.trim({"X-Security-Token": "Hello"})).toMatchObject({"X-Security-Token": "***"});
91
+ })
92
+
93
+ it("Should remove session tokens from cookies", () => {
94
+ expect(Util.trim({"Cookie": "__sessiontoken=ABC"})).toMatchObject({"Cookie": "__sessiontoken=***"});
95
+ expect(Util.trim({"Cookie": "__sessionToken=ABC"})).toMatchObject({"Cookie": "__sessionToken=***"});
96
+ expect(Util.trim({"Cookie": "__sessiontoken=ABC;"})).toMatchObject({"Cookie": "__sessiontoken=***;"});
97
+ expect(Util.trim({"Cookie": "__sessiontoken =ABC"})).toMatchObject({"Cookie": "__sessiontoken =***"});
98
+ expect(Util.trim({"Cookie": "__sessiontoken ABC"})).toMatchObject({"Cookie": "__sessiontoken ABC"}); // no = sign => no token value
99
+ expect(Util.trim({"Cookie": "a=b; __sessiontoken =ABC"})).toMatchObject({"Cookie": "a=b; __sessiontoken =***"});
100
+ expect(Util.trim({"Cookie": "a=b; __sessiontoken =ABC; c=d"})).toMatchObject({"Cookie": "a=b; __sessiontoken =***; c=d"});
101
+ expect(Util.trim({"Cookie": "a=b; __token =ABC; c=d"})).toMatchObject({"Cookie": "a=b; __token =ABC; c=d"});
102
+ })
103
+ })
104
+
105
+
106
+ describe("Safe storage", () => {
107
+ it("Should support undefined delegate", () => {
108
+ const storage = new SafeStorage();
109
+ expect(storage.getItem("Hello")).toBeUndefined();
110
+ storage.setItem("Hello", { text: "World" });
111
+ expect(storage.getItem("Hello")).toBeUndefined();
112
+ storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
113
+ expect(storage.getItem("Hello")).toBeUndefined();
114
+ storage.removeItem("Hello");
115
+ expect(storage.getItem("Hello")).toBeUndefined();
116
+ })
117
+
118
+ it("Should handle map", () => {
119
+ const map = {};
120
+ const delegate = {
121
+ getItem: (key) => map[key],
122
+ setItem: (key, value) => { map[key] = value },
123
+ removeItem: (key) => { delete map[key] }
124
+ };
125
+ const storage = new SafeStorage(delegate);
126
+ expect(storage.getItem("Hello")).toBeUndefined();
127
+ storage.setItem("Hello", { text: "World" });
128
+ expect(map["Hello"]).toStrictEqual(JSON.stringify({text: "World"}));
129
+ expect(storage.getItem("Hello")).toStrictEqual({"text": "World"});
130
+ storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
131
+ expect(storage.getItem("Hello")).toBeUndefined();
132
+ storage.setItem("Hello", { text: "World" });
133
+ storage.removeItem("Hello");
134
+ expect(storage.getItem("Hello")).toBeUndefined();
135
+ })
136
+
137
+ it("Should handle root key", () => {
138
+ const map = {};
139
+ const delegate = {
140
+ getItem: (key) => map[key],
141
+ setItem: (key, value) => { map[key] = value },
142
+ removeItem: (key) => { delete map[key] }
143
+ };
144
+ const storage = new SafeStorage(delegate, "root");
145
+ expect(storage.getItem("Hello")).toBeUndefined();
146
+ storage.setItem("Hello", { text: "World" });
147
+ expect(map["root$Hello"]).toStrictEqual(JSON.stringify({text: "World"}));
148
+ expect(storage.getItem("Hello")).toStrictEqual({"text": "World"});
149
+ storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
150
+ expect(map["root$Hello"]).toBeUndefined();
151
+ expect(storage.getItem("Hello")).toBeUndefined();
152
+ storage.setItem("Hello", { text: "World" });
153
+ storage.removeItem("Hello");
154
+ expect(map["root$Hello"]).toBeUndefined();
155
+ expect(storage.getItem("Hello")).toBeUndefined();
156
+ })
157
+
158
+ it("Edge cases", () => {
159
+ expect(new SafeStorage()._storage).toBeUndefined();
160
+ expect(new SafeStorage()._rootKey).toBe("");
161
+ expect(new SafeStorage(null)._storage).toBeUndefined();
162
+ expect(new SafeStorage(null)._rootKey).toBe("");
163
+ expect(new SafeStorage(null, "")._storage).toBeUndefined();
164
+ expect(new SafeStorage(null, "")._rootKey).toBe("");
165
+ })
166
+
167
+ it("Should remove invalid items on get", () => {
168
+ const map = {};
169
+ const delegate = {
170
+ getItem: (key) => map[key],
171
+ setItem: (key, value) => { map[key] = value },
172
+ removeItem: (key) => { delete map[key] }
173
+ };
174
+ const storage = new SafeStorage(delegate, "root");
175
+ // value is not valid because not a JSON serialized string
176
+ map["root$Hello"] = "Invalid";
177
+ expect(storage.getItem("Hello")).toBeUndefined();
178
+ // Get should have removed invalid value
179
+ expect(map["root$Hello"]).toBeUndefined()
180
+ })
181
+
182
+ it("Should handle cache last cleared", () => {
183
+ const map = {};
184
+ const delegate = {
185
+ getItem: (key) => map[key],
186
+ setItem: (key, value) => { map[key] = value },
187
+ removeItem: (key) => { delete map[key] }
188
+ };
189
+ const cache = new Cache(delegate, "root");
190
+ cache.put("Hello", "World");
191
+ expect(JSON.parse(map["root$Hello"])).toMatchObject({ value:"World" });
192
+ expect(cache.get("Hello")).toBe("World");
193
+ cache.clear();
194
+ // Clear could not remove the item from the map
195
+ expect(JSON.parse(map["root$Hello"])).toMatchObject({ value:"World" });
196
+ // But get from cache will
197
+ expect(cache.get("Hello")).toBeUndefined();
198
+ })
199
+ })
200
+
201
+ it("Should preserve last cleared", () => {
202
+ const map = {};
203
+ const delegate = {
204
+ getItem: (key) => map[key],
205
+ setItem: (key, value) => { map[key] = value },
206
+ removeItem: (key) => { delete map[key] }
207
+ };
208
+ const cache = new Cache(delegate, "root");
209
+ expect(cache._lastCleared).toBeUndefined();
210
+ cache.put("Hello", "World");
211
+ cache.clear();
212
+ const lastCleared = cache._lastCleared;
213
+ expect(lastCleared).not.toBeUndefined();
214
+ expect(cache.get("Hello")).toBeUndefined();
215
+ expect(map["root$lastCleared"]).toBe(JSON.stringify({timestamp:lastCleared}));
216
+ // New cache with same delegate storage should preserve lastCleared date
217
+ const cache2 = new Cache(delegate, "root");
218
+ expect(cache2._lastCleared).toBe(lastCleared);
219
+ })
220
+
221
+ it("Should cache in memory value which is in local storage", () => {
222
+ const map = {};
223
+ const delegate = {
224
+ getItem: (key) => map[key],
225
+ setItem: (key, value) => { map[key] = value },
226
+ removeItem: (key) => { delete map[key] }
227
+ };
228
+ const cache = new Cache(delegate, "root");
229
+ map["root$Hello"] = JSON.stringify({ value: "World", cachedAt:Date.now() + 99999999 });
230
+ const value = cache.get("Hello");
231
+ expect(value).toBe("World");
232
+ expect(cache._cache["Hello"].value).toBe("World");
83
233
  })
84
234
 
85
235
  });