Skip to content

Commit

Permalink
UsersClient tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Jan 17, 2021
1 parent 22ece80 commit 02ec15e
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
19 changes: 19 additions & 0 deletions clients/internal/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package internal

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func RandomString() string {
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, 8)
for i := range s {
s[i] = chars[rand.Intn(len(chars))]
}
return string(s)
}
57 changes: 57 additions & 0 deletions clients/internal/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package internal

import (
"context"
"log"
"os"

"github.com/manicminer/hamilton/auth"
"github.com/manicminer/hamilton/environments"
)

var (
tenantId = os.Getenv("TENANT_ID")
tenantDomain = os.Getenv("TENANT_DOMAIN")
clientId = os.Getenv("CLIENT_ID")
//clientCertificate = os.Getenv("CLIENT_CERTIFICATE")
clientSecret = os.Getenv("CLIENT_SECRET")
)

type Connection struct {
AuthConfig *auth.Config
Authorizer auth.Authorizer
Context context.Context
DomainName string
}

func NewConnection() *Connection {
t := Connection{
AuthConfig: &auth.Config{
Environment: environments.Global,
TenantID: tenantId,
ClientID: clientId,
ClientSecret: clientSecret,
EnableClientCertAuth: true,
EnableClientSecretAuth: true,
EnableAzureCliToken: true,
},
Context: context.Background(),
DomainName: tenantDomain,
}

var err error
t.Authorizer, err = t.AuthConfig.NewAuthorizer(t.Context)
if err != nil {
log.Fatal(err)
}

return &t
}

func Bool(b bool) *bool {
return &b
}

func String(s string) *string {
return &s
}
78 changes: 78 additions & 0 deletions clients/users_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package clients_test

import (
"context"
"fmt"
"testing"

"github.com/manicminer/hamilton/clients"
"github.com/manicminer/hamilton/clients/internal"
"github.com/manicminer/hamilton/models"
)

type UsersClient struct {
connection *internal.Connection
context context.Context
client *clients.UsersClient
randomString string
}

func TestUsersClient(t *testing.T) {
c := UsersClient{
context: context.Background(),
randomString: internal.RandomString(),
}
c.connection = internal.NewConnection()
c.client = clients.NewUsersClient(c.connection.AuthConfig.TenantID)
c.client.BaseClient.Authorizer = c.connection.Authorizer

user := testUsersClient_Create(t, c, models.User{
AccountEnabled: internal.Bool(true),
DisplayName: internal.String("Test User"),
MailNickname: internal.String(fmt.Sprintf("testuser-%s", c.randomString)),
UserPrincipalName: internal.String(fmt.Sprintf("testuser-%s@%s", c.randomString, c.connection.DomainName)),
PasswordProfile: &models.UserPasswordProfile{
Password: internal.String(fmt.Sprintf("IrPa55w0rd%s", c.randomString)),
},
})
testUsersClient_List(t, c)
testUsersClient_Delete(t, c, *user.ID)
}

func testUsersClient_Create(t *testing.T, c UsersClient, u models.User) (user *models.User) {
user, status, err := c.client.Create(c.context, u)
if err != nil {
t.Errorf("UsersClient.Create(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("UsersClient.Create(): invalid status: %d", status)
}
if user == nil {
t.Fatal("UsersClient.Create(): user was nil")
}
if user.ID == nil {
t.Fatal("UsersClient.Create(): user.ID was nil")
}
return
}

func testUsersClient_List(t *testing.T, c UsersClient) (users *[]models.User) {
users, _, err := c.client.List(c.context, "")
if err != nil {
t.Errorf("UsersClient.List(): %v", err)
}
if users == nil {
t.Error("UsersClient.List(): users was nil")
}
return
}

func testUsersClient_Delete(t *testing.T, c UsersClient, id string) {
status, err := c.client.Delete(c.context, id)
if err != nil {
t.Errorf("UsersClient.Delete(): %v", err)
}
if status < 200 || status >= 300 {
t.Errorf("UsersClient.Delete(): invalid status: %d", status)
}
}

0 comments on commit 02ec15e

Please sign in to comment.