Skip to content

Commit

Permalink
Merge 3d75858 into 8988758
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 19, 2018
2 parents 8988758 + 3d75858 commit a926c2c
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 2 deletions.
20 changes: 20 additions & 0 deletions selvpcclient/resell/v2/projects/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
`
316 changes: 316 additions & 0 deletions selvpcclient/resell/v2/projects/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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")
}
}
4 changes: 2 additions & 2 deletions selvpcclient/resell/v2/subnets/testing/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit a926c2c

Please sign in to comment.