github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/logging/LoggingUtil_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func CheckObfuscateHeaderWithMatch(t *testing.T, name, originalValue, expectedObfuscatedValue string) {
     8  	obfuscatedValue, err := ObfuscateHeader(name, originalValue)
     9  
    10  	if err != nil {
    11  		t.Fatal(err)
    12  	}
    13  
    14  	if obfuscatedValue != expectedObfuscatedValue {
    15  		t.Fatalf("CheckObfuscateHeaderWithMatch : expected '%s' got '%s'", expectedObfuscatedValue, obfuscatedValue)
    16  	}
    17  }
    18  
    19  func CheckObfuscateHeaderWithNoMatch(t *testing.T, name, originalValue string) {
    20  	obfuscatedValue, err := ObfuscateHeader(name, originalValue)
    21  
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	if obfuscatedValue != originalValue {
    27  		t.Fatalf("CheckObfuscateHeaderWithNoMatch : expected '%s' got '%s'", originalValue, obfuscatedValue)
    28  	}
    29  }
    30  
    31  func TestObfuscateHeader(t *testing.T) {
    32  	CheckObfuscateHeaderWithMatch(t, "Authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********")
    33  	CheckObfuscateHeaderWithMatch(t, "authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********")
    34  	CheckObfuscateHeaderWithMatch(t, "AUTHORIZATION", "Basic QWxhZGRpbjpPcGVuU2VzYW1l", "********")
    35  
    36  	CheckObfuscateHeaderWithMatch(t, "X-GCS-Authentication-Token", "foobar", "********")
    37  	CheckObfuscateHeaderWithMatch(t, "x-gcs-authentication-token", "foobar", "********")
    38  	CheckObfuscateHeaderWithMatch(t, "X-GCS-AUTHENTICATION-TOKEN", "foobar", "********")
    39  
    40  	CheckObfuscateHeaderWithMatch(t, "X-GCS-CallerPassword", "foobar", "********")
    41  	CheckObfuscateHeaderWithMatch(t, "x-gcs-callerpassword", "foobar", "********")
    42  	CheckObfuscateHeaderWithMatch(t, "X-GCS-CALLERPASSWORD", "foobar", "********")
    43  
    44  	CheckObfuscateHeaderWithNoMatch(t, "Content-Type", "application/json")
    45  	CheckObfuscateHeaderWithNoMatch(t, "content-type", "application/json")
    46  	CheckObfuscateHeaderWithNoMatch(t, "CONTENT-TYPE", "application/json")
    47  }
    48  
    49  func TestObfuscateBodyWithEmptyBody(t *testing.T) {
    50  	emptyString := ""
    51  	obfuscatedBody, err := ObfuscateBody(emptyString)
    52  
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	if obfuscatedBody != emptyString {
    58  		t.Fatalf("TestObfuscateBodyWithEmptyBody : expected '%s' got '%s'", emptyString, obfuscatedBody)
    59  	}
    60  }
    61  
    62  func TestObfuscateBodyWithCard(t *testing.T) {
    63  	cardObfuscated := `{
    64      "cardPaymentMethodSpecificInput": {
    65          "card": {
    66              "cardNumber": "************3456",
    67              "cvv": "***",
    68              "expiryDate": "**20"
    69          },
    70          "paymentProductId": 1
    71      },
    72      "order": {
    73          "amountOfMoney": {
    74              "amount": 2345,
    75              "currencyCode": "CAD"
    76          },
    77          "customer": {
    78              "billingAddress": {
    79                  "countryCode": "CA"
    80              }
    81          }
    82      }
    83  }`
    84  
    85  	cardUnObfuscated := `{
    86  	    "order": {
    87  	        "amountOfMoney": {
    88  	            "currencyCode": "CAD",
    89  	            "amount": 2345
    90  	        },
    91  	        "customer": {
    92  	            "billingAddress": {
    93  	                "countryCode": "CA"
    94  	            }
    95  	        }
    96  	    },
    97  	    "cardPaymentMethodSpecificInput": {
    98  	        "paymentProductId": 1,
    99  	        "card": {
   100  	            "cvv": "123",
   101  	            "cardNumber": "1234567890123456",
   102  	            "expiryDate": "1220"
   103  	        }
   104  	    }
   105  	}`
   106  
   107  	obfuscatedBody, err := ObfuscateBody(cardUnObfuscated)
   108  
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	if cardObfuscated != obfuscatedBody {
   114  		t.Fatalf("TestObfuscateBodyWithCard : expected \n%s\ngot\n%s", cardObfuscated, obfuscatedBody)
   115  	}
   116  }
   117  
   118  func TestObfuscateBodyWithIban(t *testing.T) {
   119  	ibanObfuscated := `{
   120      "paymentProductId": 770,
   121      "sepaDirectDebit": {
   122          "customer": {
   123              "billingAddress": {
   124                  "countryCode": "NL"
   125              }
   126          },
   127          "mandate": {
   128              "bankAccountIban": {
   129                  "iban": "**************4567"
   130              },
   131              "debtor": {
   132                  "surname": "Jones"
   133              },
   134              "isRecurring": false
   135          }
   136      }
   137  }`
   138  	ibanUnobfuscated := `{
   139      "sepaDirectDebit": {
   140          "mandate": {
   141              "bankAccountIban": {
   142                  "iban": "NL00INGB0001234567"
   143              },
   144              "debtor": {
   145                  "surname": "Jones"
   146              },
   147              "isRecurring": false
   148          },
   149          "customer": {
   150              "billingAddress": {
   151                  "countryCode": "NL"
   152              }
   153          }
   154      },
   155      "paymentProductId": 770
   156  }`
   157  	obfuscatedBody, err := ObfuscateBody(ibanUnobfuscated)
   158  
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	if ibanObfuscated != obfuscatedBody {
   164  		t.Fatalf("TestObfuscateBodyWithIban : expected \n%s\ngot\n%s", ibanObfuscated, obfuscatedBody)
   165  	}
   166  }
   167  
   168  func TestObfuscateBodyWithBin(t *testing.T) {
   169  	binObfuscated := `{
   170      "bin": "123456**"
   171  }`
   172  	binUnobfuscated := `{
   173      "bin": "12345678"
   174  }`
   175  
   176  	obfuscatedBody, err := ObfuscateBody(binUnobfuscated)
   177  
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  
   182  	if binObfuscated != obfuscatedBody {
   183  		t.Fatalf("TestObfuscateBodyWithBin : expected \n%s\ngot\n%s", binObfuscated, obfuscatedBody)
   184  	}
   185  }
   186  
   187  func TestObfuscateBodyWithNoMatches(t *testing.T) {
   188  	noObfuscation := `{
   189      "order": {
   190          "amountOfMoney": {
   191              "currencyCode": "EUR",
   192              "amount": 1000
   193          },
   194          "customer": {
   195              "locale": "nl_NL",
   196              "billingAddress": {
   197                  "countryCode": "NL"
   198              }
   199          }
   200      },
   201      "bankTransferPaymentMethodSpecificInput": {
   202          "paymentProductId": 11
   203      }
   204  }`
   205  	postObfuscation := `{
   206      "bankTransferPaymentMethodSpecificInput": {
   207          "paymentProductId": 11
   208      },
   209      "order": {
   210          "amountOfMoney": {
   211              "amount": 1000,
   212              "currencyCode": "EUR"
   213          },
   214          "customer": {
   215              "billingAddress": {
   216                  "countryCode": "NL"
   217              },
   218              "locale": "nl_NL"
   219          }
   220      }
   221  }`
   222  
   223  	obfuscatedBody, err := ObfuscateBody(noObfuscation)
   224  
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	if postObfuscation != obfuscatedBody {
   230  		t.Fatalf("TestObfuscateBodyWithNoMatches : expected \n%s\ngot\n%s", postObfuscation, obfuscatedBody)
   231  	}
   232  }
   233  
   234  func TestObfuscateBodyWithObject(t *testing.T) {
   235  	jsonObfuscated := `[
   236      {
   237          "value": "****"
   238      },
   239      {
   240          "value": {}
   241      }
   242  ]`
   243  	jsonUnobfuscated := `[ {
   244  	"value": true
   245  }, {
   246  	"value": {
   247  	}
   248  } ]`
   249  
   250  	obfuscatedBody, err := ObfuscateBody(jsonUnobfuscated)
   251  
   252  	if err != nil {
   253  		t.Fatal(err)
   254  	}
   255  
   256  	if jsonObfuscated != obfuscatedBody {
   257  		t.Fatalf("TestObfuscateBodyWithObject : expected \n%s\ngot\n%s", jsonObfuscated, obfuscatedBody)
   258  	}
   259  }