Files
qv/cmd/web/handlers_test.go

183 lines
4.8 KiB
Go
Raw Normal View History

2024-12-28 17:58:01 +01:00
package main
import (
"bytes"
"code.dlmw.ch/dlmw/qv/internal"
2024-12-28 17:58:01 +01:00
"encoding/json"
2024-12-31 14:35:31 +01:00
"github.com/stretchr/testify/assert"
2024-12-28 17:58:01 +01:00
"net/http"
"testing"
"time"
)
func TestCreateElection(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())
defer server.Close()
2025-01-02 17:44:44 +01:00
path := "/election"
2024-12-28 17:58:01 +01:00
tests := []struct {
name string
urlPath string
2024-12-31 14:35:31 +01:00
body any
2024-12-28 17:58:01 +01:00
expectedCode int
}{
{
2024-12-31 14:35:31 +01:00
name: "Valid request (small name, other language)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
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,
},
2024-12-31 14:35:31 +01:00
{
name: "Valid request (voters unknown with max voters)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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,
},
2024-12-28 17:58:01 +01:00
expectedCode: http.StatusOK,
2024-12-31 14:35:31 +01:00
},
{
name: "Invalid request (not enough choices)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
2024-12-31 14:35:31 +01:00
body: api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Unix(0, 0),
AreVotersKnown: true,
MaxVoters: 0,
Name: "",
Tokens: 100,
},
expectedCode: http.StatusUnprocessableEntity,
2024-12-28 17:58:01 +01:00
},
{
name: "Invalid request (choices are not unique)",
2025-01-02 17:44:44 +01:00
urlPath: path,
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)",
2025-01-02 17:44:44 +01:00
urlPath: path,
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,
},
2024-12-28 17:58:01 +01:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2024-12-31 14:35:31 +01:00
requestBody, err := json.Marshal(tt.body)
2024-12-28 17:58:01 +01:00
if err != nil {
t.Fatal(err)
}
2024-12-31 14:35:31 +01:00
code, _, _ := server.post(t, tt.urlPath, bytes.NewReader(requestBody))
assert.Equal(t, tt.expectedCode, code)
2024-12-28 17:58:01 +01:00
})
}
}