func TestCreateUser(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { if isUser, err := tm.UserExists("NameFoo"); err != nil { t.ErrorNow(err) } else if isUser { t.FailNow() } if users, err := tm.ListUsers(); err != nil { t.ErrorNow(err) } else if len(users) != 0 { t.FailNow() } if _, err := tm.CreateUser("NameFoo", "PublicKey", CategoryTest); err != nil { t.ErrorNow(err) } if isUser, err := tm.UserExists("NameFoo"); err != nil { t.ErrorNow(err) } else if !isUser { t.FailNow() } else if users, err := tm.ListUsers(); err != nil { t.ErrorNow(err) } else if len(users) != 1 { t.FailNow() } else if users[0] != "NameFoo" { t.FailNow() } }) }
func TestUnescaptedParsingFails(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { if r := sendPostExpectStatus(t, server, http.StatusInternalServerError, "/register", strings.NewReader("name=NameFoo&category="+string(tournament.CategoryTest)+"&public_key="+UnescapedSamplePublicKey)); Json(t, r).Key("error").Key("message").String() != "Invalid Public Key" { t.ErrorNow("expected 'Invalid Public Key'") } }) }
func TestSubmitPlayerNameError(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { if r := sendJSONPostExpectStatus(t, server, http.StatusInternalServerError, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": SampleCommitHash}); Json(t, r).Key("error").Key("message").String() != "Unknown player" { t.ErrorNow(r, "expected 'Unknown player'") } }) }
func TestCategories(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { if r := sendGet(t, server, "/categories"); !compareStringsUnordered(Json(t, r).Key("data").Key("categories").Array(), []string{string(tournament.CategoryBattlecode2014), string(tournament.CategoryBattlecode2015), string(tournament.CategoryBattlecode2016)}) { t.ErrorNow("expected 3 categories", r) } }) }
func TestInitGitoliteRepo(t *testing.T) { GitoliteHostTest(t, func(t *testutil.T, host *GitoliteHost) { if privateKey, publicKey, err := testutil.CreateKeyPair(); err != nil { t.ErrorNow(err) } else if file, err := ioutil.TempFile(os.TempDir(), "battlecode_private_key"); err != nil { t.ErrorNow(err) } else if _, err := file.WriteString(privateKey); err != nil { t.ErrorNow(err) } else { file.Close() defer os.Remove(file.Name()) if err := host.InitRepository("foo", map[string]int64{"foo": 1}, map[int64]string{1: publicKey}); err != nil { t.ErrorNow(err) } defer host.DeleteRepository("foo") repoURL := host.RepositoryURL("foo") if repo, err := (TempRemote{}).CheckoutRepositoryWithKeyFile(repoURL, file.Name()); err != nil { t.ErrorNow(err) } else { defer repo.Delete() CheckDirectoryContent(t, repo.Dir(), []string{".git"}) } } }) }
func TestCreateExistingMapError(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { t.CheckError(tm.CreateMap("NameFoo", "SourceFoo", CategoryTest)) if err := tm.CreateMap("NameFoo", "SourceFoo", CategoryTest); err == nil { t.ErrorNow("expected error") } }) }
func TestSubmitCommitHashError(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)}) if r := sendJSONPostExpectStatus(t, server, http.StatusInternalServerError, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": "InvalidCommitHash"}); Json(t, r).Key("error").Key("message").String() != "Invalid commit hash" { t.ErrorNow(r, "expected 'Unknown player'") } }) }
func TestRunLatestMatches(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { runLatestMatches(t, tm) if matches, err := tm.ListMatches(CategoryTest); err != nil { t.ErrorNow(err) } else if len(matches) != 18 { t.ErrorNow("Expected 1 got", len(matches)) } }) }
func TestCreateExistingKey(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil { t.ErrorNow(err) } if _, err := tm.CreateUser("NameBar", "PublicKeyFoo", CategoryTest); err != nil { t.ErrorNow(err) } }) }
func TestCreateDuplicateUserError(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil { t.ErrorNow(err) } if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err == nil { t.ErrorNow("expected error") } }) }
func TestDeleteLocalRepo(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, local *LocalDirHost) { t.CheckError(local.InitRepository("foo", nil, nil)) t.CheckError(local.DeleteRepository("foo")) repoURL := local.RepositoryURL("foo") if _, err := os.Stat(repoURL); err == nil { t.FailNow() } else if !os.IsNotExist(err) { t.ErrorNow(err) } }) }
func TestCheckoutRepository(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, host *LocalDirHost) { t.CheckError(host.InitRepository("foo", nil, nil)) repoURL := host.RepositoryURL("foo") if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil { t.ErrorNow(err) } else { defer repo.Delete() CheckDirectoryContent(t, repo.Dir(), []string{".git"}) } }) }
func TestInitLocalRepo(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, local *LocalDirHost) { t.CheckError(local.InitRepository("foo", nil, nil)) repoURL := local.RepositoryURL("foo") if stat, err := os.Stat(repoURL); err != nil { t.ErrorNow(err) } else if !stat.IsDir() { t.ErrorNowf("%s is not a directory", repoURL) } else { CheckDirectoryContent(t, repoURL, []string{"HEAD", "branches", "config", "description", "hooks", "info", "objects", "refs"}) } }) }
func TestCreateMatch(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { p1 := Submission{"p1", "c1"} p2 := Submission{"p2", "c2"} if id, err := tm.CreateMatch(CategoryTest, "MapFoo", p1, p2, time.Now()); err != nil { t.FailNow() } else if result, err := tm.GetMatchResult(id); err != nil { t.ErrorNow(err) } else if result != MatchResultInProgress { t.FailNow() } }) }
func TestPush(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, host *LocalDirHost) { t.CheckError(host.InitRepository("foo", nil, nil)) repoURL := host.RepositoryURL("foo") var head string if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil { t.ErrorNow(err) } else { defer repo.Delete() t.CheckError(ioutil.WriteFile(filepath.Join(repo.Dir(), "foo.txt"), []byte("hello"), os.ModePerm)) t.CheckError(repo.AddFiles([]string{"foo.txt"})) t.CheckError(repo.CommitFiles([]string{"foo.txt"}, "commit message")) t.CheckError(repo.Push()) if h, err := repo.Head(); err != nil { t.ErrorNow(err) } else { head = h } } if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil { t.ErrorNow(err) } else { defer repo.Delete() if head2, err := repo.Head(); err != nil { t.ErrorNow(err) } else if head != head2 { t.ErrorNowf("Expected <%v> != Actual <%v>", head, head2) } } }) }
func TestMaps(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); Json(t, r).Key("data").Key("maps").Len() > 0 { t.Error("expected no maps", r) t.FailNow() } sendJSONPost(t, server, "/map/create", map[string]string{"name": "NameFoo", "source": "SourceFoo", "category": string(tournament.CategoryTest)}) if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); !compareStrings(Json(t, r).Key("data").Key("maps").Array(), []string{"NameFoo"}) { t.Error("expected single player NameFoo", r) t.FailNow() } sendJSONPost(t, server, "/map/create", map[string]string{"name": "NameBar", "source": "SourceBar", "category": string(tournament.CategoryTest)}) if r := sendGet(t, server, "/maps?category="+string(tournament.CategoryTest)); !compareStringsUnordered(Json(t, r).Key("data").Key("maps").Array(), []string{"NameFoo", "NameBar"}) { t.ErrorNow("expected two maps NameFoo, NameBar", r) } }) }
func TestCreateMap(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { t.CheckError(tm.CreateMap("MapFoo", "MapString", CategoryTest)) if maps, err := tm.ListMaps(CategoryTest); err != nil { t.ErrorNow(err) } else if len(maps) != 1 { t.ErrorNow(len(maps), "but expected", 1) } else if maps[0] != "MapFoo" { t.ErrorNow(maps[0], "but expected", "MapFoo") } if mapSource, err := tm.GetMapSource("MapFoo", CategoryTest); err != nil { t.ErrorNow(err) } else if mapSource != "MapString" { t.ErrorNow(mapSource, "but expected", "MapString") } }) }
func TestCommitFiles(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, host *LocalDirHost) { t.CheckError(host.InitRepository("foo", nil, nil)) repoURL := host.RepositoryURL("foo") if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil { t.ErrorNow(err) } else { defer repo.Delete() t.CheckError(ioutil.WriteFile(filepath.Join(repo.Dir(), "foo.txt"), []byte("hello"), os.ModePerm)) t.CheckError(repo.AddFiles([]string{"foo.txt"})) t.CheckError(repo.CommitFiles([]string{"foo.txt"}, "commit message")) if log, err := repo.Log(); err != nil { t.ErrorNow(err) } else if len(log) != 1 { t.FailNow() } } }) }
func TestUpdateMatch(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { p1 := Submission{"p1", "c1"} p2 := Submission{"p2", "c2"} if id, err := tm.CreateMatch(CategoryTest, "MapFoo", p1, p2, time.Now()); err != nil { t.ErrorNow(err) } else { t.CheckError(tm.UpdateMatch(CategoryTest, "MapFoo", p1, p2, time.Now(), MatchResultWinA, []byte("LogFoo"))) if result, err := tm.GetMatchResult(id); err != nil { t.ErrorNow(t, err) } else if result != MatchResultWinA { t.ErrorNow(result, " expected ", MatchResultWinA) } else if replay, err := tm.GetMatchReplayRaw(id); err != nil { t.ErrorNow(err) } else if string(replay) != "LogFoo" { t.ErrorNow(replay, " expected LogFoo") } } }) }
func TestSubmitCommit(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { if _, err := tm.CreateUser("NameFoo", "PublicKeyFoo", CategoryTest); err != nil { t.ErrorNow(err) } t.CheckError(tm.SubmitCommit("NameFoo", CategoryTest, "abcdef", time.Now())) if commits, err := tm.ListCommits("NameFoo", CategoryTest); err != nil { t.ErrorNow(err) } else if len(commits) != 1 { t.ErrorNow(len(commits), "but expected", 1) } else if commits[0] != "abcdef" { t.ErrorNow(commits[0], "but expected", "abcdef") } }) }
func TestSubmit(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)}) if r := sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": string(tournament.CategoryTest), "commit_hash": SampleCommitHash}); Json(t, r).Key("data").Key("name").String() != "NameFoo" { t.ErrorNow(r["name"], " expected ", "NameFoo") } else if Json(t, r).Key("data").Key("category").String() != string(tournament.CategoryTest) { t.ErrorNow(r["category"], " expected ", string(tournament.CategoryTest)) } else if Json(t, r).Key("data").Key("commit_hash").String() != SampleCommitHash { t.ErrorNow(r["commit_hash"], " expected ", SampleCommitHash) } }) }
func TestPlayers(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { if r := sendGet(t, server, "/players"); Json(t, r).Key("data").Key("players").Len() > 0 { t.ErrorNow("expected no players", r) } sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)}) if r := sendGet(t, server, "/players"); !compareStrings(Json(t, r).Key("data").Key("players").Array(), []string{"NameFoo"}) { t.ErrorNow("expected single player NameFoo", r) } sendJSONPost(t, server, "/register", map[string]string{"name": "NameBar", "public_key": SamplePublicKey2, "category": string(tournament.CategoryTest)}) if r := sendGet(t, server, "/players"); !compareStringsUnordered(Json(t, r).Key("data").Key("players").Array(), []string{"NameFoo", "NameBar"}) { t.ErrorNow("expected two players NameFoo, NameBar", r) } }) }
func TestDeleteFiles(t *testing.T) { LocalDirHostTest(t, func(t *testutil.T, host *LocalDirHost) { t.CheckError(host.InitRepository("foo", nil, nil)) repoURL := host.RepositoryURL("foo") if repo, err := (TempRemote{}).CheckoutRepository(repoURL); err != nil { t.ErrorNow(err) } else { defer repo.Delete() t.CheckError(ioutil.WriteFile(filepath.Join(repo.Dir(), "foo.txt"), []byte("hello"), os.ModePerm)) t.CheckError(os.Mkdir(filepath.Join(repo.Dir(), "bar"), os.ModeDir|0755)) t.CheckError(ioutil.WriteFile(filepath.Join(repo.Dir(), "bar", "moo.txt"), []byte("world"), os.ModePerm)) t.CheckError(repo.AddFiles([]string{"foo.txt", "bar"})) t.CheckError(repo.CommitFiles(nil, "commit message")) if log, err := repo.Log(); err != nil { t.ErrorNow(err) } else if len(log) != 1 { t.FailNow() } CheckDirectoryContent(t, repo.Dir(), []string{".git", "foo.txt", "bar"}) CheckDirectoryContent(t, filepath.Join(repo.Dir(), "bar"), []string{"moo.txt"}) //Delete t.CheckError(repo.DeleteFiles([]string{"foo.txt"})) t.CheckError(repo.DeleteFiles([]string{"bar"})) if err := repo.DeleteFiles([]string{"other"}); err == nil { t.ErrorNow("Expected error") } CheckDirectoryContent(t, repo.Dir(), []string{".git"}) t.CheckError(repo.CommitFiles([]string{}, "commit message")) if log, err := repo.Log(); err != nil { t.ErrorNow(err) } else if len(log) != 2 { t.FailNow() } } }) }
func TestCommits(t *testing.T) { ServerTest(t, func(t *testutil.T, server *ServerState) { sendJSONPost(t, server, "/register", map[string]string{"name": "NameFoo", "public_key": SamplePublicKey, "category": string(tournament.CategoryTest)}) if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); Json(t, r).Key("data").Key("commits").Len() > 0 { t.ErrorNow("expected no commits", r) } sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": "General", "commit_hash": "abcdef"}) if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); !compareStringsUnordered(Json(t, r).Key("data").Key("commits").Array(), []string{"abcdef"}) { t.ErrorNow("expected single commit abcdef", r) } sendJSONPost(t, server, "/submit", map[string]string{"name": "NameFoo", "category": "General", "commit_hash": "012345"}) if r := sendGet(t, server, "/commits?name=NameFoo&category=General"); !compareStringsUnordered(Json(t, r).Key("data").Key("commits").Array(), []string{"abcdef", "012345"}) { t.ErrorNow("expected two commits abcdef, 012345", r) } }) }
func TestRunMatch(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { p1 := Submission{"p1", "c1"} p2 := Submission{"p2", "c2"} t.CheckError(tm.CreateMap("MapFoo", "SourceFoo", CategoryTest)) if id, result, err := tm.RunMatch(CategoryTest, "MapFoo", p1, p2, SystemClock()); err != nil { t.ErrorNow(err) } else if result != "WinA" { t.ErrorNowf("Expected WinA not %v\n", result) } else if result2, err := tm.GetMatchResult(id); err != nil { t.ErrorNow(err) } else if result != result2 { t.ErrorNowf("Expected %v not %v\n", result, result2) } else if matches, err := tm.ListMatches(CategoryTest); err != nil { t.ErrorNow(err) } else if len(matches) != 1 { t.ErrorNowf("Expected 1 match not %v\n", len(matches)) } else if matches[0].Result != result { t.ErrorNowf("Expected %v not %v\n", result, matches[0].Result) } }) }
func TestCalculateLeaderboard(t *testing.T) { TournamentTest(t, func(t *testutil.T, tm *Tournament) { runLatestMatches(t, tm) rank := LeaderboardStats{12, 6, 0, 6} if err := tm.CalculateLeaderboard(CategoryTest); err != nil { t.ErrorNow(err) } else if ranks, matches, err := tm.GetLeaderboard(CategoryTest); err != nil { t.ErrorNow(err) } else if len(ranks) != 3 { t.ErrorNow("Expected 3 ranks", len(ranks)) } else if v, _ := ranks["Name1"]; v != rank { t.ErrorNow("Unexpected rank", ranks["Name1"]) } else if ranks["Name2"] != rank { t.ErrorNow("Unexpected rank", ranks["Name2"]) } else if ranks["Name3"] != rank { t.ErrorNow("Unexpected rank", ranks["Name3"]) } else if len(matches) != 18 { t.ErrorNow("Expected 18 matches", len(matches)) } }) }