diff --git a/api/integration/console_test.go b/api/integration/console_test.go index eb67a5275..ada08b522 100644 --- a/api/integration/console_test.go +++ b/api/integration/console_test.go @@ -2,6 +2,7 @@ package integration import ( "encoding/json" + "errors" "net/http" "net/http/httptest" "testing" @@ -9,8 +10,7 @@ import ( "github.com/gorilla/mux" "github.com/replicatedhq/embedded-cluster/api" "github.com/replicatedhq/embedded-cluster/api/controllers/console" - "github.com/replicatedhq/embedded-cluster/api/controllers/install" - "github.com/replicatedhq/embedded-cluster/api/pkg/installation" + "github.com/replicatedhq/embedded-cluster/api/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,18 +24,9 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) { ) require.NoError(t, err) - // Create an install controller - installController, err := install.NewInstallController( - install.WithInstallationManager(installation.NewInstallationManager( - installation.WithNetUtils(netutils), - )), - ) - require.NoError(t, err) - // Create the API with the install controller apiInstance, err := api.New( "password", - api.WithInstallController(installController), api.WithConsoleController(consoleController), api.WithAuthController(&staticAuthController{"TOKEN"}), api.WithLogger(api.NewDiscardLogger()), @@ -68,3 +59,84 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) { // Verify the response contains the expected network interfaces assert.Equal(t, []string{"eth0", "eth1"}, response.NetworkInterfaces) } + +func TestConsoleListAvailableNetworkInterfacesUnauthorized(t *testing.T) { + netutils := &mockNetUtils{ifaces: []string{"eth0", "eth1"}} + + // Create a console controller + consoleController, err := console.NewConsoleController( + console.WithNetUtils(netutils), + ) + require.NoError(t, err) + + // Create the API with the install controller + apiInstance, err := api.New( + "password", + api.WithConsoleController(consoleController), + api.WithAuthController(&staticAuthController{"VALID_TOKEN"}), + api.WithLogger(api.NewDiscardLogger()), + ) + require.NoError(t, err) + + // Create a router and register the API routes + router := mux.NewRouter() + apiInstance.RegisterRoutes(router) + + // Create a request with an invalid token + req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil) + req.Header.Set("Authorization", "Bearer INVALID_TOKEN") + rec := httptest.NewRecorder() + + // Serve the request + router.ServeHTTP(rec, req) + + // Check the response is unauthorized + assert.Equal(t, http.StatusUnauthorized, rec.Code) + assert.Equal(t, "application/json", rec.Header().Get("Content-Type")) + // Check that the API response is of type APIError + var apiErr *types.APIError + err = json.NewDecoder(rec.Body).Decode(&apiErr) + require.NoError(t, err) + assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode) +} + +func TestConsoleListAvailableNetworkInterfacesError(t *testing.T) { + // Create a mock that returns an error + netutils := &mockNetUtils{err: errors.New("failed to list network interfaces")} + + // Create a console controller + consoleController, err := console.NewConsoleController( + console.WithNetUtils(netutils), + ) + require.NoError(t, err) + + // Create the API with the install controller + apiInstance, err := api.New( + "password", + api.WithConsoleController(consoleController), + api.WithAuthController(&staticAuthController{"TOKEN"}), + api.WithLogger(api.NewDiscardLogger()), + ) + require.NoError(t, err) + + // Create a router and register the API routes + router := mux.NewRouter() + apiInstance.RegisterRoutes(router) + + // Create a request to the network interfaces endpoint + req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil) + req.Header.Set("Authorization", "Bearer TOKEN") + rec := httptest.NewRecorder() + + // Serve the request + router.ServeHTTP(rec, req) + + // Check the response + assert.Equal(t, http.StatusInternalServerError, rec.Code) + assert.Equal(t, "application/json", rec.Header().Get("Content-Type")) + // Check that the API response is of type APIError + var apiErr *types.APIError + err = json.NewDecoder(rec.Body).Decode(&apiErr) + require.NoError(t, err) + assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode) +} diff --git a/api/integration/netutils.go b/api/integration/netutils.go index a44e70314..4ba355bee 100644 --- a/api/integration/netutils.go +++ b/api/integration/netutils.go @@ -5,13 +5,20 @@ import "github.com/replicatedhq/embedded-cluster/api/pkg/utils" var _ utils.NetUtils = &mockNetUtils{} type mockNetUtils struct { + err error ifaces []string } func (m *mockNetUtils) ListValidNetworkInterfaces() ([]string, error) { + if m.err != nil { + return nil, m.err + } return m.ifaces, nil } func (m *mockNetUtils) DetermineBestNetworkInterface() (string, error) { + if m.err != nil { + return "", m.err + } return m.ifaces[0], nil }