Skip to content

Commit

Permalink
fix: wrong InjectValidBody
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Feb 19, 2023
1 parent 6721d64 commit e21eb2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/controller/v2/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func RegisterReport(v2 *svr.V2, c Report) {
Storage: fiberstore.NewRedis(c.Redis, constant.ReportIdempotencyRedisHashKey),
RedSync: c.RedSync,
}), middlewares.InjectValidBody[types.SingularReportRequest](), c.MiddlewareGetOrCreateAccount, c.SingularReport)
v2.Post("/report/recall", c.RecallSingularReport)
v2.Post("/report/recognition", middlewares.InjectValidBody[types.SingularReportRecallRequest](), c.MiddlewareGetOrCreateAccount, c.RecognitionReport)
v2.Post("/report/recall", middlewares.InjectValidBody[types.SingularReportRecallRequest](), c.RecallSingularReport)
v2.Post("/report/recognition", c.MiddlewareGetOrCreateAccount, c.RecognitionReport)
}

func (c *Report) MiddlewareGetOrCreateAccount(ctx *fiber.Ctx) error {
Expand Down
36 changes: 32 additions & 4 deletions test/v2_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/dchest/uniuri"
"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +18,7 @@ func TestAPIV2Reports(t *testing.T) {
t.Parallel()

// helpers
reportCustom := func(req *http.Request) (*http.Response, *gjson.Result) {
jsonReqCustom := func(req *http.Request) (*http.Response, *gjson.Result) {
t.Helper()

resp := request(t, req)
Expand All @@ -30,15 +31,25 @@ func TestAPIV2Reports(t *testing.T) {
return resp, &body
}

report := func(body string, headers *http.Header) (*http.Response, *gjson.Result) {
jsonReq := func(path, body string, headers *http.Header) (*http.Response, *gjson.Result) {
t.Helper()

req := httptest.NewRequest(http.MethodPost, "/PenguinStats/api/v2/report", bytes.NewBufferString(body))
req := httptest.NewRequest(http.MethodPost, path, bytes.NewBufferString(body))
if headers != nil {
req.Header = *headers
}
req.Header.Set("Content-Type", "application/json")
return reportCustom(req)
return jsonReqCustom(req)
}

report := func(body string, headers *http.Header) (*http.Response, *gjson.Result) {
t.Helper()
return jsonReq("/PenguinStats/api/v2/report", body, headers)
}

recall := func(body string, headers *http.Header) (*http.Response, *gjson.Result) {
t.Helper()
return jsonReq("/PenguinStats/api/v2/report/recall", body, headers)
}

// tests
Expand Down Expand Up @@ -127,4 +138,21 @@ func TestAPIV2Reports(t *testing.T) {
assert.Equal(t, "hit", h.Header.Get("X-Penguin-Idempotency"), "idempotency key should be hit on duplicate request")
})
})

t.Run("recall", func(t *testing.T) {
t.Run("basic", func(t *testing.T) {
var reportHash string
{
h, j := report(ReportValidBody, nil)
assert.Equal(t, http.StatusOK, h.StatusCode)
assert.Equal(t, len(j.Get("reportHash").String()), ReportHashLen)
reportHash = j.Get("reportHash").String()
}
time.Sleep(time.Second * 2) // FIXME: wait for the report to be consumed
{
h, j := recall(`{"reportHash":"`+reportHash+`"}`, nil)
assert.Equal(t, http.StatusOK, h.StatusCode, j.String())
}
})
})
}

0 comments on commit e21eb2d

Please sign in to comment.