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) {
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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 }

View File

@ -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,
@ -79,8 +80,7 @@ func New(assert *assert.Assertions) *testServer {
Close: mock.Close,
gin: r,
ws: ws,
assert: assert,
}
}, 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)
}