2020-04-05 12:37:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-03-02 06:40:26 +00:00
|
|
|
"time"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"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{
|
2022-03-02 06:40:26 +00:00
|
|
|
Regulation: schema.RegulationConfiguration{},
|
2022-02-28 03:15:01 +00:00
|
|
|
}
|
|
|
|
|
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-03-02 06:40:26 +00:00
|
|
|
config.Regulation.FindTime = time.Minute
|
|
|
|
config.Regulation.BanTime = time.Second * 10
|
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
|
|
|
}
|