|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + stderrors "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + breverrors "github.com/brevdev/brev-cli/pkg/errors" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_hostFromURL(t *testing.T) { |
| 15 | + cases := []struct { |
| 16 | + in string |
| 17 | + want string |
| 18 | + }{ |
| 19 | + {"https://api.ngc.nvidia.com", "api.ngc.nvidia.com"}, |
| 20 | + {"https://api.ngc.nvidia.com/token", "api.ngc.nvidia.com"}, |
| 21 | + {"http://localhost:8080", "localhost:8080"}, |
| 22 | + {"", ""}, |
| 23 | + {"://nonsense", ""}, |
| 24 | + } |
| 25 | + for _, tc := range cases { |
| 26 | + t.Run(tc.in, func(t *testing.T) { |
| 27 | + assert.Equal(t, tc.want, hostFromURL(tc.in)) |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// closedServerURL returns a URL that is guaranteed to fail to connect: an |
| 33 | +// httptest.Server that has already been Close()d. |
| 34 | +func closedServerURL(t *testing.T) string { |
| 35 | + t.Helper() |
| 36 | + server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) |
| 37 | + server.Close() |
| 38 | + return server.URL |
| 39 | +} |
| 40 | + |
| 41 | +// MakeLoginCall against an unreachable host should surface a friendly |
| 42 | +// *NetworkError instead of the raw transport error, and the host should |
| 43 | +// match the BaseURL we tried to reach. |
| 44 | +func TestMakeLoginCall_NetworkError(t *testing.T) { |
| 45 | + baseURL := closedServerURL(t) |
| 46 | + auth := KasAuthenticator{BaseURL: baseURL} |
| 47 | + |
| 48 | + _, err := auth.MakeLoginCall("device-id", "user@example.com") |
| 49 | + if !assert.Error(t, err) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + var netErr *breverrors.NetworkError |
| 54 | + if !assert.True(t, stderrors.As(err, &netErr), "expected *NetworkError, got %T: %v", err, err) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + expectedHost, _ := url.Parse(baseURL) |
| 59 | + assert.Equal(t, expectedHost.Host, netErr.Host) |
| 60 | + assert.Contains(t, netErr.Error(), "internet connection") |
| 61 | +} |
| 62 | + |
| 63 | +// retrieveIDToken against an unreachable host should surface a friendly |
| 64 | +// *NetworkError. This is the path hit by `brev shell` when the cached |
| 65 | +// access token has expired and api.ngc.nvidia.com is unreachable. |
| 66 | +func TestRetrieveIDToken_NetworkError(t *testing.T) { |
| 67 | + baseURL := closedServerURL(t) |
| 68 | + auth := KasAuthenticator{BaseURL: baseURL} |
| 69 | + |
| 70 | + _, err := auth.retrieveIDToken("session-key", "device-id") |
| 71 | + if !assert.Error(t, err) { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + var netErr *breverrors.NetworkError |
| 76 | + if !assert.True(t, stderrors.As(err, &netErr), "expected *NetworkError, got %T: %v", err, err) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + expectedHost, _ := url.Parse(baseURL) |
| 81 | + assert.Equal(t, expectedHost.Host, netErr.Host) |
| 82 | +} |
| 83 | + |
| 84 | +// retrieveIDToken against a server that returns HTTP 4xx/5xx (i.e. a |
| 85 | +// non-network error) should NOT be classified as a network error. |
| 86 | +func TestRetrieveIDToken_NonNetworkErrorUnchanged(t *testing.T) { |
| 87 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 88 | + w.WriteHeader(http.StatusInternalServerError) |
| 89 | + _, _ = w.Write([]byte(`{"requestStatus":{"statusCode":"INTERNAL_ERROR"}}`)) |
| 90 | + })) |
| 91 | + defer server.Close() |
| 92 | + |
| 93 | + auth := KasAuthenticator{BaseURL: server.URL} |
| 94 | + _, err := auth.retrieveIDToken("session-key", "device-id") |
| 95 | + if !assert.Error(t, err) { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + var netErr *breverrors.NetworkError |
| 100 | + assert.False(t, stderrors.As(err, &netErr), "HTTP error should not be classified as a network error: %v", err) |
| 101 | + assert.Contains(t, err.Error(), "status code: 500") |
| 102 | +} |
0 commit comments