webtest: improve error
continuous-integration/drone the build failed
Details
continuous-integration/drone the build failed
Details
This commit is contained in:
parent
979f5fef7d
commit
48b828b029
|
@ -17,7 +17,8 @@ func TestAPIStatus(t *testing.T) {
|
||||||
|
|
||||||
obj := Status{}
|
obj := Status{}
|
||||||
// GET
|
// GET
|
||||||
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(VERSION, obj.Version)
|
||||||
assert.Equal(EXTRAS, obj.Extras)
|
assert.Equal(EXTRAS, obj.Extras)
|
||||||
assert.True(obj.Up)
|
assert.True(obj.Up)
|
||||||
|
@ -26,7 +27,8 @@ func TestAPIStatus(t *testing.T) {
|
||||||
|
|
||||||
obj = Status{}
|
obj = Status{}
|
||||||
// GET - failed status
|
// GET - failed status
|
||||||
s.Request(http.MethodGet, "/api/status", nil, http.StatusInternalServerError, &obj)
|
err = s.Request(http.MethodGet, "/api/status", nil, http.StatusInternalServerError, &obj)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal(VERSION, obj.Version)
|
assert.Equal(VERSION, obj.Version)
|
||||||
assert.Equal(EXTRAS, obj.Extras)
|
assert.Equal(EXTRAS, obj.Extras)
|
||||||
assert.False(obj.Up)
|
assert.False(obj.Up)
|
||||||
|
|
|
@ -20,24 +20,28 @@ func TestAPILogin(t *testing.T) {
|
||||||
|
|
||||||
hErr := web.HTTPError{}
|
hErr := web.HTTPError{}
|
||||||
// invalid
|
// invalid
|
||||||
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)
|
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)
|
||||||
|
|
||||||
req := login{}
|
req := login{}
|
||||||
hErr = web.HTTPError{}
|
hErr = web.HTTPError{}
|
||||||
// invalid - user
|
// invalid - user
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr)
|
err = s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal(APIErrorUserNotFound, hErr.Message)
|
assert.Equal(APIErrorUserNotFound, hErr.Message)
|
||||||
|
|
||||||
req.Username = "admin"
|
req.Username = "admin"
|
||||||
hErr = web.HTTPError{}
|
hErr = web.HTTPError{}
|
||||||
// invalid - password
|
// invalid - password
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr)
|
err = s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal(APIErrorIncorrectPassword, hErr.Message)
|
assert.Equal(APIErrorIncorrectPassword, hErr.Message)
|
||||||
|
|
||||||
req.Password = "CHANGEME"
|
req.Password = "CHANGEME"
|
||||||
obj := User{}
|
obj := User{}
|
||||||
// valid login
|
// valid login
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusOK, &obj)
|
err = s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusOK, &obj)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal("admin", obj.Username)
|
assert.Equal("admin", obj.Username)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,23 +27,26 @@ func TestAPIPasswordCode(t *testing.T) {
|
||||||
|
|
||||||
hErr := web.HTTPError{}
|
hErr := web.HTTPError{}
|
||||||
// invalid
|
// invalid
|
||||||
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)
|
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)
|
||||||
|
|
||||||
res := ""
|
res := ""
|
||||||
// set new password
|
// set new password
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
err = s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
||||||
ForgetCode: forgetCode,
|
ForgetCode: forgetCode,
|
||||||
Password: passwordNew,
|
Password: passwordNew,
|
||||||
}, http.StatusOK, &res)
|
}, http.StatusOK, &res)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal("admin", res)
|
assert.Equal("admin", res)
|
||||||
|
|
||||||
hErr = web.HTTPError{}
|
hErr = web.HTTPError{}
|
||||||
// set password without code
|
// set password without code
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
err = s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
||||||
ForgetCode: forgetCode,
|
ForgetCode: forgetCode,
|
||||||
Password: passwordCurrent,
|
Password: passwordCurrent,
|
||||||
}, http.StatusBadRequest, &hErr)
|
}, http.StatusBadRequest, &hErr)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal(APIErrorUserNotFound, hErr.Message)
|
assert.Equal(APIErrorUserNotFound, hErr.Message)
|
||||||
|
|
||||||
forgetCode = uuid.New()
|
forgetCode = uuid.New()
|
||||||
|
@ -51,9 +54,10 @@ func TestAPIPasswordCode(t *testing.T) {
|
||||||
|
|
||||||
res = ""
|
res = ""
|
||||||
// set old password
|
// set old password
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
err = s.Request(http.MethodPost, "/api/v1/auth/password/code", &PasswordWithForgetCode{
|
||||||
ForgetCode: forgetCode,
|
ForgetCode: forgetCode,
|
||||||
Password: passwordCurrent,
|
Password: passwordCurrent,
|
||||||
}, http.StatusOK, &res)
|
}, http.StatusOK, &res)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal("admin", res)
|
assert.Equal("admin", res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,23 +23,28 @@ func TestAPIPassword(t *testing.T) {
|
||||||
|
|
||||||
hErr := web.HTTPError{}
|
hErr := web.HTTPError{}
|
||||||
// no auth
|
// no auth
|
||||||
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)
|
assert.Equal(APIErrorNoSession, hErr.Message)
|
||||||
|
|
||||||
s.TestLogin()
|
err = s.TestLogin()
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
hErr = web.HTTPError{}
|
hErr = web.HTTPError{}
|
||||||
// invalid
|
// invalid
|
||||||
s.Request(http.MethodPost, "/api/v1/my/auth/password", nil, http.StatusBadRequest, &hErr)
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", nil, http.StatusBadRequest, &hErr)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)
|
assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message)
|
||||||
|
|
||||||
res := false
|
res := false
|
||||||
// set new password
|
// set new password
|
||||||
s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusOK, &res)
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusOK, &res)
|
||||||
|
assert.NoError(err)
|
||||||
assert.True(res)
|
assert.True(res)
|
||||||
|
|
||||||
res = false
|
res = false
|
||||||
// set old password
|
// set old password
|
||||||
s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordCurrent, http.StatusOK, &res)
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordCurrent, http.StatusOK, &res)
|
||||||
|
assert.NoError(err)
|
||||||
assert.True(res)
|
assert.True(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,17 @@ func TestAPIStatus(t *testing.T) {
|
||||||
|
|
||||||
hErr := web.HTTPError{}
|
hErr := web.HTTPError{}
|
||||||
// invalid
|
// invalid
|
||||||
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)
|
assert.Equal(APIErrorNoSession, hErr.Message)
|
||||||
|
|
||||||
s.TestLogin()
|
err = s.TestLogin()
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
obj := User{}
|
obj := User{}
|
||||||
// invalid - user
|
// invalid - user
|
||||||
s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusOK, &obj)
|
err = s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusOK, &obj)
|
||||||
|
assert.NoError(err)
|
||||||
assert.Equal("admin", obj.Username)
|
assert.Equal("admin", obj.Username)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,12 @@ func TestMetricsLoaded(t *testing.T) {
|
||||||
assert.NotNil(s)
|
assert.NotNil(s)
|
||||||
|
|
||||||
// GET
|
// GET
|
||||||
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 }
|
UP = func() bool { return false }
|
||||||
|
|
||||||
// GET
|
// GET
|
||||||
s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil)
|
err = s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil)
|
||||||
|
assert.NoError(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,19 +90,23 @@ func (s *testServer) DatabaseForget() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request sends a request to webtest WebService
|
// Request sends a request to webtest WebService
|
||||||
func (s *testServer) Request(method, url string, body interface{}, expectCode int, jsonObj interface{}) {
|
func (s *testServer) Request(method, url string, body interface{}, expectCode int, jsonObj interface{}) error {
|
||||||
var jsonBody io.Reader
|
var jsonBody io.Reader
|
||||||
if body != nil {
|
if body != nil {
|
||||||
if strBody, ok := body.(string); ok {
|
if strBody, ok := body.(string); ok {
|
||||||
jsonBody = strings.NewReader(strBody)
|
jsonBody = strings.NewReader(strBody)
|
||||||
} else {
|
} else {
|
||||||
jsonBodyArray, err := json.Marshal(body)
|
jsonBodyArray, err := json.Marshal(body)
|
||||||
s.assert.Nil(err, "no request created")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(method, url, jsonBody)
|
req, err := http.NewRequest(method, url, jsonBody)
|
||||||
s.assert.Nil(err, "no request created")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if len(s.lastCookies) > 0 {
|
if len(s.lastCookies) > 0 {
|
||||||
for _, c := range s.lastCookies {
|
for _, c := range s.lastCookies {
|
||||||
req.AddCookie(c)
|
req.AddCookie(c)
|
||||||
|
@ -114,14 +118,15 @@ func (s *testServer) Request(method, url string, body interface{}, expectCode in
|
||||||
// valid statusCode
|
// valid statusCode
|
||||||
s.assert.Equal(expectCode, w.Code, "expected http status code")
|
s.assert.Equal(expectCode, w.Code, "expected http status code")
|
||||||
if expectCode != w.Code {
|
if expectCode != w.Code {
|
||||||
fmt.Printf("wrong status code, body:%v\n", w.Body)
|
return fmt.Errorf("wrong status code, body: %v", w.Body)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonObj != nil {
|
if jsonObj != nil {
|
||||||
// fetch JSON
|
// fetch JSON
|
||||||
err = json.NewDecoder(w.Body).Decode(jsonObj)
|
err = json.NewDecoder(w.Body).Decode(jsonObj)
|
||||||
s.assert.Nil(err, "decode json")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result := w.Result()
|
result := w.Result()
|
||||||
|
@ -131,17 +136,18 @@ func (s *testServer) Request(method, url string, body interface{}, expectCode in
|
||||||
s.lastCookies = cookies
|
s.lastCookies = cookies
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login to API by send request
|
// Login to API by send request
|
||||||
func (s *testServer) Login(login Login) {
|
func (s *testServer) Login(login Login) error {
|
||||||
// POST: correct login
|
// POST: correct login
|
||||||
s.Request(http.MethodPost, "/api/v1/auth/login", &login, http.StatusOK, nil)
|
return s.Request(http.MethodPost, "/api/v1/auth/login", &login, http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestLogin to API by default login data
|
// TestLogin to API by default login data
|
||||||
func (s *testServer) TestLogin() {
|
func (s *testServer) TestLogin() error {
|
||||||
s.Login(Login{
|
return s.Login(Login{
|
||||||
Username: "admin",
|
Username: "admin",
|
||||||
Password: "CHANGEME",
|
Password: "CHANGEME",
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue