Skip to content

Commit

Permalink
Make the ValidateRegistrationDuration() more useful and add NoUntil().
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Aug 27, 2023
1 parent 2f0ce53 commit e927272
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
28 changes: 24 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func (deviceIDRegexMustCompileOption) String() string {

// ValidateRegistrationDuration ensures that the requsted registration duration
// of a webhook is valid. This option checks the values set in either the
// Duration or Until fields of the webhook. The ttl passed in must be non-negative.
// Duration or Until fields of the webhook. If the ttl is less than or equal to
// zero, this option will not boundary check the registration duration, but will
// still ensure that the Duration or Until fields are set.
func ValidateRegistrationDuration(ttl time.Duration) Option {
return validateRegistrationDurationOption{ttl: ttl}
}
Expand All @@ -107,10 +109,10 @@ type validateRegistrationDurationOption struct {

func (v validateRegistrationDurationOption) Validate(r *Registration) error {
if v.ttl <= 0 {
return fmt.Errorf("%w: the duration must be positive", ErrInvalidInput)
v.ttl = time.Duration(0)
}

if v.ttl < time.Duration(r.Duration) {
if v.ttl != 0 && v.ttl < time.Duration(r.Duration) {
return fmt.Errorf("%w: the registration is for too long", ErrInvalidInput)
}

Expand All @@ -129,7 +131,7 @@ func (v validateRegistrationDurationOption) Validate(r *Registration) error {
}

now := nowFunc()
if r.Until.After(now.Add(v.ttl)) {
if v.ttl != 0 && r.Until.After(now.Add(v.ttl)) {
return fmt.Errorf("%w: the registration is for too long", ErrInvalidInput)
}

Expand Down Expand Up @@ -256,3 +258,21 @@ func (p provideAlternativeURLValidatorOption) String() string {
}
return "ProvideAlternativeURLValidator(" + p.checker.String() + ")"
}

// NoUntil is an option that ensures that the Until field is not set.
func NoUntil() Option {
return noUntilOption{}
}

type noUntilOption struct{}

func (noUntilOption) Validate(r *Registration) error {
if !r.Until.IsZero() {
return fmt.Errorf("%w: Until is not allowed", ErrInvalidInput)
}
return nil
}

func (noUntilOption) String() string {
return "NoUntil()"
}
34 changes: 32 additions & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ func TestValidateRegistrationDuration(t *testing.T) {
},
expectedErr: ErrInvalidInput,
}, {
description: "failure with max ttl out of bounds",
description: "success with max ttl ignored",
opt: ValidateRegistrationDuration(-5 * time.Minute),
in: Registration{
Duration: CustomDuration(1 * time.Minute),
},
expectedErr: ErrInvalidInput,
}, {
description: "success with max ttl ignored, 0 duration",
opt: ValidateRegistrationDuration(0),
in: Registration{
Duration: CustomDuration(1 * time.Minute),
},
}, {
description: "success with until in bounds",
opts: []Option{
Expand Down Expand Up @@ -193,6 +198,15 @@ func TestValidateRegistrationDuration(t *testing.T) {
Until: time.Date(2021, 1, 1, 0, 6, 0, 0, time.UTC),
},
expectedErr: ErrInvalidInput,
}, {
description: "success with until just needing to be present",
opts: []Option{
ProvideTimeNowFunc(now),
ValidateRegistrationDuration(0),
},
in: Registration{
Until: time.Date(2021, 1, 1, 0, 6, 0, 0, time.UTC),
},
}, {
description: "failure, both expirations set",
opt: ValidateRegistrationDuration(5 * time.Minute),
Expand Down Expand Up @@ -337,6 +351,22 @@ func TestProvideAlternativeURLValidator(t *testing.T) {
})
}

func TestNoUntil(t *testing.T) {
run_tests(t, []optionTest{
{
description: "success, no until set",
opt: NoUntil(),
str: "NoUntil()",
}, {
description: "detect until set",
opt: NoUntil(),
in: Registration{
Until: time.Now(),
},
expectedErr: ErrInvalidInput,
},
})
}
func run_tests(t *testing.T, tests []optionTest) {
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
Expand Down

0 comments on commit e927272

Please sign in to comment.