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.

97 lines
2.5 KiB

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