From c4dd39aae2581961bbb719aafb26b092bcf798e4 Mon Sep 17 00:00:00 2001 From: "Schmitt, Max" Date: Tue, 7 Nov 2017 13:14:56 +0100 Subject: [PATCH] Fixed unit tests 1/2 --- handlers/handlers.go | 29 ++++++++++++++++------------- handlers/handlers_test.go | 4 ++-- store/store_test.go | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 02456da..d21ac6d 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -19,10 +19,11 @@ import ( // Handler holds the funcs and attributes for the // http communication type Handler struct { - config config.Handlers - store store.Store - engine *gin.Engine - oAuthConf *oauth2.Config + config config.Handlers + store store.Store + engine *gin.Engine + oAuthConf *oauth2.Config + DoNotCheckConfigViaGet bool // DoNotCheckConfigViaGet is for the unit testing usage } // New initializes the http handlers @@ -56,15 +57,17 @@ func (h *Handler) setTemplateFromFS(name string) error { } func (h *Handler) checkIfSecretExist() error { - conf := config.Get() - if conf.Handlers.Secret == nil { - b := make([]byte, 128) - if _, err := rand.Read(b); err != nil { - return err - } - conf.Handlers.Secret = b - if err := config.Set(conf); err != nil { - return err + if h.DoNotCheckConfigViaGet { + conf := config.Get() + if conf.Handlers.Secret == nil { + b := make([]byte, 128) + if _, err := rand.Read(b); err != nil { + return err + } + conf.Handlers.Secret = b + if err := config.Set(conf); err != nil { + return err + } } } return nil diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index ea86238..c83510c 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -244,13 +244,13 @@ func getBackend() (func(), error) { } handler, err := New(config.Handlers{ ListenAddr: ":8080", - Secret: []byte(""), + Secret: []byte("our really great secret"), BaseURL: "http://127.0.0.1", }, *store) if err != nil { return nil, errors.Wrap(err, "could not create handler") } - + handler.DoNotCheckConfigViaGet = true server = httptest.NewServer(handler.engine) return func() { server.Close() diff --git a/store/store_test.go b/store/store_test.go index e81743d..f645254 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -20,7 +20,7 @@ var validConfig = config.Store{ func TestGenerateRandomString(t *testing.T) { tt := []struct { name string - length int + length uint }{ {"fourtytwo long", 42}, {"sixteen long", 16},