public async Task RewardAsync_AllOperationsSucceeded_ReturnsSuccess() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeCustomerWalletAddress)); _deduplicationLogMock .Setup(x => x.IsDuplicateAsync(It.IsAny <string>())) .ReturnsAsync(false); _operationsProducerMock .Setup(x => x.AddAsync( It.IsAny <string>(), It.IsAny <OperationType>(), It.IsAny <object>(), It.IsAny <string>())) .ReturnsAsync(Guid.Empty); var sut = CreateSutInstance(); var result = await sut.RewardAsync(FakeCustomerId, 1, default(string), "bonusReason", "campaignId", "conditionId"); Assert.Equal(BonusRewardError.None, result.Error); }
public async Task GetAsync_AllOperationsSucceeded_ReturnsSuccess( long total, long reservedForTransfers, long reservedForSeize, long expectedBalance) { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeCustomerWalletAddress)); _quorumOperationExecutorClientMock .Setup(x => x.AddressesApi.GetBalanceForAddressAsync(It.IsAny <string>())) .ReturnsAsync(new AddressBalanceResponse { Balance = total }); _operationsFetcherMock .Setup(x => x.GetTransfersInProgressAsync(It.IsAny <string>())) .ReturnsAsync(new List <IOperation> { new OperationEntity { Type = OperationType.TokensTransfer, ContextJson = new TokensTransferContext { Amount = reservedForTransfers }.ToJson() } }); _operationsFetcherMock .Setup(x => x.GetSeizeOperationsInProgressAsync(It.IsAny <string>())) .ReturnsAsync(new List <IOperation> { new OperationEntity { Type = OperationType.SeizeToInternal, ContextJson = new SeizeToInternalContext { Amount = reservedForSeize }.ToJson() } }); _distributedCacheMock .Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync((byte[])null); _distributedCacheMock .Setup(x => x.SetAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); var sut = CreateSutInstance(); var result = await sut.GetAsync(FakeCustomerId); Assert.Equal(CustomerBalanceError.None, result.Error); Assert.Equal(expectedBalance, result.Total); }
public async Task TransferAsync_SenderWalletNotAssigned_ReturnsFail() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(FakeSenderCustomerId)) .ReturnsAsync(CustomerWalletAddressResultModel.Failed(CustomerWalletAddressError.CustomerWalletMissing)); var sut = CreateSutInstance(); var result = await sut.P2PTransferAsync(FakeSenderCustomerId, FakeRecipientCustomerId, 1, FakeTransferId); Assert.Equal(TransferError.SenderWalletMissing, result.Error); }
public async Task ForceBalanceUpdateAsync_UpdateSucceeded_PublisherGetsCalled() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeCustomerWalletAddress)); _quorumOperationExecutorClientMock .Setup(x => x.AddressesApi.GetBalanceForAddressAsync(It.IsAny <string>())) .ReturnsAsync(new AddressBalanceResponse { Balance = ValidBalance }); _operationsFetcherMock .Setup(x => x.GetTransfersInProgressAsync(It.IsAny <string>())) .ReturnsAsync(Enumerable.Empty <IOperation>().ToList()); _operationsFetcherMock .Setup(x => x.GetSeizeOperationsInProgressAsync(It.IsAny <string>())) .ReturnsAsync(Enumerable.Empty <IOperation>().ToList()); _customerBalanceUpdatePublisherMock .Setup(x => x.PublishAsync(It.IsAny <CustomerBalanceUpdatedEvent>())) .Returns(Task.CompletedTask) .Verifiable(); _distributedCacheMock .Setup(x => x.RemoveAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); _distributedCacheMock .Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync((byte[])null); _distributedCacheMock .Setup(x => x.SetAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); var sut = CreateSutInstance(); await sut.ForceBalanceUpdateAsync( FakeCustomerId, OperationType.CustomerBonusReward, Guid.NewGuid()); _customerBalanceUpdatePublisherMock.Verify( x => x.PublishAsync(It.IsAny <CustomerBalanceUpdatedEvent>()), Times.Once()); }
public async Task RewardAsync_CustomerWallet_HasNotBeenRegisteredYet_ReturnsFail() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync( CustomerWalletAddressResultModel.Failed(CustomerWalletAddressError.CustomerWalletMissing)); var sut = CreateSutInstance(); var result = await sut.RewardAsync(FakeCustomerId, 1, default(string), "bonusReason", "campaignId", "conditionId"); Assert.Equal(BonusRewardError.CustomerWalletMissing, result.Error); }
public async Task <CustomerWalletAddressResultModel> GetCustomerWalletAsync(string customerId) { if (string.IsNullOrEmpty(customerId)) { return(CustomerWalletAddressResultModel.Failed(CustomerWalletAddressError.InvalidCustomerId)); } var walletOwner = await _walletOwnersRepository.GetByOwnerIdAsync(customerId); if (string.IsNullOrEmpty(walletOwner?.WalletId)) { return(CustomerWalletAddressResultModel.Failed(CustomerWalletAddressError.CustomerWalletMissing)); } return(CustomerWalletAddressResultModel.Succeeded(walletOwner.WalletId)); }
public async Task TransferAsync_BalanceServiceReturnsError_ReturnsFail(CustomerBalanceError balanceError, TransferError expectedError) { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeWalletAddress)); _balanceServiceMock .Setup(x => x.GetAsync(It.IsAny <string>())) .ReturnsAsync(CustomerBalanceResultModel.Failed(balanceError)); var sut = CreateSutInstance(); var result = await sut.P2PTransferAsync(FakeSenderCustomerId, FakeRecipientCustomerId, 1, FakeTransferId); Assert.Equal(expectedError, result.Error); }
public async Task TransferToExternalAsync_SenderWalletNotEnoughFunds_ReturnsFail(long transferAmount, long fee, long balanceAmount) { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeWalletAddress)); _balanceServiceMock .Setup(x => x.GetAsync(It.IsAny <string>())) .ReturnsAsync(CustomerBalanceResultModel.Succeeded(balanceAmount, FakeStakedAmount)); var sut = CreateSutInstance(); var result = await sut.TransferToExternalAsync(FakeSenderCustomerId, FakeWalletAddress, transferAmount, fee, FakeTransferId); Assert.Equal(TransferError.NotEnoughFunds, result.Error); }
public async Task GetAsync_NoWalletRegistered_ReturnsFail() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Failed(CustomerWalletAddressError.CustomerWalletMissing)); _distributedCacheMock .Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync((byte[])null); var sut = CreateSutInstance(); var result = await sut.GetAsync(FakeCustomerId); Assert.Equal(CustomerBalanceError.CustomerWalletMissing, result.Error); Assert.Equal(0, result.Total); }
public async Task RewardAsync_BonusHasAlreadyBeenAdded_ReturnsFail() { _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeCustomerWalletAddress)); _deduplicationLogMock .Setup(x => x.IsDuplicateAsync(It.IsAny <string>())) .ReturnsAsync(true); var sut = CreateSutInstance(); var result = await sut.RewardAsync(FakeCustomerId, 1, default(string), "bonusReason", "campaignId", "conditionId"); Assert.Equal(BonusRewardError.DuplicateRequest, result.Error); }
public async Task TransferToExternalAsync_IsDuplicateCheck_WorksCorrectly(bool isDuplicate, TransferError expectedError) { const long amount = 100; _walletsServiceMock .Setup(x => x.GetCustomerWalletAsync(It.IsAny <string>())) .ReturnsAsync(CustomerWalletAddressResultModel.Succeeded(FakeWalletAddress)); _balanceServiceMock .Setup(x => x.GetAsync(It.IsAny <string>())) .ReturnsAsync(CustomerBalanceResultModel.Succeeded(amount, FakeStakedAmount)); _deduplicationLogMock .Setup(x => x.IsDuplicateAsync(It.IsAny <string>())) .ReturnsAsync(isDuplicate); var sut = CreateSutInstance(); var result = await sut.TransferToExternalAsync(FakeSenderCustomerId, FakeWalletAddress, amount, 0, FakeTransferId); Assert.Equal(expectedError, result.Error); }