Updating tests to work with participant ID

This commit is contained in:
John Donaldson 2022-01-28 15:26:27 -08:00
parent 8363ca589a
commit a163561431
3 changed files with 91 additions and 9 deletions

View File

@ -69,6 +69,26 @@ func TestHttpd(t *testing.T) {
} 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())
}
// Test participant assignment
if r := hs.TestRequest("/assign", map[string]string{"pid": "badParticipantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"unable to associate participant and team","description":"participant ID not found in list of valid participant IDs"}}` {
t.Error("Assignment failed", r.Body.String())
}
if r := hs.TestRequest("/assign", map[string]string{"id": "bad team id", "pid": "participantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"unable to associate participant and team","description":"Provided team does not exist, or is not registered"}}` {
t.Error("Assignment failed", r.Body.String())
}
if r := hs.TestRequest("/assign", map[string]string{"id": "teamID", "pid": "participantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"success","data":{"short":"assigned","description":"participant and team have been associated"}}` {
t.Error("Assignment failed", r.Body.String())
}
time.Sleep(TestMaintenanceInterval)
@ -100,13 +120,19 @@ func TestHttpd(t *testing.T) {
t.Error("Unexpected body", r.Body.String())
}
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "moo"}); r.Result().StatusCode != 200 {
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123", "pid": "bad participant ID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"not accepted","description":"invalid participant ID"}}` {
t.Error("Unexpected body", r.Body.String())
}
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "moo", "pid": "participantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"not accepted","description":"incorrect answer"}}` {
t.Error("Unexpected body", r.Body.String())
}
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123"}); r.Result().StatusCode != 200 {
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123", "pid": "participantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"success","data":{"short":"accepted","description":"1 points awarded in pategory"}}` {
t.Error("Unexpected body", r.Body.String())
@ -129,7 +155,7 @@ func TestHttpd(t *testing.T) {
t.Error("Didn't unlock next puzzle")
}
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123"}); r.Result().StatusCode != 200 {
if r := hs.TestRequest("/answer", map[string]string{"cat": "pategory", "points": "1", "answer": "answer123", "pid": "participantID"}); r.Result().StatusCode != 200 {
t.Error(r.Result())
} else if r.Body.String() != `{"status":"fail","data":{"short":"not accepted","description":"error awarding points: points already awarded to this team in this category"}}` {
t.Error("Unexpected body", r.Body.String())

View File

@ -17,6 +17,7 @@ func NewTestServer() *MothServer {
state := NewTestState()
afero.WriteFile(state, "teamids.txt", []byte("teamID\n"), 0644)
afero.WriteFile(state, "participantids.txt", []byte("participantID\n"), 0644)
afero.WriteFile(state, "messages.html", []byte("messages.html"), 0644)
go state.Maintain(TestMaintenanceInterval)
@ -72,6 +73,16 @@ func TestProdServer(t *testing.T) {
t.Error("Wrong error for duplicate registration:", err)
}
if err := handler.AssignParticipant(); err != nil {
t.Error(err)
}
if err := handler.AssignParticipant(); err == nil {
t.Error("Assigning a participant twice should have raised an error")
} else if err != ErrAlreadyRegistered {
t.Error("Wrong error for duplicate registration:", err)
}
if r, _, err := handler.ThemeOpen("/index.html"); err != nil {
t.Error(err)
} else if contents, err := ioutil.ReadAll(r); err != nil {
@ -128,7 +139,7 @@ func TestProdServer(t *testing.T) {
}
if err := anonHandler.CheckAnswer("pategory", 1, "answer123"); err == nil {
t.Error("Invalid team ID was able to get points with correct answer")
t.Error("Invalid participant ID was able to get points with correct answer")
}
if err := handler.CheckAnswer("pategory", 1, "answer123"); err != nil {
t.Error("Right answer marked wrong", err)

View File

@ -35,6 +35,7 @@ func TestState(t *testing.T) {
mustExist("initialized")
mustExist("enabled")
mustExist("hours.txt")
mustExist("participantids.txt")
teamIDsBuf, err := afero.ReadFile(s.Fs, "teamids.txt")
if err != nil {
@ -69,9 +70,53 @@ func TestState(t *testing.T) {
t.Error("Incorrect team name:", name)
}
// Participant ID tests
participantIDsBuf, err := afero.ReadFile(s.Fs, "participantids.txt")
if err != nil {
t.Errorf("Reading participantids.txt: %v", err)
}
participantIDs := bytes.Split(participantIDsBuf, []byte("\n"))
if (len(participantIDs) != 101) || (len(participantIDs[100]) > 0) {
t.Errorf("There weren't 100 participantIDs, there were %d", len(participantIDs))
}
participantID := string(participantIDs[0])
if _, err := s.ParticipantTeam(participantID); err == nil {
t.Errorf("Bad participant ID lookup didn't return error")
}
if err := s.AssignParticipant("bad participant ID", "bad team ID"); err == nil {
t.Errorf("Assigning bad participant ID didn't raise an error")
}
if err := s.AssignParticipant(participantID, "bad team ID"); err == nil {
t.Errorf("Assigning participant to bad team ID didn't raise an error")
}
if err := s.AssignParticipant("bad participant ID", teamID); err == nil {
t.Errorf("Assigning bad participant ID to valid team ID didn't raise an error")
}
if err:= s.AssignParticipant(participantID, teamID); err != nil {
t.Errorf("Error assigning participant to team: %s -> %s", participantID, teamID)
}
if err:= s.AssignParticipant(participantID, teamID); err == nil {
t.Errorf("Assigning participant to team a second time should fail")
}
s.refresh()
if id, err := s.ParticipantTeam(participantID); err != nil {
t.Error(err)
} else if id != teamID {
t.Error("Incorrect team ID:", id)
}
// Award tests
category := "poot"
points := 3928
if err := s.AwardPoints(teamID, category, points); err != nil {
if err := s.AwardPoints(participantID, category, points); err != nil {
t.Error(err)
}
// Flex duplicate detection with different timestamp
@ -82,7 +127,7 @@ func TestState(t *testing.T) {
f.Close()
}
s.AwardPoints(teamID, category, points)
s.AwardPoints(participantID, category, points)
s.refresh()
pl = s.PointsLog()
if len(pl) != 1 {
@ -94,11 +139,11 @@ func TestState(t *testing.T) {
t.Errorf("Incorrect logged award %v", pl)
}
if err := s.AwardPoints(teamID, category, points); err == nil {
if err := s.AwardPoints(participantID, category, points); err == nil {
t.Error("Duplicate points award after refresh didn't fail")
}
if err := s.AwardPoints(teamID, category, points+1); err != nil {
if err := s.AwardPoints(participantID, category, points+1); err != nil {
t.Error("Awarding more points:", err)
}
@ -112,7 +157,7 @@ func TestState(t *testing.T) {
if len(s.PointsLog()) != 0 {
t.Errorf("Intentional parse error breaks pointslog")
}
if err := s.AwardPoints(teamID, category, points); err != nil {
if err := s.AwardPoints(participantID, category, points); err != nil {
t.Error(err)
}
s.refresh()