summaryrefslogtreecommitdiff
path: root/internal/configuration/validator/log_test.go
blob: 0f4ead4542ff6231aa795eb1ff0ed4514d8b7c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package validator

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/authelia/authelia/v4/internal/configuration/schema"
)

func TestShouldSetDefaultLoggingValues(t *testing.T) {
	config := &schema.Configuration{}

	validator := schema.NewStructValidator()

	ValidateLog(config, validator)

	assert.Len(t, validator.Warnings(), 0)
	assert.Len(t, validator.Errors(), 0)

	require.NotNil(t, config.Log.KeepStdout)

	assert.Equal(t, "info", config.Log.Level)
	assert.Equal(t, "text", config.Log.Format)
	assert.Equal(t, "", config.Log.FilePath)
}

func TestShouldRaiseErrorOnInvalidLoggingLevel(t *testing.T) {
	config := &schema.Configuration{
		Log: schema.Log{
			Level: "TRACE",
		},
	}

	validator := schema.NewStructValidator()

	ValidateLog(config, validator)

	assert.Len(t, validator.Warnings(), 0)
	require.Len(t, validator.Errors(), 1)

	assert.EqualError(t, validator.Errors()[0], "log: option 'level' must be one of 'trace', 'debug', 'info', 'warn', or 'error' but it's configured as 'TRACE'")
}

func TestShouldRaiseErrorOnInvalidLoggingFormat(t *testing.T) {
	config := &schema.Configuration{
		Log: schema.Log{
			Level:  "trace",
			Format: "FORMAT",
		},
	}

	validator := schema.NewStructValidator()

	ValidateLog(config, validator)

	assert.Len(t, validator.Warnings(), 0)
	require.Len(t, validator.Errors(), 1)

	assert.EqualError(t, validator.Errors()[0], "log: option 'format' must be one of 'text' or 'json' but it's configured as 'FORMAT'")
}