Skip to content

Commit

Permalink
test: fix tests and add one more
Browse files Browse the repository at this point in the history
  • Loading branch information
markettes committed Jun 28, 2024
1 parent e9d7315 commit 8fb6d7c
Showing 1 changed file with 103 additions and 4 deletions.
107 changes: 103 additions & 4 deletions test/NodeGuard.Tests/Services/BitcoinServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ async Task GenerateTemplatePSBT_LegacyMultiSigSucceeds()
var walletWithdrawalRequestPsbtRepository = new Mock<IWalletWithdrawalRequestPsbtRepository>();
var fmutxoRepository = new Mock<IFMUTXORepository>();
var nbXplorerService = new Mock<INBXplorerService>();
var utxoTagRepository = new Mock<IUTXOTagRepository>();
var mapper = new Mock<IMapper>();
walletWithdrawalRequestRepository
.Setup((w) => w.GetById(It.IsAny<int>()))
Expand Down Expand Up @@ -183,7 +184,10 @@ async Task GenerateTemplatePSBT_LegacyMultiSigSucceeds()
fmutxoRepository
.Setup(x => x.GetLockedUTXOs(null, null))
.ReturnsAsync(new List<FMUTXO>());
var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, null);
utxoTagRepository
.Setup(x => x.GetByKeyValue(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new List<UTXOTag>());
var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, utxoTagRepository.Object);

var bitcoinService = new BitcoinService(_logger, mapper.Object, walletWithdrawalRequestRepository.Object, walletWithdrawalRequestPsbtRepository.Object, null, null, nbXplorerService.Object, coinSelectionService);

Expand Down Expand Up @@ -214,6 +218,7 @@ async Task GenerateTemplatePSBT_MultiSigSucceeds()
var walletWithdrawalRequestPsbtRepository = new Mock<IWalletWithdrawalRequestPsbtRepository>();
var fmutxoRepository = new Mock<IFMUTXORepository>();
var nbXplorerService = new Mock<INBXplorerService>();
var utxoTagRepository = new Mock<IUTXOTagRepository>();
var mapper = new Mock<IMapper>();
walletWithdrawalRequestRepository
.Setup((w) => w.GetById(It.IsAny<int>()))
Expand Down Expand Up @@ -251,8 +256,11 @@ async Task GenerateTemplatePSBT_MultiSigSucceeds()
fmutxoRepository
.Setup(x => x.GetLockedUTXOs(null , null))
.ReturnsAsync(new List<FMUTXO>());
utxoTagRepository
.Setup(x => x.GetByKeyValue(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new List<UTXOTag>());

var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, null);
var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, utxoTagRepository.Object);

var bitcoinService = new BitcoinService(_logger, mapper.Object, walletWithdrawalRequestRepository.Object, walletWithdrawalRequestPsbtRepository.Object, null, null, nbXplorerService.Object, coinSelectionService);

Expand Down Expand Up @@ -283,6 +291,7 @@ async Task GenerateTemplatePSBT_SingleSigSucceeds()
var walletWithdrawalRequestPsbtRepository = new Mock<IWalletWithdrawalRequestPsbtRepository>();
var fmutxoRepository = new Mock<IFMUTXORepository>();
var nbXplorerService = new Mock<INBXplorerService>();
var utxoTagRepository = new Mock<IUTXOTagRepository>();
var mapper = new Mock<IMapper>();
walletWithdrawalRequestRepository
.Setup((w) => w.GetById(It.IsAny<int>()))
Expand Down Expand Up @@ -319,8 +328,11 @@ async Task GenerateTemplatePSBT_SingleSigSucceeds()
fmutxoRepository
.Setup(x => x.GetLockedUTXOs(null, null))
.ReturnsAsync(new List<FMUTXO>());
utxoTagRepository
.Setup(x => x.GetByKeyValue(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new List<UTXOTag>());

var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, null);
var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, utxoTagRepository.Object);

var bitcoinService = new BitcoinService(_logger, mapper.Object, walletWithdrawalRequestRepository.Object, walletWithdrawalRequestPsbtRepository.Object, null, null, nbXplorerService.Object, coinSelectionService);

Expand All @@ -331,6 +343,89 @@ async Task GenerateTemplatePSBT_SingleSigSucceeds()
var psbt = PSBT.Parse("cHNidP8BAIkBAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/////wD/////AkBCDwAAAAAAIgAgPaPWaBQgTxHOMVfMfpX21blroUe8KAd6w2gLRelFuiCsUYkAAAAAACIAIDx3862ZOy+vKdDZ4oysyRZX0HARoqQ9LqqK2ukxoopiAAAAAE8BBDWHzwN9uUaNAAAAAYPR/OiA1LbTzxbLPvbXvtAwckIG3g+0T1zblR/ZodaiA5zBFsigPpL8htN/KJ/Ph8SPvQA/K+mSNXTSA0hgvPNuEO0CEMgwAACAAQAAgAEAAAAAAQEfgJaYAAAAAAAWABTpOvUBMqNMfl7P81etji6x4fXrMyIGA3uD9HVjgF5E+eQhHp+Na6femVYpc4bCA4DmimehAdWcGO0CEMgwAACAAQAAgAEAAAAAAAAAAAAAAAAAAA==", Network.RegTest);
result.Should().BeEquivalentTo(psbt);
}

[Fact]
async Task GenerateTemplatePSBT_SingleSigFailsFrozenUTXO()
{
// Arrange
var wallet = CreateWallet.SingleSig(_internalWallet);
var withdrawalRequest = new WalletWithdrawalRequest()
{
Id = 1,
Status = WalletWithdrawalRequestStatus.Pending,
Wallet = wallet,
WalletWithdrawalRequestPSBTs = new List<WalletWithdrawalRequestPSBT>(),
Amount = 0.01m,
DestinationAddress = "bcrt1q8k3av6q5yp83rn332lx8a90k6kukhg28hs5qw7krdq95t629hgsqk6ztmf"
};

var walletWithdrawalRequestRepository = new Mock<IWalletWithdrawalRequestRepository>();
var walletWithdrawalRequestPsbtRepository = new Mock<IWalletWithdrawalRequestPsbtRepository>();
var fmutxoRepository = new Mock<IFMUTXORepository>();
var nbXplorerService = new Mock<INBXplorerService>();
var utxoTagRepository = new Mock<IUTXOTagRepository>();
var mapper = new Mock<IMapper>();
walletWithdrawalRequestRepository
.Setup((w) => w.GetById(It.IsAny<int>()))
.ReturnsAsync(withdrawalRequest);
walletWithdrawalRequestRepository
.Setup((w) => w.AddUTXOs(It.IsAny<WalletWithdrawalRequest>(), It.IsAny<List<FMUTXO>>()))
.ReturnsAsync((true, null));
walletWithdrawalRequestPsbtRepository
.Setup((w) => w.AddAsync(It.IsAny<WalletWithdrawalRequestPSBT>()))
.ReturnsAsync((true, null));
nbXplorerService
.Setup(x => x.GetStatusAsync(default))
.ReturnsAsync(new StatusResult() { IsFullySynched = true });
nbXplorerService
.Setup(x => x.GetUnusedAsync(It.IsAny<DerivationStrategyBase>(), DerivationFeature.Change, 0, false, default))
.ReturnsAsync(new KeyPathInformation() { Address = BitcoinAddress.Create("bcrt1q83ml8tve8vh672wsm83getxfzetaquq352jr6t423tdwjvdz3f3qe4r4t7", Network.RegTest) });
nbXplorerService
.Setup(x => x.GetUTXOsAsync(It.IsAny<DerivationStrategyBase>(), default))
.ReturnsAsync(new UTXOChanges()
{
Confirmed = new UTXOChange()
{
UTXOs = new List<UTXO>()
{
new UTXO()
{
Outpoint = new OutPoint(1234, 1),
Value = new Money((long)10000000),
ScriptPubKey = wallet.GetDerivationStrategy().GetDerivation(KeyPath.Parse("0/0")).ScriptPubKey,
KeyPath = KeyPath.Parse("0/0")
}
}
}
});
fmutxoRepository
.Setup(x => x.GetLockedUTXOs(null, null))
.ReturnsAsync(new List<FMUTXO>());
utxoTagRepository
.Setup(x => x.GetByKeyValue(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new List<UTXOTag>()
{
new UTXOTag()
{
Key = Constants.IsFrozenTag,
Value = "true",
Outpoint = "00000000000000000000000000000000000000000000000000000000000004d2-1"
}
});

var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, utxoTagRepository.Object);

var bitcoinService = new BitcoinService(_logger, mapper.Object, walletWithdrawalRequestRepository.Object, walletWithdrawalRequestPsbtRepository.Object, null, null, nbXplorerService.Object, coinSelectionService);

// Act
var act = () => bitcoinService.GenerateTemplatePSBT(withdrawalRequest);

// Assert
await act
.Should()
.ThrowAsync<NoUTXOsAvailableException>()
.WithMessage("Exception of type 'NodeGuard.Helpers.NoUTXOsAvailableException' was thrown.");
}

[Fact]
async Task GenerateTemplatePSBT_Changeless_SingleSigSucceeds()
Expand Down Expand Up @@ -363,6 +458,7 @@ async Task GenerateTemplatePSBT_Changeless_SingleSigSucceeds()
var walletWithdrawalRequestRepository = new Mock<IWalletWithdrawalRequestRepository>();
var walletWithdrawalRequestPsbtRepository = new Mock<IWalletWithdrawalRequestPsbtRepository>();
var fmutxoRepository = new Mock<IFMUTXORepository>();
var utxoTagRepository = new Mock<IUTXOTagRepository>();
var nbXplorerService = new Mock<INBXplorerService>();
var mapper = new Mock<IMapper>();
walletWithdrawalRequestRepository
Expand All @@ -380,6 +476,9 @@ async Task GenerateTemplatePSBT_Changeless_SingleSigSucceeds()
nbXplorerService
.Setup(x => x.GetUnusedAsync(It.IsAny<DerivationStrategyBase>(), DerivationFeature.Change, 0, false, default))
.ReturnsAsync(new KeyPathInformation() { Address = BitcoinAddress.Create("bcrt1q83ml8tve8vh672wsm83getxfzetaquq352jr6t423tdwjvdz3f3qe4r4t7", Network.RegTest) });
utxoTagRepository
.Setup(x => x.GetByKeyValue(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new List<UTXOTag>());
nbXplorerService
.Setup(x => x.GetUTXOsAsync(It.IsAny<DerivationStrategyBase>(), default))
.ReturnsAsync(new UTXOChanges()
Expand All @@ -399,7 +498,7 @@ async Task GenerateTemplatePSBT_Changeless_SingleSigSucceeds()
.Setup(x => x.GetUTXOs(It.IsAny<IBitcoinRequest>()))
.ReturnsAsync((true, fmUtxos));

var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, null);
var coinSelectionService = new CoinSelectionService(_logger, mapper.Object, fmutxoRepository.Object, nbXplorerService.Object, null, walletWithdrawalRequestRepository.Object, utxoTagRepository.Object);

var bitcoinService = new BitcoinService(_logger, mapper.Object, walletWithdrawalRequestRepository.Object, walletWithdrawalRequestPsbtRepository.Object, null, null, nbXplorerService.Object, coinSelectionService);

Expand Down

0 comments on commit 8fb6d7c

Please sign in to comment.