Write test for expired election

This commit is contained in:
2025-01-09 18:26:22 +01:00
parent b25e090a7b
commit 0b0ecaba9b

View File

@ -640,3 +640,55 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) {
assert.Equal(t, http.StatusUnprocessableEntity, code)
}
func TestCreateVotes_ExpiredElection(t *testing.T) {
app := newTestApplication(t)
server := newTestServer(t, app.routes())
defer server.Close()
mockElections := app.elections.(*mockElectionModel)
mockElections.
On("GetById", mock.Anything).
Return(&models.Election{
ID: 1,
Name: "Guy of the year",
Tokens: 100,
AreVotersKnown: false,
MaxVoters: 10,
CreatedAt: time.UnixMilli(1),
ExpiresAt: time.UnixMilli(100000),
Choices: []string{"Gandhi", "Buddha"},
}, nil)
mockVoters := app.voters.(*mockVoterModel)
mockVoters.
On("Exists", mock.Anything, mock.Anything).
Return(false, nil)
mockVoters.
On("Insert", mock.Anything, mock.Anything).
Return(1, nil)
mockVoters.
On("CountByElection", mock.Anything).
Return(10, nil)
path := "/votes"
requestBody := api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
Tokens int `json:"tokens"`
}{
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
}
requestBodyJson, err := json.Marshal(requestBody)
if err != nil {
t.Fatal(err)
}
code, _, _ := server.post(t, path, bytes.NewReader(requestBodyJson))
assert.Equal(t, http.StatusUnprocessableEntity, code)
}