Skip to content

Commit

Permalink
Merge pull request #22 from open-networks/develop
Browse files Browse the repository at this point in the history
Fixes #17: docs: add documentation for enabling and deleting a user
  • Loading branch information
TerraTalpi authored Aug 30, 2021
2 parents 3f33ffb + 657b0b0 commit ae385fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
12 changes: 6 additions & 6 deletions User.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,19 @@ func (u User) GetShortName() string {
if len(supn) != 2 {
return u.UserPrincipalName
}
return strings.ToUpper(supn[0])
return supn[0]
}

// GetFullName returns the full name in that format: <firstname> <lastname>
func (u User) GetFullName() string {
return fmt.Sprintf("%v %v", u.GivenName, u.Surname)
}

// PrettySimpleString returns the User-instance simply formatted for logging purposes: {FullName (email) (activePhone)}
func (u User) PrettySimpleString() string {
return fmt.Sprintf("{ %v (%v) (%v) }", u.GetFullName(), u.Mail, u.GetActivePhone())
}

// UpdateUser patches this user object. Note, only set the fields that should be changed.
//
// IMPORTANT: the user cannot be disabled (field AccountEnabled) this way, because the
Expand Down Expand Up @@ -208,11 +213,6 @@ func (u User) DeleteUser(opts ...DeleteQueryOption) error {
return err
}

// PrettySimpleString returns the User-instance simply formatted for logging purposes: {FullName (email) (activePhone)}
func (u User) PrettySimpleString() string {
return fmt.Sprintf("{ %v (%v) (%v) }", u.GetFullName(), u.Mail, u.GetActivePhone())
}

// Equal returns wether the user equals the other User by comparing every property
// of the user including the ID
func (u User) Equal(other User) bool {
Expand Down
44 changes: 44 additions & 0 deletions User_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func TestUser_ListCalendars(t *testing.T) {
if skipCalendarTests {
t.Skip("Skipping due to missing 'MSGraphExistingCalendarsOfUser' value")
}
// testing for ErrNotGraphClientSourced
notGraphClientSourcedUser := User{ID: "none"}
_, err := notGraphClientSourcedUser.ListCalendars()
if err != ErrNotGraphClientSourced {
t.Errorf("Expected error \"ErrNotGraphClientSourced\", but got: %v", err)
}
// continue with normal tests
userToTest := GetTestUser(t)

var wantedCalendars []Calendar
Expand Down Expand Up @@ -160,6 +167,14 @@ func TestUser_String(t *testing.T) {
}

func TestUser_UpdateUser(t *testing.T) {
// testing for ErrNotGraphClientSourced
notGraphClientSourcedUser := User{ID: "none"}
err := notGraphClientSourcedUser.UpdateUser(User{})
if err != ErrNotGraphClientSourced {
t.Errorf("Expected error \"ErrNotGraphClientSourced\", but got: %v", err)
}

// continue with normal tests
testuser := createUnitTestUser(t)

targetedCompanyName := "go-msgraph unit test suite UpdateUser" + randomString(25)
Expand All @@ -178,3 +193,32 @@ func TestUser_UpdateUser(t *testing.T) {
t.Errorf("Could not User.DeleteUser() (for %v) after User.UpdateUser tests: %v", testuser, err)
}
}

func TestUser_GetShortName(t *testing.T) {
// test a normal case
testuser := User{UserPrincipalName: "[email protected]"}
if testuser.GetShortName() != "dumpty" {
t.Errorf("user.GetShortName() should return \"dumpty\", but returns: %v", testuser.GetShortName())
}
// test a case that actually should never happen... but we all know murphy
testuser = User{UserPrincipalName: "alice"}
if testuser.GetShortName() != "alice" {
t.Errorf("user.GetShortName() should return \"alice\", but returns: %v", testuser.GetShortName())
}
}

func TestUser_GetFullName(t *testing.T) {
testuser := User{GivenName: "Bob", Surname: "Rabbit"}
wanted := fmt.Sprintf("%v %v", testuser.GivenName, testuser.Surname)
if testuser.GetFullName() != wanted {
t.Errorf("user.GetFullName() should return \"%v\", but returns: \"%v\"", wanted, testuser.GetFullName())
}
}

func TestUser_PrettySimpleString(t *testing.T) {
testuser := User{GivenName: "Bob", Surname: "Rabbit", Mail: "[email protected]", MobilePhone: "+1 23456789"}
wanted := fmt.Sprintf("{ %v (%v) (%v) }", testuser.GetFullName(), testuser.Mail, testuser.GetActivePhone())
if testuser.PrettySimpleString() != wanted {
t.Errorf("user.GetFullName() should return \"%v\", but returns: \"%v\"", wanted, testuser.PrettySimpleString())
}
}
4 changes: 4 additions & 0 deletions docs/example_User.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ err := user.UpdateUser(msgraph.User{DisplayName: "Rabbit 2.0"}, msgraph.UpdateWi

// disable acccount
err := user.DisableAccount()
// enable user account again
err := user.UpdateUser(User{AccountEnabled: true})
// delete a user, use with caution!
err := user.DeleteUser()
````

0 comments on commit ae385fc

Please sign in to comment.