Skip to content

Commit

Permalink
Merge pull request #35 from EOS-Nation/feature/unlink_user_identity
Browse files Browse the repository at this point in the history
Add method to unlink a users identity
  • Loading branch information
sergiught authored Apr 7, 2022
2 parents 8e4e76e + 8bf23c7 commit 9ac8228
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions management/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ func (m *UserManager) Link(id string, il *UserIdentityLink, opts ...RequestOptio
return uIDs, nil
}

// Unlink unlinks an identity from a user making it a separate account again.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_user_identity_by_user_id
func (m *UserManager) Unlink(id, provider, userID string, opts ...RequestOption) (uIDs []UserIdentity, err error) {
err = m.Request("DELETE", m.URI("users", id, "identities", provider, userID), &uIDs, opts...)
return
}

// Organizations lists user's organizations.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_organizations
Expand Down
57 changes: 57 additions & 0 deletions management/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package management

import (
"encoding/json"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -371,6 +372,62 @@ func TestUser(t *testing.T) {
m.User.Delete(batman.GetID())
})
})

t.Run("Unlink", func(t *testing.T) {
cs, err := m.Connection.ReadByName("Username-Password-Authentication")
if err != nil {
t.Error(err)
}

mainUser := &User{
Email: auth0.String("[email protected]"),
Username: auth0.String("main_user"),
Password: auth0.String("NF2QZxci3Z5NikLRoHcAu3H5"),
Connection: cs.Name,
}
if err := m.User.Create(mainUser); err != nil {
t.Error(err)
}

secondaryUser := &User{
Email: auth0.String("[email protected]"),
Username: auth0.String("secondary_user"),
Password: auth0.String("Ta9Y95PNbiCummJ3zpzCtEYy"),
Connection: cs.Name,
}
if err := m.User.Create(secondaryUser); err != nil {
t.Error(err)
}

linkedIdentities, err := m.User.Link(mainUser.GetID(), &UserIdentityLink{
Provider: auth0.String("auth0"),
UserID: secondaryUser.ID,
ConnectionID: cs.ID,
})
if err != nil {
t.Error(err)
}
jsonLinkedIdentities, err := json.Marshal(linkedIdentities)
if err != nil {
t.Error(err)
}
t.Logf("%v\n", string(jsonLinkedIdentities))

unlinkedIdentities, err := m.User.Unlink(mainUser.GetID(), "auth0", strings.TrimPrefix(secondaryUser.GetID(), "auth0|"))
if err != nil {
t.Error(err)
}
jsonUnlinkedIdentities, err := json.Marshal(unlinkedIdentities)
if err != nil {
t.Error(err)
}
t.Logf("%v\n", string(jsonUnlinkedIdentities))

t.Cleanup(func() {
m.User.Delete(mainUser.GetID())
m.User.Delete(secondaryUser.GetID())
})
})
}

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

0 comments on commit 9ac8228

Please sign in to comment.