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.

99 lines
2.5 KiB

  1. package api
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "math/big"
  8. "testing"
  9. "time"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. type testAuth struct {
  15. EthAddr string `json:"hezEthereumAddress" binding:"required"`
  16. BJJ string `json:"bjj" binding:"required"`
  17. Signature string `json:"signature" binding:"required"`
  18. Timestamp time.Time `json:"timestamp"`
  19. }
  20. func genTestAuths(auths []*common.AccountCreationAuth) []testAuth {
  21. testAuths := []testAuth{}
  22. for _, auth := range auths {
  23. testAuths = append(testAuths, testAuth{
  24. EthAddr: ethAddrToHez(auth.EthAddr),
  25. BJJ: bjjToString(auth.BJJ),
  26. Signature: "0x" + hex.EncodeToString(auth.Signature),
  27. Timestamp: auth.Timestamp,
  28. })
  29. }
  30. return testAuths
  31. }
  32. func TestAccountCreationAuth(t *testing.T) {
  33. // POST
  34. endpoint := apiURL + "account-creation-authorization"
  35. for _, auth := range tc.auths {
  36. jsonAuthBytes, err := json.Marshal(auth)
  37. assert.NoError(t, err)
  38. jsonAuthReader := bytes.NewReader(jsonAuthBytes)
  39. fmt.Println(string(jsonAuthBytes))
  40. assert.NoError(
  41. t, doGoodReq(
  42. "POST",
  43. endpoint,
  44. jsonAuthReader, nil,
  45. ),
  46. )
  47. }
  48. // GET
  49. endpoint += "/"
  50. for _, auth := range tc.auths {
  51. fetchedAuth := testAuth{}
  52. assert.NoError(
  53. t, doGoodReq(
  54. "GET",
  55. endpoint+auth.EthAddr,
  56. nil, &fetchedAuth,
  57. ),
  58. )
  59. assertAuth(t, auth, fetchedAuth)
  60. }
  61. // POST
  62. // 400
  63. // Wrong addr
  64. badAuth := tc.auths[0]
  65. badAuth.EthAddr = ethAddrToHez(ethCommon.BigToAddress(big.NewInt(1)))
  66. jsonAuthBytes, err := json.Marshal(badAuth)
  67. assert.NoError(t, err)
  68. jsonAuthReader := bytes.NewReader(jsonAuthBytes)
  69. err = doBadReq("POST", endpoint, jsonAuthReader, 400)
  70. assert.NoError(t, err)
  71. // Wrong signature
  72. badAuth = tc.auths[0]
  73. badAuth.Signature = badAuth.Signature[:len(badAuth.Signature)-1]
  74. badAuth.Signature += "F"
  75. jsonAuthBytes, err = json.Marshal(badAuth)
  76. assert.NoError(t, err)
  77. jsonAuthReader = bytes.NewReader(jsonAuthBytes)
  78. err = doBadReq("POST", endpoint, jsonAuthReader, 400)
  79. assert.NoError(t, err)
  80. // GET
  81. // 400
  82. err = doBadReq("GET", endpoint+"hez:0xFooBar", nil, 400)
  83. assert.NoError(t, err)
  84. // 404
  85. err = doBadReq("GET", endpoint+"hez:0x0000000000000000000000000000000000000001", nil, 404)
  86. assert.NoError(t, err)
  87. }
  88. func assertAuth(t *testing.T, expected, actual testAuth) {
  89. // timestamp should be very close to now
  90. assert.Less(t, time.Now().UTC().Unix()-3, actual.Timestamp.Unix())
  91. expected.Timestamp = actual.Timestamp
  92. assert.Equal(t, expected, actual)
  93. }