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 e1957e0..d179fff 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,82 @@ func TestListUsersSingle(t *testing.T) { } } +func TestListUsersHTTPError(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() + allUsers, httpResponse, 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") + } + if httpResponse.StatusCode != http.StatusBadGateway { + t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadGateway, httpResponse.StatusCode) + } +} + +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 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() 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 +190,127 @@ func TestCreateUser(t *testing.T) { } } +func TestCreateUserHTTPError(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 + user, httpResponse, 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") + } + if httpResponse.StatusCode != http.StatusBadRequest { + t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode) + } +} + +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 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() 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 +353,120 @@ func TestUpdateUser(t *testing.T) { } } +func TestUpdateUserHTTPError(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 + user, httpResponse, 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") + } + if httpResponse.StatusCode != http.StatusBadRequest { + t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode) + } +} + +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 Update method") + } + if err == nil { + 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") + } +} + func TestDeleteUser(t *testing.T) { testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() @@ -184,3 +485,41 @@ func TestDeleteUser(t *testing.T) { t.Fatal(err) } } + +func TestDeleteUserHTTPError(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) + } +} + +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") + } +}