NOISSUE - fix response for passwd endpoints (#1393)

* fix response for passwd endpoints

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>

* fix test for passwd related responses

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>

* fix test for passwd related responses

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
This commit is contained in:
Mirko Teodorovic 2021-03-23 17:43:06 +01:00 committed by GitHub
parent e87715ba31
commit f9f51470b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 23 deletions

View File

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

View File

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

View File

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