From 2ebcbcd7f23fca074475fa40099a63a6ddc5fc08 Mon Sep 17 00:00:00 2001 From: Jackson Date: Fri, 22 Nov 2024 16:04:03 -0500 Subject: [PATCH] Move redaction into function and add test --- libs/wallet/provider/uphold/uphold.go | 16 ++++++++++------ libs/wallet/provider/uphold/uphold_test.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libs/wallet/provider/uphold/uphold.go b/libs/wallet/provider/uphold/uphold.go index 8e1ac7564..173b449cb 100644 --- a/libs/wallet/provider/uphold/uphold.go +++ b/libs/wallet/provider/uphold/uphold.go @@ -237,12 +237,7 @@ func submit( if err != nil { return nil, resp, fmt.Errorf("%w: %s", errorutils.ErrFailedBodyRead, err.Error()) } - sbody := string(body) - for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { - re := regexp.MustCompile(p) - sbody = re.ReplaceAllString(sbody, "") - } - sbody = regexp.MustCompile(`,}`).ReplaceAllString(sbody, "}") + sbody := redactUnneededContent(string(body)) if logger != nil { logger.Debug(). @@ -1187,3 +1182,12 @@ func FundWallet(ctx context.Context, destWallet *Wallet, amount decimal.Decimal) return balance.TotalProbi, nil } + +func redactUnneededContent(sbody string) string { + for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} { + re := regexp.MustCompile(p) + sbody = re.ReplaceAllString(sbody, "") + } + sbody = strings.ReplaceAll(sbody, ",}", "}") + return sbody +} diff --git a/libs/wallet/provider/uphold/uphold_test.go b/libs/wallet/provider/uphold/uphold_test.go index 84d5f023f..45cccd3e6 100644 --- a/libs/wallet/provider/uphold/uphold_test.go +++ b/libs/wallet/provider/uphold/uphold_test.go @@ -3,6 +3,7 @@ package uphold import ( "context" "encoding/hex" + "encoding/json" "errors" "net/http" "net/url" @@ -351,3 +352,14 @@ func requireDonorWallet(t *testing.T) *Wallet { return &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey} } + +func TestRedactUnneeded(t *testing.T) { + response := `{"description":"some unneeded content","UKCountry":"foo","TestCountry":"bar","NoMatch": true}` + result := `{"NoMatch": true}` + testValue := redactUnneededContent(response) + assert.Equal(t, result, testValue) + var dat map[string]interface{} + if err := json.Unmarshal([]byte(testValue), &dat); err != nil { + t.Fatal(err) + } +}