Fix bug with registration on actual filesystems

This commit is contained in:
Neale Pickett 2020-10-13 19:48:37 -06:00
parent cc3f5d0e39
commit 1080783eb9
7 changed files with 31 additions and 6 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [v4.0.0-rc2] - Unreleased
### Fixed
- Multiple bugs preventing production server from working properly
- CI builds should be working now
- Team registration now correctly writes names to files
## [v4.0-rc1] - 2020-10-13
### Changed

View File

@ -110,6 +110,12 @@ func (h *HTTPServer) StateHandler(mh MothRequestHandler, w http.ResponseWriter,
// RegisterHandler handles attempts to register a team
func (h *HTTPServer) RegisterHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
teamName := req.FormValue("name")
teamName = strings.TrimSpace(teamName)
if teamName == "" {
jsend.Sendf(w, jsend.Fail, "empty name", "Team name may not be empty")
return
}
if err := mh.Register(teamName); err == ErrAlreadyRegistered {
jsend.Sendf(w, jsend.Success, "already registered", "Team ID has already been registered")
} else if err != nil {

View File

@ -66,7 +66,7 @@ func TestHttpd(t *testing.T) {
t.Error("Register failed")
}
thatt if r := hs.TestRequest("/register", map[string]string{"name": "GoTeam"}); r.Result().StatusCode != 200 {
if r := hs.TestRequest("/register", map[string]string{"name": "GoTeam"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"success","data":{"short":"already registered","description":"Team ID has already been registered"}}` {
t.Error("Register failed", r.Body.String())

View File

@ -151,14 +151,16 @@ func (s *State) SetTeamName(teamID, teamName string) error {
}
teamFilename := filepath.Join("teams", teamID)
teamFile, err := s.Fs.OpenFile(teamFilename, os.O_CREATE|os.O_EXCL, 0644)
teamFile, err := s.Fs.OpenFile(teamFilename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
if os.IsExist(err) {
return ErrAlreadyRegistered
} else if err != nil {
return err
}
defer teamFile.Close()
log.Println("Setting team name to:", teamName, teamFilename, teamFile)
fmt.Fprintln(teamFile, teamName)
teamFile.Close()
return nil
}

View File

@ -55,12 +55,18 @@ func TestState(t *testing.T) {
t.Errorf("Setting bad team ID didn't raise an error")
}
if err := s.SetTeamName(teamID, "My Team"); err != nil {
t.Errorf("Setting team name: %v", err)
teamName := "My Team"
if err := s.SetTeamName(teamID, teamName); err != nil {
t.Errorf("Setting team name: %w", err)
}
if err := s.SetTeamName(teamID, "wat"); err == nil {
t.Errorf("Registering team a second time didn't fail")
}
if name, err := s.TeamName(teamID); err != nil {
t.Error(err)
} else if name != teamName {
t.Error("Incorrect team name:", name)
}
category := "poot"
points := 3928

View File

@ -147,9 +147,13 @@ func (t *T) DumpMothball() error {
var w io.Writer
c := transpile.NewFsCategory(t.fs, "")
if t.filename == "" {
removeOnError := false
switch t.filename {
case "", "-":
w = t.Stdout
} else {
default:
removeOnError = true
log.Println("Writing mothball to", t.filename)
outf, err := t.BaseFs.Create(t.filename)
if err != nil {
@ -159,6 +163,9 @@ func (t *T) DumpMothball() error {
w = outf
}
if err := transpile.Mothball(c, w); err != nil {
if removeOnError {
t.BaseFs.Remove(t.filename)
}
return err
}
return nil

View File

@ -13,6 +13,8 @@ function scoreboardInit() {
]
function update(state) {
window.state = state
for (let rotate of document.querySelectorAll(".rotate")) {
rotate.appendChild(rotate.firstElementChild)
}