-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from manicminer/client-tests
Add tests for clients
- Loading branch information
Showing
8 changed files
with
614 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.