|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/replicatedhq/embedded-cluster/api/types" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestNew(t *testing.T) { |
| 17 | + // Test default client creation |
| 18 | + c := New("http://example.com") |
| 19 | + clientImpl, ok := c.(*client) |
| 20 | + assert.True(t, ok, "Expected c to be of type *client") |
| 21 | + assert.Equal(t, "http://example.com", clientImpl.apiURL) |
| 22 | + assert.Equal(t, defaultHTTPClient, clientImpl.httpClient) |
| 23 | + assert.Empty(t, clientImpl.token) |
| 24 | + |
| 25 | + // Test with custom HTTP client |
| 26 | + customHTTPClient := &http.Client{} |
| 27 | + c = New("http://example.com", WithHTTPClient(customHTTPClient)) |
| 28 | + clientImpl, ok = c.(*client) |
| 29 | + assert.True(t, ok, "Expected c to be of type *client") |
| 30 | + assert.Equal(t, customHTTPClient, clientImpl.httpClient) |
| 31 | + |
| 32 | + // Test with token |
| 33 | + c = New("http://example.com", WithToken("test-token")) |
| 34 | + clientImpl, ok = c.(*client) |
| 35 | + assert.True(t, ok, "Expected c to be of type *client") |
| 36 | + assert.Equal(t, "test-token", clientImpl.token) |
| 37 | + |
| 38 | + // Test with multiple options |
| 39 | + c = New("http://example.com", WithHTTPClient(customHTTPClient), WithToken("test-token")) |
| 40 | + clientImpl, ok = c.(*client) |
| 41 | + assert.True(t, ok, "Expected c to be of type *client") |
| 42 | + assert.Equal(t, customHTTPClient, clientImpl.httpClient) |
| 43 | + assert.Equal(t, "test-token", clientImpl.token) |
| 44 | +} |
| 45 | + |
| 46 | +func TestLogin(t *testing.T) { |
| 47 | + // Create a test server |
| 48 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 49 | + assert.Equal(t, "POST", r.Method) |
| 50 | + assert.Equal(t, "/api/auth/login", r.URL.Path) |
| 51 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 52 | + |
| 53 | + // Decode request body |
| 54 | + var loginReq struct { |
| 55 | + Password string `json:"password"` |
| 56 | + } |
| 57 | + err := json.NewDecoder(r.Body).Decode(&loginReq) |
| 58 | + require.NoError(t, err, "Failed to decode request body") |
| 59 | + |
| 60 | + // Check password |
| 61 | + if loginReq.Password == "correct-password" { |
| 62 | + // Return successful response |
| 63 | + w.WriteHeader(http.StatusOK) |
| 64 | + json.NewEncoder(w).Encode(struct { |
| 65 | + Token string `json:"token"` |
| 66 | + }{ |
| 67 | + Token: "test-token", |
| 68 | + }) |
| 69 | + } else { |
| 70 | + // Return error response |
| 71 | + w.WriteHeader(http.StatusUnauthorized) |
| 72 | + json.NewEncoder(w).Encode(types.APIError{ |
| 73 | + StatusCode: http.StatusUnauthorized, |
| 74 | + Message: "Invalid password", |
| 75 | + }) |
| 76 | + } |
| 77 | + })) |
| 78 | + defer server.Close() |
| 79 | + |
| 80 | + // Test successful login |
| 81 | + c := New(server.URL) |
| 82 | + err := c.Login("correct-password") |
| 83 | + assert.NoError(t, err) |
| 84 | + |
| 85 | + // Check that token was set |
| 86 | + clientImpl, ok := c.(*client) |
| 87 | + require.True(t, ok, "Expected c to be of type *client") |
| 88 | + assert.Equal(t, "test-token", clientImpl.token) |
| 89 | + |
| 90 | + // Test failed login |
| 91 | + c = New(server.URL) |
| 92 | + err = c.Login("wrong-password") |
| 93 | + assert.Error(t, err) |
| 94 | + |
| 95 | + // Check that error is of type APIError |
| 96 | + apiErr, ok := err.(*types.APIError) |
| 97 | + require.True(t, ok, "Expected err to be of type *types.APIError") |
| 98 | + assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode) |
| 99 | + assert.Equal(t, "Invalid password", apiErr.Message) |
| 100 | +} |
| 101 | + |
| 102 | +func TestGetInstall(t *testing.T) { |
| 103 | + // Create a test server |
| 104 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 105 | + assert.Equal(t, "GET", r.Method) |
| 106 | + assert.Equal(t, "/api/install", r.URL.Path) |
| 107 | + |
| 108 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 109 | + assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization")) |
| 110 | + |
| 111 | + // Return successful response |
| 112 | + w.WriteHeader(http.StatusOK) |
| 113 | + json.NewEncoder(w).Encode(types.Install{ |
| 114 | + Config: types.InstallationConfig{ |
| 115 | + GlobalCIDR: "10.0.0.0/24", |
| 116 | + AdminConsolePort: 8080, |
| 117 | + }, |
| 118 | + }) |
| 119 | + })) |
| 120 | + defer server.Close() |
| 121 | + |
| 122 | + // Test successful get |
| 123 | + c := New(server.URL, WithToken("test-token")) |
| 124 | + install, err := c.GetInstall() |
| 125 | + assert.NoError(t, err) |
| 126 | + assert.NotNil(t, install) |
| 127 | + assert.Equal(t, "10.0.0.0/24", install.Config.GlobalCIDR) |
| 128 | + assert.Equal(t, 8080, install.Config.AdminConsolePort) |
| 129 | + |
| 130 | + // Test error response |
| 131 | + errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 | + w.WriteHeader(http.StatusInternalServerError) |
| 133 | + json.NewEncoder(w).Encode(types.APIError{ |
| 134 | + StatusCode: http.StatusInternalServerError, |
| 135 | + Message: "Internal Server Error", |
| 136 | + }) |
| 137 | + })) |
| 138 | + defer errorServer.Close() |
| 139 | + |
| 140 | + c = New(errorServer.URL, WithToken("test-token")) |
| 141 | + install, err = c.GetInstall() |
| 142 | + assert.Error(t, err) |
| 143 | + assert.Nil(t, install) |
| 144 | + |
| 145 | + apiErr, ok := err.(*types.APIError) |
| 146 | + require.True(t, ok, "Expected err to be of type *types.APIError") |
| 147 | + assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode) |
| 148 | + assert.Equal(t, "Internal Server Error", apiErr.Message) |
| 149 | +} |
| 150 | + |
| 151 | +func TestSetInstallConfig(t *testing.T) { |
| 152 | + // Create a test server |
| 153 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 154 | + // Check request method and path |
| 155 | + assert.Equal(t, "POST", r.Method) // Corrected from PUT to POST based on implementation |
| 156 | + assert.Equal(t, "/api/install/config", r.URL.Path) |
| 157 | + |
| 158 | + // Check headers |
| 159 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 160 | + assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization")) |
| 161 | + |
| 162 | + // Decode request body |
| 163 | + var config types.InstallationConfig |
| 164 | + err := json.NewDecoder(r.Body).Decode(&config) |
| 165 | + require.NoError(t, err, "Failed to decode request body") |
| 166 | + |
| 167 | + // Return successful response |
| 168 | + w.WriteHeader(http.StatusOK) |
| 169 | + json.NewEncoder(w).Encode(types.Install{ |
| 170 | + Config: config, |
| 171 | + }) |
| 172 | + })) |
| 173 | + defer server.Close() |
| 174 | + |
| 175 | + // Test successful set |
| 176 | + c := New(server.URL, WithToken("test-token")) |
| 177 | + config := types.InstallationConfig{ |
| 178 | + GlobalCIDR: "20.0.0.0/24", |
| 179 | + LocalArtifactMirrorPort: 9081, |
| 180 | + } |
| 181 | + install, err := c.SetInstallConfig(config) |
| 182 | + assert.NoError(t, err) |
| 183 | + assert.NotNil(t, install) |
| 184 | + assert.Equal(t, config, install.Config) |
| 185 | + |
| 186 | + // Test error response |
| 187 | + errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 188 | + w.WriteHeader(http.StatusBadRequest) |
| 189 | + json.NewEncoder(w).Encode(types.APIError{ |
| 190 | + StatusCode: http.StatusBadRequest, |
| 191 | + Message: "Bad Request", |
| 192 | + }) |
| 193 | + })) |
| 194 | + defer errorServer.Close() |
| 195 | + |
| 196 | + c = New(errorServer.URL, WithToken("test-token")) |
| 197 | + install, err = c.SetInstallConfig(config) |
| 198 | + assert.Error(t, err) |
| 199 | + assert.Nil(t, install) |
| 200 | + |
| 201 | + apiErr, ok := err.(*types.APIError) |
| 202 | + require.True(t, ok, "Expected err to be of type *types.APIError") |
| 203 | + assert.Equal(t, http.StatusBadRequest, apiErr.StatusCode) |
| 204 | + assert.Equal(t, "Bad Request", apiErr.Message) |
| 205 | +} |
| 206 | + |
| 207 | +func TestSetInstallStatus(t *testing.T) { |
| 208 | + // Create a test server |
| 209 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 210 | + assert.Equal(t, "POST", r.Method) |
| 211 | + assert.Equal(t, "/api/install/status", r.URL.Path) |
| 212 | + |
| 213 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 214 | + assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization")) |
| 215 | + |
| 216 | + // Decode request body |
| 217 | + var status types.InstallationStatus |
| 218 | + err := json.NewDecoder(r.Body).Decode(&status) |
| 219 | + require.NoError(t, err, "Failed to decode request body") |
| 220 | + |
| 221 | + // Return successful response |
| 222 | + w.WriteHeader(http.StatusOK) |
| 223 | + json.NewEncoder(w).Encode(types.Install{ |
| 224 | + Status: status, |
| 225 | + }) |
| 226 | + })) |
| 227 | + defer server.Close() |
| 228 | + |
| 229 | + // Test successful set |
| 230 | + c := New(server.URL, WithToken("test-token")) |
| 231 | + status := types.InstallationStatus{ |
| 232 | + State: types.InstallationStateSucceeded, |
| 233 | + Description: "Installation successful", |
| 234 | + } |
| 235 | + install, err := c.SetInstallStatus(status) |
| 236 | + assert.NoError(t, err) |
| 237 | + assert.NotNil(t, install) |
| 238 | + assert.Equal(t, status, install.Status) |
| 239 | + |
| 240 | + // Test error response |
| 241 | + errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 242 | + w.WriteHeader(http.StatusBadRequest) |
| 243 | + json.NewEncoder(w).Encode(types.APIError{ |
| 244 | + StatusCode: http.StatusBadRequest, |
| 245 | + Message: "Bad Request", |
| 246 | + }) |
| 247 | + })) |
| 248 | + defer errorServer.Close() |
| 249 | + |
| 250 | + c = New(errorServer.URL, WithToken("test-token")) |
| 251 | + install, err = c.SetInstallStatus(status) |
| 252 | + assert.Error(t, err) |
| 253 | + assert.Nil(t, install) |
| 254 | + |
| 255 | + apiErr, ok := err.(*types.APIError) |
| 256 | + require.True(t, ok, "Expected err to be of type *types.APIError") |
| 257 | + assert.Equal(t, http.StatusBadRequest, apiErr.StatusCode) |
| 258 | + assert.Equal(t, "Bad Request", apiErr.Message) |
| 259 | +} |
| 260 | + |
| 261 | +func TestErrorFromResponse(t *testing.T) { |
| 262 | + // Create a response with an error |
| 263 | + resp := &http.Response{ |
| 264 | + StatusCode: http.StatusBadRequest, |
| 265 | + Body: io.NopCloser(bytes.NewBufferString(`{"status_code": 400, "message": "Bad Request"}`)), |
| 266 | + } |
| 267 | + |
| 268 | + err := errorFromResponse(resp) |
| 269 | + assert.Error(t, err) |
| 270 | + |
| 271 | + // Check that error is of type APIError |
| 272 | + apiErr, ok := err.(*types.APIError) |
| 273 | + require.True(t, ok, "Expected err to be of type *types.APIError") |
| 274 | + assert.Equal(t, http.StatusBadRequest, apiErr.StatusCode) |
| 275 | + assert.Equal(t, "Bad Request", apiErr.Message) |
| 276 | + |
| 277 | + // Test with malformed JSON |
| 278 | + resp = &http.Response{ |
| 279 | + StatusCode: http.StatusBadRequest, |
| 280 | + Body: io.NopCloser(bytes.NewBufferString(`not a json`)), |
| 281 | + } |
| 282 | + |
| 283 | + err = errorFromResponse(resp) |
| 284 | + assert.Error(t, err) |
| 285 | + assert.Contains(t, err.Error(), "unexpected response") |
| 286 | +} |
| 287 | + |
0 commit comments