diff --git a/go.sum b/go.sum index 69863940..d4ef7787 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -71,6 +72,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -86,6 +88,7 @@ github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= @@ -129,6 +132,7 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -193,9 +197,11 @@ github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgSh github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= @@ -225,11 +231,13 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9cJvm4SvQ= github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U= @@ -281,6 +289,7 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= @@ -654,3 +663,4 @@ sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h6 sigs.k8s.io/structured-merge-diff/v4 v4.3.0 h1:UZbZAZfX0wV2zr7YZorDz6GXROfDFj6LvqCRm4VUVKk= sigs.k8s.io/structured-merge-diff/v4 v4.3.0/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08= sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/pkg/client/client-methods.go b/pkg/client/client-methods.go new file mode 100644 index 00000000..330c5d25 --- /dev/null +++ b/pkg/client/client-methods.go @@ -0,0 +1,83 @@ +package client + +import ( + "encoding/json" + "net/http" + + "github.com/go-resty/resty/v2" +) + +func (cl *client) doGet(req *resty.Request, url string) error { + rl := cl.log.With("method", "GET", "path", url) + if cl.client.UserInfo != nil { + rl = rl.With("username", cl.client.UserInfo.Username) + } + req.ForceContentType("application/json") + rl.Debug("do get") + resp, err := req.Get(url) + if err != nil { + if resp != nil && resp.StatusCode() == http.StatusFound { + loc := resp.Header().Get("Location") + if loc == "/install.html" || loc == "/control/install.html" { + return ErrSetupNeeded + } + } + rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do get") + return detailedError(resp, err) + } + rl.With( + "status", resp.StatusCode(), + "body", string(resp.Body()), + "content-type", resp.Header()["Content-Type"], + ).Debug("got response") + if resp.StatusCode() != http.StatusOK { + return detailedError(resp, nil) + } + return nil +} + +func (cl *client) doPost(req *resty.Request, url string) error { + rl := cl.log.With("method", "POST", "path", url) + if cl.client.UserInfo != nil { + rl = rl.With("username", cl.client.UserInfo.Username) + } + b, _ := json.Marshal(req.Body) + rl.With("body", string(b)).Debug("do post") + resp, err := req.Post(url) + if err != nil { + rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do post") + return detailedError(resp, err) + } + rl.With( + "status", resp.StatusCode(), + "body", string(resp.Body()), + "content-type", contentType(resp), + ).Debug("got response") + if resp.StatusCode() != http.StatusOK { + return detailedError(resp, nil) + } + return nil +} + +func (cl *client) doPut(req *resty.Request, url string) error { + rl := cl.log.With("method", "PUT", "path", url) + if cl.client.UserInfo != nil { + rl = rl.With("username", cl.client.UserInfo.Username) + } + b, _ := json.Marshal(req.Body) + rl.With("body", string(b)).Debug("do put") + resp, err := req.Put(url) + if err != nil { + rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do put") + return detailedError(resp, err) + } + rl.With( + "status", resp.StatusCode(), + "body", string(resp.Body()), + "content-type", contentType(resp), + ).Debug("got response") + if resp.StatusCode() != http.StatusOK { + return detailedError(resp, nil) + } + return nil +} diff --git a/pkg/client/client.go b/pkg/client/client.go index 39aba344..5dafdcbd 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -2,7 +2,6 @@ package client import ( "crypto/tls" - "encoding/json" "errors" "fmt" "net/http" @@ -107,8 +106,10 @@ type Client interface { ToggleSafeBrowsing(enable bool) error Parental() (bool, error) ToggleParental(enable bool) error - SafeSearch() (bool, error) - ToggleSafeSearch(enable bool) error + SafeSearchConfig() (*model.SafeSearchConfig, error) + SetSafeSearchConfig(settings *model.SafeSearchConfig) error + Profile() (*model.ProfileInfo, error) + SetProfile(settings *model.ProfileInfo) error Services() (*model.BlockedServicesArray, error) SetServices(services *model.BlockedServicesArray) error Clients() (*model.Clients, error) @@ -141,58 +142,6 @@ func (cl *client) Host() string { return cl.host } -func (cl *client) doGet(req *resty.Request, url string) error { - rl := cl.log.With("method", "GET", "path", url) - if cl.client.UserInfo != nil { - rl = rl.With("username", cl.client.UserInfo.Username) - } - req.ForceContentType("application/json") - rl.Debug("do get") - resp, err := req.Get(url) - if err != nil { - if resp != nil && resp.StatusCode() == http.StatusFound { - loc := resp.Header().Get("Location") - if loc == "/install.html" || loc == "/control/install.html" { - return ErrSetupNeeded - } - } - rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do get") - return detailedError(resp, err) - } - rl.With( - "status", resp.StatusCode(), - "body", string(resp.Body()), - "content-type", resp.Header()["Content-Type"], - ).Debug("got response") - if resp.StatusCode() != http.StatusOK { - return detailedError(resp, nil) - } - return nil -} - -func (cl *client) doPost(req *resty.Request, url string) error { - rl := cl.log.With("method", "POST", "path", url) - if cl.client.UserInfo != nil { - rl = rl.With("username", cl.client.UserInfo.Username) - } - b, _ := json.Marshal(req.Body) - rl.With("body", string(b)).Debug("do post") - resp, err := req.Post(url) - if err != nil { - rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do post") - return detailedError(resp, err) - } - rl.With( - "status", resp.StatusCode(), - "body", string(resp.Body()), - "content-type", contentType(resp), - ).Debug("got response") - if resp.StatusCode() != http.StatusOK { - return detailedError(resp, nil) - } - return nil -} - func contentType(resp *resty.Response) string { if ct, ok := resp.Header()["Content-Type"]; ok { if len(ct) != 1 { @@ -256,14 +205,6 @@ func (cl *client) ToggleParental(enable bool) error { return cl.toggleBool("parental", enable) } -func (cl *client) SafeSearch() (bool, error) { - return cl.toggleStatus("safesearch") -} - -func (cl *client) ToggleSafeSearch(enable bool) error { - return cl.toggleBool("safesearch", enable) -} - func (cl *client) toggleStatus(mode string) (bool, error) { fs := &model.EnableConfig{} err := cl.doGet(cl.client.R().EnableTrace().SetResult(fs), fmt.Sprintf("/%s/status", mode)) @@ -503,3 +444,25 @@ func (cl *client) DeleteDHCPStaticLeases(leases ...model.DhcpStaticLease) error } return nil } + +func (cl *client) SafeSearchConfig() (*model.SafeSearchConfig, error) { + sss := &model.SafeSearchConfig{} + err := cl.doGet(cl.client.R().EnableTrace().SetResult(sss), "/safesearch/status") + return sss, err +} + +func (cl *client) SetSafeSearchConfig(settings *model.SafeSearchConfig) error { + cl.log.With("enabled", *settings.Enabled).Info("Set safesearch settings") + return cl.doPut(cl.client.R().EnableTrace().SetBody(settings), "/safesearch/settings") +} + +func (cl *client) Profile() (*model.ProfileInfo, error) { + p := &model.ProfileInfo{} + err := cl.doGet(cl.client.R().EnableTrace().SetResult(p), "/profile") + return p, err +} + +func (cl *client) SetProfile(profile *model.ProfileInfo) error { + cl.log.With("language", profile.Language, "theme", profile.Theme).Info("Set profile") + return cl.doPut(cl.client.R().EnableTrace().SetBody(profile), "/profile/update") +} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 84502041..993f10e9 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -165,21 +165,22 @@ var _ = Describe("Client", func() { }) }) - Context("SafeSearch", func() { + Context("SafeSearchConfig", func() { It("should read safesearch status", func() { ts, cl = ClientGet("safesearch-status.json", "/safesearch/status") - ss, err := cl.SafeSearch() + ss, err := cl.SafeSearchConfig() Ω(err).ShouldNot(HaveOccurred()) - Ω(ss).Should(BeTrue()) + Ω(ss.Enabled).ShouldNot(BeNil()) + Ω(*ss.Enabled).Should(BeTrue()) }) It("should enable safesearch", func() { - ts, cl = ClientPost("/safesearch/enable", "") - err := cl.ToggleSafeSearch(true) + ts, cl = ClientPut("/safesearch/settings", `{"enabled":true}`) + err := cl.SetSafeSearchConfig(&model.SafeSearchConfig{Enabled: ptr.To(true)}) Ω(err).ShouldNot(HaveOccurred()) }) It("should disable safesearch", func() { - ts, cl = ClientPost("/safesearch/disable", "") - err := cl.ToggleSafeSearch(false) + ts, cl = ClientPut("/safesearch/settings", `{"enabled":false}`) + err := cl.SetSafeSearchConfig(&model.SafeSearchConfig{Enabled: ptr.To(false)}) Ω(err).ShouldNot(HaveOccurred()) }) }) @@ -352,3 +353,18 @@ func ClientPost(path string, content ...string) (*httptest.Server, client.Client Ω(err).ShouldNot(HaveOccurred()) return ts, cl } + +func ClientPut(path string, content ...string) (*httptest.Server, client.Client) { + index := 0 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + Ω(r.URL.Path).Should(Equal(types.DefaultAPIPath + path)) + body, err := io.ReadAll(r.Body) + Ω(err).ShouldNot(HaveOccurred()) + Ω(body).Should(Equal([]byte(content[index]))) + index++ + })) + + cl, err := client.New(types.AdGuardInstance{URL: ts.URL, Username: username, Password: password}) + Ω(err).ShouldNot(HaveOccurred()) + return ts, cl +} diff --git a/pkg/client/model/model-functions.go b/pkg/client/model/model-functions.go index 410b8f10..3bd19c37 100644 --- a/pkg/client/model/model-functions.go +++ b/pkg/client/model/model-functions.go @@ -343,3 +343,19 @@ func ptrEquals[T comparable](a *T, b *T) bool { type EnableConfig struct { Enabled bool `json:"enabled"` } + +func (ssc *SafeSearchConfig) Equals(o *SafeSearchConfig) bool { + return ptrEquals(ssc.Enabled, o.Enabled) && + ptrEquals(ssc.Bing, o.Bing) && + ptrEquals(ssc.Duckduckgo, o.Duckduckgo) && + ptrEquals(ssc.Google, o.Google) && + ptrEquals(ssc.Pixabay, o.Pixabay) && + ptrEquals(ssc.Yandex, o.Yandex) && + ptrEquals(ssc.Youtube, o.Youtube) +} + +func (pi *ProfileInfo) Equals(o *ProfileInfo) bool { + return pi.Name == o.Name && + pi.Language == o.Language && + pi.Theme == o.Theme +} diff --git a/pkg/mocks/client/mock.go b/pkg/mocks/client/mock.go index 8f8d18a0..303e8107 100644 --- a/pkg/mocks/client/mock.go +++ b/pkg/mocks/client/mock.go @@ -284,6 +284,21 @@ func (mr *MockClientMockRecorder) Parental() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Parental", reflect.TypeOf((*MockClient)(nil).Parental)) } +// Profile mocks base method. +func (m *MockClient) Profile() (*model.ProfileInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Profile") + ret0, _ := ret[0].(*model.ProfileInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Profile indicates an expected call of Profile. +func (mr *MockClientMockRecorder) Profile() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Profile", reflect.TypeOf((*MockClient)(nil).Profile)) +} + // QueryLogConfig mocks base method. func (m *MockClient) QueryLogConfig() (*model.QueryLogConfig, error) { m.ctrl.T.Helper() @@ -343,19 +358,19 @@ func (mr *MockClientMockRecorder) SafeBrowsing() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SafeBrowsing", reflect.TypeOf((*MockClient)(nil).SafeBrowsing)) } -// SafeSearch mocks base method. -func (m *MockClient) SafeSearch() (bool, error) { +// SafeSearchConfig mocks base method. +func (m *MockClient) SafeSearchConfig() (*model.SafeSearchConfig, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SafeSearch") - ret0, _ := ret[0].(bool) + ret := m.ctrl.Call(m, "SafeSearchConfig") + ret0, _ := ret[0].(*model.SafeSearchConfig) ret1, _ := ret[1].(error) return ret0, ret1 } -// SafeSearch indicates an expected call of SafeSearch. -func (mr *MockClientMockRecorder) SafeSearch() *gomock.Call { +// SafeSearchConfig indicates an expected call of SafeSearchConfig. +func (mr *MockClientMockRecorder) SafeSearchConfig() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SafeSearch", reflect.TypeOf((*MockClient)(nil).SafeSearch)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SafeSearchConfig", reflect.TypeOf((*MockClient)(nil).SafeSearchConfig)) } // Services mocks base method. @@ -429,6 +444,20 @@ func (mr *MockClientMockRecorder) SetDhcpConfig(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDhcpConfig", reflect.TypeOf((*MockClient)(nil).SetDhcpConfig), arg0) } +// SetProfile mocks base method. +func (m *MockClient) SetProfile(arg0 *model.ProfileInfo) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetProfile", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetProfile indicates an expected call of SetProfile. +func (mr *MockClientMockRecorder) SetProfile(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetProfile", reflect.TypeOf((*MockClient)(nil).SetProfile), arg0) +} + // SetQueryLogConfig mocks base method. func (m *MockClient) SetQueryLogConfig(arg0 *model.QueryLogConfig) error { m.ctrl.T.Helper() @@ -443,6 +472,20 @@ func (mr *MockClientMockRecorder) SetQueryLogConfig(arg0 interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetQueryLogConfig", reflect.TypeOf((*MockClient)(nil).SetQueryLogConfig), arg0) } +// SetSafeSearchConfig mocks base method. +func (m *MockClient) SetSafeSearchConfig(arg0 *model.SafeSearchConfig) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetSafeSearchConfig", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetSafeSearchConfig indicates an expected call of SetSafeSearchConfig. +func (mr *MockClientMockRecorder) SetSafeSearchConfig(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetSafeSearchConfig", reflect.TypeOf((*MockClient)(nil).SetSafeSearchConfig), arg0) +} + // SetServices mocks base method. func (m *MockClient) SetServices(arg0 *[]string) error { m.ctrl.T.Helper() @@ -571,20 +614,6 @@ func (mr *MockClientMockRecorder) ToggleSafeBrowsing(arg0 interface{}) *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ToggleSafeBrowsing", reflect.TypeOf((*MockClient)(nil).ToggleSafeBrowsing), arg0) } -// ToggleSafeSearch mocks base method. -func (m *MockClient) ToggleSafeSearch(arg0 bool) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ToggleSafeSearch", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// ToggleSafeSearch indicates an expected call of ToggleSafeSearch. -func (mr *MockClientMockRecorder) ToggleSafeSearch(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ToggleSafeSearch", reflect.TypeOf((*MockClient)(nil).ToggleSafeSearch), arg0) -} - // UpdateClients mocks base method. func (m *MockClient) UpdateClients(arg0 ...*model.Client) error { m.ctrl.T.Helper() diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index d29a0be6..1c49f339 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -126,7 +126,7 @@ func (w *worker) sync() { sl.With("error", err).Error("Error getting parental status") return } - o.safeSearch, err = oc.SafeSearch() + o.safeSearch, err = oc.SafeSearchConfig() if err != nil { sl.With("error", err).Error("Error getting safe search status") return @@ -416,10 +416,10 @@ func (w *worker) syncGeneralSettings(o *origin, rs *model.ServerStatus, replica return err } } - if rs, err := replica.SafeSearch(); err != nil { + if ssc, err := replica.SafeSearchConfig(); err != nil { return err - } else if o.safeSearch != rs { - if err = replica.ToggleSafeSearch(o.safeSearch); err != nil { + } else if !o.safeSearch.Equals(ssc) { + if err = replica.SetSafeSearchConfig(o.safeSearch); err != nil { return err } } @@ -538,6 +538,6 @@ type origin struct { dnsConfig *model.DNSConfig dhcpServerConfig *model.DhcpStatus parental bool - safeSearch bool + safeSearch *model.SafeSearchConfig safeBrowsing bool } diff --git a/pkg/sync/sync_test.go b/pkg/sync/sync_test.go index e27189c5..79dd15b0 100644 --- a/pkg/sync/sync_test.go +++ b/pkg/sync/sync_test.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "k8s.io/utils/ptr" ) var boolTrue = true @@ -203,13 +204,14 @@ var _ = Describe("Sync", func() { ) BeforeEach(func() { o = &origin{ - status: &model.ServerStatus{}, + status: &model.ServerStatus{}, + safeSearch: &model.SafeSearchConfig{}, } rs = &model.ServerStatus{} }) It("should have no changes", func() { cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() err := w.syncGeneralSettings(o, rs, cl) Ω(err).ShouldNot(HaveOccurred()) @@ -218,7 +220,7 @@ var _ = Describe("Sync", func() { o.status.ProtectionEnabled = true cl.EXPECT().ToggleProtection(true) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() err := w.syncGeneralSettings(o, rs, cl) Ω(err).ShouldNot(HaveOccurred()) @@ -227,16 +229,25 @@ var _ = Describe("Sync", func() { o.parental = true cl.EXPECT().Parental() cl.EXPECT().ToggleParental(true) - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() err := w.syncGeneralSettings(o, rs, cl) Ω(err).ShouldNot(HaveOccurred()) }) It("should have safeSearch enabled changes", func() { - o.safeSearch = true + o.safeSearch = &model.SafeSearchConfig{Enabled: ptr.To(true)} cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() - cl.EXPECT().ToggleSafeSearch(true) + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) + cl.EXPECT().SetSafeSearchConfig(o.safeSearch) + cl.EXPECT().SafeBrowsing() + err := w.syncGeneralSettings(o, rs, cl) + Ω(err).ShouldNot(HaveOccurred()) + }) + It("should have Duckduckgo safeSearch enabled changed", func() { + o.safeSearch = &model.SafeSearchConfig{Duckduckgo: ptr.To(true)} + cl.EXPECT().Parental() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) + cl.EXPECT().SetSafeSearchConfig(o.safeSearch) cl.EXPECT().SafeBrowsing() err := w.syncGeneralSettings(o, rs, cl) Ω(err).ShouldNot(HaveOccurred()) @@ -244,7 +255,7 @@ var _ = Describe("Sync", func() { It("should have safeBrowsing enabled changes", func() { o.safeBrowsing = true cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().ToggleSafeBrowsing(true) err := w.syncGeneralSettings(o, rs, cl) @@ -506,7 +517,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().RewriteList().Return(&model.RewriteEntries{}, nil) cl.EXPECT().Services() @@ -522,7 +533,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().QueryLogConfig().Return(&model.QueryLogConfig{}, nil) cl.EXPECT().StatsConfig().Return(&model.StatsConfig{}, nil) @@ -555,7 +566,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().RewriteList().Return(&model.RewriteEntries{}, nil) cl.EXPECT().Services() @@ -570,7 +581,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().QueryLogConfig().Return(&model.QueryLogConfig{}, nil) cl.EXPECT().StatsConfig().Return(&model.StatsConfig{}, nil) @@ -604,7 +615,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().RewriteList().Return(&model.RewriteEntries{}, nil) cl.EXPECT().Services() @@ -626,7 +637,7 @@ var _ = Describe("Sync", func() { cl.EXPECT().Host() cl.EXPECT().Status().Return(&model.ServerStatus{Version: versions.MinAgh}, nil) cl.EXPECT().Parental() - cl.EXPECT().SafeSearch() + cl.EXPECT().SafeSearchConfig().Return(&model.SafeSearchConfig{}, nil) cl.EXPECT().SafeBrowsing() cl.EXPECT().RewriteList().Return(&model.RewriteEntries{}, nil) cl.EXPECT().Services()