2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Controllers;
|
2018-03-20 16:31:19 +01:00
|
|
|
|
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;
|
2018-03-20 16:31:19 +01:00
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
2018-08-22 17:24:33 +02:00
|
|
|
|
using BTCPayServer.Tests.Logging;
|
2018-08-30 04:50:39 +02:00
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
|
using BTCPayServer.Lightning.CLightning;
|
2019-01-04 16:37:09 +01:00
|
|
|
|
using BTCPayServer.Data;
|
2019-10-12 13:35:30 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-02-19 09:35:23 +01:00
|
|
|
|
using NBXplorer.Models;
|
2020-03-16 08:36:55 +01:00
|
|
|
|
using BTCPayServer.Client;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
|
{
|
|
|
|
|
public class TestAccount
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
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
|
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
|
public void GrantAccess(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
|
GrantAccessAsync(isAdmin).GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-10-11 05:20:44 +02:00
|
|
|
|
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public async Task MakeAdmin(bool isAdmin = true)
|
2019-10-12 13:35:30 +02:00
|
|
|
|
{
|
|
|
|
|
var userManager = parent.PayTester.GetService<UserManager<ApplicationUser>>();
|
|
|
|
|
var u = await userManager.FindByIdAsync(UserId);
|
2020-03-19 11:11:15 +01:00
|
|
|
|
if (isAdmin)
|
|
|
|
|
await userManager.AddToRoleAsync(u, Roles.ServerAdmin);
|
|
|
|
|
else
|
|
|
|
|
await userManager.RemoveFromRoleAsync(u, Roles.ServerAdmin);
|
2020-02-19 09:35:23 +01:00
|
|
|
|
IsAdmin = true;
|
2019-10-12 13:35:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 16:57:54 +01:00
|
|
|
|
public Task<BTCPayServerClient> CreateClient()
|
2020-03-20 17:14:47 +01:00
|
|
|
|
{
|
2020-03-25 16:57:54 +01:00
|
|
|
|
return Task.FromResult(new BTCPayServerClient(parent.PayTester.ServerUri, RegisterDetails.Email, RegisterDetails.Password));
|
2020-03-20 17:14:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 08:36:55 +01:00
|
|
|
|
public async Task<BTCPayServerClient> CreateClient(params string[] permissions)
|
|
|
|
|
{
|
|
|
|
|
var manageController = parent.PayTester.GetController<ManageController>(UserId, StoreId, IsAdmin);
|
|
|
|
|
var x = Assert.IsType<RedirectToActionResult>(await manageController.AddApiKey(
|
|
|
|
|
new ManageController.AddApiKeyViewModel()
|
|
|
|
|
{
|
|
|
|
|
PermissionValues = permissions.Select(s => new ManageController.AddApiKeyViewModel.PermissionValueItem()
|
|
|
|
|
{
|
|
|
|
|
Permission = s,
|
|
|
|
|
Value = true
|
|
|
|
|
}).ToList(),
|
|
|
|
|
StoreMode = ManageController.AddApiKeyViewModel.ApiKeyStoreMode.AllStores
|
|
|
|
|
}));
|
|
|
|
|
var statusMessage = manageController.TempData.GetStatusMessageModel();
|
|
|
|
|
Assert.NotNull(statusMessage);
|
|
|
|
|
var apiKey = statusMessage.Html.Substring(statusMessage.Html.IndexOf("<code>") + 6);
|
|
|
|
|
apiKey = apiKey.Substring(0, apiKey.IndexOf("</code>"));
|
|
|
|
|
return new BTCPayServerClient(parent.PayTester.ServerUri, apiKey);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
|
public void Register(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
|
RegisterAsync(isAdmin).GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2020-01-06 13:57:32 +01:00
|
|
|
|
public async Task GrantAccessAsync(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
|
await RegisterAsync(isAdmin);
|
2018-04-30 15:28:00 +02:00
|
|
|
|
await CreateStoreAsync();
|
|
|
|
|
var store = this.GetController<StoresController>();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var pairingCode = BitPay.RequestClientAuthorization("test", Facade.Merchant);
|
|
|
|
|
Assert.IsType<ViewResult>(await store.RequestPairing(pairingCode.ToString()));
|
|
|
|
|
await store.Pair(pairingCode.ToString(), StoreId);
|
|
|
|
|
}
|
2020-03-27 06:17:31 +01:00
|
|
|
|
|
|
|
|
|
public BTCPayServerClient CreateClientFromAPIKey(string apiKey)
|
|
|
|
|
{
|
|
|
|
|
return new BTCPayServerClient(parent.PayTester.ServerUri, apiKey);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 15:28:00 +02:00
|
|
|
|
public void CreateStore()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-04-30 15:28:00 +02:00
|
|
|
|
CreateStoreAsync().GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2018-01-13 14:01:09 +01:00
|
|
|
|
|
2019-01-04 16:37:09 +01:00
|
|
|
|
public void SetNetworkFeeMode(NetworkFeeMode mode)
|
2019-01-06 07:04:30 +01:00
|
|
|
|
{
|
|
|
|
|
ModifyStore((store) =>
|
|
|
|
|
{
|
|
|
|
|
store.NetworkFeeMode = mode;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public void ModifyStore(Action<StoreViewModel> modify)
|
2019-01-04 16:37:09 +01:00
|
|
|
|
{
|
|
|
|
|
var storeController = GetController<StoresController>();
|
|
|
|
|
StoreViewModel store = (StoreViewModel)((ViewResult)storeController.UpdateStore()).Model;
|
2019-01-06 07:04:30 +01:00
|
|
|
|
modify(store);
|
2019-01-04 16:37:09 +01:00
|
|
|
|
storeController.UpdateStore(store).GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 15:00:43 +02:00
|
|
|
|
public T GetController<T>(bool setImplicitStore = true) where T : Controller
|
2018-04-03 09:53:55 +02:00
|
|
|
|
{
|
2019-10-12 13:35:30 +02:00
|
|
|
|
var controller = parent.PayTester.GetController<T>(UserId, setImplicitStore ? StoreId : null, IsAdmin);
|
|
|
|
|
return controller;
|
2018-04-03 09:53:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 15:28:00 +02:00
|
|
|
|
public async Task CreateStoreAsync()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-04-30 15:28:00 +02:00
|
|
|
|
var store = this.GetController<UserStoresController>();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
await store.CreateStore(new CreateStoreViewModel() { Name = "Test Store" });
|
|
|
|
|
StoreId = store.CreatedStoreId;
|
2018-07-19 12:31:17 +02:00
|
|
|
|
parent.Stores.Add(StoreId);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-10-11 05:20:44 +02:00
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
public BTCPayNetwork SupportedNetwork { get; set; }
|
|
|
|
|
|
2020-02-19 09:35:23 +01:00
|
|
|
|
public WalletId RegisterDerivationScheme(string crytoCode, bool segwit = false, bool importKeysToNBX = false)
|
2018-01-11 09:29:48 +01:00
|
|
|
|
{
|
2020-02-19 09:35:23 +01:00
|
|
|
|
return RegisterDerivationSchemeAsync(crytoCode, segwit, importKeysToNBX).GetAwaiter().GetResult();
|
2018-01-11 09:29:48 +01:00
|
|
|
|
}
|
2020-02-19 09:35:23 +01:00
|
|
|
|
public async Task<WalletId> RegisterDerivationSchemeAsync(string cryptoCode, bool segwit = false, bool importKeysToNBX = false)
|
2018-01-11 09:29:48 +01:00
|
|
|
|
{
|
2019-05-29 11:43:50 +02:00
|
|
|
|
SupportedNetwork = parent.NetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2018-04-29 19:33:42 +02:00
|
|
|
|
var store = parent.PayTester.GetController<StoresController>(UserId, StoreId);
|
2020-02-19 09:35:23 +01:00
|
|
|
|
GenerateWalletResponseV = await parent.ExplorerClient.GenerateWalletAsync(new GenerateWalletRequest()
|
|
|
|
|
{
|
|
|
|
|
ScriptPubKeyType = segwit ? ScriptPubKeyType.Segwit : ScriptPubKeyType.Legacy,
|
|
|
|
|
SavePrivateKeys = importKeysToNBX
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-11 09:29:48 +01:00
|
|
|
|
await store.AddDerivationScheme(StoreId, new DerivationSchemeViewModel()
|
|
|
|
|
{
|
2020-02-19 09:35:23 +01:00
|
|
|
|
Enabled = true,
|
|
|
|
|
CryptoCode = cryptoCode,
|
|
|
|
|
Network = SupportedNetwork,
|
|
|
|
|
RootFingerprint = GenerateWalletResponseV.AccountKeyPath.MasterFingerprint.ToString(),
|
|
|
|
|
RootKeyPath = SupportedNetwork.GetRootKeyPath(),
|
|
|
|
|
Source = "NBXplorer",
|
|
|
|
|
AccountKey = GenerateWalletResponseV.AccountHDKey.Neuter().ToWif(),
|
|
|
|
|
DerivationSchemeFormat = "BTCPay",
|
|
|
|
|
KeyPath = GenerateWalletResponseV.AccountKeyPath.KeyPath.ToString(),
|
2018-02-23 07:21:42 +01:00
|
|
|
|
DerivationScheme = DerivationScheme.ToString(),
|
2018-02-10 14:21:52 +01:00
|
|
|
|
Confirmation = true
|
2018-03-20 18:48:11 +01:00
|
|
|
|
}, cryptoCode);
|
2018-10-09 17:13:37 +02:00
|
|
|
|
return new WalletId(StoreId, cryptoCode);
|
2018-01-11 09:29:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
|
public async Task EnablePayJoin()
|
|
|
|
|
{
|
|
|
|
|
var storeController = parent.PayTester.GetController<StoresController>(UserId, StoreId);
|
2020-03-17 08:43:42 +01:00
|
|
|
|
var storeVM =
|
|
|
|
|
Assert.IsType<StoreViewModel>(Assert
|
|
|
|
|
.IsType<ViewResult>(storeController.UpdateStore()).Model);
|
2020-01-06 13:57:32 +01:00
|
|
|
|
|
2020-03-17 08:43:42 +01:00
|
|
|
|
storeVM.PayJoinEnabled = true;
|
2020-01-06 13:57:32 +01:00
|
|
|
|
|
2020-03-17 08:43:42 +01:00
|
|
|
|
Assert.Equal(nameof(storeController.UpdateStore),
|
2020-01-06 13:57:32 +01:00
|
|
|
|
Assert.IsType<RedirectToActionResult>(
|
2020-03-17 08:43:42 +01:00
|
|
|
|
await storeController.UpdateStore(storeVM)).ActionName);
|
2020-01-06 13:57:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 09:35:23 +01:00
|
|
|
|
public GenerateWalletResponse GenerateWalletResponseV { get; set; }
|
|
|
|
|
|
|
|
|
|
public DerivationStrategyBase DerivationScheme
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GenerateWalletResponseV.DerivationScheme;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-06 09:31:02 +01:00
|
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
|
private async Task RegisterAsync(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
var account = parent.PayTester.GetController<AccountController>();
|
2019-05-02 14:01:08 +02:00
|
|
|
|
RegisterDetails = new RegisterViewModel()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
Email = Guid.NewGuid() + "@toto.com",
|
|
|
|
|
ConfirmPassword = "Kitten0@",
|
|
|
|
|
Password = "Kitten0@",
|
2020-01-06 13:57:32 +01:00
|
|
|
|
IsAdmin = isAdmin
|
2019-05-02 14:01:08 +02:00
|
|
|
|
};
|
|
|
|
|
await account.Register(RegisterDetails);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
UserId = account.RegisteredUserId;
|
2020-02-29 06:15:06 +01:00
|
|
|
|
IsAdmin = account.RegisteredAdmin;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2019-05-02 14:01:08 +02:00
|
|
|
|
public RegisterViewModel RegisterDetails{ get; set; }
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public Bitpay BitPay
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public string UserId
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public string StoreId
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2019-10-12 13:35:30 +02:00
|
|
|
|
public bool IsAdmin { get; internal set; }
|
2018-07-01 09:10:17 +02:00
|
|
|
|
|
2018-03-20 16:31:19 +01:00
|
|
|
|
public void RegisterLightningNode(string cryptoCode, LightningConnectionType connectionType)
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
2018-03-20 16:31:19 +01:00
|
|
|
|
RegisterLightningNodeAsync(cryptoCode, connectionType).GetAwaiter().GetResult();
|
2018-02-25 16:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 16:31:19 +01:00
|
|
|
|
public async Task RegisterLightningNodeAsync(string cryptoCode, LightningConnectionType connectionType)
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
2018-04-29 19:33:42 +02:00
|
|
|
|
var storeController = this.GetController<StoresController>();
|
2018-05-25 17:44:59 +02:00
|
|
|
|
|
2018-07-01 14:41:06 +02:00
|
|
|
|
string connectionString = null;
|
2018-05-25 17:44:59 +02:00
|
|
|
|
if (connectionType == LightningConnectionType.Charge)
|
2018-07-01 14:41:06 +02:00
|
|
|
|
connectionString = "type=charge;server=" + parent.MerchantCharge.Client.Uri.AbsoluteUri;
|
2018-05-25 17:44:59 +02:00
|
|
|
|
else if (connectionType == LightningConnectionType.CLightning)
|
2018-08-30 04:50:39 +02:00
|
|
|
|
connectionString = "type=clightning;server=" + ((CLightningClient)parent.MerchantLightningD).Address.AbsoluteUri;
|
2018-07-08 08:33:42 +02:00
|
|
|
|
else if (connectionType == LightningConnectionType.LndREST)
|
2018-07-08 13:58:37 +02:00
|
|
|
|
connectionString = $"type=lnd-rest;server={parent.MerchantLnd.Swagger.BaseUrl};allowinsecure=true";
|
2018-05-25 17:44:59 +02:00
|
|
|
|
else
|
|
|
|
|
throw new NotSupportedException(connectionType.ToString());
|
|
|
|
|
|
2018-02-25 16:48:12 +01:00
|
|
|
|
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
|
2018-03-20 18:48:11 +01:00
|
|
|
|
}, "save", "BTC");
|
2018-03-20 16:31:19 +01:00
|
|
|
|
if (storeController.ModelState.ErrorCount != 0)
|
|
|
|
|
Assert.False(true, storeController.ModelState.FirstOrDefault().Value.Errors[0].ErrorMessage);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
}
|
2018-07-01 09:10:17 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|