webtest: without assert
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-06-28 13:59:27 +02:00
parent 48b828b029
commit d38a2a28c3
7 changed files with 32 additions and 27 deletions

View File

@ -11,13 +11,14 @@ import (
func TestAPIStatus(t *testing.T) { func TestAPIStatus(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
obj := Status{} obj := Status{}
// GET // 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.NoError(err)
assert.Equal(VERSION, obj.Version) assert.Equal(VERSION, obj.Version)
assert.Equal(EXTRAS, obj.Extras) assert.Equal(EXTRAS, obj.Extras)

View File

@ -12,7 +12,8 @@ import (
func TestAPILogin(t *testing.T) { func TestAPILogin(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB) SetupMigration(s.DB)
@ -20,7 +21,7 @@ func TestAPILogin(t *testing.T) {
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // 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.NoError(err)
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)

View File

@ -13,7 +13,8 @@ import (
func TestAPIPasswordCode(t *testing.T) { func TestAPIPasswordCode(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB) SetupMigration(s.DB)
@ -27,7 +28,7 @@ func TestAPIPasswordCode(t *testing.T) {
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // 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.NoError(err)
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)

View File

@ -12,7 +12,8 @@ import (
func TestAPIPassword(t *testing.T) { func TestAPIPassword(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB) SetupMigration(s.DB)
@ -23,7 +24,7 @@ func TestAPIPassword(t *testing.T) {
hErr := web.HTTPError{} hErr := web.HTTPError{}
// no auth // 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.NoError(err)
assert.Equal(APIErrorNoSession, hErr.Message) assert.Equal(APIErrorNoSession, hErr.Message)

View File

@ -12,7 +12,8 @@ import (
func TestAPIStatus(t *testing.T) { func TestAPIStatus(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB) SetupMigration(s.DB)
@ -20,7 +21,7 @@ func TestAPIStatus(t *testing.T) {
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // 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.NoError(err)
assert.Equal(APIErrorNoSession, hErr.Message) assert.Equal(APIErrorNoSession, hErr.Message)

View File

@ -11,12 +11,13 @@ import (
func TestMetricsLoaded(t *testing.T) { func TestMetricsLoaded(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s := webtest.New(assert) s, err := webtest.New()
assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
// GET // GET
err := s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil) err = s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil)
assert.NoError(err) assert.NoError(err)
UP = func() bool { return false } UP = func() bool { return false }

View File

@ -10,7 +10,6 @@ import (
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"dev.sum7.eu/genofire/golang-lib/database" "dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/golang-lib/mailer" "dev.sum7.eu/genofire/golang-lib/mailer"
@ -28,7 +27,6 @@ type testServer struct {
Close func() Close func()
gin *gin.Engine gin *gin.Engine
ws *web.Service ws *web.Service
assert *assert.Assertions
lastCookies []*http.Cookie lastCookies []*http.Cookie
} }
@ -39,7 +37,7 @@ type Login struct {
} }
// New starts WebService for testing // New starts WebService for testing
func New(assert *assert.Assertions) *testServer { func New() (*testServer, error) {
// db setup // db setup
dbConfig := database.Database{ dbConfig := database.Database{
Connection: DBConnection, Connection: DBConnection,
@ -49,10 +47,11 @@ func New(assert *assert.Assertions) *testServer {
} }
err := dbConfig.Run() err := dbConfig.Run()
if err != nil && err != database.ErrNothingToMigrate { if err != nil && err != database.ErrNothingToMigrate {
fmt.Println(err.Error()) return nil, err
assert.Nil(err) }
if dbConfig.DB == nil {
return nil, database.ErrNotConnected
} }
assert.NotNil(dbConfig.DB)
// api setup // api setup
gin.EnableJsonDecoderDisallowUnknownFields() gin.EnableJsonDecoderDisallowUnknownFields()
@ -61,7 +60,9 @@ func New(assert *assert.Assertions) *testServer {
mock, mail := mailer.NewFakeServer() mock, mail := mailer.NewFakeServer()
err = mail.Setup() err = mail.Setup()
assert.Nil(err) if err != nil {
return nil, err
}
ws := &web.Service{ ws := &web.Service{
DB: dbConfig.DB, DB: dbConfig.DB,
@ -79,8 +80,7 @@ func New(assert *assert.Assertions) *testServer {
Close: mock.Close, Close: mock.Close,
gin: r, gin: r,
ws: ws, ws: ws,
assert: assert, }, nil
}
} }
// DatabaseForget, to run a test without a database // 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) s.gin.ServeHTTP(w, req)
// valid statusCode // valid statusCode
s.assert.Equal(expectCode, w.Code, "expected http status code")
if expectCode != w.Code { if expectCode != w.Code {
return fmt.Errorf("wrong status code, body: %v", w.Body) return fmt.Errorf("wrong status code, body: %v", w.Body)
} }