Skip to content

Commit

Permalink
GraphClient: add unit tests to create and delete a User
Browse files Browse the repository at this point in the history
  • Loading branch information
TerraTalpi committed Aug 19, 2021
1 parent 37a8c3b commit 56ce669
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions GraphClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 56ce669

Please sign in to comment.