Add some more tests and implement getElection

This commit is contained in:
2025-01-17 14:49:51 +01:00
parent 60d1bb382c
commit 410e8f39d3
6 changed files with 236 additions and 1 deletions

View File

@ -192,6 +192,50 @@ func TestCreateElection(t *testing.T) {
}
}
func TestCreateElection_KnownVoters(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())
defer server.Close()
mockElections := app.elections.(*mockElectionModel)
mockElections.
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(1, nil)
mockVoters := app.voters.(*mockVoterModel)
mockVoters.
On("InsertMultiple", mock.Anything, mock.Anything).
Return([]int{1}, nil)
path := baseUri + "election"
requestBody := api.CreateElectionRequest{
Choices: []string{"宮本武蔵", "伊東一刀斎"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: true,
MaxVoters: 100,
Name: "強",
Tokens: 100,
}
requestBodyJson, err := json.Marshal(requestBody)
if err != nil {
t.Fatal(err)
}
code, _, body := server.post(t, path, bytes.NewReader(requestBodyJson))
assert.Equal(t, http.StatusOK, code)
var voterIdentities struct {
VoterIdentities []string
}
err = json.Unmarshal([]byte(body), &voterIdentities)
if err != nil {
t.Fatal(err)
}
assert.Len(t, voterIdentities.VoterIdentities, 100)
}
func TestCreateElection_ServerError(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())