AdGuardHome/internal/stats/http_test.go

153 lines
3.4 KiB
Go
Raw Normal View History

2023-02-17 12:54:40 +00:00
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"
)
2023-02-20 15:34:35 +00:00
func TestHandleStatsConfig(t *testing.T) {
2023-02-27 10:20:53 +00:00
const (
smallIvl = 1 * time.Minute
minIvl = 1 * time.Hour
maxIvl = 365 * timeutil.Day
2023-02-17 12:54:40 +00:00
)
2023-02-27 10:20:53 +00:00
conf := Config{
Filename: filepath.Join(t.TempDir(), "stats.db"),
Limit: time.Hour * 24,
Enabled: true,
UnitID: func() (id uint32) { return 0 },
ConfigModified: func() {},
}
2023-02-17 12:54:40 +00:00
testCases := []struct {
name string
2023-02-20 15:34:35 +00:00
body getConfigResp
2023-02-17 12:54:40 +00:00
wantCode int
2023-02-20 08:17:28 +00:00
wantErr string
2023-02-17 12:54:40 +00:00
}{{
2023-02-27 10:20:53 +00:00
name: "set_ivl_1_minIvl",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(minIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: nil,
},
wantCode: http.StatusOK,
2023-02-20 08:17:28 +00:00
wantErr: "",
2023-02-17 12:54:40 +00:00
}, {
name: "small_interval",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(smallIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: []string{},
},
2023-02-20 15:34:35 +00:00
wantCode: http.StatusUnprocessableEntity,
2023-02-20 08:17:28 +00:00
wantErr: "unsupported interval\n",
2023-02-17 12:54:40 +00:00
}, {
name: "big_interval",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(maxIvl.Milliseconds() + minIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: []string{},
},
2023-02-20 15:34:35 +00:00
wantCode: http.StatusUnprocessableEntity,
2023-02-20 08:17:28 +00:00
wantErr: "unsupported interval\n",
2023-02-17 12:54:40 +00:00
}, {
2023-02-27 10:20:53 +00:00
name: "set_ignored_ivl_1_maxIvl",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(maxIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: []string{
"ignor.ed",
},
},
2023-02-20 08:17:28 +00:00
wantCode: http.StatusOK,
wantErr: "",
2023-02-17 12:54:40 +00:00
}, {
name: "ignored_duplicate",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(minIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: []string{
"ignor.ed",
"ignor.ed",
},
},
2023-02-20 15:34:35 +00:00
wantCode: http.StatusUnprocessableEntity,
2023-02-27 10:20:53 +00:00
wantErr: "ignored: duplicate or empty host\n",
2023-02-17 12:54:40 +00:00
}, {
name: "ignored_empty",
2023-02-20 15:34:35 +00:00
body: getConfigResp{
2023-02-17 12:54:40 +00:00
Enabled: aghalg.NBTrue,
2023-02-27 10:20:53 +00:00
Interval: float64(minIvl.Milliseconds()),
2023-02-17 12:54:40 +00:00
Ignored: []string{
"",
},
},
2023-02-20 15:34:35 +00:00
wantCode: http.StatusUnprocessableEntity,
2023-02-27 10:20:53 +00:00
wantErr: "ignored: duplicate or empty host\n",
2023-02-20 15:34:35 +00:00
}, {
name: "enabled_is_null",
body: getConfigResp{
Enabled: aghalg.NBNull,
2023-02-27 10:20:53 +00:00
Interval: float64(minIvl.Milliseconds()),
2023-02-20 15:34:35 +00:00
Ignored: []string{},
},
wantCode: http.StatusUnprocessableEntity,
wantErr: "enabled is null\n",
2023-02-17 12:54:40 +00:00
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s, err := New(conf)
require.NoError(t, err)
2023-02-20 08:17:28 +00:00
2023-02-17 12:54:40 +00:00
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()
2023-02-20 08:17:28 +00:00
2023-02-27 10:20:53 +00:00
s.handlePutStatsConfig(rw, req)
2023-02-17 12:54:40 +00:00
require.Equal(t, tc.wantCode, rw.Code)
2023-02-20 08:17:28 +00:00
if tc.wantCode != http.StatusOK {
assert.Equal(t, tc.wantErr, rw.Body.String())
2023-02-17 12:54:40 +00:00
2023-02-20 08:17:28 +00:00
return
2023-02-17 12:54:40 +00:00
}
2023-02-20 08:17:28 +00:00
resp := httptest.NewRequest(http.MethodGet, configGet, nil)
rw = httptest.NewRecorder()
2023-02-27 10:20:53 +00:00
s.handleGetStatsConfig(rw, resp)
2023-02-20 08:17:28 +00:00
require.Equal(t, http.StatusOK, rw.Code)
2023-02-20 15:34:35 +00:00
ans := getConfigResp{}
2023-02-27 10:20:53 +00:00
err = json.Unmarshal(rw.Body.Bytes(), &ans)
require.NoError(t, err)
2023-02-20 08:17:28 +00:00
assert.Equal(t, tc.body, ans)
2023-02-17 12:54:40 +00:00
})
}
}