Files
qv/cmd/web/handlers_test.go

181 lines
4.8 KiB
Go

package main
import (
"bytes"
"code.dlmw.ch/dlmw/qv/internal"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)
func TestCreateElection(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())
defer server.Close()
tests := []struct {
name string
urlPath string
body any
expectedCode int
}{
{
name: "Valid request (small name, other language)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"宮本武蔵", "伊東一刀斎"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 0,
Name: "強",
Tokens: 100,
},
expectedCode: http.StatusOK,
},
{
name: "Valid request (voters unknown with unlimited voters)",
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.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",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 10,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusOK,
},
{
name: "Valid request (voters known with max voters)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: 10,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusOK,
},
{
name: "Invalid request (not enough choices)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi"},
ExpiresAt: time.Unix(0, 0),
AreVotersKnown: false,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
},
{
name: "Invalid request (expiresAt is not in the future)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Unix(0, 0),
AreVotersKnown: false,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
},
{
name: "Invalid request (max voters must be greater than 0 for known elections)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Unix(0, 0),
AreVotersKnown: true,
MaxVoters: 0,
Name: "Guy of the year",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
},
{
name: "Invalid request (blank name)",
urlPath: "/election",
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Unix(0, 0),
AreVotersKnown: true,
MaxVoters: 0,
Name: "",
Tokens: 100,
},
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 {
t.Run(tt.name, func(t *testing.T) {
requestBody, err := json.Marshal(tt.body)
if err != nil {
t.Fatal(err)
}
code, _, _ := server.post(t, tt.urlPath, bytes.NewReader(requestBody))
assert.Equal(t, tt.expectedCode, code)
})
}
}