From 56ce6692412615044f0039507f9c9c35cc4474b9 Mon Sep 17 00:00:00 2001 From: Felix Hartung <7132415+TerraTalpi@users.noreply.github.com> Date: Thu, 19 Aug 2021 13:30:06 +0000 Subject: [PATCH] GraphClient: add unit tests to create and delete a User --- GraphClient_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/GraphClient_test.go b/GraphClient_test.go index 8261a82..297bb30 100644 --- a/GraphClient_test.go +++ b/GraphClient_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "net/url" "os" "reflect" @@ -33,6 +34,8 @@ var ( msGraphExistingCalendarsOfUser []string // the number of expected results when searching for the msGraphExistingGroupDisplayName with $search or $filter msGraphExistingGroupDisplayNameNumRes uint64 + // a domain-name for unit tests to create a user or other objects, e.g. contoso.com - omit the @ + msGraphDomainNameForCreateTests string // the graphclient used to perform all tests graphClient *GraphClient // marker if the calendar tests should be skipped - set if msGraphExistingCalendarsOfUser is empty @@ -53,6 +56,7 @@ func TestMain(m *testing.M) { msGraphClientSecret = getEnvOrPanic("MSGraphClientSecret") msGraphExistingGroupDisplayName = getEnvOrPanic("MSGraphExistingGroupDisplayName") msGraphExistingUserPrincipalInGroup = getEnvOrPanic("MSGraphExistingUserPrincipalInGroup") + msGraphDomainNameForCreateTests = getEnvOrPanic("MSGraphDomainNameForCreateTests") if msGraphAzureADAuthEndpoint = os.Getenv("MSGraphAzureADAuthEndpoint"); msGraphAzureADAuthEndpoint == "" { msGraphAzureADAuthEndpoint = AzureADAuthEndpointGlobal @@ -76,9 +80,20 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("Cannot initialize a new GraphClient, error: %v", err)) } + rand.Seed(time.Now().UnixNano()) + os.Exit(m.Run()) } +func randomString(n int) string { + var runes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + b := make([]rune, n) + for i := range b { + b[i] = runes[rand.Intn(len(runes))] + } + return string(b) +} + func TestNewGraphClient(t *testing.T) { if msGraphAzureADAuthEndpoint != AzureADAuthEndpointGlobal || msGraphServiceRootEndpoint != ServiceRootEndpointGlobal { t.Skip("Skipping TestNewGraphClient because the endpoint is not the default - global - endpoint") @@ -557,6 +572,43 @@ func TestGraphClient_GetGroup(t *testing.T) { } } +func TestGraphClient_CreateAndDeleteUser(t *testing.T) { + var rndstring = randomString(32) + tests := []struct { + name string + g *GraphClient + want User + wantErr bool + }{ + { + name: "Create new User", + g: graphClient, + want: 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)}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.g.CreateUser(tt.want) + if (err != nil) != tt.wantErr { + t.Errorf("GraphClient.CreateUser() error = %v, wantErr %v", err, tt.wantErr) + return + } + fmt.Printf("Got: %v\n", got) + err = got.DeleteUser() + if (err != nil) != tt.wantErr { + t.Errorf("User.DeleteUser() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + func TestGetQueryOptions_Context(t *testing.T) { t.Parallel() tests := []struct {