Skip to content

Commit

Permalink
fix: do not omit last page on identity list (#3169)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Mar 16, 2023
1 parent 48a4469 commit f95f48a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ require (
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.7
github.com/ory/x v0.0.537
github.com/peterhellberg/link v1.2.0
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,8 @@ github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac=
github.com/peterhellberg/link v1.2.0 h1:UA5pg3Gp/E0F2WdX7GERiNrPQrM1K6CVJUUWfHa4t6c=
github.com/peterhellberg/link v1.2.0/go.mod h1:gYfAh+oJgQu2SrZHg5hROVRQe1ICoK0/HHJTcE0edxc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
Expand Down
2 changes: 1 addition & 1 deletion identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Para
isam[i] = WithCredentialsMetadataAndAdminMetadataInJSON(identity)
}

migrationpagination.PaginationHeader(w, urlx.AppendPaths(h.r.Config().SelfAdminURL(r.Context()), RouteCollection), total, page, itemsPerPage)
migrationpagination.PaginationHeader(w, urlx.AppendPaths(h.r.Config().SelfAdminURL(r.Context()), RouteCollection), total+int64(itemsPerPage), page, itemsPerPage)
h.r.Writer().Write(w, r, isam)
}

Expand Down
45 changes: 41 additions & 4 deletions identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ory/x/snapshotx"

"github.com/bxcodec/faker/v3"
"github.com/peterhellberg/link"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -1004,13 +1005,49 @@ func TestHandler(t *testing.T) {
})

t.Run("case=should list all identities", func(t *testing.T) {
expected, err := reg.IdentityPool().ListIdentities(ctx, identity.ListIdentityParameters{PerPage: 1000})
require.NoError(t, err)
expectedIDs := make([]string, len(expected))
for k, v := range expected {
expectedIDs[k] = v.ID.String()
}

for name, ts := range map[string]*httptest.Server{"public": publicTS, "admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
res := get(t, ts, "/identities", http.StatusOK)
assert.False(t, res.Get("0.credentials").Exists(), "credentials config should be omitted: %s", res.Raw)
assert.True(t, res.Get("0.metadata_public").Exists(), "metadata_public config should be included: %s", res.Raw)
assert.True(t, res.Get("0.metadata_admin").Exists(), "metadata_admin config should be included: %s", res.Raw)
assert.EqualValues(t, "baz", res.Get(`#(traits.bar=="baz").traits.bar`).String(), "%s", res.Raw)
assert.Falsef(t, res.Get("0.credentials").Exists(), "credentials config should be omitted: %s", res.Raw)
assert.Truef(t, res.Get("0.metadata_public").Exists(), "metadata_public config should be included: %s", res.Raw)
assert.Truef(t, res.Get("0.metadata_admin").Exists(), "metadata_admin config should be included: %s", res.Raw)
assert.Truef(t, res.Get(`#(traits.bar=="baz").traits.bar`).Exists(), "some identity should have the traits {bar: baz}: %s", res.Raw)

assert.EqualValuesf(t, len(expectedIDs), int(res.Get("#").Num), "%s", res.Raw)
res.Get("#.id").ForEach(func(key, value gjson.Result) bool {
return assert.Contains(t, expectedIDs, value.String(), "%s", res.Raw)
})

actualIDs := make([]string, 0, len(expectedIDs))
for reqURL := ts.URL + "/identities?per_page=5&page=1"; ; {
res, err := ts.Client().Get(reqURL)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close())

require.EqualValues(t, http.StatusOK, res.StatusCode, "%s", body)
var ids []identity.Identity
require.NoError(t, json.Unmarshal(body, &ids))
for _, id := range ids {
actualIDs = append(actualIDs, id.ID.String())
}

links := link.ParseHeader(res.Header)
next, ok := links["next"]
if !ok {
break
}
reqURL = next.URI
}
assert.ElementsMatch(t, expectedIDs, actualIDs)
})
}
})
Expand Down

0 comments on commit f95f48a

Please sign in to comment.