|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package config |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/mongodb/atlas-cli-core/mocks" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.uber.org/mock/gomock" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGetConfig(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + isSecure bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "secure store redaction", |
| 33 | + isSecure: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "config file redaction", |
| 37 | + isSecure: false, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + ctrl := gomock.NewController(t) |
| 44 | + defer ctrl.Finish() |
| 45 | + |
| 46 | + profileName := "test" |
| 47 | + mockStore := mocks.NewMockStore(ctrl) |
| 48 | + |
| 49 | + // Set up mock expectations |
| 50 | + mockStore.EXPECT().GetProfileStringMap(profileName).Return(map[string]string{ |
| 51 | + "project_id": "proj123", |
| 52 | + "public_api_key": "public-key", |
| 53 | + }).Times(1) |
| 54 | + mockStore.EXPECT().GetProfileValue(profileName, "public_api_key").Return("pub-key").Times(1) |
| 55 | + mockStore.EXPECT().GetProfileValue(profileName, "private_api_key").Return("priv-key").Times(1) |
| 56 | + mockStore.EXPECT().GetProfileValue(profileName, "access_token").Return("").Times(1) |
| 57 | + mockStore.EXPECT().GetProfileValue(profileName, "refresh_token").Return("").Times(1) |
| 58 | + mockStore.EXPECT().GetProfileValue(profileName, "client_id").Return("").Times(1) |
| 59 | + mockStore.EXPECT().GetProfileValue(profileName, "client_secret").Return("").Times(1) |
| 60 | + |
| 61 | + mockStore.EXPECT().IsSecure().Return(tt.isSecure).Times(1) |
| 62 | + |
| 63 | + opts := &describeOpts{} |
| 64 | + result, err := opts.GetConfig(mockStore, profileName) |
| 65 | + require.NoError(t, err) |
| 66 | + assert.Equal(t, "proj123", result["project_id"]) |
| 67 | + if tt.isSecure { |
| 68 | + assert.Contains(t, result["public_api_key"], redactedSecureText) |
| 69 | + assert.Contains(t, result["private_api_key"], redactedSecureText) |
| 70 | + } else { |
| 71 | + assert.Contains(t, result["public_api_key"], redactedConfigText) |
| 72 | + assert.Contains(t, result["private_api_key"], redactedConfigText) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments