Skip to content

Commit

Permalink
feat(#239): support allowed_counts configuration, validate config, bu…
Browse files Browse the repository at this point in the history
…t do not check it yet
  • Loading branch information
Jumpy-Squirrel committed Dec 12, 2024
1 parent cd1d450 commit 129c9c4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
17 changes: 16 additions & 1 deletion docs/config-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,27 @@ choices:
visible_for:
- sponsordesk
ultrasponsor:
allowed_counts:
- 1
- 2
- 3
- 4
- 5
- 6
- 8
- 10
- 15
- 20
- 30
- 40
- 50
- 100
constraint: 'sponsor2'
constraint_msg: Only available for supersponsors.
description: Ultra Sponsor
max_count: 100
price: 5000
vat_percent: 19
max_count: 100
visible_for:
- regdesk
- sponsordesk
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/eurofurence/reg-attendee-service

go 1.20
go 1.21

require (
github.com/StephanHCB/go-autumn-logging v0.4.0
Expand Down
1 change: 1 addition & 0 deletions internal/repository/config/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type (
}

ChoiceConfig struct {
AllowedCounts []int `yaml:"allowed_counts"` // only supported for packages, ignored unless you also set max_count
Description string `yaml:"description"`
Price int64 `yaml:"price"`
VatPercent float64 `yaml:"vat_percent"`
Expand Down
8 changes: 8 additions & 0 deletions internal/repository/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/golang-jwt/jwt/v4"
"net/url"
"os"
"slices"
"strings"
"time"
)
Expand Down Expand Up @@ -169,6 +170,13 @@ func validatePackagesConfiguration(errs url.Values, c map[string]ChoiceConfig) {
if v.AdminOnly {
errs.Add("choices.packages."+k+".admin", "packages cannot be admin_only (they cost money). Try read_only instead.")
}
if len(v.AllowedCounts) > 0 {
if v.MaxCount <= 1 {
errs.Add("choices.packages."+k+".allowed_counts", "can only list allowed counts if max_count is set to at least 2")
} else if slices.Max(v.AllowedCounts) > v.MaxCount {
errs.Add("choices.packages."+k+".allowed_counts", "maximum allowed_counts value cannot be larger than max_count for package")
}
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/repository/config/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ func TestCheckFlags(t *testing.T) {
func TestCheckPackages(t *testing.T) {
c := make(map[string]ChoiceConfig)
c["myadmin"] = ChoiceConfig{Default: true, AdminOnly: true, Description: "admin only package - invalid"}
c["counttoohigh"] = ChoiceConfig{AllowedCounts: []int{1, 17, 34}, Description: "allowed_counts higher than max_count", MaxCount: 17}
c["maxcountunset"] = ChoiceConfig{AllowedCounts: []int{1, 17, 34}, Description: "allowed_counts but no max_count"}

actualErrors := url.Values{}
validatePackagesConfiguration(actualErrors, c)
expectedErrors := url.Values{
"choices.packages.myadmin.admin": []string{"packages cannot be admin_only (they cost money). Try read_only instead."},
"choices.packages.myadmin.admin": []string{"packages cannot be admin_only (they cost money). Try read_only instead."},
"choices.packages.counttoohigh.allowed_counts": []string{"maximum allowed_counts value cannot be larger than max_count for package"},
"choices.packages.maxcountunset.allowed_counts": []string{"can only list allowed counts if max_count is set to at least 2"},
}
prettyprintedActualErrors, _ := json.MarshalIndent(actualErrors, "", " ")
prettyprintedExpectedErrors, _ := json.MarshalIndent(expectedErrors, "", " ")
Expand Down
10 changes: 5 additions & 5 deletions test/acceptance/attendee_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,13 @@ func TestCreateNewAttendee_MultiPackageTooMany(t *testing.T) {

docs.When("when they attempt to create a new attendee, adding a package too many times")
attendeeSent := tstBuildValidAttendee("na67-")
tstAddPackages(&attendeeSent, "mountain-trip,mountain-trip,mountain-trip")
tstAddPackages(&attendeeSent, "mountain-trip,mountain-trip,mountain-trip,mountain-trip")
response := tstPerformPost("/api/rest/v1/attendees", tstRenderJson(attendeeSent), token)

docs.Then("then the attempt is rejected as invalid (400) with an appropriate error response")
tstRequireErrorResponse(t, response, http.StatusBadRequest, "attendee.data.invalid", url.Values{
"packages": []string{"package mountain-trip occurs too many times, can occur at most 2 times"},
"packages_list": []string{"package mountain-trip occurs too many times, can occur at most 2 times"},
"packages": []string{"package mountain-trip occurs too many times, can occur at most 3 times"},
"packages_list": []string{"package mountain-trip occurs too many times, can occur at most 3 times"},
})
}

Expand Down Expand Up @@ -1589,13 +1589,13 @@ func TestUpdateExistingAttendee_AddPackageTooManyTimes(t *testing.T) {

docs.When("when they send updated attendee info and try to add a package too many times")
changedAttendee := att
tstAddPackages(&changedAttendee, "mountain-trip,mountain-trip,mountain-trip")
tstAddPackages(&changedAttendee, "mountain-trip,mountain-trip,mountain-trip,mountain-trip")
changedAttendee.Packages = att.Packages // should be ignored, so let's make it produce no error if used
response := tstPerformPut(loc, tstRenderJson(changedAttendee), token)

docs.Then("then the request fails with the expected error")
tstRequireErrorResponse(t, response, http.StatusBadRequest, "attendee.data.invalid", url.Values{
"packages_list": []string{"package mountain-trip occurs too many times, can occur at most 2 times"},
"packages_list": []string{"package mountain-trip occurs too many times, can occur at most 3 times"},
})
}

Expand Down
5 changes: 4 additions & 1 deletion test/testconfig-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ choices:
visible_for:
- regdesk
mountain-trip:
allowed_counts:
- 1
- 3
description: 'Mountain Trip'
price: 3000
vat_percent: 19
max_count: 2
max_count: 3
day-thu:
description: 'Day Guest (Thursday)'
price: 6000
Expand Down

0 comments on commit 129c9c4

Please sign in to comment.