115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"code.dlmw.ch/dlmw/qv/internal/models"
|
|
"github.com/stretchr/testify/mock"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func newTestApplication(t *testing.T) *application {
|
|
return &application{
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
elections: &mockElectionModel{},
|
|
voters: &mockVoterModel{},
|
|
votes: &mockVoteModel{},
|
|
}
|
|
}
|
|
|
|
type testServer struct {
|
|
*httptest.Server
|
|
}
|
|
|
|
func newTestServer(t *testing.T, h http.Handler) *testServer {
|
|
server := httptest.NewServer(h)
|
|
return &testServer{server}
|
|
}
|
|
|
|
func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, string) {
|
|
rs, err := ts.Client().Get(ts.URL + urlPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer rs.Body.Close()
|
|
|
|
body, err := io.ReadAll(rs.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body = bytes.TrimSpace(body)
|
|
|
|
return rs.StatusCode, rs.Header, string(body)
|
|
}
|
|
|
|
func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, http.Header, string) {
|
|
res, err := ts.Client().Post(ts.URL+urlPath, "application/json", body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
responseBody, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return res.StatusCode, res.Header, string(responseBody)
|
|
}
|
|
|
|
type mockElectionModel struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (e *mockElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters int, choices []string, expiresAt time.Time) (string, error) {
|
|
args := e.Called(name, tokens, areVotersKnown, maxVoters, choices, expiresAt)
|
|
return args.String(0), args.Error(1)
|
|
}
|
|
|
|
func (e *mockElectionModel) GetById(id string) (*models.Election, error) {
|
|
args := e.Called(id)
|
|
return args.Get(0).(*models.Election), args.Error(1)
|
|
}
|
|
|
|
type mockVoterModel struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (v *mockVoterModel) InsertMultiple(identities []string, electionID string) ([]int, error) {
|
|
args := v.Called(identities, electionID)
|
|
return args.Get(0).([]int), args.Error(1)
|
|
}
|
|
|
|
func (v *mockVoterModel) CountByElection(electionID string) (int, error) {
|
|
args := v.Called(electionID)
|
|
return args.Int(0), args.Error(1)
|
|
}
|
|
|
|
func (v *mockVoterModel) Exists(voterIdentity string, electionID string) (bool, error) {
|
|
args := v.Called(voterIdentity, electionID)
|
|
return args.Bool(0), args.Error(1)
|
|
}
|
|
|
|
type mockVoteModel struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (v *mockVoteModel) Insert(voterIdentity string, electionId string, choiceText string, tokens int) (int, error) {
|
|
args := v.Called(voterIdentity, electionId, choiceText, tokens)
|
|
return args.Int(0), args.Error(1)
|
|
}
|
|
|
|
func (v *mockVoteModel) Exists(voterIdentity string, electionID string) (bool, error) {
|
|
args := v.Called(voterIdentity, electionID)
|
|
return args.Bool(0), args.Error(1)
|
|
}
|
|
|
|
func (v *mockVoteModel) GetByElection(electionID string) (*[]models.Vote, error) {
|
|
args := v.Called(electionID)
|
|
return args.Get(0).(*[]models.Vote), args.Error(1)
|
|
}
|