mirror of https://github.com/dirtbags/moth.git
parent
e5a3b26c93
commit
5b6555cd9a
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIssue156(t *testing.T) {
|
||||||
|
puzzles := NewTestMothballs()
|
||||||
|
state := NewTestState()
|
||||||
|
theme := NewTestTheme()
|
||||||
|
server := NewMothServer(Configuration{}, theme, state, puzzles)
|
||||||
|
|
||||||
|
afero.WriteFile(state, "teams/bloop", []byte("bloop: the team"), 0644)
|
||||||
|
state.refresh()
|
||||||
|
|
||||||
|
handler := server.NewHandler("", "bloop")
|
||||||
|
es := handler.ExportState()
|
||||||
|
if _, ok := es.TeamNames["self"]; !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
err := handler.Register("bloop: the other team")
|
||||||
|
if err != ErrAlreadyRegistered {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
|
@ -184,12 +184,15 @@ func (mh *MothRequestHandler) ExportState() *StateExport {
|
||||||
return mh.exportStateIfRegistered(false)
|
return mh.exportStateIfRegistered(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mh *MothRequestHandler) exportStateIfRegistered(override bool) *StateExport {
|
// Export state, replacing the team ID with "self" if the team is registered.
|
||||||
|
//
|
||||||
|
// If forceRegistered is true, go ahead and export it anyway
|
||||||
|
func (mh *MothRequestHandler) exportStateIfRegistered(forceRegistered bool) *StateExport {
|
||||||
export := StateExport{}
|
export := StateExport{}
|
||||||
export.Config = mh.Config
|
export.Config = mh.Config
|
||||||
|
|
||||||
teamName, err := mh.State.TeamName(mh.teamID)
|
teamName, err := mh.State.TeamName(mh.teamID)
|
||||||
registered := override || mh.Config.Devel || (err == nil)
|
registered := forceRegistered || mh.Config.Devel || (err == nil)
|
||||||
|
|
||||||
export.Messages = mh.State.Messages()
|
export.Messages = mh.State.Messages()
|
||||||
export.TeamNames = make(map[string]string)
|
export.TeamNames = make(map[string]string)
|
||||||
|
|
|
@ -141,6 +141,13 @@ func (s *State) TeamName(teamID string) (string, error) {
|
||||||
// SetTeamName writes out team name.
|
// SetTeamName writes out team name.
|
||||||
// This can only be done once per team.
|
// This can only be done once per team.
|
||||||
func (s *State) SetTeamName(teamID, teamName string) error {
|
func (s *State) SetTeamName(teamID, teamName string) error {
|
||||||
|
s.lock.RLock()
|
||||||
|
_, ok := s.teamNames[teamID]
|
||||||
|
s.lock.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return ErrAlreadyRegistered
|
||||||
|
}
|
||||||
|
|
||||||
idsFile, err := s.Open("teamids.txt")
|
idsFile, err := s.Open("teamids.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("team IDs file does not exist")
|
return fmt.Errorf("team IDs file does not exist")
|
||||||
|
|
Loading…
Reference in New Issue