Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip cleanup for non-2xx status #1877

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading