btcpayserver/BTCPayServer.Tests/TestAccount.cs

150 lines
5.3 KiB
C#
Raw Normal View History

2017-09-13 08:47:34 +02:00
using BTCPayServer.Controllers;
using System.Linq;
2017-09-13 08:47:34 +02:00
using BTCPayServer.Models.AccountViewModels;
2017-09-13 16:50:36 +02:00
using BTCPayServer.Models.StoreViewModels;
2017-10-20 21:06:37 +02:00
using BTCPayServer.Services.Invoices;
2017-09-13 08:47:34 +02:00
using Microsoft.AspNetCore.Mvc;
using NBitcoin;
using NBitpayClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;
2017-11-06 09:31:02 +01:00
using NBXplorer.DerivationStrategy;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Lightning;
2017-09-13 08:47:34 +02:00
namespace BTCPayServer.Tests
{
public class TestAccount
{
ServerTester parent;
public TestAccount(ServerTester parent)
{
this.parent = parent;
BitPay = new Bitpay(new Key(), parent.PayTester.ServerUri);
}
2017-09-13 08:47:34 +02:00
public void GrantAccess()
{
GrantAccessAsync().GetAwaiter().GetResult();
}
public void Register()
{
RegisterAsync().GetAwaiter().GetResult();
}
public BitcoinExtKey ExtKey
{
get; set;
}
public async Task GrantAccessAsync()
{
await RegisterAsync();
await CreateStoreAsync();
var store = this.GetController<StoresController>();
var pairingCode = BitPay.RequestClientAuthorization("test", Facade.Merchant);
Assert.IsType<ViewResult>(await store.RequestPairing(pairingCode.ToString()));
await store.Pair(pairingCode.ToString(), StoreId);
}
public void CreateStore()
{
CreateStoreAsync().GetAwaiter().GetResult();
}
public T GetController<T>(bool setImplicitStore = true) where T : Controller
2018-04-03 09:53:55 +02:00
{
return parent.PayTester.GetController<T>(UserId, setImplicitStore ? StoreId : null);
2018-04-03 09:53:55 +02:00
}
public async Task CreateStoreAsync()
{
var store = this.GetController<UserStoresController>();
await store.CreateStore(new CreateStoreViewModel() { Name = "Test Store" });
StoreId = store.CreatedStoreId;
}
public BTCPayNetwork SupportedNetwork { get; set; }
public void RegisterDerivationScheme(string crytoCode)
{
RegisterDerivationSchemeAsync(crytoCode).GetAwaiter().GetResult();
}
public async Task RegisterDerivationSchemeAsync(string cryptoCode)
{
SupportedNetwork = parent.NetworkProvider.GetNetwork(cryptoCode);
2018-04-29 19:33:42 +02:00
var store = parent.PayTester.GetController<StoresController>(UserId, StoreId);
ExtKey = new ExtKey().GetWif(SupportedNetwork.NBitcoinNetwork);
DerivationScheme = new DerivationStrategyFactory(SupportedNetwork.NBitcoinNetwork).Parse(ExtKey.Neuter().ToString() + "-[legacy]");
2018-05-03 18:46:52 +02:00
var vm = (StoreViewModel)((ViewResult)store.UpdateStore()).Model;
vm.SpeedPolicy = SpeedPolicy.MediumSpeed;
await store.UpdateStore(vm);
await store.AddDerivationScheme(StoreId, new DerivationSchemeViewModel()
{
DerivationScheme = DerivationScheme.ToString(),
2018-02-10 14:21:52 +01:00
Confirmation = true
}, cryptoCode);
}
2017-11-06 09:31:02 +01:00
public DerivationStrategyBase DerivationScheme { get; set; }
private async Task RegisterAsync()
{
var account = parent.PayTester.GetController<AccountController>();
await account.Register(new RegisterViewModel()
{
Email = Guid.NewGuid() + "@toto.com",
ConfirmPassword = "Kitten0@",
Password = "Kitten0@",
});
UserId = account.RegisteredUserId;
}
2017-09-13 08:47:34 +02:00
public Bitpay BitPay
{
get; set;
}
public string UserId
{
get; set;
}
2017-09-13 08:47:34 +02:00
public string StoreId
{
get; set;
}
2018-07-01 09:10:17 +02:00
public void RegisterLightningNode(string cryptoCode, LightningConnectionType connectionType)
{
RegisterLightningNodeAsync(cryptoCode, connectionType).GetAwaiter().GetResult();
}
public async Task RegisterLightningNodeAsync(string cryptoCode, LightningConnectionType connectionType)
{
2018-04-29 19:33:42 +02:00
var storeController = this.GetController<StoresController>();
2018-07-01 14:41:06 +02:00
string connectionString = null;
if (connectionType == LightningConnectionType.Charge)
2018-07-01 14:41:06 +02:00
connectionString = "type=charge;server=" + parent.MerchantCharge.Client.Uri.AbsoluteUri;
else if (connectionType == LightningConnectionType.CLightning)
2018-07-01 14:41:06 +02:00
connectionString = "type=clightning;server=" + parent.MerchantLightningD.Address.AbsoluteUri;
else if (connectionType == LightningConnectionType.LndREST)
connectionString = $"type=lnd-rest;server={parent.MerchantLnd.Swagger.BaseUrl};allowinsecure=true";
else
throw new NotSupportedException(connectionType.ToString());
await storeController.AddLightningNode(StoreId, new LightningNodeViewModel()
{
2018-07-01 14:41:06 +02:00
ConnectionString = connectionString,
2018-04-09 09:25:31 +02:00
SkipPortTest = true
}, "save", "BTC");
if (storeController.ModelState.ErrorCount != 0)
Assert.False(true, storeController.ModelState.FirstOrDefault().Value.Errors[0].ErrorMessage);
}
2018-07-01 09:10:17 +02:00
}
2017-09-13 08:47:34 +02:00
}