mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-18 05:41:55 +03:00
f9fe3172c4
Merge in DNS/adguard-home from 4299-querylog-stats-clients to master Squashed commit of the following: commit 33b80b67224f7c1a15bee8e6a23d9d5bab6ac629 Merge: 61964fdd5d5a7295
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:43:22 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 61964fdd02221abbddedf2d6d02bb0bce6845362 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:42:01 2023 +0300 dnsforward: imp code commit 7382168500bab6ca7494d39aabfc2d7bfceb5d24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 11:13:07 2023 +0300 all: imp code, chlog commit c7852902f635af6c296dcb6735f7b0bfb83f4e87 Merge: aa4dc0a5a55cbbe7
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 14:34:24 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit aa4dc0a54e95bc5b24718ec158340b631a822801 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 12:54:02 2023 +0300 all: imp code commit dd541f0cd7ecbf0afcf10ccbd130fd1d1fa4c1c4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 13:01:53 2023 +0300 querylog: fix typo commit d2c8fdb35b04d27c8957fa027882fde704cc07be Merge: 83d0baa12eb3bf6e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:36:49 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 83d0baa1f1202f9c62d4be2041d7aed12ee9ab2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:35:15 2023 +0300 all: add tests commit a459f19f25cf9646d145813fe7834b2d9979c516 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 29 16:51:53 2023 +0300 all: add clients querylog stats ignore
154 lines
3.5 KiB
Go
154 lines
3.5 KiB
Go
package stats
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestHandleStatsConfig(t *testing.T) {
|
|
const (
|
|
smallIvl = 1 * time.Minute
|
|
minIvl = 1 * time.Hour
|
|
maxIvl = 365 * timeutil.Day
|
|
)
|
|
|
|
conf := Config{
|
|
UnitID: func() (id uint32) { return 0 },
|
|
ConfigModified: func() {},
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: time.Hour * 24,
|
|
Enabled: true,
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
wantErr string
|
|
body getConfigResp
|
|
wantCode int
|
|
}{{
|
|
name: "set_ivl_1_minIvl",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(minIvl.Milliseconds()),
|
|
Ignored: []string{},
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantErr: "",
|
|
}, {
|
|
name: "small_interval",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(smallIvl.Milliseconds()),
|
|
Ignored: []string{},
|
|
},
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantErr: "unsupported interval: less than an hour\n",
|
|
}, {
|
|
name: "big_interval",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(maxIvl.Milliseconds() + minIvl.Milliseconds()),
|
|
Ignored: []string{},
|
|
},
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantErr: "unsupported interval: more than a year\n",
|
|
}, {
|
|
name: "set_ignored_ivl_1_maxIvl",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(maxIvl.Milliseconds()),
|
|
Ignored: []string{
|
|
"ignor.ed",
|
|
},
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantErr: "",
|
|
}, {
|
|
name: "ignored_duplicate",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(minIvl.Milliseconds()),
|
|
Ignored: []string{
|
|
"ignor.ed",
|
|
"ignor.ed",
|
|
},
|
|
},
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantErr: "ignored: duplicate host name \"ignor.ed\" at index 1\n",
|
|
}, {
|
|
name: "ignored_empty",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBTrue,
|
|
Interval: float64(minIvl.Milliseconds()),
|
|
Ignored: []string{
|
|
"",
|
|
},
|
|
},
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantErr: "ignored: host name is empty\n",
|
|
}, {
|
|
name: "enabled_is_null",
|
|
body: getConfigResp{
|
|
Enabled: aghalg.NBNull,
|
|
Interval: float64(minIvl.Milliseconds()),
|
|
Ignored: []string{},
|
|
},
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantErr: "enabled is null\n",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s, err := New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
buf, err := json.Marshal(tc.body)
|
|
require.NoError(t, err)
|
|
|
|
const (
|
|
configGet = "/control/stats/config"
|
|
configPut = "/control/stats/config/update"
|
|
)
|
|
|
|
req := httptest.NewRequest(http.MethodPut, configPut, bytes.NewReader(buf))
|
|
rw := httptest.NewRecorder()
|
|
|
|
s.handlePutStatsConfig(rw, req)
|
|
require.Equal(t, tc.wantCode, rw.Code)
|
|
|
|
if tc.wantCode != http.StatusOK {
|
|
assert.Equal(t, tc.wantErr, rw.Body.String())
|
|
|
|
return
|
|
}
|
|
|
|
resp := httptest.NewRequest(http.MethodGet, configGet, nil)
|
|
rw = httptest.NewRecorder()
|
|
|
|
s.handleGetStatsConfig(rw, resp)
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
ans := getConfigResp{}
|
|
err = json.Unmarshal(rw.Body.Bytes(), &ans)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.body, ans)
|
|
})
|
|
}
|
|
}
|