Move voting to a new endpoint

/election/{id}/votes
This commit is contained in:
2025-01-11 18:29:01 +01:00
parent 86d7d0e881
commit cad5cfe636
8 changed files with 186 additions and 100 deletions

View File

@ -254,7 +254,7 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) {
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(1, nil)
path := "/votes"
path := "/election/1/votes"
tests := []struct {
name string
@ -273,7 +273,6 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
},
expectedCode: http.StatusCreated,
@ -289,7 +288,6 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 41},
},
ElectionId: 1,
VoterIdentity: nil,
},
expectedCode: http.StatusUnprocessableEntity,
@ -305,7 +303,6 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddh", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
},
expectedCode: http.StatusUnprocessableEntity,
@ -362,7 +359,7 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
On("Exists", mock.Anything, mock.Anything).
Return(false, nil)
path := "/votes"
path := "/election/1/votes"
tests := []struct {
name string
@ -381,7 +378,6 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: &EXISTING_VOTER_IDENTITY,
},
expectedCode: http.StatusCreated,
@ -397,7 +393,6 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: &NON_EXISTING_VOTER_IDENTITY,
},
expectedCode: http.StatusUnprocessableEntity,
@ -413,7 +408,6 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
},
expectedCode: http.StatusUnprocessableEntity,
@ -429,7 +423,6 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 41},
},
ElectionId: 1,
VoterIdentity: &EXISTING_VOTER_IDENTITY,
},
expectedCode: http.StatusUnprocessableEntity,
@ -445,7 +438,6 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddh", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: &EXISTING_VOTER_IDENTITY,
},
expectedCode: http.StatusUnprocessableEntity,
@ -475,7 +467,7 @@ func TestCreateVotes_NonExistingElection(t *testing.T) {
On("GetById", mock.Anything).
Return((*models.Election)(nil), sql.ErrNoRows)
path := "/votes"
path := "/election/1/votes"
requestBody := api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
@ -484,7 +476,6 @@ func TestCreateVotes_NonExistingElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
}
requestBodyJson, err := json.Marshal(requestBody)
@ -494,7 +485,38 @@ func TestCreateVotes_NonExistingElection(t *testing.T) {
code, _, _ := server.post(t, path, bytes.NewReader(requestBodyJson))
assert.Equal(t, http.StatusUnprocessableEntity, code)
assert.Equal(t, http.StatusNotFound, code)
}
func TestCreateVotes_NonNumberElectionID(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)(nil), sql.ErrNoRows)
path := "/election/1a/votes"
requestBody := api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
Tokens int `json:"tokens"`
}{
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
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.StatusBadRequest, code)
}
func TestCreateVotes_AlreadyVoted(t *testing.T) {
@ -533,7 +555,8 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
On("Exists", mock.Anything, mock.Anything).
Return(true, nil)
path := "/votes"
existingElectionPath := "/election/1/votes"
nonExistingElectionPath := "/election/1/votes"
voterIdentity := "anything"
tests := []struct {
@ -544,7 +567,7 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
}{
{
name: "Invalid request for known voters election (already voted)",
urlPath: path,
urlPath: existingElectionPath,
body: api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
@ -553,14 +576,13 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
{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,
urlPath: nonExistingElectionPath,
body: api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
@ -569,7 +591,6 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 2,
VoterIdentity: &voterIdentity,
},
expectedCode: http.StatusUnprocessableEntity,
@ -619,7 +640,7 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) {
On("CountByElection", mock.Anything).
Return(10, nil)
path := "/votes"
path := "/election/1/votes"
requestBody := api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
@ -628,7 +649,6 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
}
requestBodyJson, err := json.Marshal(requestBody)
@ -671,7 +691,7 @@ func TestCreateVotes_ExpiredElection(t *testing.T) {
On("CountByElection", mock.Anything).
Return(10, nil)
path := "/votes"
path := "/election/1/votes"
requestBody := api.CreateVotesRequest{
Choices: []struct {
ChoiceText string `json:"choiceText"`
@ -680,7 +700,6 @@ func TestCreateVotes_ExpiredElection(t *testing.T) {
{ChoiceText: "Gandhi", Tokens: 60},
{ChoiceText: "Buddha", Tokens: 40},
},
ElectionId: 1,
VoterIdentity: nil,
}
requestBodyJson, err := json.Marshal(requestBody)