Skip to content

Commit

Permalink
Merge pull request #21 from open-networks/develop
Browse files Browse the repository at this point in the history
Fixes #18: add unit tests for `User.go`: func `UpdateUser`
  • Loading branch information
TerraTalpi authored Aug 30, 2021
2 parents 1a0763c + afb3516 commit 3f33ffb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
23 changes: 23 additions & 0 deletions GraphClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -907,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")
}
}
12 changes: 12 additions & 0 deletions Groups_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions User_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
36 changes: 36 additions & 0 deletions supportedTimeZones_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 3f33ffb

Please sign in to comment.