Add integration tests for createVotes

This commit is contained in:
2025-01-17 17:11:16 +01:00
parent 5e4a089b89
commit f94e08fc7f

View File

@ -8,10 +8,12 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"fmt"
. "github.com/Eun/go-hit" . "github.com/Eun/go-hit"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
) )
@ -40,6 +42,7 @@ func main() {
logger: logger, logger: logger,
elections: &models.ElectionModel{DB: db}, elections: &models.ElectionModel{DB: db},
voters: &models.VoterModel{DB: db}, voters: &models.VoterModel{DB: db},
votes: &models.VoteModel{DB: db},
} }
logger.Info("Starting integration tests server", "addr", addr) logger.Info("Starting integration tests server", "addr", addr)
@ -82,6 +85,7 @@ func openDb() (*sql.DB, error) {
func runTests() { func runTests() {
runCreateElectionTests() runCreateElectionTests()
runCreateVotesTests()
} }
func runCreateElectionTests() { func runCreateElectionTests() {
@ -194,3 +198,84 @@ func runCreateElectionTests() {
Expect().Status().Equal(http.StatusUnprocessableEntity), Expect().Status().Equal(http.StatusUnprocessableEntity),
) )
} }
func runCreateVotesTests() {
uri := "http://127.0.0.1:8080/api/election/%v/votes"
MustDo(
Post(fmt.Sprintf(uri, "1")),
Send().Body().String(`
{
"choices": [
{
"choiceText": "Buddha",
"tokens": 90
},
{
"choiceText": "Gandhi",
"tokens": 10
}
]
}`),
Expect().Status().Equal(http.StatusCreated),
)
MustDo(
Post(fmt.Sprintf(uri, "3")),
Send().Body().String(`
{
"choices": [
{
"choiceText": "Buddha",
"tokens": 90
},
{
"choiceText": "Gandhi",
"tokens": 10
}
]
}`),
Expect().Status().Equal(http.StatusUnprocessableEntity),
)
voterIdentities := make([]string, 0)
locationHeader := ""
MustDo(
Post("http://127.0.0.1:8080/api/election"),
Send().Body().String(`
{
"name": "Guy of the year",
"tokens": 100,
"areVotersKnown": true,
"maxVoters": 10,
"expiresAt": "2124-12-31T14:15:22Z",
"choices": [
"Gandhi",
"Buddha"
]
}`),
Expect().Status().Equal(http.StatusOK),
Store().Response().Body().JSON().JQ(".voterIdentities").In(&voterIdentities),
Store().Response().Headers("Location").In(&locationHeader),
)
electionId := strings.Split(locationHeader, "/")[2]
MustDo(
Post(fmt.Sprintf(uri, electionId)),
Send().Body().String(fmt.Sprintf(`
{
"voterIdentity": "%v",
"choices": [
{
"choiceText": "Buddha",
"tokens": 90
},
{
"choiceText": "Gandhi",
"tokens": 10
}
]
}`, voterIdentities[0])),
Expect().Status().Equal(http.StatusCreated),
)
}