func TestPaymentListener(t *testing.T) { mockEntityManager := new(mocks.MockEntityManager) mockHorizon := new(mocks.MockHorizon) mockRepository := new(mocks.MockRepository) var receiveHookStatusCode int receiveHookServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "POST", r.Method) assert.Equal(t, "1", r.FormValue("id")) assert.Equal(t, "GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ", r.FormValue("from")) assert.Equal(t, "200", r.FormValue("amount")) assert.Equal(t, "USD", r.FormValue("asset_code")) assert.Equal(t, "text", r.FormValue("memo_type")) assert.Equal(t, "testing", r.FormValue("memo")) w.WriteHeader(receiveHookStatusCode) fmt.Fprintln(w, "Response") })) defer receiveHookServer.Close() IssuingSeed := "SC34WILLHVADXMP6ACPMIRA6TRAWJMVCLPFNW7S6MUMXJVLAZUC4EWHP" ReceivingAccountId := "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" config := &config.Config{ Assets: []string{"USD", "EUR"}, Accounts: &config.Accounts{ // GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR IssuingSeed: &IssuingSeed, ReceivingAccountId: &ReceivingAccountId, }, Hooks: &config.Hooks{ Receive: &receiveHookServer.URL, }, } paymentListener, _ := NewPaymentListener( config, mockEntityManager, mockHorizon, mockRepository, mocks.Now, ) Convey("PaymentListener", t, func() { operation := horizon.PaymentResponse{ Id: "1", From: "GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ", PagingToken: "2", Amount: "200", } mocks.PredefinedTime = time.Now() dbPayment := db.ReceivedPayment{ OperationId: operation.Id, ProcessedAt: mocks.PredefinedTime, PagingToken: operation.PagingToken, } Convey("When operation is not a payment", func() { operation.Type = "create_account" dbPayment.Status = "Not a payment operation" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When payment is sent not received", func() { operation.Type = "payment" operation.To = "GDNXBMIJLLLXZYKZBHXJ45WQ4AJQBRVT776YKGQTDBHTSPMNAFO3OZOS" dbPayment.Status = "Operation sent not received" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When asset is not allowed (issuer)", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GC4WWLMUGZJMRVJM7JUVVZBY3LJ5HL4RKIPADEGKEMLAAJEDRONUGYG7" dbPayment.Status = "Asset not allowed" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When asset is not allowed (code)", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "GBP" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" dbPayment.Status = "Asset not allowed" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When transaction does not have memo", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" dbPayment.Status = "Transaction does not have memo" mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertExpectations(t) }) }) Convey("When unable to load transaction memo", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" mockHorizon.On("LoadMemo", &operation).Return(errors.New("Connection error")).Once() Convey("it should return error", func() { err := paymentListener.onPayment(operation) assert.Error(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertNotCalled(t, "Persist") }) }) Convey("When receive hook returns error", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" operation.Memo.Type = "text" operation.Memo.Value = "testing" mockHorizon.On("LoadMemo", &operation).Return(nil).Once() receiveHookStatusCode = 503 Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Error(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertNotCalled(t, "Persist") }) }) Convey("When receive hook returns success", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" operation.Memo.Type = "text" operation.Memo.Value = "testing" dbPayment.Status = "Success" mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() receiveHookStatusCode = 200 Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertExpectations(t) }) }) }) }
func TestPaymentListener(t *testing.T) { mockEntityManager := new(mocks.MockEntityManager) mockHorizon := new(mocks.MockHorizon) mockRepository := new(mocks.MockRepository) mockHTTPClient := new(mocks.MockHTTPClient) config := &config.Config{ Assets: []config.Asset{ {Code: "USD", Issuer: "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR"}, {Code: "EUR", Issuer: "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR"}, }, Accounts: config.Accounts{ IssuingAccountID: "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB", ReceivingAccountID: "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB", }, Callbacks: config.Callbacks{ Receive: "http://receive_callback", }, } paymentListener, _ := NewPaymentListener( config, mockEntityManager, mockHorizon, mockRepository, mocks.Now, ) paymentListener.client = mockHTTPClient Convey("PaymentListener", t, func() { operation := horizon.PaymentResponse{ ID: "1", From: "GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ", PagingToken: "2", Amount: "200", } mocks.PredefinedTime = time.Now() dbPayment := entities.ReceivedPayment{ OperationID: operation.ID, ProcessedAt: mocks.PredefinedTime, PagingToken: operation.PagingToken, } Convey("When operation exists", func() { operation.Type = "payment" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(&entities.ReceivedPayment{}, nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When operation is not a payment", func() { operation.Type = "create_account" dbPayment.Status = "Not a payment operation" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When payment is sent not received", func() { operation.Type = "payment" operation.To = "GDNXBMIJLLLXZYKZBHXJ45WQ4AJQBRVT776YKGQTDBHTSPMNAFO3OZOS" dbPayment.Status = "Operation sent not received" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When asset is not allowed (issuer)", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GC4WWLMUGZJMRVJM7JUVVZBY3LJ5HL4RKIPADEGKEMLAAJEDRONUGYG7" dbPayment.Status = "Asset not allowed" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When asset is not allowed (code)", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "GBP" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" dbPayment.Status = "Asset not allowed" mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockEntityManager.AssertExpectations(t) }) }) Convey("When unable to load transaction memo", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() mockHorizon.On("LoadMemo", &operation).Return(errors.New("Connection error")).Once() Convey("it should return error", func() { err := paymentListener.onPayment(operation) assert.Error(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertNotCalled(t, "Persist") }) }) Convey("When receive callback returns error", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" operation.Memo.Type = "text" operation.Memo.Value = "testing" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockHTTPClient.On( "PostForm", "http://receive_callback", url.Values{ "id": {"1"}, "from": {"GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ"}, "amount": {"200"}, "asset_code": {"USD"}, "memo_type": {"text"}, "memo": {"testing"}, "data": {""}, }, ).Return( net.BuildHTTPResponse(503, "ok"), nil, ).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Error(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertNotCalled(t, "Persist") }) }) Convey("When receive callback returns success", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" operation.Memo.Type = "text" operation.Memo.Value = "testing" dbPayment.Status = "Success" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockHTTPClient.On( "PostForm", "http://receive_callback", url.Values{ "id": {"1"}, "from": {"GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ"}, "amount": {"200"}, "asset_code": {"USD"}, "memo_type": {"text"}, "memo": {"testing"}, "data": {""}, }, ).Return( net.BuildHTTPResponse(200, "ok"), nil, ).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertExpectations(t) }) }) Convey("When receive callback returns success (no memo)", func() { operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" dbPayment.Status = "Success" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockHTTPClient.On( "PostForm", "http://receive_callback", url.Values{ "id": {"1"}, "from": {"GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ"}, "amount": {"200"}, "asset_code": {"USD"}, "memo_type": {""}, "memo": {""}, "data": {""}, }, ).Return( net.BuildHTTPResponse(200, "ok"), nil, ).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertExpectations(t) }) }) Convey("When receive callback returns success and compliance server is connected", func() { paymentListener.config.Compliance = "http://compliance" operation.Type = "payment" operation.To = "GATKP6ZQM5CSLECPMTAC5226PE367QALCPM6AFHTSULPPZMT62OOPMQB" operation.AssetCode = "USD" operation.AssetIssuer = "GD4I7AFSLZGTDL34TQLWJOM2NHLIIOEKD5RHHZUW54HERBLSIRKUOXRR" operation.Memo.Type = "hash" operation.Memo.Value = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" dbPayment.Status = "Success" mockRepository.On("GetReceivedPaymentByID", int64(1)).Return(nil, nil).Once() mockHorizon.On("LoadMemo", &operation).Return(nil).Once() mockEntityManager.On("Persist", &dbPayment).Return(nil).Once() mockHTTPClient.On( "PostForm", "http://compliance/receive", url.Values{"memo": {"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"}}, ).Return( net.BuildHTTPResponse(200, "{\"data\": \"hello world\"}"), nil, ).Once() mockHTTPClient.On( "PostForm", "http://receive_callback", url.Values{ "id": {"1"}, "from": {"GBIHSMPXC2KJ3NJVHEYTG3KCHYEUQRT45X6AWYWXMAXZOAX4F5LFZYYQ"}, "amount": {"200"}, "asset_code": {"USD"}, "memo_type": {"hash"}, "memo": {"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"}, "data": {"hello world"}, }, ).Return( net.BuildHTTPResponse(200, "ok"), nil, ).Once() Convey("it should save the status", func() { err := paymentListener.onPayment(operation) assert.Nil(t, err) mockHorizon.AssertExpectations(t) mockEntityManager.AssertExpectations(t) }) }) }) }
func TestTransactionSubmitter(t *testing.T) { mockHorizon := new(mocks.MockHorizon) mockEntityManager := new(mocks.MockEntityManager) mocks.PredefinedTime = time.Now() Convey("TransactionSubmitter", t, func() { seed := "SDZT3EJZ7FZRYNTLOZ7VH6G5UYBFO2IO3Q5PGONMILPCZU3AL7QNZHTE" accountID := "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H" Convey("LoadAccount", func() { transactionSubmitter := NewTransactionSubmitter( mockHorizon, mockEntityManager, "Test SDF Network ; September 2015", mocks.Now, ) Convey("When seed is invalid", func() { _, err := transactionSubmitter.LoadAccount("invalidSeed") assert.NotNil(t, err) }) Convey("When there is a problem loading an account", func() { mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{}, errors.New("Account not found"), ).Once() _, err := transactionSubmitter.LoadAccount(seed) assert.NotNil(t, err) mockHorizon.AssertExpectations(t) }) Convey("Successfully loads an account", func() { mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "10372672437354496", }, nil, ).Once() account, err := transactionSubmitter.LoadAccount(seed) assert.Nil(t, err) assert.Equal(t, account.Keypair.Address(), accountID) assert.Equal(t, account.Seed, seed) assert.Equal(t, account.SequenceNumber, uint64(10372672437354496)) mockHorizon.AssertExpectations(t) }) }) Convey("SubmitTransaction", func() { Convey("Submits transaction without a memo", func() { operation := b.Payment( b.Destination{"GB3W7VQ2A2IOQIS4LUFUMRC2DWXONUDH24ROLE6RS4NGUNHVSXKCABOM"}, b.NativeAmount{"100"}, ) Convey("Error response from horizon", func() { transactionSubmitter := NewTransactionSubmitter( mockHorizon, mockEntityManager, "Test SDF Network ; September 2015", mocks.Now, ) mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "10372672437354496", }, nil, ).Once() err := transactionSubmitter.InitAccount(seed) assert.Nil(t, err) txB64 := "AAAAAJbmB/pwwloZXCaCr9WR3Fue2lNhHGaDWKVOWO7MPq4QAAAAZAAk2eQAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAd2/WGgaQ6CJcXQtGRFodrubQZ9ci5ZPRlxpqNPWV1CAAAAAAAAAAADuaygAAAAAAAAAAAcw+rhAAAABAyFjIMIZOtstCWtZlVBDj1AhTmsk5v1i2GGY4by2b5mgZoXXGgFTB8sfbQav0LzFKCcxY8h+9xPMT2e9xznAfDw==" // Persist sending transaction mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "sending", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) // Persist failure mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "failure", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) mockHorizon.On("SubmitTransaction", txB64).Return( horizon.SubmitTransactionResponse{ Ledger: nil, Extras: &horizon.SubmitTransactionResponseExtras{ ResultXdr: "AAAAAAAAAGT/////AAAAAQAAAAAAAAAB////+wAAAAA=", // no_destination }, }, nil, ).Once() _, err = transactionSubmitter.SubmitTransaction(seed, operation, nil) assert.Nil(t, err) mockHorizon.AssertExpectations(t) }) Convey("Bad Sequence response from horizon", func() { transactionSubmitter := NewTransactionSubmitter( mockHorizon, mockEntityManager, "Test SDF Network ; September 2015", mocks.Now, ) mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "10372672437354496", }, nil, ).Once() err := transactionSubmitter.InitAccount(seed) assert.Nil(t, err) txB64 := "AAAAAJbmB/pwwloZXCaCr9WR3Fue2lNhHGaDWKVOWO7MPq4QAAAAZAAk2eQAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAd2/WGgaQ6CJcXQtGRFodrubQZ9ci5ZPRlxpqNPWV1CAAAAAAAAAAADuaygAAAAAAAAAAAcw+rhAAAABAyFjIMIZOtstCWtZlVBDj1AhTmsk5v1i2GGY4by2b5mgZoXXGgFTB8sfbQav0LzFKCcxY8h+9xPMT2e9xznAfDw==" // Persist sending transaction mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "sending", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) // Persist failure mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "failure", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) mockHorizon.On("SubmitTransaction", txB64).Return( horizon.SubmitTransactionResponse{ Ledger: nil, Extras: &horizon.SubmitTransactionResponseExtras{ ResultXdr: "AAAAAAAAAAD////7AAAAAA==", // tx_bad_seq }, }, nil, ).Once() // Updating sequence number mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "100", }, nil, ).Once() _, err = transactionSubmitter.SubmitTransaction(seed, operation, nil) assert.Nil(t, err) assert.Equal(t, uint64(100), transactionSubmitter.Accounts[seed].SequenceNumber) mockHorizon.AssertExpectations(t) }) Convey("Successfully submits a transaction", func() { transactionSubmitter := NewTransactionSubmitter( mockHorizon, mockEntityManager, "Test SDF Network ; September 2015", mocks.Now, ) mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "10372672437354496", }, nil, ).Once() err := transactionSubmitter.InitAccount(seed) assert.Nil(t, err) txB64 := "AAAAAJbmB/pwwloZXCaCr9WR3Fue2lNhHGaDWKVOWO7MPq4QAAAAZAAk2eQAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAd2/WGgaQ6CJcXQtGRFodrubQZ9ci5ZPRlxpqNPWV1CAAAAAAAAAAADuaygAAAAAAAAAAAcw+rhAAAABAyFjIMIZOtstCWtZlVBDj1AhTmsk5v1i2GGY4by2b5mgZoXXGgFTB8sfbQav0LzFKCcxY8h+9xPMT2e9xznAfDw==" // Persist sending transaction mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "sending", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) // Persist failure mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "4f885999be6ea7891052a53e496bcfb5c5a1a5bfb31923f649b028fdc74dd050", transaction.TransactionID) assert.Equal(t, "success", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) ledger := uint64(1486276) mockHorizon.On("SubmitTransaction", txB64).Return( horizon.SubmitTransactionResponse{Ledger: &ledger}, nil, ).Once() response, err := transactionSubmitter.SubmitTransaction(seed, operation, nil) assert.Nil(t, err) assert.Equal(t, *response.Ledger, ledger) assert.Equal(t, uint64(10372672437354497), transactionSubmitter.Accounts[seed].SequenceNumber) mockHorizon.AssertExpectations(t) }) }) Convey("Submits transaction with a memo", func() { operation := b.Payment( b.Destination{"GB3W7VQ2A2IOQIS4LUFUMRC2DWXONUDH24ROLE6RS4NGUNHVSXKCABOM"}, b.NativeAmount{"100"}, ) memo := b.MemoText{"Testing!"} Convey("Successfully submits a transaction", func() { transactionSubmitter := NewTransactionSubmitter( mockHorizon, mockEntityManager, "Test SDF Network ; September 2015", mocks.Now, ) mockHorizon.On( "LoadAccount", accountID, ).Return( horizon.AccountResponse{ AccountID: accountID, SequenceNumber: "10372672437354496", }, nil, ).Once() err := transactionSubmitter.InitAccount(seed) assert.Nil(t, err) txB64 := "AAAAAJbmB/pwwloZXCaCr9WR3Fue2lNhHGaDWKVOWO7MPq4QAAAAZAAk2eQAAAABAAAAAAAAAAEAAAAIVGVzdGluZyEAAAABAAAAAAAAAAEAAAAAd2/WGgaQ6CJcXQtGRFodrubQZ9ci5ZPRlxpqNPWV1CAAAAAAAAAAADuaygAAAAAAAAAAAcw+rhAAAABAU5ahFsd28sVKSUFcmAiEf+zSLXhf9HG/pJuQirR0s43zs7Y43vM8T3sIvJWHgwMADaZiy/D+evYWd/vS/uO8Ag==" // Persist sending transaction mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "60cb3c020b0c97352cbabdf68a822b04baea61927b0f1ac31260a9f8d0150316", transaction.TransactionID) assert.Equal(t, "sending", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) // Persist failure mockEntityManager.On( "Persist", mock.AnythingOfType("*entities.SentTransaction"), ).Return(nil).Once().Run(func(args mock.Arguments) { transaction := args.Get(0).(*entities.SentTransaction) assert.Equal(t, "60cb3c020b0c97352cbabdf68a822b04baea61927b0f1ac31260a9f8d0150316", transaction.TransactionID) assert.Equal(t, "success", string(transaction.Status)) assert.Equal(t, "GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H", transaction.Source) assert.Equal(t, mocks.PredefinedTime, transaction.SubmittedAt) assert.Equal(t, txB64, transaction.EnvelopeXdr) }) ledger := uint64(1486276) mockHorizon.On("SubmitTransaction", txB64).Return( horizon.SubmitTransactionResponse{Ledger: &ledger}, nil, ).Once() response, err := transactionSubmitter.SubmitTransaction(seed, operation, memo) assert.Nil(t, err) assert.Equal(t, *response.Ledger, ledger) assert.Equal(t, uint64(10372672437354497), transactionSubmitter.Accounts[seed].SequenceNumber) mockHorizon.AssertExpectations(t) }) }) }) }) }