Simplify tests to take advantage of context (#3246)

This commit is contained in:
Nicolas Dorier 2021-12-31 22:02:53 +09:00 committed by GitHub
parent 04b8eafacb
commit fd2e55e444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 68 deletions

View file

@ -421,19 +421,19 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser();
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddDerivationScheme("BTC");
//check that there is no dropdown since only one payment method is set
var invoiceId = s.CreateInvoice(storeId, 10, "USD", "a@g.com");
var invoiceId = s.CreateInvoice(10, "USD", "a@g.com");
s.GoToInvoiceCheckout(invoiceId);
s.Driver.FindElement(By.ClassName("payment__currencies_noborder"));
s.GoToHome();
s.GoToStore(storeId);
s.GoToStore();
s.AddDerivationScheme("LTC");
s.AddLightningNode("BTC", LightningConnectionType.CLightning);
s.AddLightningNode(LightningConnectionType.CLightning);
//there should be three now
invoiceId = s.CreateInvoice(storeId, 10, "USD", "a@g.com");
invoiceId = s.CreateInvoice(10, "USD", "a@g.com");
s.GoToInvoiceCheckout(invoiceId);
var currencyDropdownButton = s.Driver.FindElement(By.ClassName("payment__currencies"));
Assert.Contains("BTC", currencyDropdownButton.Text);

View file

@ -28,17 +28,17 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser();
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddDerivationScheme("BTC");
s.GoToStore(storeId, StoreNavPages.CheckoutAppearance);
s.GoToStore(StoreNavPages.CheckoutAppearance);
s.Driver.FindElement(By.Id("RequiresRefundEmail")).Click();
s.Driver.FindElement(By.Name("command")).Click();
var emailAlreadyThereInvoiceId = s.CreateInvoice(storeId, 100, "USD", "a@g.com");
var emailAlreadyThereInvoiceId = s.CreateInvoice(100, "USD", "a@g.com");
s.GoToInvoiceCheckout(emailAlreadyThereInvoiceId);
s.Driver.AssertElementNotFound(By.Id("emailAddressFormInput"));
s.GoToHome();
s.CreateInvoice(storeId);
s.CreateInvoice();
s.Driver.FindElement(By.ClassName("invoice-details-link")).Click();
s.Driver.AssertNoError();
s.Driver.Navigate().Back();
@ -79,11 +79,11 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser();
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddDerivationScheme("BTC");
// Now create an invoice that requires a refund email
var invoice = s.CreateInvoice(storeId, 100, "USD", "", null, true);
var invoice = s.CreateInvoice(100, "USD", "", null, true);
s.GoToInvoiceCheckout(invoice);
var emailInput = s.Driver.FindElement(By.Id("emailAddressFormInput"));
@ -106,7 +106,7 @@ namespace BTCPayServer.Tests
s.GoToHome();
// Now create an invoice that doesn't require a refund email
s.CreateInvoice(storeId, 100, "USD", "", null, false);
s.CreateInvoice(100, "USD", "", null, false);
s.Driver.FindElement(By.ClassName("invoice-details-link")).Click();
s.Driver.AssertNoError();
s.Driver.Navigate().Back();
@ -119,7 +119,7 @@ namespace BTCPayServer.Tests
s.GoToHome();
// Now create an invoice that requires refund email but already has one set, email input shouldn't show up
s.CreateInvoice(storeId, 100, "USD", "a@g.com", null, true);
s.CreateInvoice(100, "USD", "a@g.com", null, true);
s.Driver.FindElement(By.ClassName("invoice-details-link")).Click();
s.Driver.AssertNoError();
s.Driver.Navigate().Back();
@ -139,10 +139,10 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser();
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddDerivationScheme("BTC");
var invoiceId = s.CreateInvoice(storeId);
var invoiceId = s.CreateInvoice();
s.GoToInvoiceCheckout(invoiceId);
Assert.True(s.Driver.FindElement(By.Id("DefaultLang")).FindElements(By.TagName("option")).Count > 1);
var payWithTextEnglish = s.Driver.FindElement(By.Id("pay-with-text")).Text;
@ -171,11 +171,11 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser(true);
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddLightningNode();
s.AddDerivationScheme("BTC");
var invoiceId = s.CreateInvoice(storeId, defaultPaymentMethod: "BTC_LightningLike");
var invoiceId = s.CreateInvoice(defaultPaymentMethod: "BTC_LightningLike");
s.GoToInvoiceCheckout(invoiceId);
Assert.Equal("Bitcoin (Lightning) (BTC)", s.Driver.FindElement(By.ClassName("payment__currencies")).Text);
@ -193,15 +193,15 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser(true);
(_, string storeId) = s.CreateNewStore();
s.CreateNewStore();
s.AddLightningNode();
s.GoToStore(storeId);
s.GoToStore();
s.Driver.FindElement(By.Id("Modify-LightningBTC")).Click();
s.Driver.SetCheckbox(By.Id("LightningAmountInSatoshi"), true);
s.Driver.FindElement(By.Id("save")).Click();
Assert.Contains("BTC Lightning settings successfully updated", s.FindAlertMessage().Text);
var invoiceId = s.CreateInvoice(storeId, 10, "USD", "a@g.com");
var invoiceId = s.CreateInvoice(10, "USD", "a@g.com");
s.GoToInvoiceCheckout(invoiceId);
Assert.Contains("Sats", s.Driver.FindElement(By.ClassName("payment__currencies_noborder")).Text);
}
@ -215,10 +215,10 @@ namespace BTCPayServer.Tests
await s.StartAsync();
s.GoToRegister();
s.RegisterNewUser();
(_, string storeId) = s.CreateNewStore();
s.GoToStore(storeId);
s.CreateNewStore();
s.GoToStore();
s.AddDerivationScheme();
var invoiceId = s.CreateInvoice(storeId, 0.001m, "BTC", "a@x.com");
var invoiceId = s.CreateInvoice(0.001m, "BTC", "a@x.com");
var invoice = await s.Server.PayTester.InvoiceRepository.GetInvoice(invoiceId);
s.Driver.Navigate()
.GoToUrl(new Uri(s.ServerUri, $"tests/index.html?invoice={invoiceId}"));

View file

@ -336,6 +336,7 @@ namespace BTCPayServer.Tests
Assert.Equal(InvoiceStatusLegacy.Paid, invoice.Status);
});
s.SelectStoreContext(receiver.storeId);
s.GoToInvoices();
var paymentValueRowColumn = s.Driver.FindElement(By.Id($"invoice_details_{invoiceId}"))
.FindElement(By.ClassName("payment-value"));
@ -343,7 +344,7 @@ namespace BTCPayServer.Tests
StringComparison.InvariantCultureIgnoreCase));
//let's do it all again, except now the receiver has funds and is able to payjoin
invoiceId = s.CreateInvoice(receiver.storeId);
invoiceId = s.CreateInvoice();
s.GoToInvoiceCheckout(invoiceId);
bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn"))
.GetAttribute("href");

View file

@ -215,6 +215,11 @@ namespace BTCPayServer.Tests
return new Mnemonic(seed);
}
/// <summary>
/// Assume to be in store's settings
/// </summary>
/// <param name="cryptoCode"></param>
/// <param name="derivationScheme"></param>
public void AddDerivationScheme(string cryptoCode = "BTC", string derivationScheme = "xpub661MyMwAqRbcGABgHMUXDzPzH1tU7eZaAaJQXhDXsSxsqyQzQeU6kznNfSuAyqAK9UaWSaZaMFdNiY5BCF4zBPAzSnwfUAwUhwttuAKwfRX-[legacy]")
{
Driver.FindElement(By.Id($"Modify{cryptoCode}")).Click();
@ -226,8 +231,17 @@ namespace BTCPayServer.Tests
FindAlertMessage();
}
public void AddLightningNode(string cryptoCode = "BTC", LightningConnectionType? connectionType = null, bool test = true)
public void AddLightningNode()
{
AddLightningNode(null, null, true);
}
public void AddLightningNode(LightningConnectionType? connectionType = null, bool test = true)
{
AddLightningNode(null, connectionType, test);
}
public void AddLightningNode(string cryptoCode = null, LightningConnectionType? connectionType = null, bool test = true)
{
cryptoCode ??= "BTC";
Driver.FindElement(By.Id($"Modify-Lightning{cryptoCode}")).Click();
if (Driver.PageSource.Contains("id=\"SetupLightningNodeLink\""))
@ -333,9 +347,16 @@ namespace BTCPayServer.Tests
Driver.FindElement(By.Id("LoginButton")).Click();
}
public void GoToStore(StoreNavPages storeNavPage = StoreNavPages.PaymentMethods)
{
GoToStore(null, storeNavPage);
}
public void GoToStore(string storeId, StoreNavPages storeNavPage = StoreNavPages.PaymentMethods)
{
GoToUrl($"/stores/{storeId}/");
if (storeId is null)
Driver.FindElement(By.Id("StoreNav-StoreSettings")).Click();
else
GoToUrl($"/stores/{storeId}/");
if (storeNavPage != StoreNavPages.PaymentMethods)
{
@ -364,10 +385,16 @@ namespace BTCPayServer.Tests
}
}
public void GoToLightningSettings(string storeId, string cryptoCode = "BTC")
public void GoToLightningSettings(string cryptoCode = "BTC")
{
GoToStore(storeId);
Driver.FindElement(By.Id($"Modify-Lightning{cryptoCode}")).Click();
GoToStore();
Driver.FindElement(By.Id($"StoreNav-Lightning{cryptoCode}")).Click();
}
public void SelectStoreContext(string storeId)
{
Driver.FindElement(By.Id("StoreSelectorToggle")).Click();
Driver.FindElement(By.Id($"StoreSelectorMenuItem-{storeId}")).Click();
}
public void GoToInvoiceCheckout(string invoiceId)
@ -384,7 +411,14 @@ namespace BTCPayServer.Tests
public void GoToInvoices(string storeId = null)
{
GoToUrl(storeId == null ? "/invoices/" : $"/stores/{storeId}/invoices/");
if (storeId is null)
{
this.Driver.FindElement(By.Id("StoreNav-Invoices")).Click();
}
else
{
GoToUrl(storeId == null ? "/invoices/" : $"/stores/{storeId}/invoices/");
}
}
public void GoToProfile(ManageNavPages navPages = ManageNavPages.Index)
@ -401,6 +435,17 @@ namespace BTCPayServer.Tests
GoToUrl("/login");
}
public string CreateInvoice(decimal? amount = 100,
string currency = "USD",
string refundEmail = "",
string defaultPaymentMethod = null,
bool? requiresRefundEmail = null,
StatusMessageModel.StatusSeverity expectedSeverity = StatusMessageModel.StatusSeverity.Success
)
{
return CreateInvoice(null, amount, currency, refundEmail, defaultPaymentMethod, requiresRefundEmail, expectedSeverity);
}
public string CreateInvoice(
string storeId,
decimal? amount = 100,

View file

@ -360,7 +360,7 @@ namespace BTCPayServer.Tests
s.Server.ActivateLightning();
await s.StartAsync();
var alice = s.RegisterNewUser(true);
(string storeName, string storeId) = s.CreateNewStore();
(string storeName, _) = s.CreateNewStore();
var onchainHint = "Set up your wallet to receive payments at your store.";
var offchainHint = "A connection to a Lightning node is required to receive Lightning payments.";
@ -368,21 +368,21 @@ namespace BTCPayServer.Tests
Assert.True(s.Driver.PageSource.Contains(onchainHint), "Wallet hint not present");
Assert.True(s.Driver.PageSource.Contains(offchainHint), "Lightning hint not present");
s.GoToStore(storeId);
s.GoToStore();
Assert.Contains(storeName, s.Driver.PageSource);
Assert.True(s.Driver.PageSource.Contains(onchainHint), "Wallet hint should be present at this point");
Assert.True(s.Driver.PageSource.Contains(offchainHint),
"Lightning hint should be present at this point");
// setup onchain wallet
s.GoToStore(storeId);
s.GoToStore();
s.AddDerivationScheme();
s.Driver.AssertNoError();
Assert.False(s.Driver.PageSource.Contains(onchainHint),
"Wallet hint not dismissed on derivation scheme add");
// setup offchain wallet
s.GoToStore(storeId);
s.GoToStore();
s.AddLightningNode();
s.Driver.AssertNoError();
var successAlert = s.FindAlertMessage();
@ -392,8 +392,8 @@ namespace BTCPayServer.Tests
var storeUrl = s.Driver.Url;
s.ClickOnAllSectionLinks();
s.GoToInvoices(storeId);
var invoiceId = s.CreateInvoice(storeId);
s.GoToInvoices();
var invoiceId = s.CreateInvoice();
s.FindAlertMessage();
s.Driver.FindElement(By.ClassName("invoice-details-link")).Click();
var invoiceUrl = s.Driver.Url;
@ -404,7 +404,7 @@ namespace BTCPayServer.Tests
Assert.Contains("Unarchive", s.Driver.FindElement(By.Id("btn-archive-toggle")).Text);
//check that it no longer appears in list
s.GoToInvoices(storeId);
s.GoToInvoices();
Assert.DoesNotContain(invoiceId, s.Driver.PageSource);
//ok, let's unarchive and see that it shows again
@ -412,7 +412,7 @@ namespace BTCPayServer.Tests
s.Driver.FindElement(By.Id("btn-archive-toggle")).Click();
s.FindAlertMessage();
Assert.DoesNotContain("Unarchive", s.Driver.FindElement(By.Id("btn-archive-toggle")).Text);
s.GoToInvoices(storeId);
s.GoToInvoices();
Assert.Contains(invoiceId, s.Driver.PageSource);
// When logout out we should not be able to access store and invoice details
@ -449,7 +449,7 @@ namespace BTCPayServer.Tests
// Alice should be able to delete the store
s.Logout();
s.LogIn(alice);
s.GoToStore(storeId, StoreNavPages.GeneralSettings);
s.GoToStore(StoreNavPages.GeneralSettings);
s.Driver.FindElement(By.Id("DeleteStore")).Click();
s.Driver.WaitForElement(By.Id("ConfirmInput")).SendKeys("DELETE");
s.Driver.FindElement(By.Id("ConfirmContinue")).Click();
@ -677,8 +677,8 @@ namespace BTCPayServer.Tests
{
await s.StartAsync();
s.RegisterNewUser(true);
(_, string storeId) = s.CreateNewStore();
s.GoToStore(storeId, StoreNavPages.Webhooks);
s.CreateNewStore();
s.GoToStore(StoreNavPages.Webhooks);
TestLogs.LogInformation("Let's create two webhooks");
for (var i = 0; i < 2; i++)
@ -732,9 +732,9 @@ namespace BTCPayServer.Tests
Assert.Contains(server.ServerUri.AbsoluteUri, s.Driver.PageSource);
TestLogs.LogInformation("Let's see if we can generate an event");
s.GoToStore(storeId);
s.GoToStore();
s.AddDerivationScheme();
s.CreateInvoice(storeId);
s.CreateInvoice();
var request = await server.GetNextRequest();
var headers = request.Request.Headers;
var actualSig = headers["BTCPay-Sig"].First();
@ -746,14 +746,14 @@ namespace BTCPayServer.Tests
server.Done();
TestLogs.LogInformation("Let's make a failed event");
s.CreateInvoice(storeId);
s.CreateInvoice();
request = await server.GetNextRequest();
request.Response.StatusCode = 404;
server.Done();
// The delivery is done asynchronously, so small wait here
await Task.Delay(500);
s.GoToStore(storeId, StoreNavPages.Webhooks);
s.GoToStore(StoreNavPages.Webhooks);
s.Driver.FindElement(By.LinkText("Modify")).Click();
var elements = s.Driver.FindElements(By.ClassName("redeliver"));
// One worked, one failed
@ -769,7 +769,7 @@ namespace BTCPayServer.Tests
TestLogs.LogInformation("Can we browse the json content?");
CanBrowseContent(s);
s.GoToInvoices(storeId);
s.GoToInvoices();
s.Driver.FindElement(By.LinkText("Details")).Click();
CanBrowseContent(s);
var element = s.Driver.FindElement(By.ClassName("redeliver"));
@ -781,7 +781,7 @@ namespace BTCPayServer.Tests
server.Done();
TestLogs.LogInformation("Let's see if we can delete store with some webhooks inside");
s.GoToStore(storeId, StoreNavPages.GeneralSettings);
s.GoToStore(StoreNavPages.GeneralSettings);
s.Driver.FindElement(By.Id("DeleteStore")).Click();
s.Driver.WaitForElement(By.Id("ConfirmInput")).SendKeys("DELETE");
s.Driver.FindElement(By.Id("ConfirmContinue")).Click();
@ -1160,7 +1160,7 @@ namespace BTCPayServer.Tests
Assert.Equal(PayResult.Ok, resp.Result);
newStore = s.CreateNewStore();
s.AddLightningNode("BTC");
s.AddLightningNode();
//Currently an onchain wallet is required to use the Lightning payouts feature..
s.GenerateWallet("BTC", "", true, true);
@ -1251,19 +1251,17 @@ namespace BTCPayServer.Tests
await s.Server.EnsureChannelsSetup();
s.RegisterNewUser(true);
var cryptoCode = "BTC";
(_, string storeId) = s.CreateNewStore();
s.GoToStore(storeId);
s.AddLightningNode(cryptoCode, LightningConnectionType.CLightning, false);
s.GoToLightningSettings(storeId, cryptoCode);
s.CreateNewStore();
s.GoToStore();
s.AddLightningNode(LightningConnectionType.CLightning, false);
s.GoToLightningSettings();
s.Driver.SetCheckbox(By.Id("LNURLEnabled"), true);
s.Driver.FindElement(By.Id("StoreNav-CreateApp")).Click();
s.Driver.FindElement(By.Id("SelectedAppType")).Click();
s.Driver.FindElement(By.CssSelector("option[value='PointOfSale']")).Click();
s.Driver.FindElement(By.Id("AppName")).SendKeys(Guid.NewGuid().ToString());
s.Driver.FindElement(By.Id("Create")).Click();
Thread.Sleep(5000);
Assert.Contains("App successfully created", s.FindAlertMessage().Text);
TestUtils.Eventually(() => Assert.Contains("App successfully created", s.FindAlertMessage().Text));
s.Driver.FindElement(By.Id("DefaultView")).Click();
s.Driver.FindElement(By.CssSelector("option[value='3']")).Click();
s.Driver.FindElement(By.Id("SaveSettings")).Click();
@ -1297,9 +1295,9 @@ namespace BTCPayServer.Tests
s.RegisterNewUser(true);
(_, string storeId) = s.CreateNewStore();
var network = s.Server.NetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode).NBitcoinNetwork;
s.GoToStore(storeId);
s.AddLightningNode(cryptoCode, LightningConnectionType.CLightning, false);
s.GoToLightningSettings(storeId, cryptoCode);
s.GoToStore();
s.AddLightningNode(LightningConnectionType.CLightning, false);
s.GoToLightningSettings();
// LNURL is false by default
Assert.False(s.Driver.FindElement(By.Id("LNURLEnabled")).Selected);
// LNURL settings are not expanded when LNURL is disabled
@ -1374,8 +1372,8 @@ namespace BTCPayServer.Tests
await s.Server.CustomerLightningD.Pay(lnurlResponse.Pr);
Assert.Equal(new LightMoney(0.0000001m, LightMoneyUnit.BTC),
lnurlResponse2.GetPaymentRequest(network).MinimumAmount);
s.GoToLightningSettings(s.StoreId, cryptoCode);
s.GoToHome();
s.GoToLightningSettings();
// LNURL is enabled and settings are expanded
Assert.True(s.Driver.FindElement(By.Id("LNURLEnabled")).Selected);
Assert.Contains("show", s.Driver.FindElement(By.Id("LNURLSettings")).GetAttribute("class"));
@ -1392,7 +1390,8 @@ namespace BTCPayServer.Tests
s.GoToInvoiceCheckout(i);
s.Driver.FindElement(By.ClassName("payment__currencies_noborder"));
s.GoToLightningSettings(s.StoreId, cryptoCode);
s.GoToHome();
s.GoToLightningSettings();
s.Driver.SetCheckbox(By.Id("LNURLBech32Mode"), false);
s.Driver.SetCheckbox(By.Id("LNURLStandardInvoiceEnabled"), false);
s.Driver.SetCheckbox(By.Id("DisableBolt11PaymentMethod"), true);
@ -1400,7 +1399,7 @@ namespace BTCPayServer.Tests
Assert.Contains($"{cryptoCode} Lightning settings successfully updated", s.FindAlertMessage().Text);
// Ensure the toggles are set correctly
s.GoToLightningSettings(s.StoreId, cryptoCode);
s.GoToLightningSettings();
//TODO: DisableBolt11PaymentMethod is actually disabled because LNURLStandardInvoiceEnabled is disabled
// checkboxes is not good choice here, in next release we should have multi choice instead
@ -1421,14 +1420,14 @@ namespace BTCPayServer.Tests
LNURL.LNURL.Parse(lnurl, out tag);
s.GoToHome();
var newStore = s.CreateNewStore(false);
s.AddLightningNode(cryptoCode, LightningConnectionType.LndREST, false);
s.GoToLightningSettings(newStore.storeId, cryptoCode);
s.CreateNewStore(false);
s.AddLightningNode(LightningConnectionType.LndREST, false);
s.GoToLightningSettings();
s.Driver.SetCheckbox(By.Id("LNURLEnabled"), true);
s.Driver.SetCheckbox(By.Id("DisableBolt11PaymentMethod"), true);
s.Driver.FindElement(By.Id("save")).Click();
Assert.Contains($"{cryptoCode} Lightning settings successfully updated", s.FindAlertMessage().Text);
var invForPP = s.CreateInvoice(newStore.storeId, 0.0000001m, cryptoCode);
var invForPP = s.CreateInvoice(0.0000001m, cryptoCode);
s.GoToInvoiceCheckout(invForPP);
s.Driver.FindElement(By.Id("copy-tab")).Click();
lnurl = s.Driver.FindElement(By.CssSelector("input.checkoutTextbox")).GetAttribute("value");
@ -1491,11 +1490,11 @@ namespace BTCPayServer.Tests
s.Driver.AssertElementNotFound(By.Id("StoreNav-LightningAddress"));
s.GoToStore(s.StoreId);
s.AddLightningNode("BTC", LightningConnectionType.LndREST, false);
s.AddLightningNode(LightningConnectionType.LndREST, false);
//ensure ln address is not available as lnurl is not configured
s.Driver.AssertElementNotFound(By.Id("StoreNav-LightningAddress"));
s.GoToLightningSettings(s.StoreId, cryptoCode);
s.GoToLightningSettings();
s.Driver.SetCheckbox(By.Id("LNURLEnabled"), true);
SudoForceSaveLightningSettingsRightNowAndFast(s, cryptoCode);