Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Internal Server Error on Empty PUT /identities/id body #2417

Merged
merged 5 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/go/selfservice/login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func performLogin() *ory.SuccessfulSelfServiceLoginWithoutBrowser {
ory.SubmitSelfServiceLoginFlowWithPasswordMethodBodyAsSubmitSelfServiceLoginFlowBody(&ory.SubmitSelfServiceLoginFlowWithPasswordMethodBody{
Method: "password",
Password: password,
PasswordIdentifier: email,
PasswordIdentifier: &email,
}),
).Execute()
pkg.SDKExitOnError(err, res)
Expand Down
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)
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
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) {
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
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 `+
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
`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