client_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package delinea
  14. import (
  15. "context"
  16. "errors"
  17. "testing"
  18. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  19. "github.com/stretchr/testify/assert"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. )
  22. type fakeAPI struct {
  23. secrets []*vault.Secret
  24. }
  25. // createVaultSecret assembles a vault.Secret.
  26. // vault.Secret has unexported nested types, and is therefore quite
  27. // tricky from outside the vault package. This function facilitates easy setup.
  28. func createVaultSecret(path string, data map[string]any) *vault.Secret {
  29. s := &vault.Secret{}
  30. s.Path = path
  31. s.Data = data
  32. return s
  33. }
  34. // Secret returns secret matching path.
  35. func (f *fakeAPI) Secret(path string) (*vault.Secret, error) {
  36. for _, s := range f.secrets {
  37. if s.Path == path {
  38. return s, nil
  39. }
  40. }
  41. return nil, errors.New("not found")
  42. }
  43. func newTestClient() esv1.SecretsClient {
  44. return &client{
  45. api: &fakeAPI{
  46. secrets: []*vault.Secret{
  47. createVaultSecret("a", map[string]any{}),
  48. createVaultSecret("b", map[string]any{
  49. "hello": "world",
  50. }),
  51. createVaultSecret("c", map[string]any{
  52. "foo": map[string]string{"bar": "baz"},
  53. }),
  54. },
  55. },
  56. }
  57. }
  58. func TestGetSecret(t *testing.T) {
  59. ctx := context.Background()
  60. c := newTestClient()
  61. testCases := map[string]struct {
  62. ref esv1.ExternalSecretDataRemoteRef
  63. want []byte
  64. err error
  65. }{
  66. "querying for the key returns the map": {
  67. ref: esv1.ExternalSecretDataRemoteRef{
  68. Key: "b",
  69. },
  70. want: []byte(`{"hello":"world"}`),
  71. },
  72. "querying for the key and property returns a single value": {
  73. ref: esv1.ExternalSecretDataRemoteRef{
  74. Key: "b",
  75. Property: "hello",
  76. },
  77. want: []byte(`world`),
  78. },
  79. "querying for the key and nested property returns a single value": {
  80. ref: esv1.ExternalSecretDataRemoteRef{
  81. Key: "c",
  82. Property: "foo.bar",
  83. },
  84. want: []byte(`baz`),
  85. },
  86. "querying for existent key and non-existing propery": {
  87. ref: esv1.ExternalSecretDataRemoteRef{
  88. Key: "c",
  89. Property: "foo.bar.x",
  90. },
  91. err: esv1.NoSecretErr,
  92. },
  93. }
  94. for name, tc := range testCases {
  95. t.Run(name, func(t *testing.T) {
  96. got, err := c.GetSecret(ctx, tc.ref)
  97. if tc.err == nil {
  98. assert.NoError(t, err)
  99. assert.Equal(t, tc.want, got)
  100. } else {
  101. assert.Nil(t, got)
  102. assert.ErrorIs(t, err, tc.err)
  103. assert.Equal(t, tc.err, err)
  104. }
  105. })
  106. }
  107. }