|
| 1 | +package aml |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/code-payments/code-server/pkg/code/common" |
| 12 | + code_data "github.com/code-payments/code-server/pkg/code/data" |
| 13 | + "github.com/code-payments/code-server/pkg/code/data/currency" |
| 14 | + "github.com/code-payments/code-server/pkg/code/data/intent" |
| 15 | + currency_lib "github.com/code-payments/code-server/pkg/currency" |
| 16 | + "github.com/code-payments/code-server/pkg/testutil" |
| 17 | +) |
| 18 | + |
| 19 | +func TestGuard_SendPublicPayment_TransactionValue(t *testing.T) { |
| 20 | + env := setupAmlTest(t) |
| 21 | + |
| 22 | + owner := testutil.NewRandomAccount(t) |
| 23 | + |
| 24 | + for _, acceptableValue := range []float64{ |
| 25 | + 1, |
| 26 | + maxUsdTransactionValue / 10, |
| 27 | + maxUsdTransactionValue - 1, |
| 28 | + maxUsdTransactionValue, |
| 29 | + } { |
| 30 | + intentRecord := makeSendPublicPaymentIntent(t, owner, acceptableValue, time.Now()) |
| 31 | + |
| 32 | + allow, err := env.guard.AllowMoneyMovement(env.ctx, intentRecord) |
| 33 | + require.NoError(t, err) |
| 34 | + assert.True(t, allow) |
| 35 | + } |
| 36 | + |
| 37 | + for _, unacceptableValue := range []float64{ |
| 38 | + maxUsdTransactionValue + 1, |
| 39 | + maxUsdTransactionValue * 10, |
| 40 | + } { |
| 41 | + intentRecord := makeSendPublicPaymentIntent(t, owner, unacceptableValue, time.Now()) |
| 42 | + |
| 43 | + allow, err := env.guard.AllowMoneyMovement(env.ctx, intentRecord) |
| 44 | + require.NoError(t, err) |
| 45 | + assert.False(t, allow) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestGuard_SendPublicPayment_DailyUsdLimit(t *testing.T) { |
| 50 | + env := setupAmlTest(t) |
| 51 | + |
| 52 | + for _, tc := range []struct { |
| 53 | + consumedUsdValue float64 |
| 54 | + at time.Time |
| 55 | + expected bool |
| 56 | + }{ |
| 57 | + // Intent consumes some of the daily limit, but not all |
| 58 | + { |
| 59 | + consumedUsdValue: maxDailyUsdLimit / 2, |
| 60 | + at: time.Now().Add(-12 * time.Hour), |
| 61 | + expected: true, |
| 62 | + }, |
| 63 | + // Intent consumes the remaining daily limit |
| 64 | + { |
| 65 | + consumedUsdValue: maxDailyUsdLimit - 1, |
| 66 | + at: time.Now().Add(-12 * time.Hour), |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + // Daily limit was breached, but more than a day ago |
| 70 | + { |
| 71 | + consumedUsdValue: maxDailyUsdLimit + 1, |
| 72 | + at: time.Now().Add(-24*time.Hour - time.Minute), |
| 73 | + expected: true, |
| 74 | + }, |
| 75 | + // Daily limit is breached, but is close to expiring |
| 76 | + { |
| 77 | + consumedUsdValue: maxDailyUsdLimit, |
| 78 | + at: time.Now().Add(-24*time.Hour + time.Minute), |
| 79 | + expected: false, |
| 80 | + }, |
| 81 | + // Daily limit is breached well within the time window |
| 82 | + { |
| 83 | + consumedUsdValue: maxDailyUsdLimit + 1, |
| 84 | + at: time.Now().Add(-12 * time.Hour), |
| 85 | + expected: false, |
| 86 | + }, |
| 87 | + } { |
| 88 | + owner := testutil.NewRandomAccount(t) |
| 89 | + intentRecord := makeSendPublicPaymentIntent(t, owner, 1, time.Now()) |
| 90 | + |
| 91 | + // Sanity check the intent for $1 USD is allowed |
| 92 | + allow, err := env.guard.AllowMoneyMovement(env.ctx, intentRecord) |
| 93 | + require.NoError(t, err) |
| 94 | + assert.True(t, allow) |
| 95 | + |
| 96 | + // Save an intent to bring the user up to the desired consumed daily USD value |
| 97 | + require.NoError(t, env.data.SaveIntent(env.ctx, makeSendPublicPaymentIntent(t, owner, tc.consumedUsdValue, tc.at))) |
| 98 | + |
| 99 | + // Check whether we allow the $1 USD intent |
| 100 | + allow, err = env.guard.AllowMoneyMovement(env.ctx, intentRecord) |
| 101 | + require.NoError(t, err) |
| 102 | + assert.Equal(t, tc.expected, allow) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestGuard_ReceivePaymentsPublicly(t *testing.T) { |
| 107 | + env := setupAmlTest(t) |
| 108 | + |
| 109 | + owner := testutil.NewRandomAccount(t) |
| 110 | + |
| 111 | + for _, usdMarketValue := range []float64{ |
| 112 | + 1, |
| 113 | + 1_000_000_000_000, |
| 114 | + } { |
| 115 | + intentRecord := makeReceivePaymentsPubliclyIntent(t, owner, usdMarketValue, time.Now()) |
| 116 | + |
| 117 | + // We should always allow a public receive |
| 118 | + allow, err := env.guard.AllowMoneyMovement(env.ctx, intentRecord) |
| 119 | + require.NoError(t, err) |
| 120 | + assert.True(t, allow) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +type amlTestEnv struct { |
| 125 | + ctx context.Context |
| 126 | + data code_data.Provider |
| 127 | + guard *Guard |
| 128 | +} |
| 129 | + |
| 130 | +func setupAmlTest(t *testing.T) (env amlTestEnv) { |
| 131 | + env.ctx = context.Background() |
| 132 | + env.data = code_data.NewTestDataProvider() |
| 133 | + env.guard = NewGuard(env.data) |
| 134 | + |
| 135 | + testutil.SetupRandomSubsidizer(t, env.data) |
| 136 | + |
| 137 | + env.data.ImportExchangeRates(env.ctx, ¤cy.MultiRateRecord{ |
| 138 | + Time: time.Now(), |
| 139 | + Rates: map[string]float64{ |
| 140 | + string(currency_lib.USD): 0.1, |
| 141 | + }, |
| 142 | + }) |
| 143 | + |
| 144 | + return env |
| 145 | +} |
| 146 | + |
| 147 | +func makeSendPublicPaymentIntent(t *testing.T, owner *common.Account, usdMarketValue float64, at time.Time) *intent.Record { |
| 148 | + return &intent.Record{ |
| 149 | + IntentId: testutil.NewRandomAccount(t).PublicKey().ToBase58(), |
| 150 | + IntentType: intent.SendPublicPayment, |
| 151 | + |
| 152 | + SendPublicPaymentMetadata: &intent.SendPublicPaymentMetadata{ |
| 153 | + DestinationOwnerAccount: testutil.NewRandomAccount(t).PublicKey().ToBase58(), |
| 154 | + DestinationTokenAccount: testutil.NewRandomAccount(t).PublicKey().ToBase58(), |
| 155 | + Quantity: uint64(usdMarketValue), |
| 156 | + |
| 157 | + ExchangeRate: 1, |
| 158 | + ExchangeCurrency: currency_lib.USD, |
| 159 | + NativeAmount: usdMarketValue, |
| 160 | + UsdMarketValue: usdMarketValue, |
| 161 | + }, |
| 162 | + |
| 163 | + InitiatorOwnerAccount: owner.PublicKey().ToBase58(), |
| 164 | + |
| 165 | + State: intent.StatePending, |
| 166 | + CreatedAt: at, |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func makeReceivePaymentsPubliclyIntent(t *testing.T, owner *common.Account, usdMarketValue float64, at time.Time) *intent.Record { |
| 171 | + return &intent.Record{ |
| 172 | + IntentId: testutil.NewRandomAccount(t).PublicKey().ToBase58(), |
| 173 | + IntentType: intent.ReceivePaymentsPublicly, |
| 174 | + |
| 175 | + ReceivePaymentsPubliclyMetadata: &intent.ReceivePaymentsPubliclyMetadata{ |
| 176 | + Source: testutil.NewRandomAccount(t).PublicKey().ToBase58(), |
| 177 | + Quantity: uint64(usdMarketValue), |
| 178 | + IsRemoteSend: true, |
| 179 | + |
| 180 | + OriginalExchangeCurrency: currency_lib.USD, |
| 181 | + OriginalExchangeRate: 1.0, |
| 182 | + OriginalNativeAmount: usdMarketValue, |
| 183 | + |
| 184 | + UsdMarketValue: usdMarketValue, |
| 185 | + }, |
| 186 | + |
| 187 | + InitiatorOwnerAccount: owner.PublicKey().ToBase58(), |
| 188 | + |
| 189 | + State: intent.StatePending, |
| 190 | + CreatedAt: at, |
| 191 | + } |
| 192 | +} |
0 commit comments