Add more test cases and add UniqueValues check

This commit is contained in:
2025-01-01 19:47:55 +01:00
parent 53aa4ab375
commit 941e9a36fb
3 changed files with 58 additions and 1 deletions

View File

@ -2,7 +2,7 @@ package main
import (
"bytes"
api "code.dlmw.ch/dlmw/qv/internal"
"code.dlmw.ch/dlmw/qv/internal"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
@ -47,6 +47,19 @@ func TestCreateElection(t *testing.T) {
},
expectedCode: http.StatusOK,
},
{
name: "Valid request (with 3 choices)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha", "You"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusOK,
},
{
name: "Valid request (voters unknown with max voters)",
urlPath: "/election",
@ -125,6 +138,32 @@ func TestCreateElection(t *testing.T) {
},
expectedCode: http.StatusUnprocessableEntity,
},
{
name: "Invalid request (choices are not unique)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Gandhi"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
},
{
name: "Invalid request (choices contain blank entries)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha", ""},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
},
}
for _, tt := range tests {