Skip to content

Commit

Permalink
stellar#4222: back filling tests on filter config web action handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Mar 1, 2022
1 parent d1f2c2b commit f41359d
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 4 deletions.
17 changes: 14 additions & 3 deletions services/horizon/internal/actions/filter_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type filterResource struct {
}

type QueryPathParams struct {
NAME string `schema:"name" valid:"optional"`
NAME string `schema:"filter_name" valid:"optional"`
}

type UpdatePathParams struct {
NAME string `schema:"name" valid:"required"`
NAME string `schema:"filter_name" valid:"required"`
}

type FilterRuleHandler struct{}
Expand All @@ -49,12 +49,17 @@ func (handler FilterRuleHandler) Get(w http.ResponseWriter, r *http.Request) {

if pp.NAME != "" {
responsePayload, err = handler.findOne(pp.NAME, historyQ, r.Context())
if historyQ.NoRows(err) {
err = problem.NotFound
}

} else {
responsePayload, err = handler.findAll(historyQ, r.Context())
}

if err != nil {
problem.Render(r.Context(), w, err)
return
}

enc := json.NewEncoder(w)
Expand All @@ -73,6 +78,7 @@ func (handler FilterRuleHandler) Create(w http.ResponseWriter, r *http.Request)
filterRequest, err := handler.requestedFilter(r)
if err != nil {
problem.Render(r.Context(), w, err)
return
}

existing, err := handler.findOne(filterRequest.Name, historyQ, r.Context())
Expand All @@ -89,6 +95,7 @@ func (handler FilterRuleHandler) Create(w http.ResponseWriter, r *http.Request)

if err = handler.upsert(filterRequest, historyQ, r.Context()); err != nil {
problem.Render(r.Context(), w, err)
return
}
w.WriteHeader(201)
}
Expand Down Expand Up @@ -125,6 +132,7 @@ func (handler FilterRuleHandler) Update(w http.ResponseWriter, r *http.Request)
if _, err = handler.findOne(filterRequest.Name, historyQ, r.Context()); err != nil {
// not found or other error
problem.Render(r.Context(), w, err)
return
}

if err = handler.upsert(filterRequest, historyQ, r.Context()); err != nil {
Expand All @@ -149,10 +157,12 @@ func (handler FilterRuleHandler) Delete(w http.ResponseWriter, r *http.Request)
if _, err = handler.findOne(pp.NAME, historyQ, r.Context()); err != nil {
// not found or other error
problem.Render(r.Context(), w, err)
return
}

if err = historyQ.DeleteFilterByName(r.Context(), pp.NAME); err != nil {
problem.Render(r.Context(), w, err)
return
}
w.WriteHeader(204)
}
Expand All @@ -179,7 +189,7 @@ func (handler FilterRuleHandler) upsert(filterRequest *filterResource, historyQ
filterConfig.Name = filterRequest.Name

if !filters.SupportedFilterNames(filterRequest.Name) {
p := problem.ServerError
p := problem.BadRequest
p.Extras = map[string]interface{}{
"reason": fmt.Sprintf("invalid filter name, %v, no implementation for this exists", filterRequest.Name),
}
Expand All @@ -203,6 +213,7 @@ func (handler FilterRuleHandler) findOne(name string, historyQ *history.Q, ctx c
if err != nil {
return nil, err
}

rules, err := handler.rules(filter.Rules)
if err != nil {
return nil, err
Expand Down
220 changes: 220 additions & 0 deletions services/horizon/internal/actions/filter_rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package actions

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ingest/filters"
"github.com/stellar/go/services/horizon/internal/test"
)

func TestGetFilterConfigNotFound(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)

q := &history.Q{SessionInterface: tt.HorizonSession()}
handler := &FilterRuleHandler{}
recorder := httptest.NewRecorder()
handler.Get(
recorder,
makeRequest(
t,
map[string]string{},
map[string]string{"filter_name": "xyz"},
q,
),
)

resp := recorder.Result()
tt.Assert.Equal(http.StatusNotFound, resp.StatusCode)
}

func TestGetFilterConfigOneResult(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)

q := &history.Q{SessionInterface: tt.HorizonSession()}

fc1 := history.FilterConfig{
Rules: `{"whitelist": ["1","2","3"]}`,
Name: "xyz",
Enabled: false,
}

q.UpsertFilterConfig(tt.Ctx, fc1)

handler := &FilterRuleHandler{}
recorder := httptest.NewRecorder()
handler.Get(
recorder,
makeRequest(
t,
map[string]string{},
map[string]string{"filter_name": "xyz"},
q,
),
)

resp := recorder.Result()
tt.Assert.Equal(http.StatusOK, resp.StatusCode)

raw, err := ioutil.ReadAll(resp.Body)
tt.Assert.NoError(err)

var filterCfgResource filterResource
json.Unmarshal(raw, &filterCfgResource)
tt.Assert.NoError(err)

tt.Assert.Equal(filterCfgResource.Name, "xyz")
tt.Assert.Equal(len(filterCfgResource.Rules["whitelist"].([]interface{})), 3)
tt.Assert.Equal(filterCfgResource.Enabled, false)
tt.Assert.True(filterCfgResource.LastModified > 0)
}

func TestGetFilterConfigListResult(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)

q := &history.Q{SessionInterface: tt.HorizonSession()}

fc1 := history.FilterConfig{
Rules: `{"whitelist": ["1","2","3"]}`,
Name: "xyz",
Enabled: false,
}

q.UpsertFilterConfig(tt.Ctx, fc1)

handler := &FilterRuleHandler{}
recorder := httptest.NewRecorder()
handler.Get(
recorder,
makeRequest(
t,
map[string]string{},
map[string]string{},
q,
),
)

resp := recorder.Result()
tt.Assert.Equal(http.StatusOK, resp.StatusCode)

raw, err := ioutil.ReadAll(resp.Body)
tt.Assert.NoError(err)

var filterCfgResourceList []filterResource
json.Unmarshal(raw, &filterCfgResourceList)
tt.Assert.NoError(err)

tt.Assert.Len(filterCfgResourceList, 1)
tt.Assert.Equal(filterCfgResourceList[0].Name, "xyz")
tt.Assert.Equal(len(filterCfgResourceList[0].Rules["whitelist"].([]interface{})), 3)
tt.Assert.Equal(filterCfgResourceList[0].Enabled, false)
tt.Assert.True(filterCfgResourceList[0].LastModified > 0)
}

func TestUpdateUnsupportedFilterConfig(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)

q := &history.Q{SessionInterface: tt.HorizonSession()}

fc1 := history.FilterConfig{
Rules: `{"whitelist": ["1","2","3"]}`,
Name: "unsupported",
Enabled: false,
}

q.UpsertFilterConfig(tt.Ctx, fc1)

handler := &FilterRuleHandler{}
recorder := httptest.NewRecorder()
request := makeRequest(
t,
map[string]string{},
map[string]string{"filter_name": "unsupported"},
q,
)

request.Body = ioutil.NopCloser(strings.NewReader(`
{
"rules": {
"whitelist": ["4","5","6"]
},
"enabled": true,
"name": "unsupported"
}
`))

handler.Update(
recorder,
request,
)

resp := recorder.Result()
tt.Assert.Equal(http.StatusBadRequest, resp.StatusCode)
}

func TestUpdateFilterConfig(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)

q := &history.Q{SessionInterface: tt.HorizonSession()}

fc1 := history.FilterConfig{
Rules: `{"whitelist": ["1","2","3"]}`,
Name: filters.FilterAssetFilterName,
Enabled: false,
}

q.UpsertFilterConfig(tt.Ctx, fc1)

handler := &FilterRuleHandler{}
recorder := httptest.NewRecorder()
request := makeRequest(
t,
map[string]string{},
map[string]string{"filter_name": filters.FilterAssetFilterName},
q,
)

request.Body = ioutil.NopCloser(strings.NewReader(`
{
"rules": {
"whitelist": ["4","5","6"]
},
"enabled": true,
"name": "` + filters.FilterAssetFilterName + `"
}`))

handler.Update(
recorder,
request,
)

resp := recorder.Result()
tt.Assert.Equal(http.StatusOK, resp.StatusCode)

fcUpdated, err := q.GetFilterByName(tt.Ctx, filters.FilterAssetFilterName)
tt.Assert.NoError(err)

tt.Assert.Equal(fcUpdated.Name, filters.FilterAssetFilterName)
tt.Assert.Equal(fcUpdated.Enabled, true)
tt.Assert.True(fcUpdated.LastModified > 0)

var filterRules map[string]interface{}
err = json.Unmarshal([]byte(fcUpdated.Rules), &filterRules)
tt.Assert.NoError(err)
tt.Assert.Equal(filterRules["whitelist"].([]interface{})[0], "4")
}
3 changes: 2 additions & 1 deletion services/horizon/internal/httpx/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@ func (r *Router) addRoutes(config *RouterConfig, rateLimiter *throttled.HTTPRate
// DELETE /ingestion/filters/{name}
handler := actions.FilterRuleHandler{}
r.With(historyMiddleware).Post("/", handler.Create)
r.With(historyMiddleware).Put("/", handler.Update)
r.With(historyMiddleware).Put("/{filter_name}", handler.Update)
r.With(historyMiddleware).Get("/", handler.Get)
r.With(historyMiddleware).Get("/{filter_name}", handler.Get)
r.With(historyMiddleware).Delete("/", handler.Delete)
})
}
Expand Down

0 comments on commit f41359d

Please sign in to comment.