Skip to content

Commit

Permalink
add test for spam filter (#181)
Browse files Browse the repository at this point in the history
* add test for spam filter

* remove test
  • Loading branch information
decentralgabe authored Apr 12, 2024
1 parent 74fba5a commit b9fa889
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion impl/pkg/server/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *PkarrRouter) GetRecord(c *gin.Context) {
resp, err := r.service.GetPkarr(c, *id)
if err != nil {
// TODO(gabe): provide a more maintainable way to handle custom errors
if strings.Contains("spam", err.Error()) {
if strings.Contains(err.Error(), "spam") {
LoggingRespondErrMsg(c, fmt.Sprintf("too many requests for bad key %s", *id), http.StatusTooManyRequests)
return
}
Expand Down
15 changes: 15 additions & 0 deletions impl/pkg/server/pkarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func TestPkarrRouter(t *testing.T) {
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusNotFound, w.Result().StatusCode, "unexpected %s", w.Result().Status)
})

t.Run("test get not found spam", func(t *testing.T) {
w := httptest.NewRecorder()
suffix := "cz13drbfxy3ih6xun4mw3cyiexrtfcs9gyp46o4469e93y36zhsy"
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServerURL, suffix), nil)
c := newRequestContextWithParams(w, req, map[string]string{IDParam: suffix})
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusNotFound, w.Result().StatusCode, "unexpected %s", w.Result().Status)

w = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServerURL, suffix), nil)
c = newRequestContextWithParams(w, req, map[string]string{IDParam: suffix})
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode, "unexpected %s", w.Result().Status)
})
}

func testPkarrService(t *testing.T) service.PkarrService {
Expand Down
4 changes: 2 additions & 2 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func NewPkarrService(cfg *config.Config, db storage.Storage, d *dht.DHT) (*Pkarr
}

// create a new cache for bad gets to prevent spamming the DHT
cacheConfig.LifeWindow = 120 * time.Second
cacheConfig.CleanWindow = 60 * time.Second
cacheConfig.LifeWindow = 60 * time.Second
cacheConfig.CleanWindow = 30 * time.Second
badGetCache, err := bigcache.New(context.Background(), cacheConfig)
if err != nil {
return nil, ssiutil.LoggingErrorMsg(err, "failed to instantiate badGetCache")
Expand Down

0 comments on commit b9fa889

Please sign in to comment.