Skip to content

Commit

Permalink
Merge pull request #2811 from synapsecns/fix-responses
Browse files Browse the repository at this point in the history
Fix screener HTTP responses
  • Loading branch information
golangisfun123 authored Jun 28, 2024
2 parents 7273630 + 3fd4e9c commit 6d4586b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
27 changes: 16 additions & 11 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"
)

const (
okResponse = "OK"
errResponse = "ERROR"
)

// Screener is the interface for the screener.
type Screener interface {
Start(ctx context.Context) error
Expand Down Expand Up @@ -269,7 +274,7 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {

// Grab the body of the JSON request and unmarshal it into the blacklistBody struct.
if err := c.ShouldBindBodyWith(&blacklistBody, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"error": errResponse})
return
}

Expand Down Expand Up @@ -299,38 +304,38 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
case "create":
if err := s.db.PutBlacklistedAddress(ctx, blacklistedAddress); err != nil {
span.AddEvent("error", trace.WithAttributes(attribute.String("error", err.Error())))
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"error": errResponse})
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
c.JSON(http.StatusOK, gin.H{"status": okResponse})
return

case "update":
if err := s.db.UpdateBlacklistedAddress(ctx, blacklistedAddress.ID, blacklistedAddress); err != nil {
span.AddEvent("error", trace.WithAttributes(attribute.String("error", err.Error())))
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"error": errResponse})
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
c.JSON(http.StatusOK, gin.H{"status": okResponse})
return

case "delete":
if err := s.db.DeleteBlacklistedAddress(ctx, blacklistedAddress.ID); err != nil {
span.AddEvent("error", trace.WithAttributes(attribute.String("error", err.Error())))
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"error": errResponse})
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
c.JSON(http.StatusOK, gin.H{"status": okResponse})
return

default:
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid type"})
c.JSON(http.StatusBadRequest, gin.H{"error": errResponse})
return
}
}
Expand All @@ -351,7 +356,7 @@ func (s *screenerImpl) authMiddleware(cfg config.Config) gin.HandlerFunc {

bodyBz, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "could not read request body"})
c.JSON(http.StatusBadRequest, gin.H{"error": errResponse})
c.Abort()
return
}
Expand All @@ -360,7 +365,7 @@ func (s *screenerImpl) authMiddleware(cfg config.Config) gin.HandlerFunc {

bodyStr, err := core.BytesToJSONString(bodyBz)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "could not convert bytes to json"})
c.JSON(http.StatusBadRequest, gin.H{"error": errResponse})
c.Abort()
return
}
Expand Down Expand Up @@ -396,7 +401,7 @@ func (s *screenerImpl) authMiddleware(cfg config.Config) gin.HandlerFunc {
"error",
trace.WithAttributes(attribute.String("error", "Invalid signature"+expectedSignature)),
)
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid signature"})
c.JSON(http.StatusUnauthorized, gin.H{"error": errResponse})
c.Abort()
return
}
Expand Down
21 changes: 6 additions & 15 deletions contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (s *ScreenerSuite) SetupSuite() {
if useMetrics {
localmetrics.SetupTestJaeger(s.GetSuiteContext(), s.T())
metricsHandler = metrics.Jaeger
} else {
metricsHandler = metrics.OTLP
}

var err error
Expand Down Expand Up @@ -174,19 +176,8 @@ func (s *ScreenerSuite) TestScreener() {

// unauthorized, return on err so statuses will be only one
cfg.AppSecret = "BAD"
statuses, err = blacklistTestWithOperation(s.T(), "create", apiClient, cfg)
all(s.T(), statuses, func(status string) bool {
return status == "401 Unauthorized"
})
Equal(s.T(), len(statuses), 1)
NotNil(s.T(), err)

c := chainalysis.NewClient(s.metrics, []string{"Severe", "High"}, "key", "url")
NotNil(s.T(), c)

ot, err := c.ScreenAddress(s.GetTestContext(), "0x123")
_, err = blacklistTestWithOperation(s.T(), "create", apiClient, cfg)
NotNil(s.T(), err)
False(s.T(), ot)
}

func blacklistTestWithOperation(t *testing.T, operation string, apiClient client.ScreenerClient, cfg config.Config) (statuses []string, err error) {
Expand Down Expand Up @@ -269,8 +260,6 @@ func (m mockClient) RegisterAddress(ctx context.Context, address string) error {
return nil
}

var _ chainalysis.Client = mockClient{}

type Exposure struct {
Category string `json:"category"`
Value float64 `json:"value"`
Expand Down Expand Up @@ -303,4 +292,6 @@ func all(t *testing.T, statuses []string, f func(string) bool) {
}
}

const success = "success"
const success = "OK"

var _ chainalysis.Client = mockClient{}

0 comments on commit 6d4586b

Please sign in to comment.