From d38a2a28c3dfb0bc510346534c9a591943270231 Mon Sep 17 00:00:00 2001 From: Geno Date: Mon, 28 Jun 2021 13:59:27 +0200 Subject: [PATCH] webtest: without assert --- web/api/status/main_test.go | 5 +++-- web/auth/api_login_test.go | 5 +++-- web/auth/api_password_code_test.go | 5 +++-- web/auth/api_password_test.go | 5 +++-- web/auth/api_status_test.go | 5 +++-- web/metrics/main_test.go | 5 +++-- web/webtest/main.go | 29 ++++++++++++++--------------- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/web/api/status/main_test.go b/web/api/status/main_test.go index 7ad06dc..f233a1b 100644 --- a/web/api/status/main_test.go +++ b/web/api/status/main_test.go @@ -11,13 +11,14 @@ import ( func TestAPIStatus(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) obj := Status{} // GET - err := s.Request(http.MethodGet, "/api/status", nil, http.StatusOK, &obj) + err = s.Request(http.MethodGet, "/api/status", nil, http.StatusOK, &obj) assert.NoError(err) assert.Equal(VERSION, obj.Version) assert.Equal(EXTRAS, obj.Extras) diff --git a/web/auth/api_login_test.go b/web/auth/api_login_test.go index 929dae7..6053c0e 100644 --- a/web/auth/api_login_test.go +++ b/web/auth/api_login_test.go @@ -12,7 +12,8 @@ import ( func TestAPILogin(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) SetupMigration(s.DB) @@ -20,7 +21,7 @@ func TestAPILogin(t *testing.T) { hErr := web.HTTPError{} // invalid - err := s.Request(http.MethodPost, "/api/v1/auth/login", 1, http.StatusBadRequest, &hErr) + err = s.Request(http.MethodPost, "/api/v1/auth/login", 1, http.StatusBadRequest, &hErr) assert.NoError(err) assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) diff --git a/web/auth/api_password_code_test.go b/web/auth/api_password_code_test.go index 39dfdae..220e27d 100644 --- a/web/auth/api_password_code_test.go +++ b/web/auth/api_password_code_test.go @@ -13,7 +13,8 @@ import ( func TestAPIPasswordCode(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) SetupMigration(s.DB) @@ -27,7 +28,7 @@ func TestAPIPasswordCode(t *testing.T) { hErr := web.HTTPError{} // invalid - err := s.Request(http.MethodPost, "/api/v1/auth/password/code", &passwordNew, http.StatusBadRequest, &hErr) + err = s.Request(http.MethodPost, "/api/v1/auth/password/code", &passwordNew, http.StatusBadRequest, &hErr) assert.NoError(err) assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) diff --git a/web/auth/api_password_test.go b/web/auth/api_password_test.go index db05107..6d24697 100644 --- a/web/auth/api_password_test.go +++ b/web/auth/api_password_test.go @@ -12,7 +12,8 @@ import ( func TestAPIPassword(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) SetupMigration(s.DB) @@ -23,7 +24,7 @@ func TestAPIPassword(t *testing.T) { hErr := web.HTTPError{} // no auth - err := s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusUnauthorized, &hErr) + err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusUnauthorized, &hErr) assert.NoError(err) assert.Equal(APIErrorNoSession, hErr.Message) diff --git a/web/auth/api_status_test.go b/web/auth/api_status_test.go index 55d49fb..5a89d08 100644 --- a/web/auth/api_status_test.go +++ b/web/auth/api_status_test.go @@ -12,7 +12,8 @@ import ( func TestAPIStatus(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) SetupMigration(s.DB) @@ -20,7 +21,7 @@ func TestAPIStatus(t *testing.T) { hErr := web.HTTPError{} // invalid - err := s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusUnauthorized, &hErr) + err = s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusUnauthorized, &hErr) assert.NoError(err) assert.Equal(APIErrorNoSession, hErr.Message) diff --git a/web/metrics/main_test.go b/web/metrics/main_test.go index 4cbe49a..147d17d 100644 --- a/web/metrics/main_test.go +++ b/web/metrics/main_test.go @@ -11,12 +11,13 @@ import ( func TestMetricsLoaded(t *testing.T) { assert := assert.New(t) - s := webtest.New(assert) + s, err := webtest.New() + assert.NoError(err) defer s.Close() assert.NotNil(s) // GET - err := s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil) + err = s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil) assert.NoError(err) UP = func() bool { return false } diff --git a/web/webtest/main.go b/web/webtest/main.go index 18c23a9..5d79e22 100644 --- a/web/webtest/main.go +++ b/web/webtest/main.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/gin-gonic/gin" - "github.com/stretchr/testify/assert" "dev.sum7.eu/genofire/golang-lib/database" "dev.sum7.eu/genofire/golang-lib/mailer" @@ -28,7 +27,6 @@ type testServer struct { Close func() gin *gin.Engine ws *web.Service - assert *assert.Assertions lastCookies []*http.Cookie } @@ -39,7 +37,7 @@ type Login struct { } // New starts WebService for testing -func New(assert *assert.Assertions) *testServer { +func New() (*testServer, error) { // db setup dbConfig := database.Database{ Connection: DBConnection, @@ -49,10 +47,11 @@ func New(assert *assert.Assertions) *testServer { } err := dbConfig.Run() if err != nil && err != database.ErrNothingToMigrate { - fmt.Println(err.Error()) - assert.Nil(err) + return nil, err + } + if dbConfig.DB == nil { + return nil, database.ErrNotConnected } - assert.NotNil(dbConfig.DB) // api setup gin.EnableJsonDecoderDisallowUnknownFields() @@ -61,7 +60,9 @@ func New(assert *assert.Assertions) *testServer { mock, mail := mailer.NewFakeServer() err = mail.Setup() - assert.Nil(err) + if err != nil { + return nil, err + } ws := &web.Service{ DB: dbConfig.DB, @@ -74,13 +75,12 @@ func New(assert *assert.Assertions) *testServer { ws.LoadSession(r) ws.Bind(r) return &testServer{ - DB: &dbConfig, - Mails: mock.Mails, - Close: mock.Close, - gin: r, - ws: ws, - assert: assert, - } + DB: &dbConfig, + Mails: mock.Mails, + Close: mock.Close, + gin: r, + ws: ws, + }, nil } // DatabaseForget, to run a test without a database @@ -116,7 +116,6 @@ func (s *testServer) Request(method, url string, body interface{}, expectCode in s.gin.ServeHTTP(w, req) // valid statusCode - s.assert.Equal(expectCode, w.Code, "expected http status code") if expectCode != w.Code { return fmt.Errorf("wrong status code, body: %v", w.Body) }