Add integration tests for createVotes
This commit is contained in:
@ -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),
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user