diff --git a/User.go b/User.go index e7d3603..a23b92c 100644 --- a/User.go +++ b/User.go @@ -137,7 +137,7 @@ 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: @@ -145,6 +145,11 @@ 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 @@ -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 { diff --git a/User_test.go b/User_test.go index 73c2f19..e13b1fb 100644 --- a/User_test.go +++ b/User_test.go @@ -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 @@ -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) @@ -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: "dumpty@contoso.com"} + 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: "bob.rabbit@contoso.com", 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()) + } +} diff --git a/docs/example_User.md b/docs/example_User.md index 86a33d5..a2f42e2 100644 --- a/docs/example_User.md +++ b/docs/example_User.md @@ -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() ```` \ No newline at end of file