212 lines
4.6 KiB
Go
212 lines
4.6 KiB
Go
package config_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
config "git.maronato.dev/maronato/goshort/internal/config"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetDBTypeList(t *testing.T) {
|
|
dbTypeList := []string{
|
|
config.DBTypeMemory,
|
|
config.DBTypeSQLite,
|
|
}
|
|
|
|
results := config.GetDBTypeList()
|
|
|
|
assert.ElementsMatch(t, dbTypeList, results, "DBTypeList should be equal to the list of available DB types")
|
|
}
|
|
|
|
func TestValidate(t *testing.T) {
|
|
type expecttest struct {
|
|
Debug *bool
|
|
Verbose *config.VerboseLevel
|
|
}
|
|
|
|
debugTrue := true
|
|
debugFalse := false
|
|
verboseQuiet := config.VerboseLevelQuiet
|
|
verboseInfo := config.VerboseLevelInfo
|
|
|
|
type test struct {
|
|
name string
|
|
config *config.Config
|
|
wantErr bool
|
|
expected expecttest
|
|
}
|
|
|
|
tests := []test{
|
|
{
|
|
name: "Valid config",
|
|
config: &config.Config{
|
|
Prod: false,
|
|
DBType: config.DBTypeSQLite,
|
|
DBURL: "goshort.db",
|
|
Port: "8080",
|
|
Host: "localhost",
|
|
UIPort: "3000",
|
|
Debug: false,
|
|
SessionDuration: 7,
|
|
DisableRegistration: false,
|
|
Verbose: 0,
|
|
Quiet: false,
|
|
DisableCredentialsLogin: false,
|
|
OIDCIssuerURL: "https://example.com",
|
|
OIDCClientID: "client-id",
|
|
OIDCClientSecret: "client-secret",
|
|
OIDCRedirectURL: "https://example.com/redirect",
|
|
},
|
|
expected: expecttest{
|
|
Debug: &debugFalse,
|
|
Verbose: &verboseInfo,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Minimum config",
|
|
config: &config.Config{},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Invalid Host",
|
|
config: &config.Config{
|
|
Host: "invalid host",
|
|
Port: "8080",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid Port",
|
|
config: &config.Config{
|
|
Host: "localhost",
|
|
Port: "invalid port",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid UI Port",
|
|
config: &config.Config{
|
|
Host: "localhost",
|
|
Port: "8080",
|
|
UIPort: "invalid port",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Valid DB Type",
|
|
config: &config.Config{
|
|
DBType: config.DBTypeMemory,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Invalid DB Type",
|
|
config: &config.Config{
|
|
DBType: "invalid db type",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid Verbose Level (negative)",
|
|
config: &config.Config{
|
|
Verbose: -1,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid Verbose Level (too high)",
|
|
config: &config.Config{
|
|
Verbose: 3,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Quiet mode sets Verbose to -1",
|
|
config: &config.Config{
|
|
Quiet: true,
|
|
},
|
|
expected: expecttest{
|
|
Verbose: &verboseQuiet,
|
|
},
|
|
},
|
|
{
|
|
name: "Verbose high sets Debug to true",
|
|
config: &config.Config{
|
|
Verbose: config.VerboseLevelDebug,
|
|
},
|
|
expected: expecttest{
|
|
Debug: &debugTrue,
|
|
},
|
|
},
|
|
{
|
|
name: "Fails of credentials are disabled and oidc is not configured",
|
|
config: &config.Config{
|
|
DisableCredentialsLogin: true,
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Succeeds of credentials are disabled and oidc is configured",
|
|
config: &config.Config{
|
|
DisableCredentialsLogin: true,
|
|
OIDCIssuerURL: "https://example.com",
|
|
OIDCClientID: "client-id",
|
|
OIDCClientSecret: "client-secret",
|
|
OIDCRedirectURL: "https://example.com/redirect",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Succeeds if both credentials and oidc are enabled",
|
|
config: &config.Config{
|
|
OIDCIssuerURL: "https://example.com",
|
|
OIDCClientID: "client-id",
|
|
OIDCClientSecret: "client-secret",
|
|
OIDCRedirectURL: "https://example.com/redirect",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Fails if issuer is not a valid URL",
|
|
config: &config.Config{
|
|
OIDCIssuerURL: "invalid-url",
|
|
OIDCClientID: "client-id",
|
|
OIDCClientSecret: "client-secret",
|
|
OIDCRedirectURL: "https://example.com/redirect",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Fails if redirect is not a valid URL",
|
|
config: &config.Config{
|
|
OIDCIssuerURL: "https://example.com",
|
|
OIDCClientID: "client-id",
|
|
OIDCClientSecret: "client-secret",
|
|
OIDCRedirectURL: "invalid-url",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Fails if oidc is partially configured",
|
|
config: &config.Config{
|
|
OIDCIssuerURL: "https://example.com",
|
|
OIDCClientID: "client-id",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := config.Validate(tc.config)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(t, err, "Validate should return an error")
|
|
} else {
|
|
assert.NoError(t, err, "Validate should not return an error")
|
|
}
|
|
})
|
|
}
|
|
}
|