device42_api_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 device42
  13. import (
  14. "bytes"
  15. "encoding/json"
  16. "net/http"
  17. "reflect"
  18. "testing"
  19. fakedevice42 "github.com/external-secrets/external-secrets/pkg/provider/device42/fake"
  20. )
  21. const device42PasswordID = "12345"
  22. func d42PasswordResponse() D42PasswordResponse {
  23. return D42PasswordResponse{Passwords: []D42Password{d42Password()}}
  24. }
  25. func d42Password() D42Password {
  26. return D42Password{
  27. Password: "test_Password",
  28. ID: 12345,
  29. }
  30. }
  31. func TestDevice42ApiGetSecret(t *testing.T) {
  32. type fields struct {
  33. funcStack []func(req *http.Request) (*http.Response, error)
  34. }
  35. type args struct {
  36. secretID string
  37. }
  38. tests := []struct {
  39. name string
  40. fields fields
  41. args args
  42. want D42Password
  43. wantErr bool
  44. }{
  45. {
  46. name: "get secret",
  47. fields: fields{
  48. funcStack: []func(req *http.Request) (*http.Response, error){
  49. createResponder(d42PasswordResponse(), true), //nolint:bodyclose
  50. },
  51. },
  52. args: args{
  53. secretID: device42PasswordID,
  54. },
  55. want: d42Password(),
  56. wantErr: false,
  57. },
  58. {
  59. name: "bad response on secret entry",
  60. fields: fields{
  61. funcStack: []func(req *http.Request) (*http.Response, error){
  62. createResponder([]byte("bad response body"), false), //nolint:bodyclose // linters bug
  63. },
  64. },
  65. args: args{
  66. secretID: device42PasswordID,
  67. },
  68. want: D42Password{},
  69. wantErr: true,
  70. },
  71. }
  72. for _, tt := range tests {
  73. t.Run(tt.name, func(t *testing.T) {
  74. api := &API{
  75. client: &fakedevice42.MockClient{
  76. FuncStack: tt.fields.funcStack,
  77. },
  78. baseURL: "localhost",
  79. hostPort: "8714",
  80. password: "test",
  81. username: "test",
  82. }
  83. got, err := api.GetSecret(tt.args.secretID)
  84. if (err != nil) != tt.wantErr {
  85. t.Errorf("Device42.GetSecret() error = %v, wantErr %v", err, tt.wantErr)
  86. return
  87. }
  88. if !reflect.DeepEqual(got, tt.want) {
  89. t.Errorf("Device42.GetSecret() = %v, want %v", got, tt.want)
  90. }
  91. })
  92. }
  93. }
  94. func createResponder(payload any, withMarshal bool) func(*http.Request) (*http.Response, error) {
  95. return func(req *http.Request) (*http.Response, error) {
  96. var payloadBytes []byte
  97. if withMarshal {
  98. payloadBytes, _ = json.Marshal(payload)
  99. } else {
  100. payloadBytes = payload.([]byte)
  101. }
  102. res := http.Response{
  103. Status: "OK",
  104. StatusCode: http.StatusOK,
  105. Body: &closeableBuffer{bytes.NewReader(payloadBytes)},
  106. }
  107. return &res, nil
  108. }
  109. }
  110. type closeableBuffer struct {
  111. *bytes.Reader
  112. }
  113. func (cb *closeableBuffer) Close() error {
  114. // Here you can add any cleanup code if needed
  115. return nil
  116. }