140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package channel
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// --- ConfigValidator tests ---
|
|
|
|
func TestValidateConfig_NilProvider(t *testing.T) {
|
|
err := ValidateConfig(context.Background(), nil, ChannelConfig{})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestValidateConfig_NilConfig(t *testing.T) {
|
|
provider := &mockProvider{channelType: "test_validate"}
|
|
err := ValidateConfig(context.Background(), provider, nil)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestValidateConfig_NoSchema(t *testing.T) {
|
|
provider := &mockProvider{channelType: "test_validate2"}
|
|
err := ValidateConfig(context.Background(), provider, ChannelConfig{"key": "value"})
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// --- GetConfigValue tests ---
|
|
|
|
func TestGetConfigValue_NilConfig(t *testing.T) {
|
|
_, err := GetConfigValue(context.Background(), nil, "key")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestGetConfigValue_MissingKey(t *testing.T) {
|
|
_, err := GetConfigValue(context.Background(), ChannelConfig{}, "key")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestGetConfigValue_NilValue(t *testing.T) {
|
|
_, err := GetConfigValue(context.Background(), ChannelConfig{"key": nil}, "key")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestGetConfigValue_Success(t *testing.T) {
|
|
val, err := GetConfigValue(context.Background(), ChannelConfig{"key": "value"}, "key")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "value", val)
|
|
}
|
|
|
|
func TestGetConfigString(t *testing.T) {
|
|
s, err := GetConfigString(context.Background(), ChannelConfig{"name": "test"}, "name")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test", s)
|
|
}
|
|
|
|
func TestGetConfigString_NotString(t *testing.T) {
|
|
_, err := GetConfigString(context.Background(), ChannelConfig{"name": 123}, "name")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestGetConfigInt(t *testing.T) {
|
|
i, err := GetConfigInt(context.Background(), ChannelConfig{"count": 42}, "count")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 42, i)
|
|
}
|
|
|
|
func TestGetConfigInt_FromFloat(t *testing.T) {
|
|
i, err := GetConfigInt(context.Background(), ChannelConfig{"count": float64(42)}, "count")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 42, i)
|
|
}
|
|
|
|
func TestGetConfigInt_FromString(t *testing.T) {
|
|
i, err := GetConfigInt(context.Background(), ChannelConfig{"count": "42"}, "count")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 42, i)
|
|
}
|
|
|
|
func TestGetConfigBool(t *testing.T) {
|
|
b, err := GetConfigBool(context.Background(), ChannelConfig{"enabled": true}, "enabled")
|
|
require.NoError(t, err)
|
|
assert.True(t, b)
|
|
}
|
|
|
|
func TestGetConfigBool_NotBool(t *testing.T) {
|
|
_, err := GetConfigBool(context.Background(), ChannelConfig{"enabled": "yes"}, "enabled")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestGetConfigFloat(t *testing.T) {
|
|
f, err := GetConfigFloat(context.Background(), ChannelConfig{"rate": 3.14}, "rate")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 3.14, f)
|
|
}
|
|
|
|
func TestGetConfigFloat_FromInt(t *testing.T) {
|
|
f, err := GetConfigFloat(context.Background(), ChannelConfig{"rate": 5}, "rate")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 5.0, f)
|
|
}
|
|
|
|
func TestGetConfigStringSlice(t *testing.T) {
|
|
s, err := GetConfigStringSlice(context.Background(), ChannelConfig{"tags": []string{"a", "b"}}, "tags")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"a", "b"}, s)
|
|
}
|
|
|
|
func TestGetConfigStringSlice_FromInterface(t *testing.T) {
|
|
s, err := GetConfigStringSlice(context.Background(), ChannelConfig{"tags": []interface{}{"a", "b"}}, "tags")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"a", "b"}, s)
|
|
}
|
|
|
|
// --- filterKnownSecrets tests ---
|
|
|
|
func TestFilterKnownSecrets(t *testing.T) {
|
|
config := ChannelConfig{
|
|
"api_key": "secret123",
|
|
"name": "test",
|
|
"password": "pwd",
|
|
"webhook": "http://example.com",
|
|
}
|
|
filtered := filterKnownSecrets(config)
|
|
assert.Equal(t, "***", filtered["api_key"])
|
|
assert.Equal(t, "***", filtered["password"])
|
|
assert.Equal(t, "test", filtered["name"])
|
|
assert.Equal(t, "http://example.com", filtered["webhook"])
|
|
}
|
|
|
|
// --- containsSubstring tests ---
|
|
|
|
func TestContainsSubstring(t *testing.T) {
|
|
assert.True(t, containsSubstring("api_key", "key"))
|
|
assert.True(t, containsSubstring("secret_token", "token"))
|
|
assert.False(t, containsSubstring("name", "key"))
|
|
}
|