Skip to content

Commit

Permalink
feat: add specific linking limit error for solana
Browse files Browse the repository at this point in the history
  • Loading branch information
clD11 committed Feb 5, 2024
1 parent 6b09228 commit 98fd75d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions services/wallet/controllers_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ func LinkSolanaAddress(s *Service) handlers.AppHandler {
return handlers.WrapError(model.ErrChallengeExpired, "linking challenge expired", http.StatusUnauthorized)
case errors.Is(err, model.ErrWalletNotFound):
return handlers.WrapError(model.ErrWalletNotFound, "rewards wallet not found", http.StatusNotFound)
case errors.Is(err, ErrTooManyCardsLinked):
return handlers.WrapError(ErrTooManyCardsLinked, "too many wallets linked", http.StatusConflict)
case errors.As(err, &solErr):
return handlers.WrapError(solErr, "invalid solana linking message", http.StatusUnauthorized)
default:
Expand Down
4 changes: 4 additions & 0 deletions services/wallet/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ func getEnvMaxCards(custodian string) int {
if v, err := strconv.Atoi(os.Getenv("ZEBPAY_WALLET_LINKING_LIMIT")); err == nil {
return v
}
case "solana":
if v, err := strconv.Atoi(os.Getenv("SOLANA_WALLET_LINKING_LIMIT")); err == nil {
return v
}
}
return 4
}
Expand Down
58 changes: 57 additions & 1 deletion services/wallet/datastore_pvt_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package wallet

import (
"os"
"testing"
"time"

"github.com/brave-intl/bat-go/libs/ptr"
should "github.com/stretchr/testify/assert"
must "github.com/stretchr/testify/require"
)

func TestCustodianLink_isLinked(t *testing.T) {
Expand Down Expand Up @@ -50,10 +52,64 @@ func TestCustodianLink_isLinked(t *testing.T) {
},
}

for _, tc := range tests {
for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
actual := tc.given.cl.isLinked()
should.Equal(t, tc.expected, actual)
})
}
}

func TestGetEnvMaxCards(t *testing.T) {
type tcGiven struct {
custodian string
key string
value string
}

type testCase struct {
name string
given tcGiven
exp int
}

tests := []testCase{
{
name: "solana",
given: tcGiven{
custodian: "solana",
key: "SOLANA_WALLET_LINKING_LIMIT",
value: "10",
},
exp: 10,
},
{
name: "non_existent_custodian",
given: tcGiven{
key: "NON_EXISTENT_CUSTODIAN",
value: "10",
},
exp: 4,
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {

t.Cleanup(func() {
err := os.Unsetenv(tc.given.key)
must.NoError(t, err)
})

err := os.Setenv(tc.given.key, tc.given.value)
must.NoError(t, err)

actual := getEnvMaxCards(tc.given.custodian)
should.Equal(t, tc.exp, actual)
})
}
}

0 comments on commit 98fd75d

Please sign in to comment.