-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Secret type to register secret settings
this adds a Secret type which is just a string but can be used to register a setting as a Secret setting, by using this type for the default empty value of the config this adds a new private field 'isSecret' to 'Setting' struct which is used by AddSetting to mark a Setting as secret based on this field it is decided which storage is used to persist or fetch the config setting
- Loading branch information
Showing
4 changed files
with
123 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
password = "password" | ||
secret = "secret" | ||
) | ||
|
||
func newTestConfigSecret() (*Config, error) { | ||
cfg := New(NewEmptyInMemoryStorage(), NewEmptyInMemorySecretStorage()) | ||
|
||
cfg.AddSetting(password, Secret(""), ValidateString, SuccessfullyApplied, "") | ||
cfg.AddSetting(secret, Secret("apples"), ValidateString, SuccessfullyApplied, "") | ||
return cfg, nil | ||
} | ||
|
||
func TestGetSecret(t *testing.T) { | ||
cfg, err := newTestConfigSecret() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, SettingValue{ | ||
Value: Secret("apples"), | ||
IsDefault: true, | ||
}, cfg.Get(secret)) | ||
} | ||
|
||
func TestSecretConfigUnknown(t *testing.T) { | ||
cfg, err := newTestConfigSecret() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, SettingValue{ | ||
Invalid: true, | ||
}, cfg.Get("baz")) | ||
} | ||
|
||
func TestSecretConfigSetAndGet(t *testing.T) { | ||
cfg, err := newTestConfigSecret() | ||
require.NoError(t, err) | ||
|
||
_, err = cfg.Set(password, "pass123") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, SettingValue{ | ||
Value: Secret("pass123"), | ||
IsDefault: false, | ||
}, cfg.Get(password)) | ||
} | ||
|
||
func TestSecretConfigUnsetAndGet(t *testing.T) { | ||
cfg, err := newTestConfigSecret() | ||
require.NoError(t, err) | ||
|
||
_, err = cfg.Unset(secret) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, SettingValue{ | ||
Value: Secret("apples"), | ||
IsDefault: true, | ||
}, cfg.Get(secret)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters