Skip to content

Commit

Permalink
fix: Internal Server Error on Empty PUT /identities/id body (#2417)
Browse files Browse the repository at this point in the history
  • Loading branch information
VeenaInd committed Apr 26, 2022
1 parent ca1dab8 commit 5a50231
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 9 additions & 3 deletions identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"

"github.com/ory/x/decoderx"
"github.com/ory/x/jsonx"
"github.com/ory/x/sqlxx"
"github.com/ory/x/urlx"
Expand All @@ -42,7 +43,8 @@ type (
IdentityHandler() *Handler
}
Handler struct {
r handlerDependencies
r handlerDependencies
dx *decoderx.HTTP
}
)

Expand All @@ -51,7 +53,10 @@ func (h *Handler) Config(ctx context.Context) *config.Config {
}

func NewHandler(r handlerDependencies) *Handler {
return &Handler{r: r}
return &Handler{
r: r,
dx: decoderx.NewHTTP(),
}
}

func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
Expand Down Expand Up @@ -435,7 +440,8 @@ type AdminUpdateIdentityBody struct {
// 500: jsonError
func (h *Handler) update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ur AdminUpdateIdentityBody
if err := errors.WithStack(jsonx.NewStrictDecoder(r.Body).Decode(&ur)); err != nil {
if err := h.dx.Decode(r, &ur,
decoderx.HTTPJSONDecoder()); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
Expand Down
20 changes: 19 additions & 1 deletion identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func TestHandler(t *testing.T) {

var send = func(t *testing.T, base *httptest.Server, method, href string, expectCode int, send interface{}) gjson.Result {
var b bytes.Buffer
require.NoError(t, json.NewEncoder(&b).Encode(send))
if send != nil {
require.NoError(t, json.NewEncoder(&b).Encode(send))
}
req, err := http.NewRequest(method, base.URL+href, &b)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -673,6 +675,22 @@ func TestHandler(t *testing.T) {
}
})

t.Run("case=should fail to update identity if input json is empty or json file does not exist", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"public": publicTS, "admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
var cr identity.AdminCreateIdentityBody
cr.SchemaID = "employee"
cr.Traits = []byte(`{"email":"` + x.NewUUID().String() + `@ory.sh", "department": "ory"}`)
res := send(t, ts, "POST", "/identities", http.StatusCreated, &cr)

id := res.Get("id").String()
res = send(t, ts, "PUT", "/identities/"+id, http.StatusBadRequest, nil)
assert.Contains(t, res.Get("error.reason").String(), `Unable to decode HTTP Request Body because its HTTP `+
`Header "Content-Length" is zero`, "%s", res.Raw)
})
}
})

t.Run("case=should list all identities", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"public": publicTS, "admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
Expand Down

0 comments on commit 5a50231

Please sign in to comment.