Skip to content

Commit 3254941

Browse files
authored
chore: add tests to the console API (#2204)
1 parent 4437369 commit 3254941

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

api/integration/console_test.go

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package integration
22

33
import (
44
"encoding/json"
5+
"errors"
56
"net/http"
67
"net/http/httptest"
78
"testing"
89

910
"github.com/gorilla/mux"
1011
"github.com/replicatedhq/embedded-cluster/api"
1112
"github.com/replicatedhq/embedded-cluster/api/controllers/console"
12-
"github.com/replicatedhq/embedded-cluster/api/controllers/install"
13-
"github.com/replicatedhq/embedded-cluster/api/pkg/installation"
13+
"github.com/replicatedhq/embedded-cluster/api/types"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616
)
@@ -24,18 +24,9 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
2424
)
2525
require.NoError(t, err)
2626

27-
// Create an install controller
28-
installController, err := install.NewInstallController(
29-
install.WithInstallationManager(installation.NewInstallationManager(
30-
installation.WithNetUtils(netutils),
31-
)),
32-
)
33-
require.NoError(t, err)
34-
3527
// Create the API with the install controller
3628
apiInstance, err := api.New(
3729
"password",
38-
api.WithInstallController(installController),
3930
api.WithConsoleController(consoleController),
4031
api.WithAuthController(&staticAuthController{"TOKEN"}),
4132
api.WithLogger(api.NewDiscardLogger()),
@@ -68,3 +59,84 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
6859
// Verify the response contains the expected network interfaces
6960
assert.Equal(t, []string{"eth0", "eth1"}, response.NetworkInterfaces)
7061
}
62+
63+
func TestConsoleListAvailableNetworkInterfacesUnauthorized(t *testing.T) {
64+
netutils := &mockNetUtils{ifaces: []string{"eth0", "eth1"}}
65+
66+
// Create a console controller
67+
consoleController, err := console.NewConsoleController(
68+
console.WithNetUtils(netutils),
69+
)
70+
require.NoError(t, err)
71+
72+
// Create the API with the install controller
73+
apiInstance, err := api.New(
74+
"password",
75+
api.WithConsoleController(consoleController),
76+
api.WithAuthController(&staticAuthController{"VALID_TOKEN"}),
77+
api.WithLogger(api.NewDiscardLogger()),
78+
)
79+
require.NoError(t, err)
80+
81+
// Create a router and register the API routes
82+
router := mux.NewRouter()
83+
apiInstance.RegisterRoutes(router)
84+
85+
// Create a request with an invalid token
86+
req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil)
87+
req.Header.Set("Authorization", "Bearer INVALID_TOKEN")
88+
rec := httptest.NewRecorder()
89+
90+
// Serve the request
91+
router.ServeHTTP(rec, req)
92+
93+
// Check the response is unauthorized
94+
assert.Equal(t, http.StatusUnauthorized, rec.Code)
95+
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
96+
// Check that the API response is of type APIError
97+
var apiErr *types.APIError
98+
err = json.NewDecoder(rec.Body).Decode(&apiErr)
99+
require.NoError(t, err)
100+
assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode)
101+
}
102+
103+
func TestConsoleListAvailableNetworkInterfacesError(t *testing.T) {
104+
// Create a mock that returns an error
105+
netutils := &mockNetUtils{err: errors.New("failed to list network interfaces")}
106+
107+
// Create a console controller
108+
consoleController, err := console.NewConsoleController(
109+
console.WithNetUtils(netutils),
110+
)
111+
require.NoError(t, err)
112+
113+
// Create the API with the install controller
114+
apiInstance, err := api.New(
115+
"password",
116+
api.WithConsoleController(consoleController),
117+
api.WithAuthController(&staticAuthController{"TOKEN"}),
118+
api.WithLogger(api.NewDiscardLogger()),
119+
)
120+
require.NoError(t, err)
121+
122+
// Create a router and register the API routes
123+
router := mux.NewRouter()
124+
apiInstance.RegisterRoutes(router)
125+
126+
// Create a request to the network interfaces endpoint
127+
req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil)
128+
req.Header.Set("Authorization", "Bearer TOKEN")
129+
rec := httptest.NewRecorder()
130+
131+
// Serve the request
132+
router.ServeHTTP(rec, req)
133+
134+
// Check the response
135+
assert.Equal(t, http.StatusInternalServerError, rec.Code)
136+
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
137+
// Check that the API response is of type APIError
138+
var apiErr *types.APIError
139+
err = json.NewDecoder(rec.Body).Decode(&apiErr)
140+
require.NoError(t, err)
141+
assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode)
142+
}

api/integration/netutils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import "github.com/replicatedhq/embedded-cluster/api/pkg/utils"
55
var _ utils.NetUtils = &mockNetUtils{}
66

77
type mockNetUtils struct {
8+
err error
89
ifaces []string
910
}
1011

1112
func (m *mockNetUtils) ListValidNetworkInterfaces() ([]string, error) {
13+
if m.err != nil {
14+
return nil, m.err
15+
}
1216
return m.ifaces, nil
1317
}
1418

1519
func (m *mockNetUtils) DetermineBestNetworkInterface() (string, error) {
20+
if m.err != nil {
21+
return "", m.err
22+
}
1623
return m.ifaces[0], nil
1724
}

0 commit comments

Comments
 (0)