Skip to content

Commit

Permalink
chore: empty array on no results
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kelkar committed Feb 10, 2023
1 parent 4fb6af0 commit a101504
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"time"

"github.com/ory/x/sqlcon"

"github.com/ory/x/pagination/migrationpagination"

"github.com/ory/kratos/hash"
Expand Down Expand Up @@ -158,6 +160,11 @@ func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Para
if identifier := r.URL.Query().Get("identifier"); identifier != "" {
i, err := h.r.PrivilegedIdentityPool().FindByCredentialsIdentifier(r.Context(), identifier)
if err != nil {
if errors.Is(err, sqlcon.ErrNoRows) {
h.r.Writer().Write(w, r, []Identity{})
return
}

h.r.Writer().WriteError(w, r, err)
return
}
Expand Down
9 changes: 5 additions & 4 deletions identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ func TestHandler(t *testing.T) {
}
})

t.Run("case=should return 404 on a non-existing resource for identity lookup", func(t *testing.T) {
_ = get(t, adminTS, "/identities?identifier=does-not-exist", http.StatusNotFound)
})

t.Run("case=should fail to create an identity because schema id 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) {
Expand Down Expand Up @@ -379,6 +375,11 @@ func TestHandler(t *testing.T) {
}
})

t.Run("case=should return an empty array on a failed lookup with identifier", func(t *testing.T) {
res := get(t, adminTS, "/identities?identifier=find.by.non.existing.identifier@bar.com", http.StatusOK)
assert.EqualValues(t, int64(0), res.Get("#").Int(), "%s", res.Raw)
})

t.Run("case=should be able to lookup the identity using identifier", func(t *testing.T) {
i1 := &identity.Identity{
Credentials: map[identity.CredentialsType]identity.Credentials{
Expand Down
1 change: 0 additions & 1 deletion persistence/sql/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ WHERE ici.identifier = ?
nid,
nid,
).First(&find); err != nil {

return nil, sqlcon.HandleError(err)
}

Expand Down

0 comments on commit a101504

Please sign in to comment.