Add tests for createElection handler

This commit is contained in:
2024-12-31 14:35:31 +01:00
parent 57bd72506b
commit c9b7a5796e
4 changed files with 128 additions and 46 deletions

View File

@ -4,22 +4,12 @@ import (
"bytes"
api "code.dlmw.ch/dlmw/qv/internal"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)
var (
validCreateElectionRequest = api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: nil,
Name: "Guy of the year",
Tokens: 100,
} // TODO: try to find a way to generate test data
)
func TestCreateElection(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())
@ -28,27 +18,124 @@ func TestCreateElection(t *testing.T) {
tests := []struct {
name string
urlPath string
body any
expectedCode int
expectedBody string
}{
{
name: "Valid request",
urlPath: "/election",
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,
expectedBody: "",
},
{
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 (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,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
requestBody, err := json.Marshal(validCreateElectionRequest)
requestBody, err := json.Marshal(tt.body)
if err != nil {
t.Fatal(err)
}
code, _, body := server.post(t, tt.urlPath, bytes.NewReader(requestBody))
t.Logf("Code was %v and body %v", code, body)
code, _, _ := server.post(t, tt.urlPath, bytes.NewReader(requestBody))
assert.Equal(t, tt.expectedCode, code)
})
}
}

View File

@ -1,18 +1,19 @@
package main
import (
"code.dlmw.ch/dlmw/qv/internal/models"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func newTestApplication(t *testing.T) *application {
return &application{
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
elections: &models.ElectionModel{},
elections: &mockElectionModel{},
voters: &mockVoterModel{},
}
}
@ -39,3 +40,17 @@ func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, h
return res.StatusCode, res.Header, string(responseBody)
}
type mockElectionModel struct {
}
func (e *mockElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters int, choices []string, expiresAt time.Time) (int, error) {
return 1, nil
}
type mockVoterModel struct {
}
func (v *mockVoterModel) Insert(identity string, electionID int) (int, error) {
return 1, nil
}