mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-08-03 22:19:28 +00:00
Merge in DNS/adguard-home from AGDNS-2743-auth-mw-usage to master
Squashed commit of the following:
commit 9e3054a42f1b04a00c207810a8dc08696e44c466
Merge: 610c6fc45 1317e296f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 9 17:09:26 2025 +0300
Merge branch 'master' into AGDNS-2743-auth-mw-usage
commit 610c6fc45d2d8848e9b89afb0037b98d83106afa
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Jul 8 19:03:44 2025 +0300
home: imp docs
commit 4633c8991ca77ec182b17a299d9f7e75e145f2ee
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Jul 3 21:01:54 2025 +0300
home: add tests
commit 586b714dafc342670b441fb29e3c7c8fde83ba4c
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Jul 3 14:25:28 2025 +0300
home: fix first run
commit 7c2e5d41f0feaae7b50ea0e69cf2767ba17d59df
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 2 23:52:16 2025 +0300
home: imp code
commit b90031495ae3b6fab7361528fb3a3013139a4965
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 2 14:06:42 2025 +0300
home: rm unused
commit 90cb29f2daac7b825b03857414bb04a692736e22
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 2 13:55:45 2025 +0300
home: auth mw usage
81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAuthMiddlewareGLiNet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const (
|
|
testTTL = 60 * time.Second
|
|
|
|
glTokenFileSuffix = "test"
|
|
)
|
|
|
|
tempDir := t.TempDir()
|
|
glFilePrefix = tempDir + "/gl_token_"
|
|
glTokenFile := glFilePrefix + glTokenFileSuffix
|
|
|
|
glFileData := make([]byte, 4)
|
|
binary.NativeEndian.PutUint32(glFileData, uint32(time.Now().Add(testTTL).Unix()))
|
|
|
|
err := os.WriteFile(glTokenFile, glFileData, 0o644)
|
|
require.NoError(t, err)
|
|
|
|
mw := newAuthMiddlewareGLiNet(&authMiddlewareGLiNetConfig{
|
|
logger: testLogger,
|
|
clock: timeutil.SystemClock{},
|
|
tokenFilePrefix: glFilePrefix,
|
|
maxTokenSize: MaxFileSize,
|
|
ttl: testTTL,
|
|
})
|
|
|
|
h := &testAuthHandler{}
|
|
wrapped := mw.Wrap(h)
|
|
|
|
reqValidCookie := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
reqValidCookie.AddCookie(&http.Cookie{Name: glCookieName, Value: glTokenFileSuffix})
|
|
|
|
reqInvalidCookie := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
reqInvalidCookie.AddCookie(&http.Cookie{Name: glCookieName, Value: "invalid_cookie"})
|
|
|
|
testCases := []struct {
|
|
req *http.Request
|
|
name string
|
|
wantCode int
|
|
}{{
|
|
req: httptest.NewRequest(http.MethodGet, "/", nil),
|
|
name: "no_cookie",
|
|
wantCode: http.StatusFound,
|
|
}, {
|
|
req: reqValidCookie,
|
|
name: "valid_cookie",
|
|
wantCode: http.StatusOK,
|
|
}, {
|
|
req: reqInvalidCookie,
|
|
name: "invalid_cookie",
|
|
wantCode: http.StatusFound,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
w := httptest.NewRecorder()
|
|
wrapped.ServeHTTP(w, tc.req)
|
|
|
|
assert.Equal(t, tc.wantCode, w.Code)
|
|
})
|
|
}
|
|
}
|