More tests, and a bug fix!

This commit is contained in:
Neale Pickett 2019-12-01 20:47:46 -07:00
parent b044314864
commit 5dccc6431d
2 changed files with 43 additions and 1 deletions

View File

@ -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())
}
}

View File

@ -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)
}
}