Skip to content

Commit

Permalink
Move redaction into function and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneagan committed Nov 22, 2024
1 parent dbdb02f commit 2ebcbcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libs/wallet/provider/uphold/uphold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions libs/wallet/provider/uphold/uphold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uphold
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"net/url"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2ebcbcd

Please sign in to comment.