You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/apitypes"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. "github.com/mitchellh/copystructure"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. type testAccount struct {
  14. ItemID uint64 `json:"itemId"`
  15. Idx apitypes.HezIdx `json:"accountIndex"`
  16. BatchNum common.BatchNum `json:"batchNum"`
  17. PublicKey apitypes.HezBJJ `json:"bjj"`
  18. EthAddr apitypes.HezEthAddr `json:"hezEthereumAddress"`
  19. Nonce common.Nonce `json:"nonce"`
  20. Balance *apitypes.BigIntStr `json:"balance"`
  21. Token historydb.TokenWithUSD `json:"token"`
  22. }
  23. type testAccountsResponse struct {
  24. Accounts []testAccount `json:"accounts"`
  25. PendingItems uint64 `json:"pendingItems"`
  26. }
  27. func (t testAccountsResponse) GetPending() (pendingItems, lastItemID uint64) {
  28. pendingItems = t.PendingItems
  29. lastItemID = t.Accounts[len(t.Accounts)-1].ItemID
  30. return pendingItems, lastItemID
  31. }
  32. func (t *testAccountsResponse) Len() int { return len(t.Accounts) }
  33. func (t testAccountsResponse) New() Pendinger { return &testAccountsResponse{} }
  34. func genTestAccounts(accounts []common.Account, tokens []historydb.TokenWithUSD) []testAccount {
  35. tAccounts := []testAccount{}
  36. for x, account := range accounts {
  37. token := getTokenByID(account.TokenID, tokens)
  38. tAccount := testAccount{
  39. ItemID: uint64(x + 1),
  40. Idx: apitypes.HezIdx(idxToHez(account.Idx, token.Symbol)),
  41. PublicKey: apitypes.NewHezBJJ(account.BJJ),
  42. EthAddr: apitypes.NewHezEthAddr(account.EthAddr),
  43. Nonce: account.Nonce,
  44. Balance: apitypes.NewBigIntStr(account.Balance),
  45. Token: token,
  46. }
  47. tAccounts = append(tAccounts, tAccount)
  48. }
  49. return tAccounts
  50. }
  51. func TestGetAccounts(t *testing.T) {
  52. endpoint := apiURL + "accounts"
  53. fetchedAccounts := []testAccount{}
  54. appendIter := func(intr interface{}) {
  55. for i := 0; i < len(intr.(*testAccountsResponse).Accounts); i++ {
  56. tmp, err := copystructure.Copy(intr.(*testAccountsResponse).Accounts[i])
  57. if err != nil {
  58. panic(err)
  59. }
  60. fetchedAccounts = append(fetchedAccounts, tmp.(testAccount))
  61. }
  62. }
  63. limit := 5
  64. stringIds := strconv.Itoa(int(tc.tokens[2].TokenID)) + "," + strconv.Itoa(int(tc.tokens[5].TokenID)) + "," + strconv.Itoa(int(tc.tokens[6].TokenID))
  65. // Filter by BJJ
  66. path := fmt.Sprintf("%s?BJJ=%s&limit=%d", endpoint, tc.accounts[0].PublicKey, limit)
  67. err := doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
  68. require.NoError(t, err)
  69. assert.Greater(t, len(fetchedAccounts), 0)
  70. assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
  71. fetchedAccounts = []testAccount{}
  72. // Filter by ethAddr
  73. path = fmt.Sprintf("%s?hezEthereumAddress=%s&limit=%d", endpoint, tc.accounts[3].EthAddr, limit)
  74. err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
  75. require.NoError(t, err)
  76. assert.Greater(t, len(fetchedAccounts), 0)
  77. assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
  78. fetchedAccounts = []testAccount{}
  79. // both filters (incompatible)
  80. path = fmt.Sprintf("%s?hezEthereumAddress=%s&BJJ=%s&limit=%d", endpoint, tc.accounts[0].EthAddr, tc.accounts[0].PublicKey, limit)
  81. err = doBadReq("GET", path, nil, 400)
  82. require.NoError(t, err)
  83. fetchedAccounts = []testAccount{}
  84. // Filter by token IDs
  85. path = fmt.Sprintf("%s?tokenIds=%s&limit=%d", endpoint, stringIds, limit)
  86. err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
  87. require.NoError(t, err)
  88. assert.Greater(t, len(fetchedAccounts), 0)
  89. assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
  90. fetchedAccounts = []testAccount{}
  91. // Token Ids + bjj
  92. path = fmt.Sprintf("%s?tokenIds=%s&BJJ=%s&limit=%d", endpoint, stringIds, tc.accounts[10].PublicKey, limit)
  93. err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
  94. require.NoError(t, err)
  95. assert.Greater(t, len(fetchedAccounts), 0)
  96. assert.LessOrEqual(t, len(fetchedAccounts), len(tc.accounts))
  97. fetchedAccounts = []testAccount{}
  98. // No filters (checks response content)
  99. path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
  100. err = doGoodReqPaginated(path, historydb.OrderAsc, &testAccountsResponse{}, appendIter)
  101. require.NoError(t, err)
  102. assert.Equal(t, len(tc.accounts), len(fetchedAccounts))
  103. for i := 0; i < len(fetchedAccounts); i++ {
  104. fetchedAccounts[i].Token.ItemID = 0
  105. if tc.accounts[i].Token.USDUpdate != nil {
  106. assert.Less(t, fetchedAccounts[i].Token.USDUpdate.Unix()-3, tc.accounts[i].Token.USDUpdate.Unix())
  107. fetchedAccounts[i].Token.USDUpdate = tc.accounts[i].Token.USDUpdate
  108. }
  109. assert.Equal(t, tc.accounts[i], fetchedAccounts[i])
  110. }
  111. // No filters Reverse Order (checks response content)
  112. reversedAccounts := []testAccount{}
  113. appendIter = func(intr interface{}) {
  114. for i := 0; i < len(intr.(*testAccountsResponse).Accounts); i++ {
  115. tmp, err := copystructure.Copy(intr.(*testAccountsResponse).Accounts[i])
  116. if err != nil {
  117. panic(err)
  118. }
  119. reversedAccounts = append(reversedAccounts, tmp.(testAccount))
  120. }
  121. }
  122. err = doGoodReqPaginated(path, historydb.OrderDesc, &testAccountsResponse{}, appendIter)
  123. require.NoError(t, err)
  124. assert.Equal(t, len(reversedAccounts), len(fetchedAccounts))
  125. for i := 0; i < len(fetchedAccounts); i++ {
  126. reversedAccounts[i].Token.ItemID = 0
  127. fetchedAccounts[len(fetchedAccounts)-1-i].Token.ItemID = 0
  128. if reversedAccounts[i].Token.USDUpdate != nil {
  129. assert.Less(t, fetchedAccounts[len(fetchedAccounts)-1-i].Token.USDUpdate.Unix()-3, reversedAccounts[i].Token.USDUpdate.Unix())
  130. fetchedAccounts[len(fetchedAccounts)-1-i].Token.USDUpdate = reversedAccounts[i].Token.USDUpdate
  131. }
  132. assert.Equal(t, reversedAccounts[i], fetchedAccounts[len(fetchedAccounts)-1-i])
  133. }
  134. // 400
  135. path = fmt.Sprintf("%s?hezEthereumAddress=hez:0x123456", endpoint)
  136. err = doBadReq("GET", path, nil, 400)
  137. require.NoError(t, err)
  138. // Test GetAccount
  139. path = fmt.Sprintf("%s/%s", endpoint, fetchedAccounts[2].Idx)
  140. account := testAccount{}
  141. require.NoError(t, doGoodReq("GET", path, nil, &account))
  142. account.Token.ItemID = 0
  143. assert.Equal(t, fetchedAccounts[2], account)
  144. // 400
  145. path = fmt.Sprintf("%s/hez:12345", endpoint)
  146. err = doBadReq("GET", path, nil, 400)
  147. require.NoError(t, err)
  148. // 404
  149. path = fmt.Sprintf("%s/hez:10:12345", endpoint)
  150. err = doBadReq("GET", path, nil, 404)
  151. require.NoError(t, err)
  152. }