code.vegaprotocol.io/vega@v0.79.0/wallet/service/v1/policies_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package v1_test 17 18 import ( 19 "context" 20 "sync" 21 "testing" 22 "time" 23 24 vgrand "code.vegaprotocol.io/vega/libs/rand" 25 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 26 walletpb "code.vegaprotocol.io/vega/protos/vega/wallet/v1" 27 "code.vegaprotocol.io/vega/wallet/service/v1" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestExplicitConsentPolicy(t *testing.T) { 34 t.Run("Requesting explicit consent succeeds", testRequestingExplicitConsentSucceeds) 35 t.Run("Canceling consent requests succeeds", testCancelingConsentRequestSucceeds) 36 t.Run("Reporting sent transaction succeeds", testReportingSentTransactionSucceeds) 37 } 38 39 func testRequestingExplicitConsentSucceeds(t *testing.T) { 40 // given 41 txn := &walletpb.SubmitTransactionRequest{} 42 txID := vgrand.RandomStr(5) 43 consentRequestsChan := make(chan v1.ConsentRequest, 1) 44 sentTransactionsChan := make(chan v1.SentTransaction, 1) 45 46 // setup 47 p := v1.NewExplicitConsentPolicy(context.Background(), consentRequestsChan, sentTransactionsChan) 48 49 go func() { 50 req := <-consentRequestsChan 51 d := v1.ConsentConfirmation{TxID: txID, Decision: false} 52 req.Confirmation <- d 53 }() 54 55 // when 56 answer, err := p.Ask(txn, txID, time.Now()) 57 require.Nil(t, err) 58 require.False(t, answer) 59 } 60 61 func testCancelingConsentRequestSucceeds(t *testing.T) { 62 // given 63 ctx, cancelFn := context.WithCancel(context.Background()) 64 txn := &walletpb.SubmitTransactionRequest{} 65 txID := vgrand.RandomStr(5) 66 // Channels have a smaller buffer than the number of requests, on purpose. 67 // We have to ensure channels are not blocking and preventing interruption 68 // when full. 69 consentRequestsChan := make(chan v1.ConsentRequest, 1) 70 sentTransactionsChan := make(chan v1.SentTransaction, 1) 71 72 // setup 73 p := v1.NewExplicitConsentPolicy(ctx, consentRequestsChan, sentTransactionsChan) 74 75 var wg sync.WaitGroup 76 for i := 0; i < 5; i++ { 77 wg.Add(1) 78 go func() { 79 defer wg.Done() 80 answer, err := p.Ask(txn, txID, time.Now()) 81 require.ErrorIs(t, err, v1.ErrInterruptedConsentRequest) 82 assert.False(t, answer) 83 }() 84 } 85 86 // interrupting the consent requests 87 cancelFn() 88 89 // waiting for all consent request to be interrupted 90 wg.Wait() 91 } 92 93 func testReportingSentTransactionSucceeds(t *testing.T) { 94 txID := vgrand.RandomStr(5) 95 txHash := vgrand.RandomStr(5) 96 consentRequestsChan := make(chan v1.ConsentRequest, 1) 97 sentTransactionsChan := make(chan v1.SentTransaction, 1) 98 99 // setup 100 p := v1.NewExplicitConsentPolicy(context.Background(), consentRequestsChan, sentTransactionsChan) 101 102 // when 103 p.Report(v1.SentTransaction{ 104 TxHash: txHash, 105 TxID: txID, 106 Tx: &commandspb.Transaction{}, 107 }) 108 109 // then 110 sentTransaction := <-sentTransactionsChan 111 require.Equal(t, txHash, sentTransaction.TxHash) 112 require.Equal(t, txID, sentTransaction.TxID) 113 }