Files
cheesy-arena-lite/web/setup_settings_test.go

125 lines
4.7 KiB
Go
Raw Normal View History

2014-06-06 21:26:55 -07:00
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
package web
2014-06-06 21:26:55 -07:00
import (
2014-06-07 02:02:12 -07:00
"bytes"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/Team254/cheesy-arena/tournament"
2014-06-06 21:26:55 -07:00
"github.com/stretchr/testify/assert"
2014-06-07 02:02:12 -07:00
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
2014-06-06 21:26:55 -07:00
"testing"
)
func TestSetupSettings(t *testing.T) {
web := setupTestWeb(t)
2014-06-06 21:26:55 -07:00
// Check the default setting values.
recorder := web.getHttpResponse("/setup/settings")
2014-06-06 21:26:55 -07:00
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Untitled Event")
assert.Contains(t, recorder.Body.String(), "#00ff00")
assert.Contains(t, recorder.Body.String(), "8")
2014-07-30 00:16:09 -07:00
assert.NotContains(t, recorder.Body.String(), "tbaPublishingEnabled\" checked")
2014-06-06 21:26:55 -07:00
// Change the settings and check the response.
recorder = web.postHttpResponse("/setup/settings", "name=Chezy Champs&code=CC&displayBackgroundColor=#ff00ff&"+
"numElimAlliances=16&tbaPublishingEnabled=on&tbaEventCode=2014cc&tbaSecretId=secretId&tbaSecret=tbasec")
2014-06-07 02:02:12 -07:00
assert.Equal(t, 302, recorder.Code)
recorder = web.getHttpResponse("/setup/settings")
2014-06-06 21:26:55 -07:00
assert.Contains(t, recorder.Body.String(), "Chezy Champs")
assert.Contains(t, recorder.Body.String(), "#ff00ff")
assert.Contains(t, recorder.Body.String(), "16")
2014-07-30 00:16:09 -07:00
assert.Contains(t, recorder.Body.String(), "tbaPublishingEnabled\" checked")
assert.Contains(t, recorder.Body.String(), "2014cc")
assert.Contains(t, recorder.Body.String(), "secretId")
assert.Contains(t, recorder.Body.String(), "tbasec")
2014-06-06 21:26:55 -07:00
}
func TestSetupSettingsInvalidValues(t *testing.T) {
web := setupTestWeb(t)
2014-06-06 21:26:55 -07:00
// Invalid color value.
recorder := web.postHttpResponse("/setup/settings", "numAlliances=8&displayBackgroundColor=blorpy")
2014-06-06 21:26:55 -07:00
assert.Contains(t, recorder.Body.String(), "must be a valid hex color value")
// Invalid number of alliances.
recorder = web.postHttpResponse("/setup/settings", "numAlliances=1&displayBackgroundColor=#000")
2014-06-06 21:26:55 -07:00
assert.Contains(t, recorder.Body.String(), "must be between 2 and 16")
}
2014-06-07 02:02:12 -07:00
func TestSetupSettingsClearDb(t *testing.T) {
web := setupTestWeb(t)
web.arena.Database.CreateTeam(new(model.Team))
web.arena.Database.CreateMatch(&model.Match{Type: "qualification"})
web.arena.Database.CreateMatchResult(new(model.MatchResult))
web.arena.Database.CreateRanking(new(game.Ranking))
web.arena.Database.CreateAllianceTeam(new(model.AllianceTeam))
recorder := web.postHttpResponse("/setup/db/clear", "")
2014-06-07 02:02:12 -07:00
assert.Equal(t, 302, recorder.Code)
teams, _ := web.arena.Database.GetAllTeams()
2014-06-07 02:02:12 -07:00
assert.NotEmpty(t, teams)
matches, _ := web.arena.Database.GetMatchesByType("qualification")
2014-06-07 02:02:12 -07:00
assert.Empty(t, matches)
rankings, _ := web.arena.Database.GetAllRankings()
2014-06-07 02:02:12 -07:00
assert.Empty(t, rankings)
tournament.CalculateRankings(web.arena.Database)
2014-06-07 02:02:12 -07:00
assert.Empty(t, rankings)
alliances, _ := web.arena.Database.GetAllAlliances()
2014-06-07 02:02:12 -07:00
assert.Empty(t, alliances)
}
func TestSetupSettingsBackupRestoreDb(t *testing.T) {
web := setupTestWeb(t)
2014-06-07 02:02:12 -07:00
// Modify a parameter so that we know when the database has been restored.
web.arena.EventSettings.Name = "Chezy Champs"
web.arena.Database.SaveEventSettings(web.arena.EventSettings)
2014-06-07 02:02:12 -07:00
// Back up the database.
recorder := web.getHttpResponse("/setup/db/save")
2014-06-07 02:02:12 -07:00
assert.Equal(t, 200, recorder.Code)
assert.Equal(t, "application/octet-stream", recorder.HeaderMap["Content-Type"][0])
backupBody := recorder.Body
// Wipe the database to reset the defaults.
web = setupTestWeb(t)
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
2014-06-07 02:02:12 -07:00
// Check restoring with a missing file.
recorder = web.postHttpResponse("/setup/db/restore", "")
2014-06-07 02:02:12 -07:00
assert.Contains(t, recorder.Body.String(), "No database backup file was specified")
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
2014-06-07 02:02:12 -07:00
// Check restoring with a corrupt file.
recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile",
bytes.NewBufferString("invalid"))
2014-06-07 02:02:12 -07:00
assert.Contains(t, recorder.Body.String(), "Could not read uploaded database backup file")
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
2014-06-07 02:02:12 -07:00
// Check restoring with the backup retrieved before.
recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile", backupBody)
assert.Equal(t, "Chezy Champs", web.arena.EventSettings.Name)
2014-06-07 02:02:12 -07:00
}
func (web *Web) postFileHttpResponse(path string, paramName string, file *bytes.Buffer) *httptest.ResponseRecorder {
2014-06-07 02:02:12 -07:00
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile(paramName, "file.ext")
io.Copy(part, file)
writer.Close()
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", path, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
web.newHandler().ServeHTTP(recorder, req)
2014-06-07 02:02:12 -07:00
return recorder
}