From 88706e9cf24bb399137e79f8478e1d2f761b251a Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Wed, 7 May 2025 15:46:22 +0300 Subject: [PATCH] home: auth tests --- internal/home/authhttp_internal_test.go | 103 ++++++++++++++++++++++++ internal/home/tls_internal_test.go | 8 +- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/internal/home/authhttp_internal_test.go b/internal/home/authhttp_internal_test.go index 9819298a..b2217cbd 100644 --- a/internal/home/authhttp_internal_test.go +++ b/internal/home/authhttp_internal_test.go @@ -2,6 +2,7 @@ package home import ( "net/http" + "net/http/httptest" "net/netip" "net/textproto" "net/url" @@ -9,11 +10,113 @@ import ( "testing" "github.com/AdguardTeam/golibs/httphdr" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +// TODO(s.chzhen): !! Add more tests. +func TestAuth_ServeHTTP_first_run(t *testing.T) { + storeGlobals(t) + + globalContext.firstRun = true + + mux := http.NewServeMux() + globalContext.mux = mux + + var ( + logger = slogutil.NewDiscardLogger() + ctx = testutil.ContextWithTimeout(t, testTimeout) + err error + ) + + web, err := initWeb(ctx, options{}, nil, nil, logger, nil, false) + require.NoError(t, err) + + globalContext.web = web + + testCases := []struct { + url string + method string + code int + }{{ + url: "/", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/apple/doh.mobileconfig", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/apple/dot.mobileconfig", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/i18n/change_language", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/i18n/current_language", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/install/check_config", + method: http.MethodPost, + code: http.StatusBadRequest, + }, { + url: "/control/install/configure", + method: http.MethodPost, + code: http.StatusBadRequest, + }, { + url: "/control/install/get_addresses", + method: http.MethodGet, + code: http.StatusOK, + }, { + url: "/control/login", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/logout", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/profile", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/profile/update", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/status", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/update", + method: http.MethodGet, + code: http.StatusFound, + }, { + url: "/control/version.json", + method: http.MethodGet, + code: http.StatusFound, + }} + + for _, tc := range testCases { + t.Run(tc.url, func(t *testing.T) { + r := httptest.NewRequest(tc.method, tc.url, nil) + + h, pattern := mux.Handler(r) + require.NotEmpty(t, pattern) + + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + + assert.Equal(t, tc.code, w.Code) + }) + } +} + // implements http.ResponseWriter type testResponseWriter struct { hdr http.Header diff --git a/internal/home/tls_internal_test.go b/internal/home/tls_internal_test.go index 6ad36329..1c239c81 100644 --- a/internal/home/tls_internal_test.go +++ b/internal/home/tls_internal_test.go @@ -113,10 +113,12 @@ func TestValidateCertificates(t *testing.T) { // restores them once the test is complete. // // The global variables are: -// - [configuration.dns] +// - [configuration] // - [homeContext.clients.storage] // - [homeContext.dnsServer] +// - [homeContext.firstRun] // - [homeContext.mux] +// - [homeContext.web] // // TODO(s.chzhen): Remove this once the TLS manager no longer accesses global // variables. Make tests that use this helper concurrent. @@ -126,13 +128,17 @@ func storeGlobals(tb testing.TB) { prevConfig := config storage := globalContext.clients.storage dnsServer := globalContext.dnsServer + firstRun := globalContext.firstRun mux := globalContext.mux + web := globalContext.web tb.Cleanup(func() { config = prevConfig globalContext.clients.storage = storage globalContext.dnsServer = dnsServer + globalContext.firstRun = firstRun globalContext.mux = mux + globalContext.web = web }) }