From 48b828b0294f3b9c0d2128ac3bd29a1af29133cf Mon Sep 17 00:00:00 2001 From: Geno Date: Mon, 28 Jun 2021 12:14:20 +0200 Subject: [PATCH] webtest: improve error --- web/api/status/main_test.go | 6 ++++-- web/auth/api_login_test.go | 12 ++++++++---- web/auth/api_password_code_test.go | 12 ++++++++---- web/auth/api_password_test.go | 15 ++++++++++----- web/auth/api_status_test.go | 9 ++++++--- web/metrics/main_test.go | 6 ++++-- web/webtest/main.go | 26 ++++++++++++++++---------- 7 files changed, 56 insertions(+), 30 deletions(-) diff --git a/web/api/status/main_test.go b/web/api/status/main_test.go index 668c60e..7ad06dc 100644 --- a/web/api/status/main_test.go +++ b/web/api/status/main_test.go @@ -17,7 +17,8 @@ func TestAPIStatus(t *testing.T) { obj := Status{} // 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(EXTRAS, obj.Extras) assert.True(obj.Up) @@ -26,7 +27,8 @@ func TestAPIStatus(t *testing.T) { obj = 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(EXTRAS, obj.Extras) assert.False(obj.Up) diff --git a/web/auth/api_login_test.go b/web/auth/api_login_test.go index 8061e0d..929dae7 100644 --- a/web/auth/api_login_test.go +++ b/web/auth/api_login_test.go @@ -20,24 +20,28 @@ func TestAPILogin(t *testing.T) { hErr := web.HTTPError{} // 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) req := login{} hErr = web.HTTPError{} // 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) req.Username = "admin" hErr = web.HTTPError{} // 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) req.Password = "CHANGEME" obj := User{} // 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) } diff --git a/web/auth/api_password_code_test.go b/web/auth/api_password_code_test.go index dc4215d..39dfdae 100644 --- a/web/auth/api_password_code_test.go +++ b/web/auth/api_password_code_test.go @@ -27,23 +27,26 @@ func TestAPIPasswordCode(t *testing.T) { hErr := web.HTTPError{} // 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) res := "" // 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, Password: passwordNew, }, http.StatusOK, &res) + assert.NoError(err) assert.Equal("admin", res) hErr = web.HTTPError{} // 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, Password: passwordCurrent, }, http.StatusBadRequest, &hErr) + assert.NoError(err) assert.Equal(APIErrorUserNotFound, hErr.Message) forgetCode = uuid.New() @@ -51,9 +54,10 @@ func TestAPIPasswordCode(t *testing.T) { res = "" // 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, Password: passwordCurrent, }, http.StatusOK, &res) + assert.NoError(err) assert.Equal("admin", res) } diff --git a/web/auth/api_password_test.go b/web/auth/api_password_test.go index dd298ae..db05107 100644 --- a/web/auth/api_password_test.go +++ b/web/auth/api_password_test.go @@ -23,23 +23,28 @@ func TestAPIPassword(t *testing.T) { hErr := web.HTTPError{} // 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) - s.TestLogin() + err = s.TestLogin() + assert.NoError(err) hErr = web.HTTPError{} // 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) res := false // 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) res = false // 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) } diff --git a/web/auth/api_status_test.go b/web/auth/api_status_test.go index cc595e3..55d49fb 100644 --- a/web/auth/api_status_test.go +++ b/web/auth/api_status_test.go @@ -20,14 +20,17 @@ func TestAPIStatus(t *testing.T) { hErr := web.HTTPError{} // 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) - s.TestLogin() + err = s.TestLogin() + assert.NoError(err) obj := 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) } diff --git a/web/metrics/main_test.go b/web/metrics/main_test.go index c053927..4cbe49a 100644 --- a/web/metrics/main_test.go +++ b/web/metrics/main_test.go @@ -16,10 +16,12 @@ func TestMetricsLoaded(t *testing.T) { assert.NotNil(s) // 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 } // GET - s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil) + err = s.Request(http.MethodGet, "/metrics", nil, http.StatusOK, nil) + assert.NoError(err) } diff --git a/web/webtest/main.go b/web/webtest/main.go index 1346843..18c23a9 100644 --- a/web/webtest/main.go +++ b/web/webtest/main.go @@ -90,19 +90,23 @@ func (s *testServer) DatabaseForget() { } // 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 if body != nil { if strBody, ok := body.(string); ok { jsonBody = strings.NewReader(strBody) } else { jsonBodyArray, err := json.Marshal(body) - s.assert.Nil(err, "no request created") + if err != nil { + return err + } jsonBody = bytes.NewBuffer(jsonBodyArray) } } req, err := http.NewRequest(method, url, jsonBody) - s.assert.Nil(err, "no request created") + if err != nil { + return err + } if len(s.lastCookies) > 0 { for _, c := range s.lastCookies { req.AddCookie(c) @@ -114,14 +118,15 @@ func (s *testServer) Request(method, url string, body interface{}, expectCode in // valid statusCode s.assert.Equal(expectCode, w.Code, "expected http status code") if expectCode != w.Code { - fmt.Printf("wrong status code, body:%v\n", w.Body) - return + return fmt.Errorf("wrong status code, body: %v", w.Body) } if jsonObj != nil { // fetch JSON err = json.NewDecoder(w.Body).Decode(jsonObj) - s.assert.Nil(err, "decode json") + if err != nil { + return err + } } result := w.Result() @@ -131,17 +136,18 @@ func (s *testServer) Request(method, url string, body interface{}, expectCode in s.lastCookies = cookies } } + return nil } // Login to API by send request -func (s *testServer) Login(login Login) { +func (s *testServer) Login(login Login) error { // 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 -func (s *testServer) TestLogin() { - s.Login(Login{ +func (s *testServer) TestLogin() error { + return s.Login(Login{ Username: "admin", Password: "CHANGEME", })