From 5fe464acbcf92ddfdfb542bda1664fdad039a56b Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 14 Oct 2020 10:04:13 -0600 Subject: [PATCH] Don't map the "self" team in unauthenticated states --- cmd/mothd/server.go | 11 ++++-- cmd/mothd/server_test.go | 85 ++++++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/cmd/mothd/server.go b/cmd/mothd/server.go index ad929b5..f801857 100644 --- a/cmd/mothd/server.go +++ b/cmd/mothd/server.go @@ -183,13 +183,18 @@ func (mh *MothRequestHandler) exportStateIfRegistered(override bool) *StateExpor registered := override || (err == nil) export.Messages = mh.State.Messages() - export.TeamNames = map[string]string{"self": teamName} +inin export.TeamNames = make(map[string]string) // Anonymize team IDs in points log, and write out team names pointsLog := mh.State.PointsLog() - exportIDs := map[string]string{mh.teamID: "self"} - maxSolved := map[string]int{} + exportIDs := make(map[string]string) + maxSolved := make(map[string]int) export.PointsLog = make(award.List, len(pointsLog)) + + if registered { + export.TeamNames["self"] = teamName + exportIDs[mh.teamID] = "self" + } for logno, awd := range pointsLog { if id, ok := exportIDs[awd.TeamID]; ok { awd.TeamID = id diff --git a/cmd/mothd/server_test.go b/cmd/mothd/server_test.go index f6695ac..4b0df96 100644 --- a/cmd/mothd/server_test.go +++ b/cmd/mothd/server_test.go @@ -64,24 +64,26 @@ func TestServer(t *testing.T) { t.Error("index.html wrong contents", contents) } - es := handler.ExportState() - if es.Config.Devel { - t.Error("Marked as development server", es.Config) - } - if len(es.Puzzles) != 1 { - t.Error("Puzzle categories wrong length") - } - if es.Messages != "messages.html" { - t.Error("Messages has wrong contents") - } - if len(es.PointsLog) != 0 { - t.Error("Points log not empty") - } - if len(es.TeamNames) != 1 { - t.Error("Wrong number of team names") - } - if es.TeamNames["self"] != teamName { - t.Error("TeamNames['self'] wrong") + { + es := handler.ExportState() + if es.Config.Devel { + t.Error("Marked as development server", es.Config) + } + if len(es.Puzzles) != 1 { + t.Error("Puzzle categories wrong length") + } + if es.Messages != "messages.html" { + t.Error("Messages has wrong contents") + } + if len(es.PointsLog) != 0 { + t.Error("Points log not empty") + } + if len(es.TeamNames) != 1 { + t.Error("Wrong number of team names") + } + if es.TeamNames["self"] != teamName { + t.Error("TeamNames['self'] wrong") + } } if r, _, err := handler.PuzzlesOpen("pategory", 1, "moo.txt"); err != nil { @@ -112,14 +114,16 @@ func TestServer(t *testing.T) { time.Sleep(TestMaintenanceInterval) - es = handler.ExportState() - if len(es.PointsLog) != 1 { - t.Error("I didn't get my points!") - } - if len(es.Puzzles["pategory"]) != 2 { - t.Error("The next puzzle didn't unlock!") - } else if es.Puzzles["pategory"][1] != 2 { - t.Error("The 2-point puzzle should have unlocked!") + { + es := handler.ExportState() + if len(es.PointsLog) != 1 { + t.Error("I didn't get my points!") + } + if len(es.Puzzles["pategory"]) != 2 { + t.Error("The next puzzle didn't unlock!") + } else if es.Puzzles["pategory"][1] != 2 { + t.Error("The 2-point puzzle should have unlocked!") + } } if r, _, err := handler.PuzzlesOpen("pategory", 2, "puzzle.json"); err != nil { @@ -138,20 +142,25 @@ func TestServer(t *testing.T) { } time.Sleep(TestMaintenanceInterval) - es = anonHandler.ExportState() - if len(es.TeamNames) != 2 { - t.Error("Anonymous TeamNames is wrong:", es.TeamNames) - } - if len(es.PointsLog) != 2 { - t.Error("Points log wrong length") - } - if es.PointsLog[1].TeamID != "0" { - t.Error("Second point log didn't anonymize team ID correctly:", es.PointsLog[1]) + + { + es := anonHandler.ExportState() + if len(es.TeamNames) != 1 { + t.Error("Anonymous TeamNames is wrong:", es.TeamNames) + } + if len(es.PointsLog) != 2 { + t.Error("Points log wrong length") + } + if es.PointsLog[1].TeamID != "0" { + t.Error("Second point log didn't anonymize team ID correctly:", es.PointsLog[1]) + } } - es = handler.ExportState() - if len(es.TeamNames) != 1 { - t.Error("TeamNames is wrong:", es.TeamNames) + { + es := handler.ExportState() + if len(es.TeamNames) != 1 { + t.Error("TeamNames is wrong:", es.TeamNames) + } } // BUG(neale): We aren't currently testing the various ways to disable the server