diff --git a/BTCPayServer.Tests/StorageTests.cs b/BTCPayServer.Tests/StorageTests.cs index 6360a7834..1f53caee6 100644 --- a/BTCPayServer.Tests/StorageTests.cs +++ b/BTCPayServer.Tests/StorageTests.cs @@ -190,21 +190,8 @@ namespace BTCPayServer.Tests private async Task CanUploadRemoveFiles(ServerController controller) { - var filename = "uploadtestfile.txt"; var fileContent = "content"; - File.WriteAllText(filename, fileContent); - - var fileInfo = new FileInfo(filename); - var formFile = new FormFile( - new FileStream(filename, FileMode.OpenOrCreate), - 0, - fileInfo.Length, fileInfo.Name, fileInfo.Name) - { - Headers = new HeaderDictionary() - }; - formFile.ContentType = "text/plain"; - formFile.ContentDisposition = $"form-data; name=\"file\"; filename=\"{fileInfo.Name}\""; - var uploadFormFileResult = Assert.IsType(await controller.CreateFile(formFile)); + var uploadFormFileResult = Assert.IsType(await controller.CreateFile(TestUtils.GetFormFile("uploadtestfile.txt", fileContent))); Assert.True(uploadFormFileResult.RouteValues.ContainsKey("fileId")); var fileId = uploadFormFileResult.RouteValues["fileId"].ToString(); Assert.Equal("Files", uploadFormFileResult.ActionName); diff --git a/BTCPayServer.Tests/TestUtils.cs b/BTCPayServer.Tests/TestUtils.cs new file mode 100644 index 000000000..5ea38b9c3 --- /dev/null +++ b/BTCPayServer.Tests/TestUtils.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Internal; +using Xunit.Sdk; + +namespace BTCPayServer.Tests +{ + public static class TestUtils + { + public static FormFile GetFormFile(string filename, string content) + { + File.WriteAllText(filename, content); + + var fileInfo = new FileInfo(filename); + FormFile formFile = new FormFile( + new FileStream(filename, FileMode.OpenOrCreate), + 0, + fileInfo.Length, fileInfo.Name, fileInfo.Name) + { + Headers = new HeaderDictionary() + }; + formFile.ContentType = "text/plain"; + formFile.ContentDisposition = $"form-data; name=\"file\"; filename=\"{fileInfo.Name}\""; + return formFile; + } + public static void Eventually(Action act) + { + CancellationTokenSource cts = new CancellationTokenSource(20000); + while (true) + { + try + { + act(); + break; + } + catch (XunitException) when (!cts.Token.IsCancellationRequested) + { + cts.Token.WaitHandle.WaitOne(500); + } + } + } + + public static async Task EventuallyAsync(Func act) + { + CancellationTokenSource cts = new CancellationTokenSource(20000); + while (true) + { + try + { + await act(); + break; + } + catch (XunitException) when (!cts.Token.IsCancellationRequested) + { + await Task.Delay(500); + } + } + } + } +} diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 7e2a18729..baa7bd6ea 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -1586,19 +1586,7 @@ namespace BTCPayServer.Tests derivationVM = (DerivationSchemeViewModel)Assert.IsType(controller.AddDerivationScheme(user.StoreId, "BTC")).Model; string filename = "wallet.json"; string content = "{\"keystore\": {\"ckcc_xpub\": \"xpub661MyMwAqRbcGVBsTGeNZN6QGVHmMHLdSA4FteGsRrEriu4pnVZMZWnruFFFXkMnyoBjyHndD3Qwcfz4MPzBUxjSevweNFQx7SAYZATtcDw\", \"xpub\": \"ypub6WWc2gWwHbdnAAyJDnR4SPL1phRh7REqrPBfZeizaQ1EmTshieRXJC3Z5YoU4wkcdKHEjQGkh6AYEzCQC1Kz3DNaWSwdc1pc8416hAjzqyD\", \"label\": \"Coldcard Import 0x60d1af8b\", \"ckcc_xfp\": 1624354699, \"type\": \"hardware\", \"hw_type\": \"coldcard\", \"derivation\": \"m/49'/0'/0'\"}, \"wallet_type\": \"standard\", \"use_encryption\": false, \"seed_version\": 17}"; - File.WriteAllText(filename, content); - - var fileInfo = new FileInfo(filename); - var formFile = new FormFile( - new FileStream(filename, FileMode.OpenOrCreate), - 0, - fileInfo.Length, fileInfo.Name, fileInfo.Name) - { - Headers = new HeaderDictionary() - }; - formFile.ContentType = "text/plain"; - formFile.ContentDisposition = $"form-data; name=\"file\"; filename=\"{fileInfo.Name}\""; - derivationVM.ColdcardPublicFile = formFile; + derivationVM.ColdcardPublicFile = TestUtils.GetFormFile(filename, content); derivationVM = (DerivationSchemeViewModel)Assert.IsType(controller.AddDerivationScheme(user.StoreId, derivationVM, "BTC").GetAwaiter().GetResult()).Model; Assert.True(derivationVM.Confirmation); Assert.IsType(controller.AddDerivationScheme(user.StoreId, derivationVM, "BTC").GetAwaiter().GetResult()); @@ -2780,42 +2768,5 @@ donation: var h = BitcoinAddress.Create(invoice.BitcoinAddress, Network.RegTest).ScriptPubKey.Hash.ToString(); return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetAddress() == h) != null; } - - public static class TestUtils - { - public static void Eventually(Action act) - { - CancellationTokenSource cts = new CancellationTokenSource(20000); - while (true) - { - try - { - act(); - break; - } - catch (XunitException) when (!cts.Token.IsCancellationRequested) - { - cts.Token.WaitHandle.WaitOne(500); - } - } - } - - public static async Task EventuallyAsync(Func act) - { - CancellationTokenSource cts = new CancellationTokenSource(20000); - while (true) - { - try - { - await act(); - break; - } - catch (XunitException) when (!cts.Token.IsCancellationRequested) - { - await Task.Delay(500); - } - } - } - } } }