File
Blob: gateway/overview_test.go
| 1 | package gateway |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "regexp" |
| 7 | "testing" |
| 8 | |
| 9 | "go.miragespace.co/specter/ui" |
| 10 | |
| 11 | "github.com/go-chi/chi/v5" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestOperatorOverviewUsesInternalAuthentication(t *testing.T) { |
| 16 | page := httptest.NewRecorder() |
| 17 | ui.OperatorPage().ServeHTTP(page, httptest.NewRequest(http.MethodGet, "/", nil)) |
| 18 | asset := regexp.MustCompile(`src="([^"]+)"`).FindStringSubmatch(page.Body.String()) |
| 19 | require.Len(t, asset, 2) |
| 20 | a := &apexServer{ |
| 21 | authUser: testUser, |
| 22 | authPass: testPass, |
| 23 | limiter: func(h http.Handler) http.Handler { return h }, |
| 24 | internalProxy: func(h http.Handler) http.Handler { return h }, |
| 25 | handlers: InternalHandlers{ |
| 26 | TunnelServer: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 27 | w.Write([]byte("client data")) |
| 28 | }), |
| 29 | Overview: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 30 | w.Write([]byte("local overview")) |
| 31 | }), |
| 32 | }, |
| 33 | } |
| 34 | router := chi.NewRouter() |
| 35 | a.Mount(router) |
| 36 | |
| 37 | for _, testCase := range []struct { |
| 38 | path string |
| 39 | content string |
| 40 | }{ |
| 41 | {"/_internal", "Specter · Operator"}, |
| 42 | {"/_internal/", "Specter · Operator"}, |
| 43 | {"/_internal/overview.json", "local overview"}, |
| 44 | {asset[1], ""}, |
| 45 | {"/_internal/tun/", "Specter · Operator"}, |
| 46 | {"/_internal/tun/123/client", "Specter · Operator"}, |
| 47 | {"/_internal/api/tun/", "client data"}, |
| 48 | } { |
| 49 | t.Run(testCase.path, func(t *testing.T) { |
| 50 | for _, authenticated := range []bool{false, true} { |
| 51 | |
| 52 | r := httptest.NewRequest(http.MethodGet, testCase.path, nil) |
| 53 | if authenticated { |
| 54 | r.SetBasicAuth(testUser, testPass) |
| 55 | } |
| 56 | w := httptest.NewRecorder() |
| 57 | router.ServeHTTP(w, r) |
| 58 | if authenticated { |
| 59 | require.Equal(t, http.StatusOK, w.Code) |
| 60 | require.Contains(t, w.Body.String(), testCase.content) |
| 61 | } else { |
| 62 | require.Equal(t, http.StatusUnauthorized, w.Code) |
| 63 | if testCase.content != "" { |
| 64 | require.NotContains(t, w.Body.String(), testCase.content) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |