Skip to content

Commit

Permalink
Merge pull request #2 from manicminer/client-tests
Browse files Browse the repository at this point in the history
Add tests for clients
  • Loading branch information
manicminer authored Jan 17, 2021
2 parents 02ec15e + 870d32c commit d08da81
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 22 deletions.
3 changes: 3 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lint:
golangci-lint run ./... -v

test:
go test ./... -v

todo:
grep --color=always --exclude=GNUmakefile --exclude-dir=.git --exclude-dir=vendor --recursive TODO "$(CURDIR)"
3 changes: 2 additions & 1 deletion clients/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ func (c *ApplicationsClient) RemoveOwners(ctx context.Context, applicationId str
return false
}

_, status, err := c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
var err error
_, status, err = c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
ValidStatusFunc: checkOwnerGone,
Uri: base.Uri{
Expand Down
152 changes: 152 additions & 0 deletions clients/applications_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package clients_test

import (
"fmt"
"testing"

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

type ApplicationsClientTest struct {
connection *internal.Connection
client *clients.ApplicationsClient
randomString string
}

func TestApplicationsClient(t *testing.T) {
c := ApplicationsClientTest{
connection: internal.NewConnection(),
randomString: internal.RandomString(),
}
c.client = clients.NewApplicationsClient(c.connection.AuthConfig.TenantID)
c.client.BaseClient.Authorizer = c.connection.Authorizer

app := testApplicationsClient_Create(t, c, models.Application{
DisplayName: internal.String(fmt.Sprintf("test-application-%s", c.randomString)),
})
testApplicationsClient_Get(t, c, *app.ID)
app.DisplayName = internal.String(fmt.Sprintf("test-app-updated-%s", c.randomString))
testApplicationsClient_Update(t, c, *app)
owners := testApplicationsClient_ListOwners(t, c, *app.ID)
testApplicationsClient_GetOwner(t, c, *app.ID, (*owners)[0])
testApplicationsClient_RemoveOwners(t, c, *app.ID, owners)
app.AppendOwner(c.client.BaseClient.Endpoint, c.client.BaseClient.ApiVersion, (*owners)[0])
testApplicationsClient_AddOwners(t, c, app)
testApplicationsClient_List(t, c)
testApplicationsClient_Delete(t, c, *app.ID)
}

func testApplicationsClient_Create(t *testing.T, c ApplicationsClientTest, a models.Application) (application *models.Application) {
application, status, err := c.client.Create(c.connection.Context, a)
if err != nil {
t.Fatalf("ApplicationsClient.Create(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.Create(): invalid status: %d", status)
}
if application == nil {
t.Fatal("ApplicationsClient.Create(): application was nil")
}
if application.ID == nil {
t.Fatal("ApplicationsClient.Create(): application.ID was nil")
}
return
}

func testApplicationsClient_Update(t *testing.T, c ApplicationsClientTest, a models.Application) {
status, err := c.client.Update(c.connection.Context, a)
if err != nil {
t.Fatalf("ApplicationsClient.Update(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.Update(): invalid status: %d", status)
}
}

func testApplicationsClient_List(t *testing.T, c ApplicationsClientTest) (applications *[]models.Application) {
applications, _, err := c.client.List(c.connection.Context, "")
if err != nil {
t.Fatalf("ApplicationsClient.List(): %v", err)
}
if applications == nil {
t.Fatal("ApplicationsClient.List(): applications was nil")
}
return
}

func testApplicationsClient_Get(t *testing.T, c ApplicationsClientTest, id string) (application *models.Application) {
application, status, err := c.client.Get(c.connection.Context, id)
if err != nil {
t.Fatalf("ApplicationsClient.Delete(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.Delete(): invalid status: %d", status)
}
if application == nil {
t.Fatal("ApplicationsClient.Get(): application was nil")
}
return
}

func testApplicationsClient_Delete(t *testing.T, c ApplicationsClientTest, id string) {
status, err := c.client.Delete(c.connection.Context, id)
if err != nil {
t.Fatalf("ApplicationsClient.Delete(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.Delete(): invalid status: %d", status)
}
}

func testApplicationsClient_ListOwners(t *testing.T, c ApplicationsClientTest, id string) (owners *[]string) {
owners, status, err := c.client.ListOwners(c.connection.Context, id)
if err != nil {
t.Fatalf("ApplicationsClient.ListOwners(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.ListOwners(): invalid status: %d", status)
}
if owners == nil {
t.Fatal("ApplicationsClient.ListOwners(): owners was nil")
}
if len(*owners) == 0 {
t.Fatal("ApplicationsClient.ListOwners(): owners was empty")
}
return
}

func testApplicationsClient_GetOwner(t *testing.T, c ApplicationsClientTest, appId string, ownerId string) (owner *string) {
owner, status, err := c.client.GetOwner(c.connection.Context, appId, ownerId)
if err != nil {
t.Fatalf("ApplicationsClient.GetOwner(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.GetOwner(): invalid status: %d", status)
}
if owner == nil {
t.Fatal("ApplicationsClient.GetOwner(): owner was nil")
}
return
}

func testApplicationsClient_AddOwners(t *testing.T, c ApplicationsClientTest, a *models.Application) {
status, err := c.client.AddOwners(c.connection.Context, a)
if err != nil {
t.Fatalf("ApplicationsClient.AddOwners(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.AddOwners(): invalid status: %d", status)
}
}

func testApplicationsClient_RemoveOwners(t *testing.T, c ApplicationsClientTest, appId string, ownerIds *[]string) {
status, err := c.client.RemoveOwners(c.connection.Context, appId, ownerIds)
if err != nil {
t.Fatalf("ApplicationsClient.RemoveOwners(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.RemoveOwners(): invalid status: %d", status)
}
}
52 changes: 52 additions & 0 deletions clients/domains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package clients_test

import (
"testing"

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

type DomainsClientTest struct {
connection *internal.Connection
client *clients.DomainsClient
randomString string
}

func TestDomainsClient(t *testing.T) {
c := DomainsClientTest{
connection: internal.NewConnection(),
randomString: internal.RandomString(),
}
c.client = clients.NewDomainsClient(c.connection.AuthConfig.TenantID)
c.client.BaseClient.Authorizer = c.connection.Authorizer

domains := testDomainsClient_List(t, c)
testDomainsClient_Get(t, c, *(*domains)[0].ID)
}

func testDomainsClient_List(t *testing.T, c DomainsClientTest) (domains *[]models.Domain) {
domains, _, err := c.client.List(c.connection.Context)
if err != nil {
t.Fatalf("DomainsClient.List(): %v", err)
}
if domains == nil {
t.Fatal("DomainsClient.List(): domains was nil")
}
return
}

func testDomainsClient_Get(t *testing.T, c DomainsClientTest, id string) (domain *models.Domain) {
domain, status, err := c.client.Get(c.connection.Context, id)
if err != nil {
t.Fatalf("DomainsClient.Get(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("DomainsClient.Get(): invalid status: %d", status)
}
if domain == nil {
t.Fatal("DomainsClient.Get(): domain was nil")
}
return
}
6 changes: 4 additions & 2 deletions clients/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ func (c *GroupsClient) RemoveMembers(ctx context.Context, id string, memberIds *
}
return status, err
}
_, status, err := c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
var err error
_, status, err = c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
Uri: base.Uri{
Entity: fmt.Sprintf("/groups/%s/members/%s/$ref", id, memberId),
Expand Down Expand Up @@ -368,7 +369,8 @@ func (c *GroupsClient) RemoveOwners(ctx context.Context, id string, ownerIds *[]
}
return status, err
}
_, status, err := c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
var err error
_, status, err = c.BaseClient.Delete(ctx, base.DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
Uri: base.Uri{
Entity: fmt.Sprintf("/groups/%s/owners/%s/$ref", id, ownerId),
Expand Down
Loading

0 comments on commit d08da81

Please sign in to comment.