From f30569203dc02ac86282644e1fdb7dbc4d9fd891 Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:16:14 +0000 Subject: [PATCH 1/4] User_test: add unit test for UpdateUser --- GraphClient_test.go | 16 ++++++++++++++++ User_test.go | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/GraphClient_test.go b/GraphClient_test.go index ab48424..efd3a46 100644 --- a/GraphClient_test.go +++ b/GraphClient_test.go @@ -94,6 +94,22 @@ func randomString(n int) string { return string(b) } +func createUnitTestUser(t *testing.T) User { + t.Helper() + rndstring := randomString(32) + user, err := graphClient.CreateUser(User{ + AccountEnabled: true, + DisplayName: "go-msgraph unit-test generated user " + time.Now().Format("2006-01-02") + " - random " + rndstring, + MailNickname: "go-msgraph.unit-test.generated." + rndstring, + UserPrincipalName: "go-msgraph.unit-test.generated." + rndstring + "@" + msGraphDomainNameForCreateTests, + PasswordProfile: PasswordProfile{Password: randomString(32)}, + }) + if err != nil { + t.Errorf("Cannot create a new User for unit tests: %v", err) + } + return user +} + func TestNewGraphClient(t *testing.T) { if msGraphAzureADAuthEndpoint != AzureADAuthEndpointGlobal || msGraphServiceRootEndpoint != ServiceRootEndpointGlobal { t.Skip("Skipping TestNewGraphClient because the endpoint is not the default - global - endpoint") diff --git a/User_test.go b/User_test.go index 4e719da..73c2f19 100644 --- a/User_test.go +++ b/User_test.go @@ -158,3 +158,23 @@ func TestUser_String(t *testing.T) { } }) } + +func TestUser_UpdateUser(t *testing.T) { + testuser := createUnitTestUser(t) + + targetedCompanyName := "go-msgraph unit test suite UpdateUser" + randomString(25) + testuser.UpdateUser(User{CompanyName: targetedCompanyName}) + getUser, err := graphClient.GetUser(testuser.ID, GetWithSelect("id,userPrincipalName,displayName,givenName,companyName")) + if err != nil { + testuser.DeleteUser() + t.Errorf("Cannot perform User.UpdateUser, error: %v", err) + } + if getUser.CompanyName != targetedCompanyName { + testuser.DeleteUser() + t.Errorf("Performed User.UpdateUser, but CompanyName is still \"%v\" instead of wanted \"%v\"", getUser.CompanyName, targetedCompanyName) + } + err = testuser.DeleteUser() + if err != nil { + t.Errorf("Could not User.DeleteUser() (for %v) after User.UpdateUser tests: %v", testuser, err) + } +} From 038162d9ba2b392466308c05c062c1c8913e53e3 Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:58:01 +0000 Subject: [PATCH 2/4] GraphClient: add unit test for String() --- GraphClient_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/GraphClient_test.go b/GraphClient_test.go index efd3a46..04430aa 100644 --- a/GraphClient_test.go +++ b/GraphClient_test.go @@ -923,3 +923,10 @@ func TestGraphClient_UnmarshalJSON(t *testing.T) { }) } } + +func TestGraphClient_String(t *testing.T) { + if fmt.Sprintf("GraphClient(TenantID: %v, ApplicationID: %v, ClientSecret: %v...%v, Token validity: [%v - %v])", + graphClient.TenantID, graphClient.ApplicationID, graphClient.ClientSecret[0:3], graphClient.ClientSecret[len(graphClient.ClientSecret)-3:], graphClient.token.NotBefore, graphClient.token.ExpiresOn) != graphClient.String() { + t.Errorf("GraphClient.String(): String function failed") + } +} From bd19e9c94c8cc49399448a62d3da7880e65f8a5f Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:58:12 +0000 Subject: [PATCH 3/4] Groups: add unit test for String() --- Groups_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Groups_test.go diff --git a/Groups_test.go b/Groups_test.go new file mode 100644 index 0000000..2ad9164 --- /dev/null +++ b/Groups_test.go @@ -0,0 +1,12 @@ +package msgraph + +import "testing" + +func TestGroups_String(t *testing.T) { + testGroup := GetTestGroup(t) + groups := Groups{testGroup} + wanted := "Groups(" + testGroup.String() + ")" + if wanted != groups.String() { + t.Errorf("Groups.String() result: %v, wanted: %v", testGroup, wanted) + } +} From afb351655883c07ae497623d46461b47590d1cb8 Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:58:28 +0000 Subject: [PATCH 4/4] supportedTimeZones: add unit test for String() --- supportedTimeZones_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 supportedTimeZones_test.go diff --git a/supportedTimeZones_test.go b/supportedTimeZones_test.go new file mode 100644 index 0000000..e0ac888 --- /dev/null +++ b/supportedTimeZones_test.go @@ -0,0 +1,36 @@ +package msgraph + +import ( + "math/rand" + "testing" +) + +func Test_supportedTimeZones_GetTimeZoneByAlias(t *testing.T) { + testuser := GetTestUser(t) + timezones, _ := testuser.getTimeZoneChoices(compileGetQueryOptions(nil)) + + randomTimezone := timezones.Value[rand.Intn(len(timezones.Value))] + _, err := timezones.GetTimeZoneByAlias(randomTimezone.Alias) + if err != nil { + t.Errorf("Cannot get timeZone with Alias %v, err: %v", randomTimezone.Alias, err) + } + _, err = timezones.GetTimeZoneByAlias("This is a non existing timezone") + if err == nil { + t.Errorf("Tried to get a non existing timezone, expected an error, but got nil") + } +} + +func Test_supportedTimeZones_GetTimeZoneByDisplayName(t *testing.T) { + testuser := GetTestUser(t) + timezones, _ := testuser.getTimeZoneChoices(compileGetQueryOptions(nil)) + + randomTimezone := timezones.Value[rand.Intn(len(timezones.Value))] + _, err := timezones.GetTimeZoneByDisplayName(randomTimezone.DisplayName) + if err != nil { + t.Errorf("Cannot get timeZone with DisplayName %v, err: %v", randomTimezone.DisplayName, err) + } + _, err = timezones.GetTimeZoneByDisplayName("This is a non existing timezone") + if err == nil { + t.Errorf("Tried to get a non existing timezone, expected an error, but got nil") + } +}