From 4fbf72d84dda83917ad973fec5084461906ea635 Mon Sep 17 00:00:00 2001 From: dylan Date: Thu, 9 Jan 2025 18:12:18 +0100 Subject: [PATCH] Add tests for already voted cases --- cmd/web/handlers_test.go | 112 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index b7c7662..6dc5962 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -219,7 +219,7 @@ func TestCreateElection_ServerError(t *testing.T) { assert.Equal(t, 500, code) } -func TestCreateVotesUnknownVotersElection(t *testing.T) { +func TestCreateVotes_UnknownVotersElection(t *testing.T) { app := newTestApplication(t) server := newTestServer(t, app.routes()) defer server.Close() @@ -325,7 +325,7 @@ func TestCreateVotesUnknownVotersElection(t *testing.T) { } } -func TestCreateVotesKnownVotersElection(t *testing.T) { +func TestCreateVotes_KnownVotersElection(t *testing.T) { app := newTestApplication(t) server := newTestServer(t, app.routes()) defer server.Close() @@ -402,6 +402,22 @@ func TestCreateVotesKnownVotersElection(t *testing.T) { }, expectedCode: http.StatusUnprocessableEntity, }, + { + name: "Invalid request for unknown voters election (no voter identity provided)", + urlPath: path, + body: api.CreateVotesRequest{ + Choices: []struct { + ChoiceText string `json:"choiceText"` + Tokens int `json:"tokens"` + }{ + {ChoiceText: "Gandhi", Tokens: 60}, + {ChoiceText: "Buddha", Tokens: 40}, + }, + ElectionId: 1, + VoterIdentity: nil, + }, + expectedCode: http.StatusUnprocessableEntity, + }, { name: "Invalid request for unknown voters election (too many tokens used)", urlPath: path, @@ -480,3 +496,95 @@ func TestCreateVotes_NonExistingElection(t *testing.T) { assert.Equal(t, http.StatusUnprocessableEntity, code) } + +func TestCreateVotes_AlreadyVoted(t *testing.T) { + app := newTestApplication(t) + server := newTestServer(t, app.routes()) + defer server.Close() + + unknownVotersElection := models.Election{ + ID: 1, + Name: "Guy of the year", + Tokens: 100, + AreVotersKnown: false, + MaxVoters: 10, + CreatedAt: time.Now(), + ExpiresAt: time.Now().Add(24 * time.Hour), + Choices: []string{"Gandhi", "Buddha"}, + } + knownVotersElection := unknownVotersElection + knownVotersElection.AreVotersKnown = true + + mockElections := app.elections.(*mockElectionModel) + mockElections. + On("GetById", 1). + Return(&knownVotersElection, nil) + mockElections. + On("GetById", 2). + Return(&unknownVotersElection, nil) + + mockVotes := app.votes.(*mockVoteModel) + mockVotes. + On("Exists", mock.Anything, mock.Anything). + Return(true, nil) + + mockVoters := app.voters.(*mockVoterModel) + mockVoters. + On("Exists", mock.Anything, mock.Anything). + Return(true, nil) + + path := "/votes" + voterIdentity := "anything" + + tests := []struct { + name string + urlPath string + body any + expectedCode int + }{ + { + name: "Invalid request for known voters election (already voted)", + urlPath: path, + body: api.CreateVotesRequest{ + Choices: []struct { + ChoiceText string `json:"choiceText"` + Tokens int `json:"tokens"` + }{ + {ChoiceText: "Gandhi", Tokens: 60}, + {ChoiceText: "Buddha", Tokens: 40}, + }, + ElectionId: 1, + VoterIdentity: &voterIdentity, + }, + expectedCode: http.StatusUnprocessableEntity, + }, + { + name: "Invalid request for unknown voters election (already voted)", + urlPath: path, + body: api.CreateVotesRequest{ + Choices: []struct { + ChoiceText string `json:"choiceText"` + Tokens int `json:"tokens"` + }{ + {ChoiceText: "Gandhi", Tokens: 60}, + {ChoiceText: "Buddha", Tokens: 40}, + }, + ElectionId: 2, + VoterIdentity: &voterIdentity, + }, + 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) + }) + } +}