-
Notifications
You must be signed in to change notification settings - Fork 44
SDP-1619: Add /embedded-wallets/profile to pass verification info
#973
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -400,3 +400,93 @@ func Test_EmbeddedWalletModel_GetPendingForSubmission(t *testing.T) { | |
| assert.Contains(t, ids, pending1.Token) | ||
| assert.Contains(t, ids, pending2.Token) | ||
| } | ||
|
|
||
| func Test_EmbeddedWalletModel_GetReceiverWallet(t *testing.T) { | ||
| dbt := dbtest.Open(t) | ||
| defer dbt.Close() | ||
|
|
||
| dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) | ||
| require.NoError(t, err) | ||
| defer dbConnectionPool.Close() | ||
|
|
||
| ctx := context.Background() | ||
| embeddedWalletModel := EmbeddedWalletModel{dbConnectionPool: dbConnectionPool} | ||
|
|
||
| DeleteAllEmbeddedWalletsFixtures(t, ctx, dbConnectionPool) | ||
| defer DeleteAllEmbeddedWalletsFixtures(t, ctx, dbConnectionPool) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have to. |
||
|
|
||
| wallet := CreateWalletFixture(t, ctx, dbConnectionPool, "token", "https://example.com", "wallet.example.com", "embedded://") | ||
| receiver := CreateReceiverFixture(t, ctx, dbConnectionPool, &Receiver{}) | ||
| receiverWallet := CreateReceiverWalletFixture(t, ctx, dbConnectionPool, receiver.ID, wallet.ID, ReadyReceiversWalletStatus) | ||
| contractAddress := "CBGTG3VGUMVDZE6O4CRZ2LBCFP7O5XY2VQQQU7AVXLVDQHZLVQFRMHKX" | ||
| embedded := CreateEmbeddedWalletFixture(t, ctx, dbConnectionPool, "token-2", "hash", contractAddress, "cred-id", "pub", PendingWalletStatus) | ||
| update := EmbeddedWalletUpdate{ReceiverWalletID: receiverWallet.ID} | ||
| require.NoError(t, embeddedWalletModel.Update(ctx, dbConnectionPool, embedded.Token, update)) | ||
|
|
||
| t.Run("success", func(t *testing.T) { | ||
| wallet, err := embeddedWalletModel.GetReceiverWallet(ctx, dbConnectionPool, contractAddress) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, wallet) | ||
| assert.Equal(t, receiverWallet.ID, wallet.ID) | ||
| }) | ||
|
|
||
| t.Run("not found", func(t *testing.T) { | ||
| wallet, err := embeddedWalletModel.GetReceiverWallet(ctx, dbConnectionPool, "CDZMG22Z66UUW3Q7X7XZV3CNPAQWT7DAVBBFZTCTRAESJ5AZAVOMHFXC") | ||
| require.Error(t, err) | ||
| assert.ErrorIs(t, err, ErrRecordNotFound) | ||
| assert.Nil(t, wallet) | ||
| }) | ||
| } | ||
|
|
||
| func Test_EmbeddedWalletModel_GetPendingDisbursementAsset(t *testing.T) { | ||
| dbt := dbtest.Open(t) | ||
| defer dbt.Close() | ||
|
|
||
| dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) | ||
| require.NoError(t, err) | ||
| defer dbConnectionPool.Close() | ||
|
|
||
| ctx := context.Background() | ||
| embeddedWalletModel := EmbeddedWalletModel{dbConnectionPool: dbConnectionPool} | ||
|
|
||
| DeleteAllEmbeddedWalletsFixtures(t, ctx, dbConnectionPool) | ||
| defer DeleteAllEmbeddedWalletsFixtures(t, ctx, dbConnectionPool) | ||
|
|
||
| wallet := CreateWalletFixture(t, ctx, dbConnectionPool, "fixture-wallet", "https://example.com", "wallet.example.com", "embedded://") | ||
| receiver := CreateReceiverFixture(t, ctx, dbConnectionPool, &Receiver{}) | ||
| receiverWallet := CreateReceiverWalletFixture(t, ctx, dbConnectionPool, receiver.ID, wallet.ID, ReadyReceiversWalletStatus) | ||
| asset := CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV") | ||
| contractAddress := "CBGTG3VGUMVDZE6O4CRZ2LBCFP7O5XY2VQQQU7AVXLVDQHZLVQFRMHKX" | ||
| embedded := CreateEmbeddedWalletFixture(t, ctx, dbConnectionPool, "token-asset", "hash", contractAddress, "cred", "pub", PendingWalletStatus) | ||
| require.NoError(t, embeddedWalletModel.Update(ctx, dbConnectionPool, embedded.Token, EmbeddedWalletUpdate{ReceiverWalletID: receiverWallet.ID})) | ||
|
|
||
| disbursementModel := &DisbursementModel{dbConnectionPool: dbConnectionPool} | ||
| disbursement := CreateDisbursementFixture(t, ctx, dbConnectionPool, disbursementModel, &Disbursement{ | ||
| Wallet: wallet, | ||
| Asset: asset, | ||
| Status: StartedDisbursementStatus, | ||
| }) | ||
|
|
||
| paymentModel := &PaymentModel{dbConnectionPool: dbConnectionPool} | ||
| CreatePaymentFixture(t, ctx, dbConnectionPool, paymentModel, &Payment{ | ||
| ReceiverWallet: receiverWallet, | ||
| Disbursement: disbursement, | ||
| Asset: *asset, | ||
| Status: PendingPaymentStatus, | ||
| Amount: "15", | ||
| }) | ||
|
|
||
| t.Run("success", func(t *testing.T) { | ||
| result, err := embeddedWalletModel.GetPendingDisbursementAsset(ctx, dbConnectionPool, contractAddress) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, result) | ||
| assert.Equal(t, asset.ID, result.ID) | ||
| }) | ||
|
|
||
| t.Run("not found", func(t *testing.T) { | ||
| result, err := embeddedWalletModel.GetPendingDisbursementAsset(ctx, dbConnectionPool, "CDZMG22Z66UUW3Q7X7XZV3CNPAQWT7DAVBBFZTCTRAESJ5AZAVOMHFXC") | ||
| require.Error(t, err) | ||
| assert.ErrorIs(t, err, ErrRecordNotFound) | ||
| assert.Nil(t, result) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package httphandler | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
|
|
||
| "github.com/stellar/go-stellar-sdk/support/render/httpjson" | ||
|
|
||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/data" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/sdpcontext" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/services" | ||
| ) | ||
|
|
||
| type EmbeddedWalletProfileHandler struct { | ||
| EmbeddedWalletService services.EmbeddedWalletServiceInterface | ||
| } | ||
|
|
||
| type EmbeddedWalletProfileResponse struct { | ||
| IsVerificationPending bool `json:"is_verification_pending"` | ||
| PendingAsset *data.Asset `json:"pending_asset,omitempty"` | ||
| } | ||
|
|
||
| func (h EmbeddedWalletProfileHandler) GetProfile(rw http.ResponseWriter, req *http.Request) { | ||
| ctx := req.Context() | ||
|
|
||
| contractAddress, err := sdpcontext.GetWalletContractAddressFromContext(ctx) | ||
| if err != nil { | ||
| httperror.Unauthorized("", err, nil).Render(rw) | ||
| return | ||
| } | ||
|
|
||
| isPending, err := h.EmbeddedWalletService.IsVerificationPending(ctx, contractAddress) | ||
| if err != nil { | ||
| if errors.Is(err, services.ErrInvalidContractAddress) { | ||
| httperror.Unauthorized("", err, nil).Render(rw) | ||
| } else { | ||
| httperror.InternalError(ctx, "Failed to evaluate verification requirement", err, nil).Render(rw) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| asset, err := h.EmbeddedWalletService.GetPendingDisbursementAsset(ctx, contractAddress) | ||
| if err != nil { | ||
| httperror.InternalError(ctx, "Failed to retrieve pending disbursement asset", err, nil).Render(rw) | ||
| return | ||
| } | ||
|
|
||
| resp := EmbeddedWalletProfileResponse{IsVerificationPending: isPending, PendingAsset: asset} | ||
| httpjson.Render(rw, resp, httpjson.JSON) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package httphandler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/data" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/sdpcontext" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/services" | ||
| "github.com/stellar/stellar-disbursement-platform-backend/internal/services/mocks" | ||
| ) | ||
|
|
||
| func TestEmbeddedWalletProfileHandler_GetProfile(t *testing.T) { | ||
| contractAddress := "CCYU2FUIMK23K34U3SWCN2O2JVI6JBGUGQUILYK7GRPCIDABVVTCS7R4" | ||
|
|
||
| t.Run("success", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockSvc := mocks.NewMockEmbeddedWalletService(t) | ||
| handler := EmbeddedWalletProfileHandler{EmbeddedWalletService: mockSvc} | ||
|
|
||
| asset := &data.Asset{Code: "USDC"} | ||
|
|
||
| mockSvc.On("IsVerificationPending", mock.Anything, contractAddress). | ||
| Return(true, nil).Once() | ||
| mockSvc.On("GetPendingDisbursementAsset", mock.Anything, contractAddress). | ||
| Return(asset, nil).Once() | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/embedded-wallets/profile", nil) | ||
| ctx := sdpcontext.SetWalletContractAddressInContext(req.Context(), contractAddress) | ||
| req = req.WithContext(ctx) | ||
| resp := httptest.NewRecorder() | ||
|
|
||
| handler.GetProfile(resp, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, resp.Code) | ||
|
|
||
| var body EmbeddedWalletProfileResponse | ||
| require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &body)) | ||
| assert.True(t, body.IsVerificationPending) | ||
| assert.Equal(t, asset, body.PendingAsset) | ||
| }) | ||
|
|
||
| t.Run("unauthorized when wallet missing", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockSvc := mocks.NewMockEmbeddedWalletService(t) | ||
| handler := EmbeddedWalletProfileHandler{EmbeddedWalletService: mockSvc} | ||
|
|
||
| mockSvc.On("IsVerificationPending", mock.Anything, contractAddress). | ||
| Return(false, services.ErrInvalidContractAddress).Once() | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/embedded-wallets/profile", nil) | ||
| ctx := sdpcontext.SetWalletContractAddressInContext(req.Context(), contractAddress) | ||
| req = req.WithContext(ctx) | ||
| resp := httptest.NewRecorder() | ||
|
|
||
| handler.GetProfile(resp, req) | ||
|
|
||
| assert.Equal(t, http.StatusUnauthorized, resp.Code) | ||
| }) | ||
|
|
||
| t.Run("internal error when verification fails", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockSvc := mocks.NewMockEmbeddedWalletService(t) | ||
| handler := EmbeddedWalletProfileHandler{EmbeddedWalletService: mockSvc} | ||
|
|
||
| wrappedErr := errors.New("boom") | ||
|
|
||
| mockSvc.On("IsVerificationPending", mock.Anything, contractAddress). | ||
| Return(false, wrappedErr).Once() | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/embedded-wallets/profile", nil) | ||
| ctx := sdpcontext.SetWalletContractAddressInContext(req.Context(), contractAddress) | ||
| req = req.WithContext(ctx) | ||
| resp := httptest.NewRecorder() | ||
|
|
||
| handler.GetProfile(resp, req) | ||
|
|
||
| assert.Equal(t, http.StatusInternalServerError, resp.Code) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can there be multiple pending assets?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there can be, but we only need one, and we don’t care which one the user verified with.