device42_api_test.go 3.0 KB

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