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.

190 lines
4.4 KiB

  1. package server_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/gorilla/mux"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. keys "github.com/tendermint/go-keys"
  12. "github.com/tendermint/go-keys/cryptostore"
  13. "github.com/tendermint/go-keys/server"
  14. "github.com/tendermint/go-keys/server/types"
  15. "github.com/tendermint/go-keys/storage/memstorage"
  16. )
  17. func TestKeyServer(t *testing.T) {
  18. assert, require := assert.New(t), require.New(t)
  19. r := setupServer()
  20. // let's abstract this out a bit....
  21. keys, code, err := listKeys(r)
  22. require.Nil(err)
  23. require.Equal(http.StatusOK, code)
  24. assert.Equal(0, len(keys))
  25. algo := "ed25519"
  26. n1, n2 := "personal", "business"
  27. p0, p1, p2 := "1234", "over10chars...", "really-secure!@#$"
  28. // this fails for validation
  29. _, code, err = createKey(r, n1, p0, algo)
  30. require.Nil(err, "%+v", err)
  31. require.NotEqual(http.StatusOK, code)
  32. // new password better
  33. key, code, err := createKey(r, n1, p1, algo)
  34. require.Nil(err, "%+v", err)
  35. require.Equal(http.StatusOK, code)
  36. require.Equal(key.Name, n1)
  37. // the other one works
  38. key2, code, err := createKey(r, n2, p2, algo)
  39. require.Nil(err, "%+v", err)
  40. require.Equal(http.StatusOK, code)
  41. require.Equal(key2.Name, n2)
  42. // let's abstract this out a bit....
  43. keys, code, err = listKeys(r)
  44. require.Nil(err)
  45. require.Equal(http.StatusOK, code)
  46. if assert.Equal(2, len(keys)) {
  47. // in alphabetical order
  48. assert.Equal(keys[0].Name, n2)
  49. assert.Equal(keys[1].Name, n1)
  50. }
  51. // get works
  52. k, code, err := getKey(r, n1)
  53. require.Nil(err, "%+v", err)
  54. require.Equal(http.StatusOK, code)
  55. assert.Equal(k.Name, n1)
  56. assert.NotNil(k.Address)
  57. assert.Equal(k.Address, key.Address)
  58. // delete with proper key
  59. _, code, err = deleteKey(r, n1, p1)
  60. require.Nil(err, "%+v", err)
  61. require.Equal(http.StatusOK, code)
  62. // after delete, get and list different
  63. _, code, err = getKey(r, n1)
  64. require.Nil(err, "%+v", err)
  65. require.NotEqual(http.StatusOK, code)
  66. keys, code, err = listKeys(r)
  67. require.Nil(err, "%+v", err)
  68. require.Equal(http.StatusOK, code)
  69. if assert.Equal(1, len(keys)) {
  70. assert.Equal(keys[0].Name, n2)
  71. }
  72. }
  73. func setupServer() http.Handler {
  74. // make the storage with reasonable defaults
  75. cstore := cryptostore.New(
  76. cryptostore.SecretBox,
  77. memstorage.New(),
  78. )
  79. // build your http server
  80. ks := server.New(cstore, "ed25519")
  81. r := mux.NewRouter()
  82. sk := r.PathPrefix("/keys").Subrouter()
  83. ks.Register(sk)
  84. return r
  85. }
  86. // return data, status code, and error
  87. func listKeys(h http.Handler) (keys.Infos, int, error) {
  88. rr := httptest.NewRecorder()
  89. req, err := http.NewRequest("GET", "/keys/", nil)
  90. if err != nil {
  91. return nil, 0, err
  92. }
  93. h.ServeHTTP(rr, req)
  94. if http.StatusOK != rr.Code {
  95. return nil, rr.Code, nil
  96. }
  97. data := keys.Infos{}
  98. err = json.Unmarshal(rr.Body.Bytes(), &data)
  99. return data, rr.Code, err
  100. }
  101. func getKey(h http.Handler, name string) (*keys.Info, int, error) {
  102. rr := httptest.NewRecorder()
  103. req, err := http.NewRequest("GET", "/keys/"+name, nil)
  104. if err != nil {
  105. return nil, 0, err
  106. }
  107. h.ServeHTTP(rr, req)
  108. if http.StatusOK != rr.Code {
  109. return nil, rr.Code, nil
  110. }
  111. data := keys.Info{}
  112. err = json.Unmarshal(rr.Body.Bytes(), &data)
  113. return &data, rr.Code, err
  114. }
  115. func createKey(h http.Handler, name, passphrase, algo string) (*keys.Info, int, error) {
  116. rr := httptest.NewRecorder()
  117. post := types.CreateKeyRequest{
  118. Name: name,
  119. Passphrase: passphrase,
  120. Algo: algo,
  121. }
  122. var b bytes.Buffer
  123. err := json.NewEncoder(&b).Encode(&post)
  124. if err != nil {
  125. return nil, 0, err
  126. }
  127. req, err := http.NewRequest("POST", "/keys/", &b)
  128. if err != nil {
  129. return nil, 0, err
  130. }
  131. h.ServeHTTP(rr, req)
  132. if http.StatusOK != rr.Code {
  133. return nil, rr.Code, nil
  134. }
  135. data := keys.Info{}
  136. err = json.Unmarshal(rr.Body.Bytes(), &data)
  137. return &data, rr.Code, err
  138. }
  139. func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) {
  140. rr := httptest.NewRecorder()
  141. post := types.DeleteKeyRequest{
  142. Name: name,
  143. Passphrase: passphrase,
  144. }
  145. var b bytes.Buffer
  146. err := json.NewEncoder(&b).Encode(&post)
  147. if err != nil {
  148. return nil, 0, err
  149. }
  150. req, err := http.NewRequest("DELETE", "/keys/"+name, &b)
  151. if err != nil {
  152. return nil, 0, err
  153. }
  154. h.ServeHTTP(rr, req)
  155. if http.StatusOK != rr.Code {
  156. return nil, rr.Code, nil
  157. }
  158. data := types.ErrorResponse{}
  159. err = json.Unmarshal(rr.Body.Bytes(), &data)
  160. return &data, rr.Code, err
  161. }