@@ -2,15 +2,15 @@ package integration
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
"net/http"
6
7
"net/http/httptest"
7
8
"testing"
8
9
9
10
"github.com/gorilla/mux"
10
11
"github.com/replicatedhq/embedded-cluster/api"
11
12
"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"
14
14
"github.com/stretchr/testify/assert"
15
15
"github.com/stretchr/testify/require"
16
16
)
@@ -24,18 +24,9 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
24
24
)
25
25
require .NoError (t , err )
26
26
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
-
35
27
// Create the API with the install controller
36
28
apiInstance , err := api .New (
37
29
"password" ,
38
- api .WithInstallController (installController ),
39
30
api .WithConsoleController (consoleController ),
40
31
api .WithAuthController (& staticAuthController {"TOKEN" }),
41
32
api .WithLogger (api .NewDiscardLogger ()),
@@ -68,3 +59,84 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
68
59
// Verify the response contains the expected network interfaces
69
60
assert .Equal (t , []string {"eth0" , "eth1" }, response .NetworkInterfaces )
70
61
}
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
+ }
0 commit comments