client_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 delinea
  13. import (
  14. "context"
  15. "errors"
  16. "testing"
  17. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  18. "github.com/stretchr/testify/assert"
  19. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. )
  21. type fakeAPI struct {
  22. secrets []*vault.Secret
  23. }
  24. // createVaultSecret assembles a vault.Secret.
  25. // vault.Secret has unexported nested types, and is therefore quite
  26. // tricky from outside the vault package. This function facilitates easy setup.
  27. func createVaultSecret(path string, data map[string]any) *vault.Secret {
  28. s := &vault.Secret{}
  29. s.Path = path
  30. s.Data = data
  31. return s
  32. }
  33. // Secret returns secret matching path.
  34. func (f *fakeAPI) Secret(path string) (*vault.Secret, error) {
  35. for _, s := range f.secrets {
  36. if s.Path == path {
  37. return s, nil
  38. }
  39. }
  40. return nil, errors.New("not found")
  41. }
  42. func newTestClient() esv1beta1.SecretsClient {
  43. return &client{
  44. api: &fakeAPI{
  45. secrets: []*vault.Secret{
  46. createVaultSecret("a", map[string]any{}),
  47. createVaultSecret("b", map[string]any{
  48. "hello": "world",
  49. }),
  50. createVaultSecret("c", map[string]any{
  51. "foo": map[string]string{"bar": "baz"},
  52. }),
  53. },
  54. },
  55. }
  56. }
  57. func TestGetSecret(t *testing.T) {
  58. ctx := context.Background()
  59. c := newTestClient()
  60. testCases := map[string]struct {
  61. ref esv1beta1.ExternalSecretDataRemoteRef
  62. want []byte
  63. err error
  64. }{
  65. "querying for the key returns the map": {
  66. ref: esv1beta1.ExternalSecretDataRemoteRef{
  67. Key: "b",
  68. },
  69. want: []byte(`{"hello":"world"}`),
  70. },
  71. "querying for the key and property returns a single value": {
  72. ref: esv1beta1.ExternalSecretDataRemoteRef{
  73. Key: "b",
  74. Property: "hello",
  75. },
  76. want: []byte(`world`),
  77. },
  78. "querying for the key and nested property returns a single value": {
  79. ref: esv1beta1.ExternalSecretDataRemoteRef{
  80. Key: "c",
  81. Property: "foo.bar",
  82. },
  83. want: []byte(`baz`),
  84. },
  85. "querying for existent key and non-existing propery": {
  86. ref: esv1beta1.ExternalSecretDataRemoteRef{
  87. Key: "c",
  88. Property: "foo.bar.x",
  89. },
  90. err: esv1beta1.NoSecretErr,
  91. },
  92. }
  93. for name, tc := range testCases {
  94. t.Run(name, func(t *testing.T) {
  95. got, err := c.GetSecret(ctx, tc.ref)
  96. if tc.err == nil {
  97. assert.NoError(t, err)
  98. assert.Equal(t, tc.want, got)
  99. } else {
  100. assert.Nil(t, got)
  101. assert.ErrorIs(t, err, tc.err)
  102. assert.Equal(t, tc.err, err)
  103. }
  104. })
  105. }
  106. }