2017-09-13 15:47:34 +09:00
|
|
|
|
using BTCPayServer.Controllers;
|
2018-03-21 00:31:19 +09:00
|
|
|
|
using System.Linq;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
using BTCPayServer.Models.AccountViewModels;
|
2017-09-13 23:50:36 +09:00
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2017-10-20 14:06:37 -05:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-13 15:47:34 +09: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 00:31:02 -08:00
|
|
|
|
using NBXplorer.DerivationStrategy;
|
2018-03-21 00:31:19 +09:00
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
2018-08-23 00:24:33 +09:00
|
|
|
|
using BTCPayServer.Tests.Logging;
|
2018-08-30 11:50:39 +09:00
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
|
using BTCPayServer.Lightning.CLightning;
|
2019-01-05 00:37:09 +09:00
|
|
|
|
using BTCPayServer.Data;
|
2019-07-01 05:39:25 +02:00
|
|
|
|
using OpenIddict.Abstractions;
|
|
|
|
|
using OpenIddict.Core;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
|
{
|
|
|
|
|
public class TestAccount
|
|
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
|
ServerTester parent;
|
|
|
|
|
public TestAccount(ServerTester parent)
|
|
|
|
|
{
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
BitPay = new Bitpay(new Key(), parent.PayTester.ServerUri);
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public void GrantAccess()
|
|
|
|
|
{
|
|
|
|
|
GrantAccessAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
2017-10-11 12:20:44 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public void Register()
|
|
|
|
|
{
|
|
|
|
|
RegisterAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
2017-10-11 12:20:44 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public BitcoinExtKey ExtKey
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2017-10-11 12:20:44 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public async Task GrantAccessAsync()
|
|
|
|
|
{
|
|
|
|
|
await RegisterAsync();
|
2018-04-30 22:28:00 +09:00
|
|
|
|
await CreateStoreAsync();
|
|
|
|
|
var store = this.GetController<StoresController>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
var pairingCode = BitPay.RequestClientAuthorization("test", Facade.Merchant);
|
|
|
|
|
Assert.IsType<ViewResult>(await store.RequestPairing(pairingCode.ToString()));
|
|
|
|
|
await store.Pair(pairingCode.ToString(), StoreId);
|
|
|
|
|
}
|
2018-04-30 22:28:00 +09:00
|
|
|
|
public void CreateStore()
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2018-04-30 22:28:00 +09:00
|
|
|
|
CreateStoreAsync().GetAwaiter().GetResult();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2018-01-13 22:01:09 +09:00
|
|
|
|
|
2019-01-05 00:37:09 +09:00
|
|
|
|
public void SetNetworkFeeMode(NetworkFeeMode mode)
|
2019-01-06 15:04:30 +09:00
|
|
|
|
{
|
|
|
|
|
ModifyStore((store) =>
|
|
|
|
|
{
|
|
|
|
|
store.NetworkFeeMode = mode;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public void ModifyStore(Action<StoreViewModel> modify)
|
2019-01-05 00:37:09 +09:00
|
|
|
|
{
|
|
|
|
|
var storeController = GetController<StoresController>();
|
|
|
|
|
StoreViewModel store = (StoreViewModel)((ViewResult)storeController.UpdateStore()).Model;
|
2019-01-06 15:04:30 +09:00
|
|
|
|
modify(store);
|
2019-01-05 00:37:09 +09:00
|
|
|
|
storeController.UpdateStore(store).GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 22:00:43 +09:00
|
|
|
|
public T GetController<T>(bool setImplicitStore = true) where T : Controller
|
2018-04-03 16:53:55 +09:00
|
|
|
|
{
|
2018-04-30 22:00:43 +09:00
|
|
|
|
return parent.PayTester.GetController<T>(UserId, setImplicitStore ? StoreId : null);
|
2018-04-03 16:53:55 +09:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 22:28:00 +09:00
|
|
|
|
public async Task CreateStoreAsync()
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2018-04-30 22:28:00 +09:00
|
|
|
|
var store = this.GetController<UserStoresController>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
await store.CreateStore(new CreateStoreViewModel() { Name = "Test Store" });
|
|
|
|
|
StoreId = store.CreatedStoreId;
|
2018-07-19 19:31:17 +09:00
|
|
|
|
parent.Stores.Add(StoreId);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-10-11 12:20:44 +09:00
|
|
|
|
|
2018-01-11 22:52:28 +09:00
|
|
|
|
public BTCPayNetwork SupportedNetwork { get; set; }
|
|
|
|
|
|
2018-11-04 14:59:28 +09:00
|
|
|
|
public WalletId RegisterDerivationScheme(string crytoCode, bool segwit = false)
|
2018-01-11 17:29:48 +09:00
|
|
|
|
{
|
2018-11-04 14:59:28 +09:00
|
|
|
|
return RegisterDerivationSchemeAsync(crytoCode, segwit).GetAwaiter().GetResult();
|
2018-01-11 17:29:48 +09:00
|
|
|
|
}
|
2018-11-04 14:59:28 +09:00
|
|
|
|
public async Task<WalletId> RegisterDerivationSchemeAsync(string cryptoCode, bool segwit = false)
|
2018-01-11 17:29:48 +09:00
|
|
|
|
{
|
2019-05-29 09:43:50 +00:00
|
|
|
|
SupportedNetwork = parent.NetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2018-04-30 02:33:42 +09:00
|
|
|
|
var store = parent.PayTester.GetController<StoresController>(UserId, StoreId);
|
2018-02-23 15:21:42 +09:00
|
|
|
|
ExtKey = new ExtKey().GetWif(SupportedNetwork.NBitcoinNetwork);
|
2018-11-04 14:59:28 +09:00
|
|
|
|
DerivationScheme = new DerivationStrategyFactory(SupportedNetwork.NBitcoinNetwork).Parse(ExtKey.Neuter().ToString() + (segwit ? "" : "-[legacy]"));
|
2018-01-11 17:29:48 +09:00
|
|
|
|
await store.AddDerivationScheme(StoreId, new DerivationSchemeViewModel()
|
|
|
|
|
{
|
2018-02-23 15:21:42 +09:00
|
|
|
|
DerivationScheme = DerivationScheme.ToString(),
|
2018-02-10 22:21:52 +09:00
|
|
|
|
Confirmation = true
|
2018-03-21 02:48:11 +09:00
|
|
|
|
}, cryptoCode);
|
2018-10-10 00:13:37 +09:00
|
|
|
|
|
|
|
|
|
return new WalletId(StoreId, cryptoCode);
|
2018-01-11 17:29:48 +09:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 00:31:02 -08:00
|
|
|
|
public DerivationStrategyBase DerivationScheme { get; set; }
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
private async Task RegisterAsync()
|
|
|
|
|
{
|
|
|
|
|
var account = parent.PayTester.GetController<AccountController>();
|
2019-05-02 14:01:08 +02:00
|
|
|
|
RegisterDetails = new RegisterViewModel()
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
|
|
|
|
Email = Guid.NewGuid() + "@toto.com",
|
|
|
|
|
ConfirmPassword = "Kitten0@",
|
|
|
|
|
Password = "Kitten0@",
|
2019-05-02 14:01:08 +02:00
|
|
|
|
};
|
|
|
|
|
await account.Register(RegisterDetails);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
UserId = account.RegisteredUserId;
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2019-05-02 14:01:08 +02:00
|
|
|
|
public RegisterViewModel RegisterDetails{ get; set; }
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public Bitpay BitPay
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public string UserId
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public string StoreId
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2018-07-01 16:10:17 +09:00
|
|
|
|
|
2018-03-21 00:31:19 +09:00
|
|
|
|
public void RegisterLightningNode(string cryptoCode, LightningConnectionType connectionType)
|
2018-02-26 00:48:12 +09:00
|
|
|
|
{
|
2018-03-21 00:31:19 +09:00
|
|
|
|
RegisterLightningNodeAsync(cryptoCode, connectionType).GetAwaiter().GetResult();
|
2018-02-26 00:48:12 +09:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 00:31:19 +09:00
|
|
|
|
public async Task RegisterLightningNodeAsync(string cryptoCode, LightningConnectionType connectionType)
|
2018-02-26 00:48:12 +09:00
|
|
|
|
{
|
2018-04-30 02:33:42 +09:00
|
|
|
|
var storeController = this.GetController<StoresController>();
|
2018-05-25 10:44:59 -05:00
|
|
|
|
|
2018-07-01 21:41:06 +09:00
|
|
|
|
string connectionString = null;
|
2018-05-25 10:44:59 -05:00
|
|
|
|
if (connectionType == LightningConnectionType.Charge)
|
2018-07-01 21:41:06 +09:00
|
|
|
|
connectionString = "type=charge;server=" + parent.MerchantCharge.Client.Uri.AbsoluteUri;
|
2018-05-25 10:44:59 -05:00
|
|
|
|
else if (connectionType == LightningConnectionType.CLightning)
|
2018-08-30 11:50:39 +09:00
|
|
|
|
connectionString = "type=clightning;server=" + ((CLightningClient)parent.MerchantLightningD).Address.AbsoluteUri;
|
2018-07-08 15:33:42 +09:00
|
|
|
|
else if (connectionType == LightningConnectionType.LndREST)
|
2018-07-08 20:58:37 +09:00
|
|
|
|
connectionString = $"type=lnd-rest;server={parent.MerchantLnd.Swagger.BaseUrl};allowinsecure=true";
|
2018-05-25 10:44:59 -05:00
|
|
|
|
else
|
|
|
|
|
throw new NotSupportedException(connectionType.ToString());
|
|
|
|
|
|
2018-02-26 00:48:12 +09:00
|
|
|
|
await storeController.AddLightningNode(StoreId, new LightningNodeViewModel()
|
|
|
|
|
{
|
2018-07-01 21:41:06 +09:00
|
|
|
|
ConnectionString = connectionString,
|
2018-04-09 16:25:31 +09:00
|
|
|
|
SkipPortTest = true
|
2018-03-21 02:48:11 +09:00
|
|
|
|
}, "save", "BTC");
|
2018-03-21 00:31:19 +09:00
|
|
|
|
if (storeController.ModelState.ErrorCount != 0)
|
|
|
|
|
Assert.False(true, storeController.ModelState.FirstOrDefault().Value.Errors[0].ErrorMessage);
|
2018-02-26 00:48:12 +09:00
|
|
|
|
}
|
2019-07-01 05:39:25 +02:00
|
|
|
|
|
|
|
|
|
public async Task<BTCPayOpenIdClient> RegisterOpenIdClient(OpenIddictApplicationDescriptor descriptor, string secret = null)
|
|
|
|
|
{
|
|
|
|
|
var openIddictApplicationManager = parent.PayTester.GetService<OpenIddictApplicationManager<BTCPayOpenIdClient>>();
|
|
|
|
|
var client = new BTCPayOpenIdClient {ApplicationUserId = UserId};
|
|
|
|
|
await openIddictApplicationManager.PopulateAsync(client, descriptor);
|
|
|
|
|
await openIddictApplicationManager.CreateAsync(client, secret);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
2018-07-01 16:10:17 +09:00
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
}
|