Skip to content

Commit

Permalink
fix: include metadata_admin in admin identity list response (#2791)
Browse files Browse the repository at this point in the history
Closes #2711
  • Loading branch information
brahmlower committed Oct 11, 2022
1 parent 8043371 commit aa698e0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
8 changes: 7 additions & 1 deletion identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Para
return
}

// Identities using the marshaler for including metadata_admin
isam := make([]WithAdminMetadataInJSON, len(is))
for i, identity := range is {
isam[i] = WithAdminMetadataInJSON(identity)
}

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

// swagger:parameters adminGetIdentity
Expand Down
4 changes: 3 additions & 1 deletion identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,9 @@ func TestHandler(t *testing.T) {
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.Empty(t, res.Get("0.credentials").String(), "%s", res.Raw)
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)
})
}
Expand Down
8 changes: 8 additions & 0 deletions identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ func (i *Identity) UnmarshalJSON(b []byte) error {
return err
}

type WithAdminMetadataInJSON Identity

func (i WithAdminMetadataInJSON) MarshalJSON() ([]byte, error) {
type localIdentity Identity
i.Credentials = nil
return json.Marshal(localIdentity(i))
}

type WithCredentialsAndAdminMetadataInJSON Identity

func (i WithCredentialsAndAdminMetadataInJSON) MarshalJSON() ([]byte, error) {
Expand Down
11 changes: 10 additions & 1 deletion identity/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func TestMarshalIdentityWithCredentialsWhenCredentialsNil(t *testing.T) {
assert.False(t, gjson.Get(b.String(), "credentials").Exists())
}

func TestMarshalIdentityWithAdminMetadata(t *testing.T) {
i := NewIdentity(config.DefaultIdentityTraitsSchemaID)
i.MetadataAdmin = []byte(`{"some":"metadata"}`)

var b bytes.Buffer
require.Nil(t, json.NewEncoder(&b).Encode(WithAdminMetadataInJSON(*i)))
assert.Equal(t, "metadata", gjson.GetBytes(i.MetadataAdmin, "some").String(), "Original metadata_admin should not be touched by marshalling")
}

func TestMarshalIdentityWithCredentialsMetadata(t *testing.T) {
i := NewIdentity(config.DefaultIdentityTraitsSchemaID)
credentials := map[CredentialsType]Credentials{
Expand All @@ -188,7 +197,7 @@ func TestMarshalIdentityWithCredentialsMetadata(t *testing.T) {

assert.JSONEq(t, "{\"password\":{\"type\":\"password\",\"identifiers\":null,\"updated_at\":\"0001-01-01T00:00:00Z\",\"created_at\":\"0001-01-01T00:00:00Z\",\"version\":0}}", credentialsInJson.Raw)
assert.Equal(t, credentials, i.Credentials, "Original credentials should not be touched by marshalling")
assert.Equal(t, "metadata", gjson.GetBytes(i.MetadataAdmin, "some").String(), "Original credentials should not be touched by marshalling")
assert.Equal(t, "metadata", gjson.GetBytes(i.MetadataAdmin, "some").String(), "Original metadata_admin should not be touched by marshalling")
}

func TestMarshalIdentityWithAll(t *testing.T) {
Expand Down

0 comments on commit aa698e0

Please sign in to comment.