From 84f6b0c20165ea658a1db8fd1719d12c4ab1cd2a Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Wed, 18 Apr 2018 15:31:05 +0300 Subject: [PATCH 1/3] Resell V2 Users - add more tests Add tests with errors. --- .../resell/v2/users/testing/requests_test.go | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/selvpcclient/resell/v2/users/testing/requests_test.go b/selvpcclient/resell/v2/users/testing/requests_test.go index e1957e0..d48084c 100644 --- a/selvpcclient/resell/v2/users/testing/requests_test.go +++ b/selvpcclient/resell/v2/users/testing/requests_test.go @@ -19,6 +19,7 @@ func TestListUsers(t *testing.T) { testEnv.NewTestResellV2Client() testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) fmt.Fprintf(w, TestListUsersResponseRaw) if r.Method != http.MethodGet { @@ -50,6 +51,7 @@ func TestListUsersSingle(t *testing.T) { testEnv.NewTestResellV2Client() testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) fmt.Fprintf(w, TestListUsersSingleUserResponseRaw) if r.Method != http.MethodGet { @@ -70,12 +72,40 @@ func TestListUsersSingle(t *testing.T) { } } +func TestListUsersError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusBadGateway) + + if r.Method != http.MethodGet { + t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) + } + }) + + ctx := context.Background() + users, httpResponse, err := users.List(ctx, testEnv.Client) + + if users != nil { + t.Fatal("expected no users from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } + if httpResponse.StatusCode != http.StatusBadGateway { + t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadGateway, httpResponse.StatusCode) + } +} + func TestCreateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) fmt.Fprintf(w, TestCreateUserResponseRaw) if r.Method != http.MethodPost { @@ -118,12 +148,62 @@ func TestCreateUser(t *testing.T) { } } +func TestCreateUserError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + + if r.Method != http.MethodPost { + t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("unable to read the request body: %v", err) + } + + var actualRequest interface{} + err = json.Unmarshal(b, &actualRequest) + if err != nil { + t.Errorf("unable to unmarshal the request body: %v", err) + } + + var expectedRequest interface{} + err = json.Unmarshal([]byte(TestCreateUserOptsRaw), &expectedRequest) + if err != nil { + t.Errorf("unable to unmarshal expected raw response: %v", err) + } + + if !reflect.DeepEqual(actualRequest, expectedRequest) { + t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) + } + }) + + ctx := context.Background() + createOpts := TestCreateUserOpts + actualResponse, httpResponse, err := users.Create(ctx, testEnv.Client, createOpts) + + if actualResponse != nil { + t.Fatal("expected no actualResponse 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 TestUpdateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) fmt.Fprintf(w, TestUpdateUserResponseRaw) if r.Method != http.MethodPatch { @@ -166,6 +246,55 @@ func TestUpdateUser(t *testing.T) { } } +func TestUpdateUserError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + + if r.Method != http.MethodPatch { + t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("unable to read the request body: %v", err) + } + + var actualRequest interface{} + err = json.Unmarshal(b, &actualRequest) + if err != nil { + t.Errorf("unable to unmarshal the request body: %v", err) + } + + var expectedRequest interface{} + err = json.Unmarshal([]byte(TestUpdateUserOptsRaw), &expectedRequest) + if err != nil { + t.Errorf("unable to unmarshal expected raw response: %v", err) + } + + if !reflect.DeepEqual(actualRequest, expectedRequest) { + t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) + } + }) + + ctx := context.Background() + updateOpts := TestUpdateUserOpts + actualResponse, httpResponse, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + + if actualResponse != nil { + t.Fatal("expected no actualResponse 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 TestDeleteUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -184,3 +313,27 @@ func TestDeleteUser(t *testing.T) { t.Fatal(err) } } + +func TestDeleteUserError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusBadGateway) + + if r.Method != http.MethodDelete { + t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) + } + }) + + ctx := context.Background() + httpResponse, err := users.Delete(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f") + + 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) + } +} From a938cbecd1f083b0b82558648cb2248551d7d7d8 Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Wed, 18 Apr 2018 15:50:36 +0300 Subject: [PATCH 2/3] Resell V2 Users - add timeout error tests Add more tests. --- .../resell/v2/users/testing/requests_test.go | 75 ++++++++++++++++--- 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/selvpcclient/resell/v2/users/testing/requests_test.go b/selvpcclient/resell/v2/users/testing/requests_test.go index d48084c..69d0b85 100644 --- a/selvpcclient/resell/v2/users/testing/requests_test.go +++ b/selvpcclient/resell/v2/users/testing/requests_test.go @@ -72,7 +72,7 @@ func TestListUsersSingle(t *testing.T) { } } -func TestListUsersError(t *testing.T) { +func TestListUsersHTTPError(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() @@ -86,9 +86,9 @@ func TestListUsersError(t *testing.T) { }) ctx := context.Background() - users, httpResponse, err := users.List(ctx, testEnv.Client) + allUsers, httpResponse, err := users.List(ctx, testEnv.Client) - if users != nil { + if allUsers != nil { t.Fatal("expected no users from the List method") } if err == nil { @@ -99,6 +99,23 @@ func TestListUsersError(t *testing.T) { } } +func TestListUsersTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + allUsers, _, err := users.List(ctx, testEnv.Client) + + if allUsers != nil { + t.Fatal("expected no users from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } +} + func TestCreateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -148,7 +165,7 @@ func TestCreateUser(t *testing.T) { } } -func TestCreateUserError(t *testing.T) { +func TestCreateUserHTTPError(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() @@ -184,10 +201,10 @@ func TestCreateUserError(t *testing.T) { ctx := context.Background() createOpts := TestCreateUserOpts - actualResponse, httpResponse, err := users.Create(ctx, testEnv.Client, createOpts) + user, httpResponse, err := users.Create(ctx, testEnv.Client, createOpts) - if actualResponse != nil { - t.Fatal("expected no actualResponse from the Create method") + if user != nil { + t.Fatal("expected no user from the Create method") } if err == nil { t.Fatal("expected error from the Create method") @@ -197,6 +214,24 @@ func TestCreateUserError(t *testing.T) { } } +func TestCreateUserTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + createOpts := TestCreateUserOpts + user, _, err := users.Create(ctx, testEnv.Client, createOpts) + + if user != nil { + t.Fatal("expected no users from the Create method") + } + if err == nil { + t.Fatal("expected error from the Create method") + } +} + func TestUpdateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -246,7 +281,7 @@ func TestUpdateUser(t *testing.T) { } } -func TestUpdateUserError(t *testing.T) { +func TestUpdateUserHTTPError(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() @@ -282,10 +317,10 @@ func TestUpdateUserError(t *testing.T) { ctx := context.Background() updateOpts := TestUpdateUserOpts - actualResponse, httpResponse, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + user, httpResponse, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) - if actualResponse != nil { - t.Fatal("expected no actualResponse from the Update method") + if user != nil { + t.Fatal("expected no user from the Update method") } if err == nil { t.Fatal("expected error from the Update method") @@ -295,6 +330,24 @@ func TestUpdateUserError(t *testing.T) { } } +func TestUpdateUserTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + updateOpts := TestUpdateUserOpts + user, _, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + + if user != nil { + t.Fatal("expected no users from the Create method") + } + if err == nil { + t.Fatal("expected error from the Create method") + } +} + func TestDeleteUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() From dac75d28b0032df40b806b914a8560624a5d4e7c Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Wed, 18 Apr 2018 16:06:10 +0300 Subject: [PATCH 3/3] Resell V2 Users - add err tests for unmarshall Add more tests with errors. --- .../resell/v2/users/testing/fixtures.go | 22 +++ .../resell/v2/users/testing/requests_test.go | 139 +++++++++++++++++- 2 files changed, 158 insertions(+), 3 deletions(-) diff --git a/selvpcclient/resell/v2/users/testing/fixtures.go b/selvpcclient/resell/v2/users/testing/fixtures.go index 9c414f3..e823d03 100644 --- a/selvpcclient/resell/v2/users/testing/fixtures.go +++ b/selvpcclient/resell/v2/users/testing/fixtures.go @@ -109,3 +109,25 @@ var TestUpdateUserResponse = &users.User{ Name: "UpdatedUser1", Enabled: true, } + +// TestManyUsersInvalidResponseRaw represents a raw invalid response with several users. +const TestManyUsersInvalidResponseRaw = ` +{ + "users": [ + { + "id": 222 + } + ] +} +` + +// TestSingleUserInvalidResponseRaw represents a raw invalid response with a single user. +const TestSingleUserInvalidResponseRaw = ` +{ + "user": [ + { + "id": 222 + } + ] +} +` diff --git a/selvpcclient/resell/v2/users/testing/requests_test.go b/selvpcclient/resell/v2/users/testing/requests_test.go index 69d0b85..d179fff 100644 --- a/selvpcclient/resell/v2/users/testing/requests_test.go +++ b/selvpcclient/resell/v2/users/testing/requests_test.go @@ -116,6 +116,31 @@ func TestListUsersTimeoutError(t *testing.T) { } } +func TestListUsersUnmarshallError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, TestManyUsersInvalidResponseRaw) + + if r.Method != http.MethodGet { + t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) + } + }) + + ctx := context.Background() + allUsers, _, err := users.List(ctx, testEnv.Client) + + if allUsers != nil { + t.Fatal("expected no users from the List method") + } + if err == nil { + t.Fatal("expected error from the List method") + } +} + func TestCreateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -232,6 +257,53 @@ func TestCreateUserTimeoutError(t *testing.T) { } } +func TestCreateUserUnmarshallError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, TestSingleUserInvalidResponseRaw) + + if r.Method != http.MethodPost { + t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("unable to read the request body: %v", err) + } + + var actualRequest interface{} + err = json.Unmarshal(b, &actualRequest) + if err != nil { + t.Errorf("unable to unmarshal the request body: %v", err) + } + + var expectedRequest interface{} + err = json.Unmarshal([]byte(TestCreateUserOptsRaw), &expectedRequest) + if err != nil { + t.Errorf("unable to unmarshal expected raw response: %v", err) + } + + if !reflect.DeepEqual(actualRequest, expectedRequest) { + t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) + } + }) + + ctx := context.Background() + createOpts := TestCreateUserOpts + user, _, err := users.Create(ctx, testEnv.Client, createOpts) + + if user != nil { + t.Fatal("expected no user from the Create method") + } + if err == nil { + t.Fatal("expected error from the Create method") + } +} + func TestUpdateUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -341,10 +413,57 @@ func TestUpdateUserTimeoutError(t *testing.T) { user, _, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) if user != nil { - t.Fatal("expected no users from the Create method") + t.Fatal("expected no users from the Update method") } if err == nil { - t.Fatal("expected error from the Create method") + t.Fatal("expected error from the Update method") + } +} + +func TestUpdateUserUnmarshallError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, TestSingleUserInvalidResponseRaw) + + if r.Method != http.MethodPatch { + t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("unable to read the request body: %v", err) + } + + var actualRequest interface{} + err = json.Unmarshal(b, &actualRequest) + if err != nil { + t.Errorf("unable to unmarshal the request body: %v", err) + } + + var expectedRequest interface{} + err = json.Unmarshal([]byte(TestUpdateUserOptsRaw), &expectedRequest) + if err != nil { + t.Errorf("unable to unmarshal expected raw response: %v", err) + } + + if !reflect.DeepEqual(actualRequest, expectedRequest) { + t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) + } + }) + + ctx := context.Background() + updateOpts := TestUpdateUserOpts + user, _, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + + if user != nil { + t.Fatal("expected no user from the Update method") + } + if err == nil { + t.Fatal("expected error from the Update method") } } @@ -367,7 +486,7 @@ func TestDeleteUser(t *testing.T) { } } -func TestDeleteUserError(t *testing.T) { +func TestDeleteUserHTTPError(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() @@ -390,3 +509,17 @@ func TestDeleteUserError(t *testing.T) { t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode) } } + +func TestDeleteUserTimeoutError(t *testing.T) { + testEnv := testutils.SetupTestEnv() + testEnv.Server.Close() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + + ctx := context.Background() + _, err := users.Delete(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f") + + if err == nil { + t.Fatal("expected error from the Delete method") + } +}