55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
api "code.dlmw.ch/dlmw/qv/internal"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
validCreateElectionRequest = api.CreateElectionRequest{
|
|
Choices: []string{"Gandhi", "Buddha"},
|
|
ExpiresAt: time.Now().Add(24 * time.Hour),
|
|
IsAnonymous: false,
|
|
MaxVoters: nil,
|
|
Name: "Guy of the year",
|
|
Tokens: 100,
|
|
} // TODO: try to find a way to generate test data
|
|
)
|
|
|
|
func TestCreateElection(t *testing.T) {
|
|
app := newTestApplication(t)
|
|
server := newTestServer(t, app.routes())
|
|
defer server.Close()
|
|
|
|
tests := []struct {
|
|
name string
|
|
urlPath string
|
|
expectedCode int
|
|
expectedBody string
|
|
}{
|
|
{
|
|
name: "Valid request",
|
|
urlPath: "/election",
|
|
expectedCode: http.StatusOK,
|
|
expectedBody: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
requestBody, err := json.Marshal(validCreateElectionRequest)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
code, _, body := server.post(t, tt.urlPath, bytes.NewReader(requestBody))
|
|
|
|
t.Logf("Code was %v and body %v", code, body)
|
|
})
|
|
}
|
|
}
|