From f94e08fc7fe399f0a4a8c74e291f5c3bc87f542a Mon Sep 17 00:00:00 2001 From: dylan Date: Fri, 17 Jan 2025 17:11:16 +0100 Subject: [PATCH] Add integration tests for createVotes --- cmd/web/main_integration.go | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/cmd/web/main_integration.go b/cmd/web/main_integration.go index 3e72964..94a2ef6 100644 --- a/cmd/web/main_integration.go +++ b/cmd/web/main_integration.go @@ -8,10 +8,12 @@ import ( "context" "database/sql" "errors" + "fmt" . "github.com/Eun/go-hit" "log/slog" "net/http" "os" + "strings" "time" ) @@ -40,6 +42,7 @@ func main() { logger: logger, elections: &models.ElectionModel{DB: db}, voters: &models.VoterModel{DB: db}, + votes: &models.VoteModel{DB: db}, } logger.Info("Starting integration tests server", "addr", addr) @@ -82,6 +85,7 @@ func openDb() (*sql.DB, error) { func runTests() { runCreateElectionTests() + runCreateVotesTests() } func runCreateElectionTests() { @@ -194,3 +198,84 @@ func runCreateElectionTests() { 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), + ) +}