client_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretserver
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "testing"
  21. "os"
  22. "github.com/DelineaXPM/tss-sdk-go/v2/server"
  23. "github.com/stretchr/testify/assert"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. )
  26. var (
  27. errNotFound = errors.New("not found")
  28. )
  29. type fakeAPI struct {
  30. secrets []*server.Secret
  31. }
  32. func printToScreen(w io.Writer, name interface{}) {
  33. fmt.Fprintf(w, "the value is %+v\n", name)
  34. }
  35. // createSecret assembles a server.Secret from file test_data.json.
  36. func createSecret(id int, name string) *server.Secret {
  37. var s = &server.Secret{}
  38. jsonFile, err := os.Open("test_data.json")
  39. if err != nil {
  40. printToScreen(os.Stdout, fmt.Sprintf("err opening json data file err = %+v \n\n", err.Error()))
  41. }
  42. byteValue, _ := ioutil.ReadAll(jsonFile)
  43. json.Unmarshal(byteValue, &s)
  44. s.ID = id
  45. s.Name = name
  46. return s
  47. }
  48. func (f *fakeAPI) Secret(id int) (*server.Secret, error) {
  49. for _, s := range f.secrets {
  50. if s.ID == id {
  51. /*
  52. printToScreen(os.Stdout, "found a match")
  53. */
  54. return s, nil
  55. }
  56. }
  57. return nil, errNotFound
  58. }
  59. func newTestClient() esv1beta1.SecretsClient {
  60. return &client{
  61. api: &fakeAPI{
  62. secrets: []*server.Secret{
  63. createSecret(1000, "robertOppenheimer"),
  64. createSecret(2000, "helloWorld"),
  65. createSecret(3000, "chuckTesta"),
  66. },
  67. },
  68. }
  69. }
  70. func TestGetSecret(t *testing.T) {
  71. ctx := context.Background()
  72. c := newTestClient()
  73. testCases := map[string]struct {
  74. ref esv1beta1.ExternalSecretDataRemoteRef
  75. want []byte
  76. err error
  77. }{
  78. "incorrect key returns nil and error": {
  79. ref: esv1beta1.ExternalSecretDataRemoteRef{
  80. Key: "0",
  81. },
  82. want: []byte(nil),
  83. err: errNotFound,
  84. },
  85. "key and property returns a single value": {
  86. ref: esv1beta1.ExternalSecretDataRemoteRef{
  87. Key: "1000",
  88. Property: "Name",
  89. },
  90. want: []byte(`robertOppenheimer`),
  91. },
  92. "key and nested property returns a single value": {
  93. ref: esv1beta1.ExternalSecretDataRemoteRef{
  94. Key: "2000",
  95. Property: "Items.2.ItemValue",
  96. },
  97. want: []byte(`l*3FFtvZpcXd`),
  98. },
  99. "existent key with non-existing propery": {
  100. ref: esv1beta1.ExternalSecretDataRemoteRef{
  101. Key: "3000",
  102. Property: "foo.bar.x",
  103. },
  104. err: esv1beta1.NoSecretErr,
  105. },
  106. }
  107. for name, tc := range testCases {
  108. t.Run(name, func(t *testing.T) {
  109. got, err := c.GetSecret(ctx, tc.ref)
  110. if tc.err == nil {
  111. assert.NoError(t, err)
  112. assert.Equal(t, tc.want, got)
  113. } else {
  114. assert.Nil(t, got)
  115. assert.ErrorIs(t, err, tc.err)
  116. assert.Equal(t, tc.err, err)
  117. }
  118. })
  119. }
  120. }