From a8feade4c13450756bf4f7151ecec710a4192fdd Mon Sep 17 00:00:00 2001 From: Brahm Lower Date: Sat, 8 Oct 2022 22:51:42 -0700 Subject: [PATCH] fix: #2711 include metadata_admin in admin identity list response --- identity/handler.go | 8 +++++++- identity/handler_test.go | 4 +++- identity/identity.go | 8 ++++++++ identity/identity_test.go | 11 ++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/identity/handler.go b/identity/handler.go index 6cca18e574d3..787181cbc847 100644 --- a/identity/handler.go +++ b/identity/handler.go @@ -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 diff --git a/identity/handler_test.go b/identity/handler_test.go index d0f52e1ec8fa..0dad86c21409 100644 --- a/identity/handler_test.go +++ b/identity/handler_test.go @@ -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) }) } diff --git a/identity/identity.go b/identity/identity.go index 9e72358b7af7..8677833f53fc 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -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) { diff --git a/identity/identity_test.go b/identity/identity_test.go index 1837ca925923..8727a4631c1d 100644 --- a/identity/identity_test.go +++ b/identity/identity_test.go @@ -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{ @@ -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) {