Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
45 changes: 39 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
Expand All @@ -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)
Expand Down Expand Up @@ -181,19 +184,21 @@ 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":
poll.Options = []string{"Pass", "Fail/Conditional", "Abstain"}
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:
Expand Down Expand Up @@ -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 {
Expand Down
103 changes: 103 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
9 changes: 8 additions & 1 deletion database/poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestOrderOptions(t *testing.T) {
name string
input map[string]int
output []string
alt []string
}{
{
name: "SimpleOrder",
Expand Down Expand Up @@ -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",
Expand All @@ -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)
})
}
}
Loading
Loading