Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Dec 9, 2021
1 parent 653c707 commit c3d959d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
14 changes: 9 additions & 5 deletions x/profiles/types/models_chain_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,17 @@ func NewEthAddress(value, prefix string) *EthAddress {
}

func (e EthAddress) Validate() error {
addr := e.Value[len(e.Prefix):]
if strings.TrimSpace(addr) == "" {
return fmt.Errorf("address cannot be empty or blank")
if len(strings.TrimSpace(e.Value)) <= len(e.Prefix) {
return fmt.Errorf("address cannot be smaller than prefix")
}

prefix, addrWithoutPrefix := e.Value[:len(e.Prefix)], e.Value[len(e.Prefix):]
if prefix != e.Prefix {
return fmt.Errorf("prefix does not match")
}

if _, err := hex.DecodeString(addr); err != nil {
return fmt.Errorf("invalid Hex address")
if _, err := hex.DecodeString(addrWithoutPrefix); err != nil {
return fmt.Errorf("invalid Eth address")
}
return nil
}
Expand Down
49 changes: 49 additions & 0 deletions x/profiles/types/models_chain_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,55 @@ func TestBase58Address_GetValue(t *testing.T) {

// --------------------------------------------------------------------------------------------------------------------

func TestEthAddress_Validate(t *testing.T) {
testCases := []struct {
name string
address *types.EthAddress
shouldErr bool
}{
{
name: "address smaller than prefix returns error",
address: types.NewEthAddress("", ""),
shouldErr: true,
},
{
name: "prefix does not match returns error",
address: types.NewEthAddress("0184", "0x"),
shouldErr: true,
},
{
name: "invalid address returns error",
address: types.NewEthAddress("0OiIjJ", "0x"),
shouldErr: true,
},
{
name: "valid address returns no error",
address: types.NewEthAddress("0x941991947B6eC9F5537bcaC30C1295E8154Df4cC", "0x"),
shouldErr: false,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := tc.address.Validate()

if tc.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestEthAddress_GetValue(t *testing.T) {
data := types.NewEthAddress("0x941991947B6eC9F5537bcaC30C1295E8154Df4cC", "0x")
require.Equal(t, "0x941991947B6eC9F5537bcaC30C1295E8154Df4cC", data.GetValue())
}

// --------------------------------------------------------------------------------------------------------------------

func TestUnpackAddressData(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit c3d959d

Please sign in to comment.