From caf2b41065b57b260c9125e46a94ad576d698fbf Mon Sep 17 00:00:00 2001 From: Marcelo Salloum dos Santos Date: Thu, 20 Jun 2024 13:09:57 -0700 Subject: [PATCH] Fix: Add missing tenantID when updating the tenant from `PATCH /organization/circle-config` (#334) ### What Add missing tenantID when updating the tenant from `PATCH /organization/circle-config` ### Why It should have been added in #332 --- internal/serve/httphandler/circle_config_handler.go | 7 +++++++ .../serve/httphandler/circle_config_handler_test.go | 13 ++++++++++--- internal/serve/httphandler/mfa_handler.go | 1 + internal/testutils/http.go | 5 +++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/serve/httphandler/circle_config_handler.go b/internal/serve/httphandler/circle_config_handler.go index 8cfa64e74..20b43f243 100644 --- a/internal/serve/httphandler/circle_config_handler.go +++ b/internal/serve/httphandler/circle_config_handler.go @@ -45,6 +45,12 @@ func (r PatchCircleConfigRequest) validate() error { func (h CircleConfigHandler) Patch(w http.ResponseWriter, r *http.Request) { ctx := r.Context() + tnt, err := tenant.GetTenantFromContext(ctx) + if err != nil { + httperror.InternalError(ctx, "Cannot retrieve the tenant from the context", err, nil).Render(w) + return + } + distAccount, err := h.DistributionAccountResolver.DistributionAccountFromContext(ctx) if err != nil { httperror.InternalError(ctx, "Cannot retrieve distribution account", err, nil).Render(w) @@ -106,6 +112,7 @@ func (h CircleConfigHandler) Patch(w http.ResponseWriter, r *http.Request) { // Update tenant status to active _, err = h.TenantManager.UpdateTenantConfig(ctx, &tenant.TenantUpdate{ + ID: tnt.ID, DistributionAccountStatus: schema.AccountStatusActive, }) if err != nil { diff --git a/internal/serve/httphandler/circle_config_handler_test.go b/internal/serve/httphandler/circle_config_handler_test.go index 1465615a3..8c419be0b 100644 --- a/internal/serve/httphandler/circle_config_handler_test.go +++ b/internal/serve/httphandler/circle_config_handler_test.go @@ -33,6 +33,10 @@ func TestCircleConfigHandler_Patch(t *testing.T) { require.NoError(t, outerErr) defer dbConnectionPool.Close() + // Creates a tenant and inserts it in the context + tnt := tenant.Tenant{ID: "test-tenant-id"} + ctx := tenant.SaveTenantInContext(context.Background(), &tnt) + kp := keypair.MustRandom() encryptionPassphrase := kp.Seed() encryptionPublicKey := kp.Address() @@ -131,7 +135,7 @@ func TestCircleConfigHandler_Patch(t *testing.T) { }, }, { - name: "updates Circle configuration successfully", + name: "🎉 successfully updates Circle configuration and the tenant DistributionAccountStatus", prepareMocksFn: func(t *testing.T, mDistAccResolver *sigMocks.MockDistributionAccountResolver, mCircleClient *circle.MockClient, mTenantManager *tenant.TenantManagerMock) { t.Helper() mDistAccResolver. @@ -149,7 +153,10 @@ func TestCircleConfigHandler_Patch(t *testing.T) { Once() mTenantManager. - On("UpdateTenantConfig", mock.Anything, &tenant.TenantUpdate{DistributionAccountStatus: schema.AccountStatusActive}). + On("UpdateTenantConfig", mock.Anything, &tenant.TenantUpdate{ + ID: "test-tenant-id", + DistributionAccountStatus: schema.AccountStatusActive, + }). Return(&tenant.Tenant{}, nil). Once() }, @@ -197,7 +204,7 @@ func TestCircleConfigHandler_Patch(t *testing.T) { url := "/organization/circle-config" r.Patch(url, handler.Patch) - rr := testutils.Request(t, r, url, http.MethodPatch, strings.NewReader(tc.requestBody)) + rr := testutils.Request(t, ctx, r, url, http.MethodPatch, strings.NewReader(tc.requestBody)) assert.Equal(t, tc.statusCode, rr.Code) tc.assertions(t, rr) }) diff --git a/internal/serve/httphandler/mfa_handler.go b/internal/serve/httphandler/mfa_handler.go index 915b9506f..15efa4f77 100644 --- a/internal/serve/httphandler/mfa_handler.go +++ b/internal/serve/httphandler/mfa_handler.go @@ -7,6 +7,7 @@ import ( "github.com/stellar/go/support/http/httpdecode" "github.com/stellar/go/support/log" "github.com/stellar/go/support/render/httpjson" + "github.com/stellar/stellar-disbursement-platform-backend/internal/data" "github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror" "github.com/stellar/stellar-disbursement-platform-backend/internal/serve/validators" diff --git a/internal/testutils/http.go b/internal/testutils/http.go index f4ef00f6a..f7276a327 100644 --- a/internal/testutils/http.go +++ b/internal/testutils/http.go @@ -1,6 +1,7 @@ package testutils import ( + "context" "io" "net/http" "net/http/httptest" @@ -10,10 +11,10 @@ import ( "github.com/stretchr/testify/require" ) -func Request(t *testing.T, r *chi.Mux, url, httpMethod string, body io.Reader) *httptest.ResponseRecorder { +func Request(t *testing.T, ctx context.Context, r *chi.Mux, url, httpMethod string, body io.Reader) *httptest.ResponseRecorder { t.Helper() - req, err := http.NewRequest(httpMethod, url, body) + req, err := http.NewRequestWithContext(ctx, httpMethod, url, body) require.NoError(t, err) rr := httptest.NewRecorder()