From 4be6c06af57a6ce4f2d0c13ee8d7db2fad0983e2 Mon Sep 17 00:00:00 2001 From: Kukks Date: Sat, 25 Apr 2020 16:11:00 +0200 Subject: [PATCH] add payjoin e2e test for all compatible formats --- BTCPayServer.Tests/PayJoinTests.cs | 220 ++++++++++++++------------- BTCPayServer.Tests/SeleniumTester.cs | 4 +- 2 files changed, 118 insertions(+), 106 deletions(-) diff --git a/BTCPayServer.Tests/PayJoinTests.cs b/BTCPayServer.Tests/PayJoinTests.cs index 68f0368ae..16dcb41f6 100644 --- a/BTCPayServer.Tests/PayJoinTests.cs +++ b/BTCPayServer.Tests/PayJoinTests.cs @@ -175,117 +175,127 @@ namespace BTCPayServer.Tests { await s.StartAsync(); var invoiceRepository = s.Server.PayTester.GetService(); - // var payjoinRepository = s.Server.PayTester.GetService(); - // var broadcaster = s.Server.PayTester.GetService(); s.RegisterNewUser(true); - var receiver = s.CreateNewStore(); - var receiverSeed = s.GenerateWallet("BTC", "", true, true); - var receiverWalletId = new WalletId(receiver.storeId, "BTC"); - //payjoin is not enabled by default. - var invoiceId = s.CreateInvoice(receiver.storeId); - s.GoToInvoiceCheckout(invoiceId); - var bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) - .GetAttribute("href"); - Assert.DoesNotContain($"{PayjoinClient.BIP21EndpointKey}=", bip21); - - s.GoToHome(); - s.GoToStore(receiver.storeId); - //payjoin is not enabled by default. - Assert.False(s.Driver.FindElement(By.Id("PayJoinEnabled")).Selected); - s.SetCheckbox(s,"PayJoinEnabled", true); - s.Driver.FindElement(By.Id("Save")).Click(); - Assert.True(s.Driver.FindElement(By.Id("PayJoinEnabled")).Selected); - var sender = s.CreateNewStore(); - var senderSeed = s.GenerateWallet("BTC", "", true, true); - var senderWalletId = new WalletId(sender.storeId, "BTC"); - await s.Server.ExplorerNode.GenerateAsync(1); - await s.FundStoreWallet(senderWalletId); - - invoiceId = s.CreateInvoice(receiver.storeId); - s.GoToInvoiceCheckout(invoiceId); - bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) - .GetAttribute("href"); - Assert.Contains($"{PayjoinClient.BIP21EndpointKey}=", bip21); - - s.GoToWalletSend(senderWalletId); - s.Driver.FindElement(By.Id("bip21parse")).Click(); - s.Driver.SwitchTo().Alert().SendKeys(bip21); - s.Driver.SwitchTo().Alert().Accept(); - Assert.False(string.IsNullOrEmpty(s.Driver.FindElement(By.Id("PayJoinEndpointUrl")).GetAttribute("value"))); - s.Driver.ScrollTo(By.Id("SendMenu")); - s.Driver.FindElement(By.Id("SendMenu")).ForceClick(); - s.Driver.FindElement(By.CssSelector("button[value=nbx-seed]")).Click(); - await s.Server.WaitForEvent(() => + foreach (var format in new []{ScriptPubKeyType.SegwitP2SH}) { - s.Driver.FindElement(By.CssSelector("button[value=payjoin]")).ForceClick(); - return Task.CompletedTask; - }); - //no funds in receiver wallet to do payjoin - s.AssertHappyMessage(StatusMessageModel.StatusSeverity.Warning); - await TestUtils.EventuallyAsync(async () => - { - var invoice = await s.Server.PayTester.GetService().GetInvoice(invoiceId); - Assert.Equal(InvoiceStatus.Paid, invoice.Status); - }); + var receiver = s.CreateNewStore(); + var receiverSeed = s.GenerateWallet("BTC", "", true, true, format); + var receiverWalletId = new WalletId(receiver.storeId, "BTC"); - s.GoToInvoices(); - var paymentValueRowColumn = s.Driver.FindElement(By.Id($"invoice_{invoiceId}")).FindElement(By.ClassName("payment-value")); - Assert.False(paymentValueRowColumn.Text.Contains("payjoin", StringComparison.InvariantCultureIgnoreCase)); + //payjoin is not enabled by default. + var invoiceId = s.CreateInvoice(receiver.storeId); + s.GoToInvoiceCheckout(invoiceId); + var bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) + .GetAttribute("href"); + Assert.DoesNotContain($"{PayjoinClient.BIP21EndpointKey}=", bip21); - //let's do it all again, except now the receiver has funds and is able to payjoin - invoiceId = s.CreateInvoice(receiver.storeId); - s.GoToInvoiceCheckout(invoiceId); - bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) - .GetAttribute("href"); - Assert.Contains($"{PayjoinClient.BIP21EndpointKey}", bip21); + s.GoToHome(); + s.GoToStore(receiver.storeId); + //payjoin is not enabled by default. + Assert.False(s.Driver.FindElement(By.Id("PayJoinEnabled")).Selected); + s.SetCheckbox(s, "PayJoinEnabled", true); + s.Driver.FindElement(By.Id("Save")).Click(); + Assert.True(s.Driver.FindElement(By.Id("PayJoinEnabled")).Selected); + var sender = s.CreateNewStore(); + var senderSeed = s.GenerateWallet("BTC", "", true, true, format); + var senderWalletId = new WalletId(sender.storeId, "BTC"); + await s.Server.ExplorerNode.GenerateAsync(1); + await s.FundStoreWallet(senderWalletId); - s.GoToWalletSend(senderWalletId); - s.Driver.FindElement(By.Id("bip21parse")).Click(); - s.Driver.SwitchTo().Alert().SendKeys(bip21); - s.Driver.SwitchTo().Alert().Accept(); - Assert.False(string.IsNullOrEmpty(s.Driver.FindElement(By.Id("PayJoinEndpointUrl")).GetAttribute("value"))); - s.Driver.FindElement(By.Id("FeeSatoshiPerByte")).Clear(); - s.Driver.FindElement(By.Id("FeeSatoshiPerByte")).SendKeys("1"); - s.Driver.ScrollTo(By.Id("SendMenu")); - s.Driver.FindElement(By.Id("SendMenu")).ForceClick(); - s.Driver.FindElement(By.CssSelector("button[value=nbx-seed]")).Click(); - await s.Server.WaitForEvent(() => - { - s.Driver.FindElement(By.CssSelector("button[value=payjoin]")).ForceClick(); - return Task.CompletedTask; - }); - s.AssertHappyMessage(StatusMessageModel.StatusSeverity.Success); - await TestUtils.EventuallyAsync(async () => - { - var invoice = await invoiceRepository.GetInvoice(invoiceId); - var payments = invoice.GetPayments(); - Assert.Equal(2, payments.Count); - var originalPayment = payments[0]; - var coinjoinPayment = payments[1]; - Assert.Equal(-1, ((BitcoinLikePaymentData)originalPayment.GetCryptoPaymentData()).ConfirmationCount); - Assert.Equal(0, ((BitcoinLikePaymentData)coinjoinPayment.GetCryptoPaymentData()).ConfirmationCount); - Assert.False(originalPayment.Accounted); - Assert.True(coinjoinPayment.Accounted); - Assert.Equal(((BitcoinLikePaymentData)originalPayment.GetCryptoPaymentData()).Value, - ((BitcoinLikePaymentData)coinjoinPayment.GetCryptoPaymentData()).Value); - Assert.Equal(originalPayment.GetCryptoPaymentData() - .AssertType() - .Value, - coinjoinPayment.GetCryptoPaymentData() - .AssertType() - .Value); - }); - - await TestUtils.EventuallyAsync(async () => - { - var invoice = await s.Server.PayTester.GetService().GetInvoice(invoiceId); - var dto = invoice.EntityToDTO(); - Assert.Equal(InvoiceStatus.Paid, invoice.Status); - }); - s.GoToInvoices(); - paymentValueRowColumn = s.Driver.FindElement(By.Id($"invoice_{invoiceId}")).FindElement(By.ClassName("payment-value")); - Assert.False(paymentValueRowColumn.Text.Contains("payjoin", StringComparison.InvariantCultureIgnoreCase)); + invoiceId = s.CreateInvoice(receiver.storeId); + s.GoToInvoiceCheckout(invoiceId); + bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) + .GetAttribute("href"); + Assert.Contains($"{PayjoinClient.BIP21EndpointKey}=", bip21); + + s.GoToWalletSend(senderWalletId); + s.Driver.FindElement(By.Id("bip21parse")).Click(); + s.Driver.SwitchTo().Alert().SendKeys(bip21); + s.Driver.SwitchTo().Alert().Accept(); + Assert.False(string.IsNullOrEmpty(s.Driver.FindElement(By.Id("PayJoinEndpointUrl")) + .GetAttribute("value"))); + s.Driver.ScrollTo(By.Id("SendMenu")); + s.Driver.FindElement(By.Id("SendMenu")).ForceClick(); + s.Driver.FindElement(By.CssSelector("button[value=nbx-seed]")).Click(); + await s.Server.WaitForEvent(() => + { + s.Driver.FindElement(By.CssSelector("button[value=payjoin]")).ForceClick(); + return Task.CompletedTask; + }); + //no funds in receiver wallet to do payjoin + s.AssertHappyMessage(StatusMessageModel.StatusSeverity.Warning); + await TestUtils.EventuallyAsync(async () => + { + var invoice = await s.Server.PayTester.GetService().GetInvoice(invoiceId); + Assert.Equal(InvoiceStatus.Paid, invoice.Status); + }); + + s.GoToInvoices(); + var paymentValueRowColumn = s.Driver.FindElement(By.Id($"invoice_{invoiceId}")) + .FindElement(By.ClassName("payment-value")); + Assert.False(paymentValueRowColumn.Text.Contains("payjoin", + StringComparison.InvariantCultureIgnoreCase)); + + //let's do it all again, except now the receiver has funds and is able to payjoin + invoiceId = s.CreateInvoice(receiver.storeId); + s.GoToInvoiceCheckout(invoiceId); + bip21 = s.Driver.FindElement(By.ClassName("payment__details__instruction__open-wallet__btn")) + .GetAttribute("href"); + Assert.Contains($"{PayjoinClient.BIP21EndpointKey}", bip21); + + s.GoToWalletSend(senderWalletId); + s.Driver.FindElement(By.Id("bip21parse")).Click(); + s.Driver.SwitchTo().Alert().SendKeys(bip21); + s.Driver.SwitchTo().Alert().Accept(); + Assert.False(string.IsNullOrEmpty(s.Driver.FindElement(By.Id("PayJoinEndpointUrl")) + .GetAttribute("value"))); + s.Driver.FindElement(By.Id("FeeSatoshiPerByte")).Clear(); + s.Driver.FindElement(By.Id("FeeSatoshiPerByte")).SendKeys("1"); + s.Driver.ScrollTo(By.Id("SendMenu")); + s.Driver.FindElement(By.Id("SendMenu")).ForceClick(); + s.Driver.FindElement(By.CssSelector("button[value=nbx-seed]")).Click(); + await s.Server.WaitForEvent(() => + { + s.Driver.FindElement(By.CssSelector("button[value=payjoin]")).ForceClick(); + return Task.CompletedTask; + }); + s.AssertHappyMessage(StatusMessageModel.StatusSeverity.Success); + await TestUtils.EventuallyAsync(async () => + { + var invoice = await invoiceRepository.GetInvoice(invoiceId); + var payments = invoice.GetPayments(); + Assert.Equal(2, payments.Count); + var originalPayment = payments[0]; + var coinjoinPayment = payments[1]; + Assert.Equal(-1, + ((BitcoinLikePaymentData)originalPayment.GetCryptoPaymentData()).ConfirmationCount); + Assert.Equal(0, + ((BitcoinLikePaymentData)coinjoinPayment.GetCryptoPaymentData()).ConfirmationCount); + Assert.False(originalPayment.Accounted); + Assert.True(coinjoinPayment.Accounted); + Assert.Equal(((BitcoinLikePaymentData)originalPayment.GetCryptoPaymentData()).Value, + ((BitcoinLikePaymentData)coinjoinPayment.GetCryptoPaymentData()).Value); + Assert.Equal(originalPayment.GetCryptoPaymentData() + .AssertType() + .Value, + coinjoinPayment.GetCryptoPaymentData() + .AssertType() + .Value); + }); + + await TestUtils.EventuallyAsync(async () => + { + var invoice = await s.Server.PayTester.GetService().GetInvoice(invoiceId); + var dto = invoice.EntityToDTO(); + Assert.Equal(InvoiceStatus.Paid, invoice.Status); + }); + s.GoToInvoices(); + paymentValueRowColumn = s.Driver.FindElement(By.Id($"invoice_{invoiceId}")) + .FindElement(By.ClassName("payment-value")); + Assert.False(paymentValueRowColumn.Text.Contains("payjoin", + StringComparison.InvariantCultureIgnoreCase)); + } } } diff --git a/BTCPayServer.Tests/SeleniumTester.cs b/BTCPayServer.Tests/SeleniumTester.cs index 9a01cc7c0..f654185c4 100644 --- a/BTCPayServer.Tests/SeleniumTester.cs +++ b/BTCPayServer.Tests/SeleniumTester.cs @@ -123,7 +123,7 @@ namespace BTCPayServer.Tests } - public Mnemonic GenerateWallet(string cryptoCode = "BTC", string seed = "", bool importkeys = false, bool privkeys = false) + public Mnemonic GenerateWallet(string cryptoCode = "BTC", string seed = "", bool importkeys = false, bool privkeys = false, ScriptPubKeyType format = ScriptPubKeyType.Segwit) { Driver.FindElement(By.Id($"Modify{cryptoCode}")).ForceClick(); Driver.FindElement(By.Id("import-from-btn")).ForceClick(); @@ -131,6 +131,8 @@ namespace BTCPayServer.Tests Driver.WaitForElement(By.Id("ExistingMnemonic")).SendKeys(seed); SetCheckbox(Driver.WaitForElement(By.Id("SavePrivateKeys")), privkeys); SetCheckbox(Driver.WaitForElement(By.Id("ImportKeysToRPC")), importkeys); + Driver.WaitForElement(By.Id("ScriptPubKeyType")).Click(); + Driver.WaitForElement(By.CssSelector($"#ScriptPubKeyType option[value={format}]")).Click(); Logs.Tester.LogInformation("Trying to click btn-generate"); Driver.WaitForElement(By.Id("btn-generate")).ForceClick(); AssertHappyMessage();