|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" |
| 9 | + secretmanagerclient "github.com/GoogleCloudPlatform/spanner-migration-tool/accessors/clients/secretmanager" |
| 10 | + googleapis "github.com/googleapis/gax-go/v2" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
| 13 | +) |
| 14 | + |
| 15 | +type MockSecretManagerClient struct { |
| 16 | + mock.Mock |
| 17 | +} |
| 18 | + |
| 19 | +func (m *MockSecretManagerClient) AccessSecretVersion(ctx context.Context, req *secretmanagerpb.AccessSecretVersionRequest, opts ...googleapis.CallOption) (*secretmanagerpb.AccessSecretVersionResponse, error) { |
| 20 | + args := m.Called(ctx, req, opts) |
| 21 | + return args.Get(0).(*secretmanagerpb.AccessSecretVersionResponse), args.Error(1) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *MockSecretManagerClient) Close() error { |
| 25 | + args := m.Called() |
| 26 | + return args.Error(0) |
| 27 | +} |
| 28 | + |
| 29 | +func TestFetchPasswordFromSecretManager(t *testing.T) { |
| 30 | + mockClient := new(MockSecretManagerClient) |
| 31 | + |
| 32 | + // Override the client creation function |
| 33 | + oldNewClient := secretmanagerclient.NewSecretManagerClient |
| 34 | + secretmanagerclient.NewSecretManagerClient = func(ctx context.Context) (secretmanagerclient.SecretManagerClient, error) { |
| 35 | + return mockClient, nil |
| 36 | + } |
| 37 | + defer func() { secretmanagerclient.NewSecretManagerClient = oldNewClient }() |
| 38 | + |
| 39 | + testCases := []struct { |
| 40 | + name string |
| 41 | + secretId string |
| 42 | + mockResponse *secretmanagerpb.AccessSecretVersionResponse |
| 43 | + mockError error |
| 44 | + expectedSecret string |
| 45 | + expectedPwd string |
| 46 | + expectError bool |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "Success with version", |
| 50 | + secretId: "projects/my-project/secrets/my-secret/versions/1", |
| 51 | + mockResponse: &secretmanagerpb.AccessSecretVersionResponse{ |
| 52 | + Payload: &secretmanagerpb.SecretPayload{ |
| 53 | + Data: []byte("password123"), |
| 54 | + }, |
| 55 | + }, |
| 56 | + expectedSecret: "projects/my-project/secrets/my-secret/versions/1", |
| 57 | + expectedPwd: "password123", |
| 58 | + expectError: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Success without version (appends latest)", |
| 62 | + secretId: "projects/my-project/secrets/my-secret", |
| 63 | + mockResponse: &secretmanagerpb.AccessSecretVersionResponse{ |
| 64 | + Payload: &secretmanagerpb.SecretPayload{ |
| 65 | + Data: []byte("password456"), |
| 66 | + }, |
| 67 | + }, |
| 68 | + expectedSecret: "projects/my-project/secrets/my-secret/versions/latest", |
| 69 | + expectedPwd: "password456", |
| 70 | + expectError: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "Client error", |
| 74 | + secretId: "projects/my-project/secrets/my-secret", |
| 75 | + mockError: fmt.Errorf("client error"), |
| 76 | + expectError: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Success with trailing slash (appends latest)", |
| 80 | + secretId: "projects/my-project/secrets/my-secret/", |
| 81 | + mockResponse: &secretmanagerpb.AccessSecretVersionResponse{ |
| 82 | + Payload: &secretmanagerpb.SecretPayload{ |
| 83 | + Data: []byte("password789"), |
| 84 | + }, |
| 85 | + }, |
| 86 | + expectedSecret: "projects/my-project/secrets/my-secret/versions/latest", |
| 87 | + expectedPwd: "password789", |
| 88 | + expectError: false, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range testCases { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + if !tc.expectError { |
| 95 | + mockClient.On("AccessSecretVersion", mock.Anything, mock.MatchedBy(func(req *secretmanagerpb.AccessSecretVersionRequest) bool { |
| 96 | + return req.Name == tc.expectedSecret |
| 97 | + }), mock.Anything).Return(tc.mockResponse, nil).Once() |
| 98 | + } else if tc.mockError != nil { |
| 99 | + mockClient.On("AccessSecretVersion", mock.Anything, mock.Anything, mock.Anything).Return(&secretmanagerpb.AccessSecretVersionResponse{}, tc.mockError).Once() |
| 100 | + } |
| 101 | + |
| 102 | + secretId, pwd, err := FetchPasswordFromSecretManager(tc.secretId) |
| 103 | + |
| 104 | + if tc.expectError { |
| 105 | + assert.Error(t, err) |
| 106 | + } else { |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.Equal(t, tc.expectedSecret, secretId) |
| 109 | + assert.Equal(t, tc.expectedPwd, pwd) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments