2021-09-17 11:44:35 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-28 10:15:01 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-17 11:44:35 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func newDefaultNTPConfig() schema.Configuration {
|
|
|
|
return schema.Configuration{
|
2022-03-02 13:40:26 +07:00
|
|
|
NTP: schema.NTPConfiguration{},
|
2022-02-28 10:15:01 +07:00
|
|
|
}
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func TestShouldSetDefaultNTPValues(t *testing.T) {
|
2021-09-17 11:44:35 +07:00
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.Address, config.NTP.Address)
|
2022-03-03 18:20:43 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.Version, config.NTP.Version)
|
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.MaximumDesync, config.NTP.MaximumDesync)
|
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.DisableStartupCheck, config.NTP.DisableStartupCheck)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultNtpVersion(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
config.NTP.MaximumDesync = -1
|
2021-09-17 11:44:35 +07:00
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.MaximumDesync, config.NTP.MaximumDesync)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func TestShouldRaiseErrorOnInvalidNTPVersion(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
config.NTP.Version = 1
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "ntp: option 'version' must be either 3 or 4 but it is configured as '1'")
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|