From 3d758580d9867886ae99e0aa5c710a25af4ace86 Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Thu, 19 Apr 2018 17:49:03 +0300 Subject: [PATCH] Resell V2 projects - add more tests Add tests for the error cases. --- .../resell/v2/projects/testing/fixtures.go | 20 ++ .../v2/projects/testing/requests_test.go | 316 ++++++++++++++++++ .../resell/v2/subnets/testing/request_test.go | 4 +- 3 files changed, 338 insertions(+), 2 deletions(-) diff --git a/selvpcclient/resell/v2/projects/testing/fixtures.go b/selvpcclient/resell/v2/projects/testing/fixtures.go index 7caf156..86dc24e 100644 --- a/selvpcclient/resell/v2/projects/testing/fixtures.go +++ b/selvpcclient/resell/v2/projects/testing/fixtures.go @@ -255,3 +255,23 @@ var TestUpdateProjectResponse = &projects.Project{ }, CustomURL: "", } + +// TestManyProjectsInvalidResponseRaw represents a raw invalid response with many projects. +const TestManyProjectsInvalidResponseRaw = ` +{ + "projects": [ + { + "id": 12 + } + ] +} +` + +// TestSingleProjectInvalidResponseRaw represents a raw invalid response with a single project. +const TestSingleProjectInvalidResponseRaw = ` +{ + "project": { + "id": 12 + } +} +` diff --git a/selvpcclient/resell/v2/projects/testing/requests_test.go b/selvpcclient/resell/v2/projects/testing/requests_test.go index dea4329..f876128 100644 --- a/selvpcclient/resell/v2/projects/testing/requests_test.go +++ b/selvpcclient/resell/v2/projects/testing/requests_test.go @@ -61,6 +61,75 @@ func TestGetProjectSingleQuota(t *testing.T) { } } +func TestGetProjectHTTPError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/49338ac045f448e294b25d013f890317", + TestGetProjectResponseRaw, http.MethodGet, http.StatusBadGateway, + &endpointCalled, t) + + ctx := context.Background() + project, httpResponse, err := projects.Get(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317") + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project 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 TestGetProjectTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + project, _, err := projects.Get(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317") + + if project != nil { + t.Fatal("expected no project from the Get method") + } + if err == nil { + t.Fatal("expected error from the Get method") + } +} + +func TestGetProjectUnmarshalError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/49338ac045f448e294b25d013f890317", + TestSingleProjectInvalidResponseRaw, http.MethodGet, http.StatusOK, + &endpointCalled, t) + + ctx := context.Background() + project, _, err := projects.Get(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317") + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } +} + func TestListProjects(t *testing.T) { endpointCalled := false @@ -112,6 +181,75 @@ func TestListProjectsSingle(t *testing.T) { } } +func TestListSubnetsHTTPError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects", + TestListProjectsResponseRaw, http.MethodGet, http.StatusBadGateway, + &endpointCalled, t) + + ctx := context.Background() + allProjects, httpResponse, err := projects.List(ctx, testEnv.Client) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if allProjects != nil { + t.Fatal("expected no projects 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() + allProjects, _, err := projects.List(ctx, testEnv.Client) + + if allProjects != nil { + t.Fatal("expected no projects from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } +} + +func TestListSubnetsUnmarshalError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects", + TestManyProjectsInvalidResponseRaw, http.MethodGet, http.StatusOK, + &endpointCalled, t) + + ctx := context.Background() + allProjects, _, err := projects.List(ctx, testEnv.Client) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if allProjects != nil { + t.Fatal("expected no projects from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } +} + func TestCreateProject(t *testing.T) { endpointCalled := false @@ -139,6 +277,77 @@ func TestCreateProject(t *testing.T) { } } +func TestCreateProjectsHTTPError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects", + TestCreateProjectResponseRaw, TestCreateProjectOptsRaw, http.MethodPost, + http.StatusBadRequest, &endpointCalled, t) + + ctx := context.Background() + createOpts := TestCreateProjectOpts + project, httpResponse, err := projects.Create(ctx, testEnv.Client, createOpts) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project 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 TestCreateProjectsTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + createOpts := TestCreateProjectOpts + project, _, err := projects.Create(ctx, testEnv.Client, createOpts) + + if project != nil { + t.Fatal("expected no project from the Create method") + } + if err == nil { + t.Fatal("expected error from the Create method") + } +} + +func TestCreateProjectsUnmarshalError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects", + TestSingleProjectInvalidResponseRaw, TestCreateProjectOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t) + + ctx := context.Background() + createOpts := TestCreateProjectOpts + project, _, err := projects.Create(ctx, testEnv.Client, createOpts) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project from the Create method") + } + if err == nil { + t.Fatal("expected error from the Create method") + } +} + func TestUpdateProject(t *testing.T) { endpointCalled := false @@ -166,6 +375,76 @@ func TestUpdateProject(t *testing.T) { } } +func TestUpdateProjectHTTPError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", + "", TestUpdateProjectOptsRaw, http.MethodPatch, http.StatusBadRequest, &endpointCalled, t) + + ctx := context.Background() + updateOpts := TestUpdateProjectOpts + project, httpResponse, err := projects.Update(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4", updateOpts) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project from the Update method") + } + if err == nil { + t.Fatal("expected error from the Update method") + } + if httpResponse.StatusCode != http.StatusBadRequest { + t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode) + } +} + +func TestUpdateProjectTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + updateOpts := TestUpdateProjectOpts + project, _, err := projects.Update(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4", updateOpts) + + if project != nil { + t.Fatal("expected no project from the Update method") + } + if err == nil { + t.Fatal("expected error from the Update method") + } +} + +func TestUpdateProjectUnmarshalError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", + TestSingleProjectInvalidResponseRaw, TestUpdateProjectOptsRaw, http.MethodPatch, + http.StatusOK, &endpointCalled, t) + + ctx := context.Background() + updateOpts := TestUpdateProjectOpts + project, _, err := projects.Update(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4", updateOpts) + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } + if project != nil { + t.Fatal("expected no project from the Update method") + } + if err == nil { + t.Fatal("expected error from the Update method") + } +} + func TestDeleteProject(t *testing.T) { endpointCalled := false @@ -184,3 +463,40 @@ func TestDeleteProject(t *testing.T) { t.Fatal("endpoint wasn't called") } } + +func TestDeleteProjectHTTPError(t *testing.T) { + endpointCalled := false + + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", + "", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t) + + ctx := context.Background() + httpResponse, err := projects.Delete(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4") + + 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 TestDeleteProjectTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + _, err := projects.Delete(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4") + + if err == nil { + t.Fatal("expected error from the Delete method") + } +} diff --git a/selvpcclient/resell/v2/subnets/testing/request_test.go b/selvpcclient/resell/v2/subnets/testing/request_test.go index ecadcda..6bb1c45 100644 --- a/selvpcclient/resell/v2/subnets/testing/request_test.go +++ b/selvpcclient/resell/v2/subnets/testing/request_test.go @@ -197,10 +197,10 @@ func TestListSubnetsTimeoutError(t *testing.T) { allSubnet, _, err := subnets.List(ctx, testEnv.Client) if allSubnet != nil { - t.Fatal("expected no subnets from the Get method") + t.Fatal("expected no subnets from the List method") } if err == nil { - t.Fatal("expected error from the Get method") + t.Fatal("expected error from the List method") } }