From f9f51470b1865830c820addb382f6594c790cef0 Mon Sep 17 00:00:00 2001 From: Mirko Teodorovic Date: Tue, 23 Mar 2021 17:43:06 +0100 Subject: [PATCH] NOISSUE - fix response for passwd endpoints (#1393) * fix response for passwd endpoints Signed-off-by: Mirko Teodorovic * fix test for passwd related responses Signed-off-by: Mirko Teodorovic * fix test for passwd related responses Signed-off-by: Mirko Teodorovic --- users/api/endpoint.go | 3 +-- users/api/endpoint_test.go | 22 ++-------------------- users/api/responses.go | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/users/api/endpoint.go b/users/api/endpoint.go index 032d760a..1bf3f390 100644 --- a/users/api/endpoint.go +++ b/users/api/endpoint.go @@ -47,7 +47,7 @@ func passwordResetRequestEndpoint(svc users.Service) endpoint.Endpoint { if err := req.validate(); err != nil { return nil, err } - res := passwChangeRes{} + res := passwResetReqRes{} email := req.Email if err := svc.GenerateResetToken(ctx, email, req.Host); err != nil { return nil, err @@ -71,7 +71,6 @@ func passwordResetEndpoint(svc users.Service) endpoint.Endpoint { if err := svc.ResetPassword(ctx, req.Token, req.Password); err != nil { return nil, err } - res.Msg = "" return res, nil } } diff --git a/users/api/endpoint_test.go b/users/api/endpoint_test.go index 30ccc0d8..777b2189 100644 --- a/users/api/endpoint_test.go +++ b/users/api/endpoint_test.go @@ -293,21 +293,12 @@ func TestPasswordReset(t *testing.T) { ts := newServer(svc) defer ts.Close() client := ts.Client() - resData := struct { - Msg string `json:"msg"` - }{ - "", - } reqData := struct { Token string `json:"token,omitempty"` Password string `json:"password,omitempty"` ConfPass string `json:"confirm_password,omitempty"` }{} - expectedSuccess := toJSON(resData) - - resData.Msg = users.ErrUserNotFound.Error() - _, err := svc.Register(context.Background(), user) require.Nil(t, err, fmt.Sprintf("register user got unexpected error: %s", err)) require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) @@ -343,7 +334,7 @@ func TestPasswordReset(t *testing.T) { res string tok string }{ - {"password reset with valid token", reqExisting, contentType, http.StatusCreated, expectedSuccess, token}, + {"password reset with valid token", reqExisting, contentType, http.StatusCreated, "{}", token}, {"password reset with invalid token", reqNoExist, contentType, http.StatusForbidden, unauthRes, token}, {"password reset with confirm password not matching", reqPassNoMatch, contentType, http.StatusBadRequest, malformedRes, token}, {"password reset request with invalid request format", "{", contentType, http.StatusBadRequest, malformedRes, token}, @@ -382,19 +373,12 @@ func TestPasswordChange(t *testing.T) { tkn, _ := auth.Issue(context.Background(), &mainflux.IssueReq{Id: user.ID, Email: user.Email, Type: 0}) token := tkn.GetValue() - resData := struct { - Msg string `json:"msg"` - }{ - "", - } - expectedSuccess := toJSON(resData) reqData := struct { Token string `json:"token,omitempty"` Password string `json:"password,omitempty"` OldPassw string `json:"old_password,omitempty"` }{} - resData.Msg = users.ErrUnauthorizedAccess.Error() _, err := svc.Register(context.Background(), user) require.Nil(t, err, fmt.Sprintf("register user got unexpected error: %s", err)) @@ -413,8 +397,6 @@ func TestPasswordChange(t *testing.T) { reqData.Password = invalidPass reqWeakPass := toJSON(reqData) - resData.Msg = users.ErrUnauthorizedAccess.Error() - cases := []struct { desc string req string @@ -423,7 +405,7 @@ func TestPasswordChange(t *testing.T) { res string tok string }{ - {"password change with valid token", dataResExisting, contentType, http.StatusCreated, expectedSuccess, token}, + {"password change with valid token", dataResExisting, contentType, http.StatusCreated, "{}", token}, {"password change with invalid token", reqNoExist, contentType, http.StatusForbidden, unauthRes, ""}, {"password change with invalid old password", reqWrongPass, contentType, http.StatusForbidden, unauthRes, token}, {"password change with invalid new password", reqWeakPass, contentType, http.StatusBadRequest, weakPassword, token}, diff --git a/users/api/responses.go b/users/api/responses.go index d17af144..739725df 100644 --- a/users/api/responses.go +++ b/users/api/responses.go @@ -193,10 +193,25 @@ type errorRes struct { Err string `json:"error"` } -type passwChangeRes struct { +type passwResetReqRes struct { Msg string `json:"msg"` } +func (res passwResetReqRes) Code() int { + return http.StatusCreated +} + +func (res passwResetReqRes) Headers() map[string]string { + return map[string]string{} +} + +func (res passwResetReqRes) Empty() bool { + return false +} + +type passwChangeRes struct { +} + func (res passwChangeRes) Code() int { return http.StatusCreated }