|
1 | 1 | import each from "jest-each"; |
2 | 2 | import * as http from "http"; |
| 3 | +import * as _ from "lodash"; |
3 | 4 | import * as obfuscate from "../../../src/utils/obfuscate"; |
| 5 | +import { ObfuscationRule } from "../../../src/model"; |
| 6 | + |
| 7 | +import * as connectSdk from "../../../src"; |
| 8 | + |
| 9 | +connectSdk.init({ |
| 10 | + host: "example.org", |
| 11 | + scheme: "https", |
| 12 | + port: -1, |
| 13 | + enableLogging: false, |
| 14 | + apiKeyId: "", |
| 15 | + secretApiKey: "", |
| 16 | + integrator: "Integration tests" |
| 17 | +}); |
4 | 18 |
|
5 | 19 | /** |
6 | 20 | * @group unit:obfuscate |
7 | 21 | */ |
8 | 22 | describe("obfuscate.getObfuscated", () => { |
| 23 | + beforeEach(() => { |
| 24 | + delete connectSdk.context.getContext().obfuscationRules; |
| 25 | + }); |
| 26 | + |
9 | 27 | test("undefined body", () => { |
10 | 28 | expect(obfuscate.getObfuscated(undefined)).toBe(""); |
11 | 29 | }); |
@@ -44,6 +62,41 @@ describe("obfuscate.getObfuscated", () => { |
44 | 62 | expect(obfuscate.getObfuscated(body)).toBe(JSON.stringify(expected, null, 2)); |
45 | 63 | }); |
46 | 64 |
|
| 65 | + test("cardNumber with custom rule", () => { |
| 66 | + const body = { |
| 67 | + order: { |
| 68 | + amountOfMoney: { |
| 69 | + currencyCode: "CAD", |
| 70 | + amount: 2345 |
| 71 | + }, |
| 72 | + customer: { |
| 73 | + billingAddress: { |
| 74 | + countryCode: "CA" |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + cardPaymentMethodSpecificInput: { |
| 79 | + paymentProductId: 1, |
| 80 | + card: { |
| 81 | + cvv: "123", |
| 82 | + cardNumber: "1234567890123456", |
| 83 | + expiryDate: "1220" |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + const expected = JSON.parse(JSON.stringify(body)); |
| 88 | + expected.cardPaymentMethodSpecificInput.card.cvv = "***"; |
| 89 | + expected.cardPaymentMethodSpecificInput.card.cardNumber = "123456******3456"; |
| 90 | + expected.cardPaymentMethodSpecificInput.card.expiryDate = "**20"; |
| 91 | + |
| 92 | + const obfuscationRule: ObfuscationRule = value => value.substring(0, 6) + _.padStart("", 6, "*") + value.substring(12); |
| 93 | + connectSdk.context.getContext().obfuscationRules = { |
| 94 | + cardNumber: obfuscationRule |
| 95 | + }; |
| 96 | + |
| 97 | + expect(obfuscate.getObfuscated(body)).toBe(JSON.stringify(expected, null, 2)); |
| 98 | + }); |
| 99 | + |
47 | 100 | test("iban", () => { |
48 | 101 | const body = { |
49 | 102 | sepaDirectDebit: { |
@@ -140,4 +193,37 @@ describe("obfuscate.getObfuscated", () => { |
140 | 193 |
|
141 | 194 | expect(obfuscate.getObfuscated(headers, null, true)).toBe(JSON.stringify(expected, null, 2)); |
142 | 195 | }); |
| 196 | + |
| 197 | + const customHeadersTestData = [ |
| 198 | + ["Authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********"], |
| 199 | + ["authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********"], |
| 200 | + ["AUTHORIZATION", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********"], |
| 201 | + |
| 202 | + ["X-GCS-Authentication-Token", "foobar", "********"], |
| 203 | + ["x-gcs-authentication-token", "foobar", "********"], |
| 204 | + ["X-GCS-AUTHENTICATION-TOKEN", "foobar", "********"], |
| 205 | + |
| 206 | + ["X-GCS-CallerPassword", "foobar", "********"], |
| 207 | + ["x-gcs-callerpassword", "foobar", "********"], |
| 208 | + ["X-GCS-CALLERPASSWORD", "foobar", "********"], |
| 209 | + |
| 210 | + ["Content-Type", "application/json", "****************"], |
| 211 | + ["content-type", "application/json", "****************"], |
| 212 | + ["CONTENT-TYPE", "application/json", "****************"] |
| 213 | + ]; |
| 214 | + each(customHeadersTestData).test("when the header is '%s'", (name, originalValue, expectedObfuscatedValue) => { |
| 215 | + const headers: http.IncomingHttpHeaders = {}; |
| 216 | + headers[name] = originalValue; |
| 217 | + headers["content-length"] = "5"; |
| 218 | + |
| 219 | + const expected = JSON.parse(JSON.stringify(headers)); |
| 220 | + expected[name] = expectedObfuscatedValue; |
| 221 | + |
| 222 | + const obfuscationRule: ObfuscationRule = obfuscate.all(); |
| 223 | + connectSdk.context.getContext().obfuscationRules = { |
| 224 | + "content-type": obfuscationRule |
| 225 | + }; |
| 226 | + |
| 227 | + expect(obfuscate.getObfuscated(headers, null, true)).toBe(JSON.stringify(expected, null, 2)); |
| 228 | + }); |
143 | 229 | }); |
0 commit comments