From bde4b2c86ddc9da439bb3e13e897291b360a8283 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 10 May 2022 19:48:51 -0600 Subject: [PATCH] A bit cleaner test interface, maybe --- cmd/mothd/httpd_test.go | 11 +++++------ cmd/mothd/server_test.go | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cmd/mothd/httpd_test.go b/cmd/mothd/httpd_test.go index 9ddee02..385e188 100644 --- a/cmd/mothd/httpd_test.go +++ b/cmd/mothd/httpd_test.go @@ -36,8 +36,7 @@ func (hs *HTTPServer) TestRequest(path string, args map[string]string) *httptest func TestHttpd(t *testing.T) { server := NewTestServer() - hs := NewHTTPServer("/", server) - stateProvider := server.State.(*State) + hs := NewHTTPServer("/", server.MothServer) if r := hs.TestRequest("/", nil); r.Result().StatusCode != 200 { t.Error(r.Result()) @@ -74,7 +73,7 @@ func TestHttpd(t *testing.T) { t.Error("Register failed", r.Body.String()) } - stateProvider.refresh() + server.refresh() if r := hs.TestRequest("/state", nil); r.Result().StatusCode != 200 { t.Error(r.Result()) @@ -131,7 +130,7 @@ func TestHttpd(t *testing.T) { t.Error("Unexpected body", r.Body.String()) } - stateProvider.refresh() + server.refresh() if r := hs.TestRequest("/content/pategory/2/puzzle.json", nil); r.Result().StatusCode != 200 { t.Error(r.Result()) @@ -183,7 +182,7 @@ func TestDevelMemHttpd(t *testing.T) { srv := NewTestServer() { - hs := NewHTTPServer("/", srv) + hs := NewHTTPServer("/", srv.MothServer) if r := hs.TestRequest("/mothballer/pategory.md", nil); r.Result().StatusCode != 404 { t.Error("Should have gotten a 404 for mothballer in prod mode") @@ -192,7 +191,7 @@ func TestDevelMemHttpd(t *testing.T) { { srv.Config.Devel = true - hs := NewHTTPServer("/", srv) + hs := NewHTTPServer("/", srv.MothServer) if r := hs.TestRequest("/mothballer/pategory.md", nil); r.Result().StatusCode != 500 { t.Log(r.Body.String()) diff --git a/cmd/mothd/server_test.go b/cmd/mothd/server_test.go index 92c8839..4be46c6 100644 --- a/cmd/mothd/server_test.go +++ b/cmd/mothd/server_test.go @@ -9,10 +9,14 @@ import ( const TestTeamID = "teamID" +type TestServer struct { + *MothServer +} + // NewTestServer creates a new MothServer with NewTestMothballs and some initial state. // // See function definition for details. -func NewTestServer() *MothServer { +func NewTestServer() TestServer { puzzles := NewTestMothballs() puzzles.refresh() @@ -24,7 +28,14 @@ func NewTestServer() *MothServer { theme := NewTestTheme() afero.WriteFile(theme.Fs, "/index.html", []byte("index.html"), 0644) - return NewMothServer(Configuration{}, theme, state, puzzles) + return TestServer{NewMothServer(Configuration{}, theme, state, puzzles)} +} + +func (ts TestServer) refresh() { + ts.State.(*State).refresh() + for _, pp := range ts.PuzzleProviders { + pp.(*Mothballs).refresh() + } } func TestDevelServer(t *testing.T) { @@ -49,7 +60,6 @@ func TestProdServer(t *testing.T) { teamID := TestTeamID server := NewTestServer() - state := server.State.(*State) handler := server.NewHandler(participantID, teamID) anonHandler := server.NewHandler("badParticipantId", "badTeamId") @@ -81,7 +91,7 @@ func TestProdServer(t *testing.T) { t.Error("index.html wrong contents", contents) } - state.refresh() + server.refresh() { es := handler.ExportState() @@ -134,7 +144,7 @@ func TestProdServer(t *testing.T) { t.Error("Right answer marked wrong", err) } - state.refresh() + server.refresh() { es := handler.ExportState() @@ -163,7 +173,7 @@ func TestProdServer(t *testing.T) { t.Error("Right answer marked wrong:", err) } - state.refresh() + server.refresh() { es := anonHandler.ExportState()