Skip to content

Commit

Permalink
Fix Failing Update Client Tests
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
  • Loading branch information
rodneyosodo committed Apr 13, 2023
1 parent d2c8a3b commit 70c9a0f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 deletions users/clients/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func updateClientTagsEndpoint(svc clients.Service) endpoint.Endpoint {

func updateClientIdentityEndpoint(svc clients.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateClientCredentialsReq)
req := request.(updateClientIdentityReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func passwordResetEndpoint(svc clients.Service) endpoint.Endpoint {

func updateClientSecretEndpoint(svc clients.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateClientCredentialsReq)
req := request.(updateClientSecretReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand Down
25 changes: 18 additions & 7 deletions users/clients/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,13 @@ func (req updateClientOwnerReq) validate() error {
return nil
}

type updateClientCredentialsReq struct {
token string
id string
Identity string `json:"identity,omitempty"`
OldSecret string `json:"old_secret,omitempty"`
NewSecret string `json:"new_secret,omitempty"`
type updateClientIdentityReq struct {
token string
id string
Identity string `json:"identity,omitempty"`
}

func (req updateClientCredentialsReq) validate() error {
func (req updateClientIdentityReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}
Expand All @@ -152,6 +150,19 @@ func (req updateClientCredentialsReq) validate() error {
return nil
}

type updateClientSecretReq struct {
token string
OldSecret string `json:"old_secret,omitempty"`
NewSecret string `json:"new_secret,omitempty"`
}

func (req updateClientSecretReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}
return nil
}

type changeClientStatusReq struct {
token string
id string
Expand Down
22 changes: 18 additions & 4 deletions users/clients/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func MakeClientsHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger)

mux.Patch("/users/secret", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_secret"))(updateClientSecretEndpoint(svc)),
decodeUpdateClientCredentials,
decodeUpdateClientSecret,
api.EncodeResponse,
opts...,
))
Expand All @@ -82,7 +82,7 @@ func MakeClientsHandler(svc clients.Service, mux *bone.Mux, logger mflog.Logger)

mux.Patch("/users/:id/identity", kithttp.NewServer(
otelkit.EndpointMiddleware(otelkit.WithOperation("update_client_identity"))(updateClientIdentityEndpoint(svc)),
decodeUpdateClientCredentials,
decodeUpdateClientIdentity,
api.EncodeResponse,
opts...,
))
Expand Down Expand Up @@ -254,11 +254,11 @@ func decodeUpdateClientTags(_ context.Context, r *http.Request) (interface{}, er
return req, nil
}

func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interface{}, error) {
func decodeUpdateClientIdentity(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
}
req := updateClientCredentialsReq{
req := updateClientIdentityReq{
token: apiutil.ExtractBearerToken(r),
id: bone.GetValue(r, "id"),
}
Expand All @@ -269,6 +269,20 @@ func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interfac
return req, nil
}

func decodeUpdateClientSecret(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
}
req := updateClientSecretReq{
token: apiutil.ExtractBearerToken(r),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
}

return req, nil
}

func decodePasswordResetRequest(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.ErrUnsupportedContentType
Expand Down

0 comments on commit 70c9a0f

Please sign in to comment.