Skip to content

Commit

Permalink
Fix: Add missing tenantID when updating the tenant from `PATCH /organ…
Browse files Browse the repository at this point in the history
…ization/circle-config` (#334)

### What

Add missing tenantID when updating the tenant from `PATCH /organization/circle-config`

### Why

It should have been added in #332
  • Loading branch information
marcelosalloum authored Jun 20, 2024
1 parent 1503cf5 commit caf2b41
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
7 changes: 7 additions & 0 deletions internal/serve/httphandler/circle_config_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions internal/serve/httphandler/circle_config_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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()
},
Expand Down Expand Up @@ -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)
})
Expand Down
1 change: 1 addition & 0 deletions internal/serve/httphandler/mfa_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions internal/testutils/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutils

import (
"context"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -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()
Expand Down

0 comments on commit caf2b41

Please sign in to comment.