Skip to content

Commit

Permalink
Merge branch 'develop' into sdp-974_sort-get-users
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciliaromao authored Nov 29, 2023
2 parents 80c0026 + a665505 commit 83d737a
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 129 deletions.
27 changes: 13 additions & 14 deletions cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"
"testing"

"github.com/stellar/stellar-disbursement-platform-backend/internal/services/assets"

"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -158,12 +158,12 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
testnetUSDCIssuer := keypair.MustRandom().Address()
data.CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", testnetUSDCIssuer)

assets, err := models.Assets.GetAll(ctx)
actualAssets, err := models.Assets.GetAll(ctx)
require.NoError(t, err)

assert.Len(t, assets, 1)
assert.Equal(t, "USDC", assets[0].Code)
assert.Equal(t, testnetUSDCIssuer, assets[0].Issuer)
assert.Len(t, actualAssets, 1)
assert.Equal(t, "USDC", actualAssets[0].Code)
assert.Equal(t, testnetUSDCIssuer, actualAssets[0].Issuer)

// Wallets
data.CreateWalletFixture(t, ctx, dbConnectionPool, "Vibrant Assist", "https://vibrantapp.com", "api-dev.vibrantapp.com", "https://vibrantapp.com/sdp-dev")
Expand Down Expand Up @@ -196,15 +196,14 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
require.NoError(t, err)

// Validating assets
assets, err = models.Assets.GetAll(ctx)
actualAssets, err = models.Assets.GetAll(ctx)
require.NoError(t, err)

assert.Len(t, assets, 2)
assert.Equal(t, "USDC", assets[0].Code)
assert.NotEqual(t, testnetUSDCIssuer, assets[0].Issuer)
assert.Equal(t, services.DefaultAssetsNetworkMap[utils.PubnetNetworkType]["USDC"], assets[0].Issuer)
assert.Equal(t, "XLM", assets[1].Code)
assert.Empty(t, assets[1].Issuer)
require.Len(t, actualAssets, 2)
require.Equal(t, assets.USDCAssetPubnet.Code, actualAssets[0].Code)
require.Equal(t, assets.USDCAssetPubnet.Issuer, actualAssets[0].Issuer)
require.Equal(t, assets.XLMAsset.Code, actualAssets[1].Code)
require.Empty(t, assets.XLMAsset.Issuer)

// Validating wallets
wallets, err = models.Wallets.GetAll(ctx)
Expand Down Expand Up @@ -238,7 +237,7 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
expectedLogs := []string{
"updating/inserting assets for the 'pubnet' network",
"Code: USDC",
fmt.Sprintf("Issuer: %s", services.DefaultAssetsNetworkMap[utils.PubnetNetworkType]["USDC"]),
fmt.Sprintf("Issuer: %s", assets.USDCAssetPubnet.Issuer),
"Code: XLM",
"Issuer: ",
"updating/inserting wallets for the 'pubnet' network",
Expand Down
17 changes: 15 additions & 2 deletions internal/data/disbursements.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ const (
VerificationFieldNationalID VerificationField = "NATIONAL_ID_NUMBER"
)

// GetAllVerificationFields returns all verification fields
func GetAllVerificationFields() []VerificationField {
return []VerificationField{
VerificationFieldDateOfBirth,
VerificationFieldPin,
VerificationFieldNationalID,
}
}

type DisbursementStatusHistoryEntry struct {
UserID string `json:"user_id"`
Status DisbursementStatus `json:"status"`
Expand All @@ -77,9 +86,9 @@ var (
func (d *DisbursementModel) Insert(ctx context.Context, disbursement *Disbursement) (string, error) {
const q = `
INSERT INTO
disbursements (name, status, status_history, wallet_id, asset_id, country_code)
disbursements (name, status, status_history, wallet_id, asset_id, country_code, verification_field)
VALUES
($1, $2, $3, $4, $5, $6)
($1, $2, $3, $4, $5, $6, $7)
RETURNING id
`
var newId string
Expand All @@ -90,6 +99,7 @@ func (d *DisbursementModel) Insert(ctx context.Context, disbursement *Disburseme
disbursement.Wallet.ID,
disbursement.Asset.ID,
disbursement.Country.Code,
disbursement.VerificationField,
)
if err != nil {
// check if the error is a duplicate key error
Expand Down Expand Up @@ -130,6 +140,7 @@ func (d *DisbursementModel) Get(ctx context.Context, sqlExec db.SQLExecuter, id
d.file_content,
d.created_at,
d.updated_at,
d.verification_field,
w.id as "wallet.id",
w.name as "wallet.name",
w.homepage as "wallet.homepage",
Expand Down Expand Up @@ -180,6 +191,7 @@ func (d *DisbursementModel) GetByName(ctx context.Context, sqlExec db.SQLExecute
d.file_content,
d.created_at,
d.updated_at,
d.verification_field,
w.id as "wallet.id",
w.name as "wallet.name",
w.homepage as "wallet.homepage",
Expand Down Expand Up @@ -319,6 +331,7 @@ func (d *DisbursementModel) GetAll(ctx context.Context, sqlExec db.SQLExecuter,
d.verification_field,
d.created_at,
d.updated_at,
d.verification_field,
COALESCE(d.file_name, '') as file_name,
w.id as "wallet.id",
w.name as "wallet.name",
Expand Down
8 changes: 5 additions & 3 deletions internal/data/disbursements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func Test_DisbursementModelInsert(t *testing.T) {
UserID: "user1",
},
},
Asset: asset,
Country: country,
Wallet: wallet,
Asset: asset,
Country: country,
Wallet: wallet,
VerificationField: VerificationFieldDateOfBirth,
}

t.Run("returns error when disbursement already exists is not found", func(t *testing.T) {
Expand Down Expand Up @@ -67,6 +68,7 @@ func Test_DisbursementModelInsert(t *testing.T) {
assert.Equal(t, 1, len(actual.StatusHistory))
assert.Equal(t, DraftDisbursementStatus, actual.StatusHistory[0].Status)
assert.Equal(t, "user1", actual.StatusHistory[0].UserID)
assert.Equal(t, VerificationFieldDateOfBirth, actual.VerificationField)
})
}

Expand Down
8 changes: 8 additions & 0 deletions internal/data/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ func CreateDisbursementFixture(t *testing.T, ctx context.Context, sqlExec db.SQL
if d.Country == nil {
d.Country = GetCountryFixture(t, ctx, sqlExec, FixtureCountryUKR)
}
if d.VerificationField == "" {
d.VerificationField = VerificationFieldDateOfBirth
}

// insert disbursement
if d.StatusHistory == nil {
d.StatusHistory = []DisbursementStatusHistoryEntry{{
Expand Down Expand Up @@ -583,6 +587,10 @@ func CreateDraftDisbursementFixture(t *testing.T, ctx context.Context, sqlExec d
insert.Status = DraftDisbursementStatus
}

if insert.VerificationField == "" {
insert.VerificationField = VerificationFieldDateOfBirth
}

id, err := model.Insert(ctx, &insert)
require.NoError(t, err)

Expand Down
9 changes: 5 additions & 4 deletions internal/integrationtests/integration_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ func (it *IntegrationTestsService) StartIntegrationTests(ctx context.Context, op

log.Ctx(ctx).Info("Creating disbursement using server API")
disbursement, err := it.serverAPI.CreateDisbursement(ctx, authToken, &httphandler.PostDisbursementRequest{
Name: opts.DisbursementName,
CountryCode: "USA",
WalletID: wallet.ID,
AssetID: asset.ID,
Name: opts.DisbursementName,
CountryCode: "USA",
WalletID: wallet.ID,
AssetID: asset.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
if err != nil {
return fmt.Errorf("error creating disbursement: %w", err)
Expand Down
27 changes: 17 additions & 10 deletions internal/serve/httphandler/disbursement_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ type DisbursementHandler struct {
}

type PostDisbursementRequest struct {
Name string `json:"name"`
CountryCode string `json:"country_code"`
WalletID string `json:"wallet_id"`
AssetID string `json:"asset_id"`
Name string `json:"name"`
CountryCode string `json:"country_code"`
WalletID string `json:"wallet_id"`
AssetID string `json:"asset_id"`
VerificationField data.VerificationField `json:"verification_field"`
}

type PatchDisbursementStatusRequest struct {
Expand All @@ -52,9 +53,7 @@ func (d DisbursementHandler) PostDisbursement(w http.ResponseWriter, r *http.Req
return
}

// validate request
v := validators.NewValidator()

v := validators.NewDisbursementRequestValidator(disbursementRequest.VerificationField)
v.Check(disbursementRequest.Name != "", "name", "name is required")
v.Check(disbursementRequest.CountryCode != "", "country_code", "country_code is required")
v.Check(disbursementRequest.WalletID != "", "wallet_id", "wallet_id is required")
Expand All @@ -65,6 +64,13 @@ func (d DisbursementHandler) PostDisbursement(w http.ResponseWriter, r *http.Req
return
}

verificationField := v.ValidateAndGetVerificationType()

if v.HasErrors() {
httperror.BadRequest("Verification field invalid", err, v.Errors).Render(w)
return
}

ctx := r.Context()
wallet, err := d.Models.Wallets.Get(ctx, disbursementRequest.WalletID)
if err != nil {
Expand Down Expand Up @@ -107,9 +113,10 @@ func (d DisbursementHandler) PostDisbursement(w http.ResponseWriter, r *http.Req
Status: data.DraftDisbursementStatus,
UserID: user.ID,
}},
Wallet: wallet,
Asset: asset,
Country: country,
Wallet: wallet,
Asset: asset,
Country: country,
VerificationField: verificationField,
}

newId, err := d.Models.Disbursements.Insert(ctx, &disbursement)
Expand Down
79 changes: 53 additions & 26 deletions internal/serve/httphandler/disbursement_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
{
"wallet_id": "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
"asset_id": "61dbfa89-943a-413c-b862-a2177384d321",
"country_code": "UKR"
"country_code": "UKR",
"verification_field": "date_of_birth"
}`

want := `
Expand All @@ -116,7 +117,8 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
{
"name": "My New Disbursement name 5",
"asset_id": "61dbfa89-943a-413c-b862-a2177384d321",
"country_code": "UKR"
"country_code": "UKR",
"verification_field": "date_of_birth"
}`

want := `{"error":"Request invalid", "extras": {"wallet_id": "wallet_id is required"}}`
Expand All @@ -129,7 +131,8 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
{
"name": "My New Disbursement name 5",
"wallet_id": "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
"country_code": "UKR"
"country_code": "UKR",
"verification_field": "date_of_birth"
}`

want := `{"error":"Request invalid", "extras": {"asset_id": "asset_id is required"}}`
Expand All @@ -142,20 +145,36 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
{
"name": "My New Disbursement name 5",
"wallet_id": "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
"asset_id": "61dbfa89-943a-413c-b862-a2177384d321"
"asset_id": "61dbfa89-943a-413c-b862-a2177384d321",
"verification_field": "date_of_birth"
}`

want := `{"error":"Request invalid", "extras": {"country_code": "country_code is required"}}`

assertPOSTResponse(t, ctx, handler, method, url, requestBody, want, http.StatusBadRequest)
})

t.Run("returns error when wallet_id is not valid", func(t *testing.T) {
t.Run("returns error when no verification field is provided", func(t *testing.T) {
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
WalletID: enabledWallet.ID,
})
require.NoError(t, err)

want := `{"error":"Verification field invalid", "extras": {"verification_field": "invalid parameter. valid values are: DATE_OF_BIRTH, PIN, NATIONAL_ID_NUMBER"}}`

assertPOSTResponse(t, ctx, handler, method, url, string(requestBody), want, http.StatusBadRequest)
})

t.Run("returns error when wallet_id is not valid", func(t *testing.T) {
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand All @@ -167,10 +186,11 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
t.Run("returns error when wallet is not enabled", func(t *testing.T) {
data.EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool, false, disabledWallet.ID)
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: disabledWallet.ID,
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: disabledWallet.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand All @@ -181,10 +201,11 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {

t.Run("returns error when asset_id is not valid", func(t *testing.T) {
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
WalletID: enabledWallet.ID,
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: "aab4a4a9-2493-4f37-9741-01d5bd31d68b",
WalletID: enabledWallet.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand All @@ -195,10 +216,11 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {

t.Run("returns error when country_code is not valid", func(t *testing.T) {
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: "AAA",
AssetID: asset.ID,
WalletID: enabledWallet.ID,
Name: "disbursement 1",
CountryCode: "AAA",
AssetID: asset.ID,
WalletID: enabledWallet.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand All @@ -217,10 +239,11 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {
mMonitorService.On("MonitorCounters", monitor.DisbursementsCounterTag, labels.ToMap()).Return(nil).Once()

requestBody, err := json.Marshal(PostDisbursementRequest{
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: enabledWallet.ID,
Name: "disbursement 1",
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: enabledWallet.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand All @@ -238,10 +261,11 @@ func Test_DisbursementHandler_PostDisbursement(t *testing.T) {

expectedName := "disbursement 2"
requestBody, err := json.Marshal(PostDisbursementRequest{
Name: expectedName,
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: enabledWallet.ID,
Name: expectedName,
CountryCode: country.Code,
AssetID: asset.ID,
WalletID: enabledWallet.ID,
VerificationField: data.VerificationFieldDateOfBirth,
})
require.NoError(t, err)

Expand Down Expand Up @@ -1055,6 +1079,9 @@ func Test_DisbursementHandler_PatchDisbursementStatus(t *testing.T) {
r := chi.NewRouter()
r.Patch("/disbursements/{id}/status", handler.PatchDisbursementStatus)

disbursement := data.CreateDisbursementFixture(t, ctx, dbConnectionPool, models.Disbursements, &data.Disbursement{})
require.NotNil(t, disbursement)

readyStatusHistory := []data.DisbursementStatusHistoryEntry{
{
Status: data.DraftDisbursementStatus,
Expand Down
Loading

0 comments on commit 83d737a

Please sign in to comment.