Add TOML based config file

The config file may provide default values or user sepcific settings.
This commit is contained in:
2022-01-13 08:43:05 +01:00
parent d20eb306fd
commit fdc344d6d6
7 changed files with 157 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"image/png"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
@ -19,7 +20,10 @@ var v1 []byte
var v2 []byte
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_gh.png
var gh []byte
var gh1 []byte
//go:embed testdata/a1d0c6e83f027327d8461063f4ac58a6_gh.png
var gh2 []byte
func testRouter() *mux.Router {
router := mux.NewRouter()
@ -78,7 +82,7 @@ func TestCorrectResponseForGHColorSchemeAndPattern(t *testing.T) {
rr := httptest.NewRecorder()
testRouter().ServeHTTP(rr, req)
if !reflect.DeepEqual(rr.Body.Bytes(), gh) {
if !reflect.DeepEqual(rr.Body.Bytes(), gh1) {
t.Errorf("returned image does not match expected image")
}
}
@ -108,6 +112,49 @@ func TestStringMatchesHash(t *testing.T) {
}
}
func TestUsesConfig(t *testing.T) {
configure("./testdata/testconfig.toml")
if config.Defaults.ColorScheme != "gh" ||
config.Users[0].Id != "example" ||
config.Users[0].Alias != "42" ||
config.Users[0].ColorScheme != "gh" ||
config.Users[0].Pattern != "github" {
t.Errorf("Config not applied as expected")
}
}
func TestUsesConfigWithEnvVar(t *testing.T) {
os.Setenv("COLORSCHEME", "v1")
os.Setenv("PATTERN", "default")
configure("./testdata/testconfig.toml")
if config.Defaults.ColorScheme != "v1" ||
config.Users[0].Id != "example" ||
config.Users[0].Alias != "42" ||
config.Users[0].ColorScheme != "gh" ||
config.Users[0].Pattern != "github" {
t.Errorf("Config not applied as expected")
}
}
func TestCorrectResponseForUserConfig(t *testing.T) {
configure("./testdata/testconfig.toml")
req, err := http.NewRequest("GET", "/avatar/example", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
testRouter().ServeHTTP(rr, req)
if !reflect.DeepEqual(rr.Body.Bytes(), gh2) {
t.Errorf("returned image does not match expected image for mapped alias '42'")
}
}
func TestHSLtoRGB(t *testing.T) {
red := hslToRgba(0, 100, 50)
if red.R != 255 || red.G != 0 || red.B != 0 {