mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 22:45:46 +03:00
42291cd547
Updates #5720. Squashed commit of the following: commit e8093c990f15e2efc496f1a04f87360825e34e96 Merge: df5413eef28fefaff1
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 15:06:33 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit df5413eefeac2c7e34eb725db9e2908b5b2d08cb Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 14:49:05 2023 +0300 confmigrate: imp docs commit 1644d99b730cc7f22c9d75b8e990149d3ce5b32a Merge: 9542ee1611e4517898
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 14:23:42 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit 9542ee1616c1dd4bdb6ec9a2af79a2af3858a7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Sep 5 12:48:48 2023 +0300 all: upd chlog commit 183f84a7f73c7bd33669bd108076f60514ca101e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 1 17:11:31 2023 +0300 all: imp chlog commit a704325352a577a9b6652f011b82180ec3a6e095 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 31 18:59:52 2023 +0300 all: imp code commit fe99c3b883500850399b1feb72c914ab878b3107 Merge: 7f11e94600182b9ec1
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 31 18:43:09 2023 +0300 Merge branch 'master' into 5720-wildcard-ignored-domains commit 7f11e94609027ed821a125d27a1ffde03f37334a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 19:57:51 2023 +0300 aghnet: add tests commit f10f9190ce1064a5d03155e8b6bba61db977897b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 18:32:07 2023 +0300 all: add conf migration commit a53c14df129765366966c5230dd53aa29bdd25c5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 30 13:37:30 2023 +0300 all: add ignore engine
130 lines
3 KiB
Go
130 lines
3 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: "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)
|
|
})
|
|
}
|
|
}
|