|
|
@@ -16,6 +16,7 @@ package gitlab
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "net/http"
|
|
|
"reflect"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
@@ -31,6 +32,7 @@ type secretManagerTestCase struct {
|
|
|
apiInputProjectID string
|
|
|
apiInputKey string
|
|
|
apiOutput *gitlab.ProjectVariable
|
|
|
+ apiResponse *gitlab.Response
|
|
|
ref *esv1beta1.ExternalSecretDataRemoteRef
|
|
|
projectID *string
|
|
|
apiErr error
|
|
|
@@ -48,12 +50,13 @@ func makeValidSecretManagerTestCase() *secretManagerTestCase {
|
|
|
ref: makeValidRef(),
|
|
|
projectID: nil,
|
|
|
apiOutput: makeValidAPIOutput(),
|
|
|
+ apiResponse: makeValidAPIResponse(),
|
|
|
apiErr: nil,
|
|
|
expectError: "",
|
|
|
expectedSecret: "",
|
|
|
expectedData: map[string][]byte{},
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
|
|
|
+ smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
|
|
|
return &smtc
|
|
|
}
|
|
|
|
|
|
@@ -72,6 +75,14 @@ func makeValidAPIInputKey() string {
|
|
|
return "testKey"
|
|
|
}
|
|
|
|
|
|
+func makeValidAPIResponse() *gitlab.Response {
|
|
|
+ return &gitlab.Response{
|
|
|
+ Response: &http.Response{
|
|
|
+ StatusCode: http.StatusOK,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func makeValidAPIOutput() *gitlab.ProjectVariable {
|
|
|
return &gitlab.ProjectVariable{
|
|
|
Key: "testKey",
|
|
|
@@ -84,7 +95,7 @@ func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTest
|
|
|
for _, fn := range tweaks {
|
|
|
fn(smtc)
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
|
|
|
+ smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
@@ -95,6 +106,22 @@ var setAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
smtc.expectError = "oh no"
|
|
|
}
|
|
|
|
|
|
+var setListAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
+ err := fmt.Errorf("oh no")
|
|
|
+ smtc.apiErr = err
|
|
|
+ smtc.expectError = fmt.Errorf(errList, err).Error()
|
|
|
+}
|
|
|
+
|
|
|
+var setListAPIRespNil = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiResponse = nil
|
|
|
+ smtc.expectError = errAuth
|
|
|
+}
|
|
|
+
|
|
|
+var setListAPIRespBadCode = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiResponse.StatusCode = http.StatusUnauthorized
|
|
|
+ smtc.expectError = errAuth
|
|
|
+}
|
|
|
+
|
|
|
var setNilMockClient = func(smtc *secretManagerTestCase) {
|
|
|
smtc.mockClient = nil
|
|
|
smtc.expectError = errUninitalizedGitlabProvider
|
|
|
@@ -134,6 +161,24 @@ func TestGitlabSecretManagerGetSecret(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestValidate(t *testing.T) {
|
|
|
+ successCases := []*secretManagerTestCase{
|
|
|
+ makeValidSecretManagerTestCaseCustom(),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setListAPIErr),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setListAPIRespNil),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setListAPIRespBadCode),
|
|
|
+ }
|
|
|
+ sm := Gitlab{}
|
|
|
+ for k, v := range successCases {
|
|
|
+ sm.client = v.mockClient
|
|
|
+ t.Logf("%+v", v)
|
|
|
+ err := sm.Validate()
|
|
|
+ if !ErrorContains(err, v.expectError) {
|
|
|
+ t.Errorf("[%d], unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestGetSecretMap(t *testing.T) {
|
|
|
// good case: default version & deserialization
|
|
|
setDeserialization := func(smtc *secretManagerTestCase) {
|