Skip to content

Commit

Permalink
Merge d7b4e1a into 2d1430d
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 18, 2018
2 parents 2d1430d + d7b4e1a commit 9c66e3a
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 3 deletions.
22 changes: 22 additions & 0 deletions selvpcclient/resell/v2/subnets/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ var TestCreateSubnetResponse = []*subnets.Subnet{
Status: "DOWN",
},
}

// TestManySubnetsInvalidResponseRaw represents a raw invalid response with several subnets.
const TestManySubnetsInvalidResponseRaw = `
{
"subnets": [
{
"id": "222"
}
]
}
`

// TestSingleSubnetInvalidResponseRaw represents a raw invalid response with a single subnet.
const TestSingleSubnetInvalidResponseRaw = `
{
"subnet": [
{
"id": "222"
}
]
}
`
247 changes: 247 additions & 0 deletions selvpcclient/resell/v2/subnets/testing/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@ func TestGetSubnet(t *testing.T) {
}
}

func TestGetSubnetHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets/111122",
TestGetSubnetResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)

ctx := context.Background()
subnet, httpResponse, err := subnets.Get(ctx, testEnv.Client, "111122")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if subnet != nil {
t.Fatal("expected no subnet from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestGetSubnetTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
subnet, _, err := subnets.Get(ctx, testEnv.Client, "111122")

if subnet != nil {
t.Fatal("expected no subnet from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestGetSubnetUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets/111122",
TestSingleSubnetInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
subnet, _, err := subnets.Get(ctx, testEnv.Client, "111122")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if subnet != nil {
t.Fatal("expected no subnet from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestListSubnets(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -90,6 +159,75 @@ func TestListSubnetsSingle(t *testing.T) {
}
}

func TestListSubnetsHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets",
TestListSubnetsResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)

ctx := context.Background()
allSubnet, httpResponse, err := subnets.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allSubnet != nil {
t.Fatal("expected no subnets from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestListSubnetsTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
allSubnet, _, err := subnets.List(ctx, testEnv.Client)

if allSubnet != nil {
t.Fatal("expected no subnets from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestListSubnetsUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets",
TestManySubnetsInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
allSubnet, _, err := subnets.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allSubnet != nil {
t.Fatal("expected no subnets from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestCreateSubnets(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -117,6 +255,78 @@ func TestCreateSubnets(t *testing.T) {
}
}

func TestCreateSubnetsHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/subnets/projects/9c97bdc75295493096cf5edcb8c37933",
TestCreateSubnetsResponseRaw, TestCreateSubnetsOptsRaw, http.MethodPost,
http.StatusBadRequest, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateSubnetsOpts
subnet, httpResponse, err := subnets.Create(ctx, testEnv.Client,
"9c97bdc75295493096cf5edcb8c37933", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if subnet != nil {
t.Fatal("expected no subnet from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
if httpResponse.StatusCode != http.StatusBadRequest {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestCreateSubnetsTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
createOpts := TestCreateSubnetsOpts
subnet, _, err := subnets.Create(ctx, testEnv.Client, "9c97bdc75295493096cf5edcb8c37933", createOpts)

if subnet != nil {
t.Fatal("expected no subnet from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestCreateSubnetsUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/subnets/projects/9c97bdc75295493096cf5edcb8c37933",
TestManySubnetsInvalidResponseRaw, TestCreateSubnetsOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateSubnetsOpts
subnet, _, err := subnets.Create(ctx, testEnv.Client, "9c97bdc75295493096cf5edcb8c37933", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if subnet != nil {
t.Fatal("expected no subnet from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestDeleteSubnet(t *testing.T) {
endpointCalled := false

Expand All @@ -136,3 +346,40 @@ func TestDeleteSubnet(t *testing.T) {
t.Fatal("endpoint wasn't called")
}
}

func TestDeleteSubnetHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets/112233",
"", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
httpResponse, err := subnets.Delete(ctx, testEnv.Client, "112233")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestDeleteSubnetTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
_, err := subnets.Delete(ctx, testEnv.Client, "112233")

if err == nil {
t.Fatal("expected error from the Delete method")
}
}
6 changes: 3 additions & 3 deletions selvpcclient/resell/v2/users/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestListUsersTimeoutError(t *testing.T) {
}
}

func TestListUsersUnmarshallError(t *testing.T) {
func TestListUsersUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestCreateUserTimeoutError(t *testing.T) {
}
}

func TestCreateUserUnmarshallError(t *testing.T) {
func TestCreateUserUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestUpdateUserTimeoutError(t *testing.T) {
}
}

func TestUpdateUserUnmarshallError(t *testing.T) {
func TestUpdateUserUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
Expand Down

0 comments on commit 9c66e3a

Please sign in to comment.