Skip to content

Commit

Permalink
fix: skip cleanup for non-2xx status (#1877)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Skip the cleanup function if the status returned from the
`ResponseWriter` is a non-2xx code
* Refactor the cleanup method to be passed as an interface into
`databaseCleanup` to make it easier to test
  • Loading branch information
kangmingtay authored Dec 19, 2024
1 parent a3eecd1 commit f572ced
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/sirupsen/logrus"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/observability"
Expand Down Expand Up @@ -245,15 +246,18 @@ func (a *API) requireManualLinkingEnabled(w http.ResponseWriter, req *http.Reque
return ctx, nil
}

func (a *API) databaseCleanup(cleanup *models.Cleanup) func(http.Handler) http.Handler {
func (a *API) databaseCleanup(cleanup models.Cleaner) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)

wrappedResp := chimiddleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(wrappedResp, r)
switch r.Method {
case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
if (wrappedResp.Status() / 100) != 2 {
// don't do any cleanups for non-2xx responses
return
}
// continue

default:
return
}
Expand Down
65 changes: 65 additions & 0 deletions internal/api/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/didip/tollbooth/v5/limiter"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/storage"
)

const (
Expand Down Expand Up @@ -443,3 +445,66 @@ func (ts *MiddlewareTestSuite) TestLimitHandler() {
ts.API.limitHandler(lmt).handler(okHandler).ServeHTTP(w, req)
require.Equal(ts.T(), http.StatusTooManyRequests, w.Code)
}

type MockCleanup struct {
mock.Mock
}

func (m *MockCleanup) Clean(db *storage.Connection) (int, error) {
m.Called(db)
return 0, nil
}

func (ts *MiddlewareTestSuite) TestDatabaseCleanup() {
testHandler := func(statusCode int) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
b, _ := json.Marshal(map[string]interface{}{"message": "ok"})
w.Write([]byte(b))
})
}

cases := []struct {
desc string
statusCode int
method string
}{
{
desc: "Run cleanup successfully",
statusCode: http.StatusOK,
method: http.MethodPost,
},
{
desc: "Skip cleanup if GET",
statusCode: http.StatusOK,
method: http.MethodGet,
},
{
desc: "Skip cleanup if 3xx",
statusCode: http.StatusSeeOther,
method: http.MethodPost,
},
{
desc: "Skip cleanup if 4xx",
statusCode: http.StatusBadRequest,
method: http.MethodPost,
},
{
desc: "Skip cleanup if 5xx",
statusCode: http.StatusInternalServerError,
method: http.MethodPost,
},
}

mockCleanup := new(MockCleanup)
mockCleanup.On("Clean", mock.Anything).Return(0, nil)
for _, c := range cases {
ts.Run("DatabaseCleanup", func() {
req := httptest.NewRequest(c.method, "http://localhost", nil)
w := httptest.NewRecorder()
ts.API.databaseCleanup(mockCleanup)(testHandler(c.statusCode)).ServeHTTP(w, req)
require.Equal(ts.T(), c.statusCode, w.Code)
})
}
mockCleanup.AssertNumberOfCalls(ts.T(), "Clean", 1)
}
4 changes: 4 additions & 0 deletions internal/models/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/supabase/auth/internal/storage"
)

type Cleaner interface {
Clean(*storage.Connection) (int, error)
}

type Cleanup struct {
cleanupStatements []string

Expand Down

0 comments on commit f572ced

Please sign in to comment.