Files
cheesy-arena-lite/tba_test.go

125 lines
3.5 KiB
Go
Raw Normal View History

2014-07-31 19:35:13 -07:00
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
package main
import (
2014-08-25 22:06:58 -07:00
"bytes"
2017-07-19 22:18:02 -07:00
"encoding/json"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
2014-07-31 19:35:13 -07:00
"github.com/stretchr/testify/assert"
2017-07-19 22:18:02 -07:00
"io/ioutil"
2014-07-31 19:35:13 -07:00
"net/http"
"net/http/httptest"
"testing"
2016-08-14 18:59:24 -07:00
"time"
2014-07-31 19:35:13 -07:00
)
func TestPublishTeams(t *testing.T) {
setupTest(t)
2014-07-31 19:35:13 -07:00
eventSettings.TbaEventCode = "my_event_code"
eventSettings.TbaSecretId = "my_secret_id"
eventSettings.TbaSecret = "my_secret"
db.CreateTeam(&model.Team{Id: 254})
db.CreateTeam(&model.Team{Id: 1114})
2014-07-31 19:35:13 -07:00
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.String(), "event/my_event_code")
2014-08-25 22:06:58 -07:00
var reader bytes.Buffer
reader.ReadFrom(r.Body)
assert.Equal(t, "[\"frc254\",\"frc1114\"]", reader.String())
assert.Equal(t, "my_secret_id", r.Header["X-Tba-Auth-Id"][0])
assert.Equal(t, "f5c022fde6d1186ea0719fe28ab6cc63", r.Header["X-Tba-Auth-Sig"][0])
2014-07-31 19:35:13 -07:00
}))
defer tbaServer.Close()
tbaBaseUrl = tbaServer.URL
assert.Nil(t, PublishTeams())
}
func TestPublishMatches(t *testing.T) {
setupTest(t)
match1 := model.Match{Type: "qualification", DisplayName: "2", Time: time.Unix(600, 0), Red1: 7, Red2: 8, Red3: 9,
2016-08-14 18:59:24 -07:00
Blue1: 10, Blue2: 11, Blue3: 12, Status: "complete"}
match2 := model.Match{Type: "elimination", DisplayName: "SF2-2", ElimRound: 2, ElimGroup: 2, ElimInstance: 2}
2016-08-14 18:59:24 -07:00
db.CreateMatch(&match1)
db.CreateMatch(&match2)
matchResult1 := model.BuildTestMatchResult(match1.Id, 1)
db.CreateMatchResult(matchResult1)
2014-07-31 19:35:13 -07:00
2016-08-14 18:59:24 -07:00
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-07-19 22:18:02 -07:00
body, _ := ioutil.ReadAll(r.Body)
var matches []*TbaMatch
json.Unmarshal(body, &matches)
assert.Equal(t, 2, len(matches))
assert.Equal(t, "qm", matches[0].CompLevel)
assert.Equal(t, "sf", matches[1].CompLevel)
2016-08-14 18:59:24 -07:00
}))
defer tbaServer.Close()
tbaBaseUrl = tbaServer.URL
2014-07-31 19:35:13 -07:00
2016-08-14 18:59:24 -07:00
assert.Nil(t, PublishMatches())
2014-07-31 19:35:13 -07:00
}
func TestPublishRankings(t *testing.T) {
setupTest(t)
db.CreateRanking(game.TestRanking2())
db.CreateRanking(game.TestRanking1())
2014-07-31 19:35:13 -07:00
2016-08-14 18:59:24 -07:00
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-07-19 22:18:02 -07:00
body, _ := ioutil.ReadAll(r.Body)
var response TbaRankings
json.Unmarshal(body, &response)
assert.Equal(t, 2, len(response.Rankings))
assert.Equal(t, "frc254", response.Rankings[0].TeamKey)
assert.Equal(t, "frc1114", response.Rankings[1].TeamKey)
2016-08-14 18:59:24 -07:00
}))
defer tbaServer.Close()
tbaBaseUrl = tbaServer.URL
2014-07-31 19:35:13 -07:00
2016-08-14 18:59:24 -07:00
assert.Nil(t, PublishRankings())
2014-07-31 19:35:13 -07:00
}
func TestPublishAlliances(t *testing.T) {
setupTest(t)
model.BuildTestAlliances(db)
2014-07-31 19:35:13 -07:00
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2014-08-25 22:06:58 -07:00
var reader bytes.Buffer
reader.ReadFrom(r.Body)
2014-07-31 19:35:13 -07:00
assert.Equal(t, "[[\"frc254\",\"frc469\",\"frc2848\",\"frc74\"],[\"frc1718\",\"frc2451\"]]",
2014-08-25 22:06:58 -07:00
reader.String())
2014-07-31 19:35:13 -07:00
}))
defer tbaServer.Close()
tbaBaseUrl = tbaServer.URL
assert.Nil(t, PublishAlliances())
}
func TestPublishingErrors(t *testing.T) {
setupTest(t)
model.BuildTestAlliances(db)
2014-07-31 19:35:13 -07:00
// Mock the TBA server.
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "oh noes", 500)
}))
defer tbaServer.Close()
tbaBaseUrl = tbaServer.URL
assert.NotNil(t, PublishTeams())
assert.NotNil(t, PublishMatches())
assert.NotNil(t, PublishRankings())
assert.NotNil(t, PublishAlliances())
}