client_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 infisical
  13. import (
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestGetSecretAddress(t *testing.T) {
  18. t.Run("when the key is not addressing a path and uses the default path", func(t *testing.T) {
  19. path, key, err := getSecretAddress("/", "foo")
  20. assert.NoError(t, err)
  21. assert.Equal(t, "/", path)
  22. assert.Equal(t, "foo", key)
  23. path, key, err = getSecretAddress("/foo", "bar")
  24. assert.NoError(t, err)
  25. assert.Equal(t, "/foo", path)
  26. assert.Equal(t, "bar", key)
  27. })
  28. t.Run("when the key is addressing a path", func(t *testing.T) {
  29. path, key, err := getSecretAddress("/", "/foo/bar")
  30. assert.NoError(t, err)
  31. assert.Equal(t, path, "/foo")
  32. assert.Equal(t, key, "bar")
  33. })
  34. t.Run("when the key is addressing a path and ignores the default path", func(t *testing.T) {
  35. path, key, err := getSecretAddress("/foo", "/bar/baz")
  36. assert.NoError(t, err)
  37. assert.Equal(t, "/bar", path)
  38. assert.Equal(t, "baz", key)
  39. })
  40. t.Run("works with a nested directory", func(t *testing.T) {
  41. path, key, err := getSecretAddress("/", "/foo/bar/baz")
  42. assert.NoError(t, err)
  43. assert.Equal(t, "/foo/bar", path)
  44. assert.Equal(t, "baz", key, "baz")
  45. })
  46. t.Run("fails when the key is a folder but does not begin with a slash", func(t *testing.T) {
  47. _, _, err := getSecretAddress("/", "bar/baz")
  48. assert.Error(t, err)
  49. assert.Equal(t, err.Error(), "a secret key referencing a folder must start with a '/' as it is an absolute path, key: bar/baz")
  50. })
  51. }