2018-10-24 07:52:19 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Controllers;
|
2018-10-27 15:41:07 +02:00
|
|
|
using BTCPayServer.Data;
|
2018-10-24 07:52:19 +02:00
|
|
|
using BTCPayServer.Models;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
|
|
|
using BTCPayServer.Payments.Changelly;
|
|
|
|
using BTCPayServer.Payments.Changelly.Models;
|
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public class ChangellyTests
|
|
|
|
{
|
2019-10-08 09:31:48 +02:00
|
|
|
public const int TestTimeout = 60_000;
|
2018-10-24 07:52:19 +02:00
|
|
|
public ChangellyTests(ITestOutputHelper helper)
|
|
|
|
{
|
|
|
|
Logs.Tester = new XUnitLog(helper) {Name = "Tests"};
|
|
|
|
Logs.LogProvider = new XUnitLogProvider(helper);
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:48 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2018-10-28 14:51:02 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
2018-10-24 07:52:19 +02:00
|
|
|
public async void CanSetChangellyPaymentMethod()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
2019-10-07 09:04:25 +02:00
|
|
|
await tester.StartAsync();
|
2018-10-24 07:52:19 +02:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
var controller = tester.PayTester.GetController<StoresController>(user.UserId, user.StoreId);
|
|
|
|
|
|
|
|
|
2019-10-12 13:35:30 +02:00
|
|
|
var storeBlob = controller.CurrentStore.GetStoreBlob();
|
2018-10-24 07:52:19 +02:00
|
|
|
Assert.Null(storeBlob.ChangellySettings);
|
|
|
|
|
|
|
|
var updateModel = new UpdateChangellySettingsViewModel()
|
|
|
|
{
|
|
|
|
ApiSecret = "secret",
|
|
|
|
ApiKey = "key",
|
|
|
|
ApiUrl = "http://gozo.com",
|
|
|
|
ChangellyMerchantId = "aaa",
|
|
|
|
};
|
|
|
|
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await controller.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
var store = await tester.PayTester.StoreRepository.FindStore(user.StoreId);
|
2019-10-12 13:35:30 +02:00
|
|
|
storeBlob = controller.CurrentStore.GetStoreBlob();
|
2018-10-24 07:52:19 +02:00
|
|
|
Assert.NotNull(storeBlob.ChangellySettings);
|
|
|
|
Assert.NotNull(storeBlob.ChangellySettings);
|
|
|
|
Assert.IsType<ChangellySettings>(storeBlob.ChangellySettings);
|
|
|
|
Assert.Equal(storeBlob.ChangellySettings.ApiKey, updateModel.ApiKey);
|
|
|
|
Assert.Equal(storeBlob.ChangellySettings.ApiSecret,
|
|
|
|
updateModel.ApiSecret);
|
|
|
|
Assert.Equal(storeBlob.ChangellySettings.ApiUrl, updateModel.ApiUrl);
|
|
|
|
Assert.Equal(storeBlob.ChangellySettings.ChangellyMerchantId,
|
|
|
|
updateModel.ChangellyMerchantId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-08 09:31:48 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2018-10-28 14:51:02 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
2019-10-07 09:04:25 +02:00
|
|
|
public async Task CanToggleChangellyPaymentMethod()
|
2018-10-24 07:52:19 +02:00
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
2019-10-07 09:04:25 +02:00
|
|
|
await tester.StartAsync();
|
2018-10-24 07:52:19 +02:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
var controller = tester.PayTester.GetController<StoresController>(user.UserId, user.StoreId);
|
|
|
|
|
|
|
|
var updateModel = new UpdateChangellySettingsViewModel()
|
|
|
|
{
|
|
|
|
ApiSecret = "secret",
|
|
|
|
ApiKey = "key",
|
|
|
|
ApiUrl = "http://gozo.com",
|
|
|
|
ChangellyMerchantId = "aaa",
|
2018-10-27 18:10:07 +02:00
|
|
|
Enabled = true
|
2018-10-24 07:52:19 +02:00
|
|
|
};
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await controller.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
|
|
|
|
var store = await tester.PayTester.StoreRepository.FindStore(user.StoreId);
|
|
|
|
|
|
|
|
Assert.True(store.GetStoreBlob().ChangellySettings.Enabled);
|
|
|
|
|
|
|
|
updateModel.Enabled = false;
|
|
|
|
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await controller.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
store = await tester.PayTester.StoreRepository.FindStore(user.StoreId);
|
|
|
|
|
|
|
|
Assert.False(store.GetStoreBlob().ChangellySettings.Enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:48 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2018-10-28 14:51:02 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
2018-10-24 07:52:19 +02:00
|
|
|
public async void CannotUseChangellyApiWithoutChangellyPaymentMethodSet()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
2019-10-07 09:04:25 +02:00
|
|
|
await tester.StartAsync();
|
2018-10-24 07:52:19 +02:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
var changellyController =
|
|
|
|
tester.PayTester.GetController<ChangellyController>(user.UserId, user.StoreId);
|
2018-10-27 15:41:07 +02:00
|
|
|
changellyController.IsTest = true;
|
2018-10-28 15:07:36 +01:00
|
|
|
|
2018-10-24 07:52:19 +02:00
|
|
|
//test non existing payment method
|
|
|
|
Assert.IsType<BitpayErrorModel>(Assert
|
|
|
|
.IsType<BadRequestObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
|
|
|
.Value);
|
|
|
|
|
2018-10-27 18:10:07 +02:00
|
|
|
var updateModel = CreateDefaultChangellyParams(false);
|
2018-10-24 07:52:19 +02:00
|
|
|
var storesController = tester.PayTester.GetController<StoresController>(user.UserId, user.StoreId);
|
|
|
|
//set payment method but disabled
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.IsType<BitpayErrorModel>(Assert
|
|
|
|
.IsType<BadRequestObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
|
|
|
.Value);
|
|
|
|
|
|
|
|
updateModel.Enabled = true;
|
|
|
|
//test with enabled method
|
|
|
|
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.IsNotType<BitpayErrorModel>(Assert
|
|
|
|
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
|
|
|
.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 18:10:07 +02:00
|
|
|
UpdateChangellySettingsViewModel CreateDefaultChangellyParams(bool enabled)
|
|
|
|
{
|
|
|
|
return new UpdateChangellySettingsViewModel()
|
|
|
|
{
|
|
|
|
ApiKey = "6ed02cdf1b614d89a8c0ceb170eebb61",
|
|
|
|
ApiSecret = "8fbd66a2af5fd15a6b5f8ed0159c5842e32a18538521ffa145bd6c9e124d3483",
|
|
|
|
ChangellyMerchantId = "804298eb5753",
|
|
|
|
Enabled = enabled
|
|
|
|
};
|
|
|
|
}
|
2018-10-24 07:52:19 +02:00
|
|
|
|
2019-10-08 09:31:48 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2018-10-28 14:51:02 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
2018-10-24 07:52:19 +02:00
|
|
|
public async void CanGetCurrencyListFromChangelly()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
2019-10-07 09:04:25 +02:00
|
|
|
await tester.StartAsync();
|
2018-10-24 07:52:19 +02:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
|
|
|
|
//save changelly settings
|
2018-10-27 18:10:07 +02:00
|
|
|
var updateModel = CreateDefaultChangellyParams(true);
|
2018-10-24 07:52:19 +02:00
|
|
|
var storesController = tester.PayTester.GetController<StoresController>(user.UserId, user.StoreId);
|
|
|
|
|
|
|
|
//confirm saved
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
2018-10-28 15:07:36 +01:00
|
|
|
|
2018-10-24 07:52:19 +02:00
|
|
|
var factory = UnitTest1.CreateBTCPayRateFactory();
|
|
|
|
var fetcher = new RateFetcher(factory);
|
2020-01-18 11:23:40 +01:00
|
|
|
var httpClientFactory = TestUtils.CreateHttpFactory();
|
2018-10-27 15:41:07 +02:00
|
|
|
var changellyController = new ChangellyController(
|
2018-10-28 15:07:36 +01:00
|
|
|
new ChangellyClientProvider(tester.PayTester.StoreRepository, httpClientFactory),
|
|
|
|
tester.NetworkProvider, fetcher);
|
2018-10-27 15:41:07 +02:00
|
|
|
changellyController.IsTest = true;
|
2018-10-24 07:52:19 +02:00
|
|
|
var result = Assert
|
|
|
|
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
|
|
|
.Value as IEnumerable<CurrencyFull>;
|
2018-10-27 15:41:07 +02:00
|
|
|
Assert.True(result.Any());
|
2018-10-24 07:52:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-08 09:31:48 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2018-10-28 14:51:02 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
2018-10-24 07:52:19 +02:00
|
|
|
public async void CanCalculateToAmountForChangelly()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
2019-10-07 09:04:25 +02:00
|
|
|
await tester.StartAsync();
|
2018-10-24 07:52:19 +02:00
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
|
2018-10-27 18:10:07 +02:00
|
|
|
var updateModel = CreateDefaultChangellyParams(true);
|
2018-10-24 07:52:19 +02:00
|
|
|
var storesController = tester.PayTester.GetController<StoresController>(user.UserId, user.StoreId);
|
|
|
|
|
|
|
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
|
|
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
|
|
|
|
|
|
|
var factory = UnitTest1.CreateBTCPayRateFactory();
|
|
|
|
var fetcher = new RateFetcher(factory);
|
2020-01-18 11:23:40 +01:00
|
|
|
var httpClientFactory = TestUtils.CreateHttpFactory();
|
2018-10-27 15:41:07 +02:00
|
|
|
var changellyController = new ChangellyController(
|
2018-10-28 15:07:36 +01:00
|
|
|
new ChangellyClientProvider(tester.PayTester.StoreRepository, httpClientFactory),
|
|
|
|
tester.NetworkProvider, fetcher);
|
2018-10-27 15:41:07 +02:00
|
|
|
changellyController.IsTest = true;
|
2018-10-24 07:52:19 +02:00
|
|
|
Assert.IsType<decimal>(Assert
|
2019-03-05 09:29:52 +01:00
|
|
|
.IsType<OkObjectResult>(await changellyController.CalculateAmount(user.StoreId, "ltc", "btc", 1.0m, default))
|
2018-10-28 15:07:36 +01:00
|
|
|
.Value);
|
2018-10-24 07:52:19 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 15:07:36 +01:00
|
|
|
|
2019-10-12 13:35:30 +02:00
|
|
|
[Fact]
|
2019-10-08 09:31:48 +02:00
|
|
|
[Trait("Fast", "Fast")]
|
2018-10-28 15:07:36 +01:00
|
|
|
public void CanComputeBaseAmount()
|
|
|
|
{
|
|
|
|
Assert.Equal(1, ChangellyCalculationHelper.ComputeBaseAmount(1, 1));
|
|
|
|
Assert.Equal(0.5m, ChangellyCalculationHelper.ComputeBaseAmount(1, 0.5m));
|
|
|
|
Assert.Equal(2, ChangellyCalculationHelper.ComputeBaseAmount(0.5m, 1));
|
|
|
|
Assert.Equal(4m, ChangellyCalculationHelper.ComputeBaseAmount(1, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2019-10-08 09:31:48 +02:00
|
|
|
[Trait("Fast", "Fast")]
|
2018-10-28 15:07:36 +01:00
|
|
|
public void CanComputeCorrectAmount()
|
|
|
|
{
|
|
|
|
Assert.Equal(1, ChangellyCalculationHelper.ComputeCorrectAmount(0.5m, 1, 2));
|
|
|
|
Assert.Equal(0.25m, ChangellyCalculationHelper.ComputeCorrectAmount(0.5m, 1, 0.5m));
|
|
|
|
Assert.Equal(20, ChangellyCalculationHelper.ComputeCorrectAmount(10, 1, 2));
|
|
|
|
}
|
2018-10-24 07:52:19 +02:00
|
|
|
}
|
|
|
|
}
|