diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index cd3bf9d..2e06771 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -31,7 +31,7 @@ jobs: gofmt -s -l database logging sse *.go - name: Run Tests run: | - go test ./database + go test ./... - name: Run vet run: | go vet ./database/ ./logging/ ./sse/ diff --git a/api.go b/api.go index 8fe6c7c..44c3ae2 100644 --- a/api.go +++ b/api.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "math" "net/http" @@ -21,6 +22,8 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +const MAX_CUSTOM_OPTIONS = 50 + // Gets the number of people eligible to vote in a poll func GetVoterCount(poll database.Poll) int { return len(poll.AllowedUsers) @@ -181,6 +184,10 @@ func CreatePoll(c *gin.Context) { if c.PostForm("rankedChoice") == "true" { poll.VoteType = database.POLL_TYPE_RANKED } + if poll.VoteType == database.POLL_TYPE_RANKED && c.PostForm("options") != "custom" { + c.JSON(http.StatusBadRequest, gin.H{"error": "ranked choice polls must use custom options"}) + return + } switch c.PostForm("options") { case "pass-fail-conditional": @@ -188,12 +195,10 @@ func CreatePoll(c *gin.Context) { case "fail-conditional": poll.Options = []string{"Fail", "Conditional", "Abstain"} case "custom": - poll.Options = []string{} - for opt := range strings.SplitSeq(c.PostForm("customOptions"), ",") { - poll.Options = append(poll.Options, strings.TrimSpace(opt)) - if !slices.Contains(poll.Options, "Abstain") && (poll.VoteType == database.POLL_TYPE_SIMPLE) { - poll.Options = append(poll.Options, "Abstain") - } + poll.Options, err = parseCustomOptions(c.PostFormArray("customOptions"), poll.VoteType == database.POLL_TYPE_RANKED) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return } case "pass-fail": default: @@ -613,6 +618,34 @@ func containsPoll(polls []*database.Poll, poll *database.Poll) bool { return false } +// Trims and validates custom poll options, appending Abstain for simple polls +func parseCustomOptions(raw []string, ranked bool) ([]string, error) { + options := []string{} + seen := make(map[string]bool) + for _, opt := range raw { + opt = strings.TrimSpace(opt) + if opt == "" { + continue + } + key := strings.ToLower(opt) + if seen[key] { + return nil, fmt.Errorf("duplicate option: %s", opt) + } + seen[key] = true + options = append(options, opt) + } + if len(options) == 0 { + return nil, errors.New("at least one option is required") + } + if len(options) > MAX_CUSTOM_OPTIONS { + return nil, fmt.Errorf("a poll may have at most %d options", MAX_CUSTOM_OPTIONS) + } + if !ranked && !seen["abstain"] { + options = append(options, "Abstain") + } + return options, nil +} + func hasOption(poll *database.Poll, option string) bool { for _, opt := range poll.Options { if opt == option { diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..7770088 --- /dev/null +++ b/api_test.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func makeOptions(n int) []string { + options := make([]string, n) + for i := range options { + options[i] = fmt.Sprintf("Option %d", i+1) + } + return options +} + +func TestParseCustomOptions(t *testing.T) { + tests := []struct { + name string + raw []string + ranked bool + want []string + wantErr bool + }{ + { + name: "trims and keeps order", + raw: []string{" A ", "B"}, + want: []string{"A", "B", "Abstain"}, + }, + { + name: "commas kept", + raw: []string{"Smith, John", "Doe, Jane"}, + want: []string{"Smith, John", "Doe, Jane", "Abstain"}, + }, + { + name: "blanks dropped", + raw: []string{"A", "", " ", "B"}, + want: []string{"A", "B", "Abstain"}, + }, + { + name: "user abstain kept without a second one", + raw: []string{"Yes", "abstain"}, + want: []string{"Yes", "abstain"}, + }, + { + name: "ranked gets no abstain", + raw: []string{"A", "B", "C"}, + ranked: true, + want: []string{"A", "B", "C"}, + }, + { + name: "single option", + raw: []string{"Only"}, + ranked: true, + want: []string{"Only"}, + }, + { + name: "no options", + raw: []string{}, + wantErr: true, + }, + { + name: "only blank options", + raw: []string{"", " "}, + ranked: true, + wantErr: true, + }, + { + name: "case insensitive duplicate", + raw: []string{"Alice", " alice"}, + wantErr: true, + }, + { + name: "50 options allowed", + raw: makeOptions(50), + ranked: true, + want: makeOptions(50), + }, + { + name: "51 options rejected", + raw: makeOptions(51), + ranked: true, + wantErr: true, + }, + { + name: "limit applies before abstain", + raw: makeOptions(50), + want: append(makeOptions(50), "Abstain"), + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := parseCustomOptions(test.raw, test.ranked) + if test.wantErr { + assert.Error(t, err) + return + } + assert.NoError(t, err) + assert.Equal(t, test.want, got) + }) + } +} diff --git a/database/poll_test.go b/database/poll_test.go index 9787399..d18bf93 100644 --- a/database/poll_test.go +++ b/database/poll_test.go @@ -142,6 +142,7 @@ func TestOrderOptions(t *testing.T) { name string input map[string]int output []string + alt []string }{ { name: "SimpleOrder", @@ -201,7 +202,9 @@ func TestOrderOptions(t *testing.T) { "three": 1, "four": 2, }, + // ties resolve by map iteration order, so either duplicate may win output: []string{"one", "three", "three", "four"}, + alt: []string{"one", "two", "two", "four"}, }, { name: "Gap", @@ -219,7 +222,11 @@ func TestOrderOptions(t *testing.T) { t.Run(test.name, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - assert.Equal(t, test.output, orderOptions(ctx, test.input)) + actual := orderOptions(ctx, test.input) + if test.alt != nil && assert.ObjectsAreEqual(test.alt, actual) { + return + } + assert.Equal(t, test.output, actual) }) } } diff --git a/templates/create.tmpl b/templates/create.tmpl index 84e823e..e38fcc7 100644 --- a/templates/create.tmpl +++ b/templates/create.tmpl @@ -2,7 +2,7 @@