goshort/internal/config/config_test.go

212 lines
4.6 KiB
Go
Raw Normal View History

2023-08-27 20:44:58 -03:00
package config_test
import (
"testing"
2024-03-09 06:44:22 -03:00
config "git.maronato.dev/maronato/goshort/internal/config"
2023-08-27 20:44:58 -03:00
"github.com/stretchr/testify/assert"
)
func TestGetDBTypeList(t *testing.T) {
dbTypeList := []string{
2024-03-09 06:44:22 -03:00
config.DBTypeMemory,
config.DBTypeSQLite,
2023-08-27 20:44:58 -03:00
}
2024-03-09 06:44:22 -03:00
results := config.GetDBTypeList()
2023-08-27 20:44:58 -03:00
assert.ElementsMatch(t, dbTypeList, results, "DBTypeList should be equal to the list of available DB types")
}
func TestValidate(t *testing.T) {
2023-09-06 18:43:56 -03:00
type expecttest struct {
Debug *bool
2024-03-09 06:44:22 -03:00
Verbose *config.VerboseLevel
2023-09-06 18:43:56 -03:00
}
debugTrue := true
debugFalse := false
2024-03-09 06:44:22 -03:00
verboseQuiet := config.VerboseLevelQuiet
verboseInfo := config.VerboseLevelInfo
2023-09-06 18:43:56 -03:00
2023-08-27 20:44:58 -03:00
type test struct {
2023-09-06 18:43:56 -03:00
name string
2024-03-09 06:44:22 -03:00
config *config.Config
2023-09-06 18:43:56 -03:00
wantErr bool
expected expecttest
2023-08-27 20:44:58 -03:00
}
tests := []test{
{
name: "Valid config",
2024-03-09 06:44:22 -03:00
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",
2023-09-06 18:43:56 -03:00
},
expected: expecttest{
Debug: &debugFalse,
Verbose: &verboseInfo,
2023-08-27 20:44:58 -03:00
},
wantErr: false,
},
{
name: "Minimum config",
2024-03-09 06:44:22 -03:00
config: &config.Config{},
2023-08-27 20:44:58 -03:00
wantErr: false,
},
{
name: "Invalid Host",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-08-27 20:44:58 -03:00
Host: "invalid host",
Port: "8080",
},
wantErr: true,
},
{
name: "Invalid Port",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-08-27 20:44:58 -03:00
Host: "localhost",
Port: "invalid port",
},
wantErr: true,
},
{
name: "Invalid UI Port",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-08-27 20:44:58 -03:00
Host: "localhost",
Port: "8080",
UIPort: "invalid port",
},
wantErr: true,
},
{
name: "Valid DB Type",
2024-03-09 06:44:22 -03:00
config: &config.Config{
DBType: config.DBTypeMemory,
2023-08-27 20:44:58 -03:00
},
wantErr: false,
},
{
name: "Invalid DB Type",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-08-27 20:44:58 -03:00
DBType: "invalid db type",
},
wantErr: true,
},
2023-09-06 18:43:56 -03:00
{
name: "Invalid Verbose Level (negative)",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-09-06 18:43:56 -03:00
Verbose: -1,
},
wantErr: true,
},
{
name: "Invalid Verbose Level (too high)",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-09-06 18:43:56 -03:00
Verbose: 3,
},
wantErr: true,
},
{
name: "Quiet mode sets Verbose to -1",
2024-03-09 06:44:22 -03:00
config: &config.Config{
2023-09-06 18:43:56 -03:00
Quiet: true,
},
expected: expecttest{
Verbose: &verboseQuiet,
},
},
{
name: "Verbose high sets Debug to true",
2024-03-09 06:44:22 -03:00
config: &config.Config{
Verbose: config.VerboseLevelDebug,
2023-09-06 18:43:56 -03:00
},
expected: expecttest{
Debug: &debugTrue,
},
},
2024-03-09 06:44:22 -03:00
{
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,
},
2023-08-27 20:44:58 -03:00
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
2024-03-09 06:44:22 -03:00
err := config.Validate(tc.config)
2023-08-27 20:44:58 -03:00
if tc.wantErr {
assert.Error(t, err, "Validate should return an error")
} else {
assert.NoError(t, err, "Validate should not return an error")
}
})
}
}