2020-08-19 18:01:21 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TestMaintenanceInterval = time.Millisecond * 1
|
|
|
|
const TestTeamID = "teamID"
|
|
|
|
|
|
|
|
func NewTestServer() *MothServer {
|
|
|
|
puzzles := NewTestMothballs()
|
|
|
|
go puzzles.Maintain(TestMaintenanceInterval)
|
|
|
|
|
|
|
|
state := NewTestState()
|
|
|
|
afero.WriteFile(state, "teamids.txt", []byte("teamID\n"), 0644)
|
|
|
|
afero.WriteFile(state, "messages.html", []byte("messages.html"), 0644)
|
|
|
|
go state.Maintain(TestMaintenanceInterval)
|
|
|
|
|
|
|
|
theme := NewTestTheme()
|
2020-08-21 17:02:38 -06:00
|
|
|
afero.WriteFile(theme.Fs, "/index.html", []byte("index.html"), 0644)
|
2020-08-19 18:01:21 -06:00
|
|
|
go theme.Maintain(TestMaintenanceInterval)
|
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
return NewMothServer(Configuration{Devel: true}, theme, state, puzzles)
|
2020-08-19 18:01:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer(t *testing.T) {
|
|
|
|
teamName := "OurTeam"
|
|
|
|
participantID := "participantID"
|
|
|
|
teamID := TestTeamID
|
|
|
|
|
|
|
|
server := NewTestServer()
|
|
|
|
handler := server.NewHandler(participantID, teamID)
|
|
|
|
if err := handler.Register(teamName); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-08-21 17:02:38 -06:00
|
|
|
if r, _, err := handler.ThemeOpen("/index.html"); err != nil {
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error(err)
|
|
|
|
} else if contents, err := ioutil.ReadAll(r); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if string(contents) != "index.html" {
|
|
|
|
t.Error("index.html wrong contents", contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
es := handler.ExportState()
|
|
|
|
if es.Config.Devel {
|
|
|
|
t.Error("Marked as development server")
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
t.Error(err)
|
|
|
|
} else if contents, err := ioutil.ReadAll(r); err != nil {
|
2020-08-21 17:02:38 -06:00
|
|
|
r.Close()
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error(err)
|
|
|
|
} else if string(contents) != "moo" {
|
2020-08-21 17:02:38 -06:00
|
|
|
r.Close()
|
2020-08-19 18:01:21 -06:00
|
|
|
t.Error("moo.txt has wrong contents", contents)
|
2020-08-21 17:02:38 -06:00
|
|
|
} else {
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, _, err := handler.PuzzlesOpen("pategory", 2, "puzzles.json"); err == nil {
|
|
|
|
t.Error("Opening locked puzzle shouldn't work")
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, _, err := handler.PuzzlesOpen("pategory", 20, "puzzles.json"); err == nil {
|
|
|
|
t.Error("Opening non-existent puzzle shouldn't work")
|
|
|
|
r.Close()
|
2020-08-19 18:01:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := handler.CheckAnswer("pategory", 1, "answer123"); err != nil {
|
|
|
|
t.Error("Right answer marked wrong", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(TestMaintenanceInterval)
|
|
|
|
|
|
|
|
es = handler.ExportState()
|
|
|
|
if len(es.PointsLog) != 1 {
|
|
|
|
t.Error("I didn't get my points!")
|
|
|
|
}
|
2020-08-21 17:02:38 -06:00
|
|
|
|
|
|
|
// BUG(neale): We aren't currently testing the various ways to disable the server
|
2020-08-19 18:01:21 -06:00
|
|
|
}
|