2021-06-01 04:09:50 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-06-01 04:09:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldSetDefaultLoggingValues(t *testing.T) {
|
|
|
|
config := &schema.Configuration{}
|
|
|
|
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
ValidateLogging(config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Warnings(), 0)
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
require.NotNil(t, config.Log.KeepStdout)
|
2021-06-01 04:09:50 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
assert.Equal(t, "", config.LogLevel) // TODO: DEPRECATED TEST. Remove in 4.33.0.
|
|
|
|
assert.Equal(t, "", config.LogFormat) // TODO: DEPRECATED TEST. Remove in 4.33.0.
|
|
|
|
assert.Equal(t, "", config.LogFilePath) // TODO: DEPRECATED TEST. Remove in 4.33.0.
|
2021-06-01 04:09:50 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
assert.Equal(t, "info", config.Log.Level)
|
|
|
|
assert.Equal(t, "text", config.Log.Format)
|
|
|
|
assert.Equal(t, "", config.Log.FilePath)
|
2021-06-01 04:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnInvalidLoggingLevel(t *testing.T) {
|
|
|
|
config := &schema.Configuration{
|
2021-08-03 09:55:21 +00:00
|
|
|
Log: schema.LogConfiguration{
|
2021-06-01 04:09:50 +00:00
|
|
|
Level: "TRACE",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
ValidateLogging(config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Warnings(), 0)
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "the log level 'TRACE' is invalid, must be one of: trace, debug, info, warn, error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: DEPRECATED TEST. Remove in 4.33.0.
|
|
|
|
func TestShouldMigrateDeprecatedLoggingConfig(t *testing.T) {
|
|
|
|
config := &schema.Configuration{
|
|
|
|
LogLevel: "trace",
|
|
|
|
LogFormat: "json",
|
|
|
|
LogFilePath: "/a/b/c",
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
ValidateLogging(config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
|
|
|
require.Len(t, validator.Warnings(), 3)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
require.NotNil(t, config.Log.KeepStdout)
|
2021-06-01 04:09:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "trace", config.LogLevel)
|
|
|
|
assert.Equal(t, "json", config.LogFormat)
|
|
|
|
assert.Equal(t, "/a/b/c", config.LogFilePath)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
assert.Equal(t, "trace", config.Log.Level)
|
|
|
|
assert.Equal(t, "json", config.Log.Format)
|
|
|
|
assert.Equal(t, "/a/b/c", config.Log.FilePath)
|
2021-06-01 04:09:50 +00:00
|
|
|
|
2021-06-08 13:15:43 +00:00
|
|
|
assert.EqualError(t, validator.Warnings()[0], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_level", "4.33.0", "log.level"))
|
|
|
|
assert.EqualError(t, validator.Warnings()[1], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_format", "4.33.0", "log.format"))
|
|
|
|
assert.EqualError(t, validator.Warnings()[2], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_file_path", "4.33.0", "log.file_path"))
|
2021-06-01 04:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorsAndNotOverwriteConfigurationWhenUsingDeprecatedLoggingConfig(t *testing.T) {
|
|
|
|
config := &schema.Configuration{
|
2021-08-03 09:55:21 +00:00
|
|
|
Log: schema.LogConfiguration{
|
2021-06-01 04:09:50 +00:00
|
|
|
Level: "info",
|
|
|
|
Format: "text",
|
|
|
|
FilePath: "/x/y/z",
|
|
|
|
KeepStdout: true,
|
|
|
|
},
|
|
|
|
LogLevel: "debug",
|
|
|
|
LogFormat: "json",
|
|
|
|
LogFilePath: "/a/b/c",
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
ValidateLogging(config, validator)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
require.NotNil(t, config.Log.KeepStdout)
|
2021-06-01 04:09:50 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
assert.Equal(t, "info", config.Log.Level)
|
|
|
|
assert.Equal(t, "text", config.Log.Format)
|
|
|
|
assert.True(t, config.Log.KeepStdout)
|
|
|
|
assert.Equal(t, "/x/y/z", config.Log.FilePath)
|
2021-06-01 04:09:50 +00:00
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
|
|
|
require.Len(t, validator.Warnings(), 3)
|
|
|
|
|
2021-06-08 13:15:43 +00:00
|
|
|
assert.EqualError(t, validator.Warnings()[0], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_level", "4.33.0", "log.level"))
|
|
|
|
assert.EqualError(t, validator.Warnings()[1], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_format", "4.33.0", "log.format"))
|
|
|
|
assert.EqualError(t, validator.Warnings()[2], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_file_path", "4.33.0", "log.file_path"))
|
2021-06-01 04:09:50 +00:00
|
|
|
}
|