github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/request/get_account_key_test.go (about)

     1  package request
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  )
    10  
    11  func Test_GetAccountKey_InvalidParse(t *testing.T) {
    12  	var getAccountKey GetAccountKey
    13  
    14  	tests := []struct {
    15  		name    string
    16  		address string
    17  		index   string
    18  		height  string
    19  		err     string
    20  	}{
    21  		{
    22  			"parse with invalid address",
    23  			"0xxxaddr",
    24  			"1",
    25  			"100",
    26  			"invalid address",
    27  		},
    28  		{
    29  			"parse with invalid keyIndex",
    30  			"0xf8d6e0586b0a20c7",
    31  			"-1.2",
    32  			"100",
    33  			"invalid key index: value must be an unsigned 64 bit integer",
    34  		},
    35  		{
    36  			"parse with invalid height",
    37  			"0xf8d6e0586b0a20c7",
    38  			"2",
    39  			"-100",
    40  			"invalid height format",
    41  		},
    42  	}
    43  
    44  	chain := flow.Localnet.Chain()
    45  	for _, test := range tests {
    46  		t.Run(test.name, func(t *testing.T) {
    47  			err := getAccountKey.Parse(test.address, test.index, test.height, chain)
    48  			assert.EqualError(t, err, test.err)
    49  		})
    50  	}
    51  }
    52  
    53  func Test_GetAccountKey_ValidParse(t *testing.T) {
    54  	var getAccountKey GetAccountKey
    55  
    56  	addr := "f8d6e0586b0a20c7"
    57  	keyIndex := "5"
    58  	height := "100"
    59  	chain := flow.Localnet.Chain()
    60  	err := getAccountKey.Parse(addr, keyIndex, height, chain)
    61  	assert.NoError(t, err)
    62  	assert.Equal(t, getAccountKey.Address.String(), addr)
    63  	assert.Equal(t, getAccountKey.Index, uint64(5))
    64  	assert.Equal(t, getAccountKey.Height, uint64(100))
    65  
    66  	err = getAccountKey.Parse(addr, keyIndex, "", chain)
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, getAccountKey.Address.String(), addr)
    69  	assert.Equal(t, getAccountKey.Index, uint64(5))
    70  	assert.Equal(t, getAccountKey.Height, SealedHeight)
    71  
    72  	err = getAccountKey.Parse(addr, keyIndex, "sealed", chain)
    73  	assert.NoError(t, err)
    74  	assert.Equal(t, getAccountKey.Address.String(), addr)
    75  	assert.Equal(t, getAccountKey.Index, uint64(5))
    76  	assert.Equal(t, getAccountKey.Height, SealedHeight)
    77  
    78  	err = getAccountKey.Parse(addr, keyIndex, "final", chain)
    79  	assert.NoError(t, err)
    80  	assert.Equal(t, getAccountKey.Address.String(), addr)
    81  	assert.Equal(t, getAccountKey.Index, uint64(5))
    82  	assert.Equal(t, getAccountKey.Height, FinalHeight)
    83  }