2020-04-05 12:37:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-04-05 12:37:21 +00:00
|
|
|
)
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
func newDefaultRegulationConfig() schema.Configuration {
|
|
|
|
config := schema.Configuration{
|
|
|
|
Regulation: &schema.RegulationConfiguration{},
|
|
|
|
}
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultRegulationBanTime(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultRegulationConfig()
|
|
|
|
|
|
|
|
ValidateRegulation(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.Equal(t, schema.DefaultRegulationConfiguration.BanTime, config.Regulation.BanTime)
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultRegulationFindTime(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultRegulationConfig()
|
|
|
|
|
|
|
|
ValidateRegulation(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.Equal(t, schema.DefaultRegulationConfiguration.FindTime, config.Regulation.FindTime)
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenFindTimeLessThanBanTime(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultRegulationConfig()
|
2022-02-28 03:15:01 +00:00
|
|
|
config.Regulation.FindTime = "1m"
|
|
|
|
config.Regulation.BanTime = "10s"
|
2021-03-05 04:18:31 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
ValidateRegulation(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 1)
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.EqualError(t, validator.Errors()[0], "regulation: option 'find_time' must be less than or equal to option 'ban_time'")
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
2021-03-05 04:18:31 +00:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnBadDurationStrings(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultRegulationConfig()
|
2022-02-28 03:15:01 +00:00
|
|
|
config.Regulation.FindTime = "a year"
|
|
|
|
config.Regulation.BanTime = "forever"
|
2021-03-05 04:18:31 +00:00
|
|
|
|
|
|
|
ValidateRegulation(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 2)
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.EqualError(t, validator.Errors()[0], "regulation: option 'find_time' could not be parsed: could not parse 'a year' as a duration")
|
|
|
|
assert.EqualError(t, validator.Errors()[1], "regulation: option 'ban_time' could not be parsed: could not parse 'forever' as a duration")
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|