From 5dccc6431dec00661113be72ef210d7d9b43629a Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 1 Dec 2019 20:47:46 -0700 Subject: [PATCH] More tests, and a bug fix! --- cmd/mothd/state.go | 19 ++++++++++++++++++- cmd/mothd/state_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cmd/mothd/state.go b/cmd/mothd/state.go index 852028c..551a89d 100644 --- a/cmd/mothd/state.go +++ b/cmd/mothd/state.go @@ -115,6 +115,23 @@ func (s *State) TeamName(teamId string) (string, error) { // Write out team name. This can only be done once. func (s *State) SetTeamName(teamId string, teamName string) error { + if f, err := s.fs.Open("teamids.txt"); err != nil { + return fmt.Errorf("Team IDs file does not exist") + } else { + ok := false + scanner := bufio.NewScanner(f) + for scanner.Scan() { + if scanner.Text() == teamId { + ok = true + break + } + } + f.Close() + if !ok { + return fmt.Errorf("Team ID not found in list of valid Team IDs") + } + } + teamFile := filepath.Join("teams", teamId) err := afero.WriteFile(s.fs, teamFile, []byte(teamName), os.ModeExclusive|0644) return err @@ -309,7 +326,7 @@ func (s *State) maybeInitialize() { // Preseed available team ids if file doesn't exist if f, err := s.fs.OpenFile("teamids.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil { defer f.Close() - for i := 0; i <= 100; i += 1 { + for i := 0; i < 100; i += 1 { fmt.Fprintln(f, mktoken()) } } diff --git a/cmd/mothd/state_test.go b/cmd/mothd/state_test.go index 2d8c6f0..90defdc 100644 --- a/cmd/mothd/state_test.go +++ b/cmd/mothd/state_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "github.com/spf13/afero" "os" "testing" @@ -19,7 +20,31 @@ func TestState(t *testing.T) { s := NewState(fs) s.Cleanup() + pl := s.PointsLog() + if len(pl) != 0 { + t.Errorf("Empty points log is not empty") + } + mustExist("initialized") mustExist("enabled") mustExist("hours") + + teamidsBuf, err := afero.ReadFile(fs, "teamids.txt") + if err != nil { + t.Errorf("Reading teamids.txt: %v", err) + } + + teamids := bytes.Split(teamidsBuf, []byte("\n")) + if (len(teamids) != 101) || (len(teamids[100]) > 0) { + t.Errorf("There weren't 100 teamids, there were %d", len(teamids)) + } + myTeam := string(teamids[0]) + + if err := s.SetTeamName("bad team ID", "bad team name"); err == nil { + t.Errorf("Setting bad team ID didn't raise an error") + } + + if err := s.SetTeamName(myTeam, "My Team"); err != nil { + t.Errorf("Setting team name: %v", err) + } }