Skip to content

Commit

Permalink
Fix the test + Fix handling of missing to/from fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Mar 11, 2024
1 parent 9650c65 commit 43c9ff9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
10 changes: 5 additions & 5 deletions backend/Application/Api/Rest/ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public async Task<ActionResult> GetStatementExport(string accountAddress, DateTi
return BadRequest("Time zone missing on 'fromTime'.");
}

DateTimeOffset from = fromTime ?? DateTime.Now.AddDays(-31);
DateTimeOffset from = fromTime ?? DateTime.UtcNow.AddDays(-31);

if (toTime != null && toTime.Value.Kind != DateTimeKind.Utc)
{
return BadRequest("Time zone missing on 'toTime'.");
}

DateTimeOffset to = toTime ?? DateTime.Now;
DateTimeOffset to = toTime ?? DateTime.UtcNow;

if ((to - from).TotalDays > MAX_DAYS)
{
Expand All @@ -84,20 +84,20 @@ public async Task<ActionResult> GetStatementExport(string accountAddress, DateTi
FileDownloadName = $"statement-{accountAddress}_{from:yyyyMMdd}Z-{to:yyyyMMdd}Z.csv"
};
}
private class StreamWritingAdapter : IStreamWritingAdapter<String>
private class StreamWritingAdapter : IStreamWritingAdapter<string>
{
public string ContentType => "text/csv";

public async Task WriteAsync(string item, Stream stream)
{
byte[] line = Encoding.ASCII.GetBytes(item);
byte[] line = Encoding.UTF8.GetBytes(item);
await stream.WriteAsync(line);
}

public Task WriteFooterAsync(Stream stream, int recordCount) => Task.CompletedTask;
public async Task WriteHeaderAsync(Stream stream)
{
byte[] line = Encoding.ASCII.GetBytes("Time,Amount (CCD),Balance (CCD),Label\n");
byte[] line = Encoding.UTF8.GetBytes("Time,Amount (CCD),Balance (CCD),Label\n");
await stream.WriteAsync(line);
}
}
Expand Down
35 changes: 26 additions & 9 deletions backend/Tests/Api/Rest/ExportControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
using FluentAssertions;
using Tests.TestUtilities;
using Tests.TestUtilities.Builders.GraphQL;
using Ductus.FluentDocker.Common;
using EnumerableStreamFileResult;
using System.IO;
using Microsoft.AspNetCore.Http;
using Moq;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace Tests.Api.Rest;

Expand All @@ -32,7 +36,7 @@ public async Task DisposeAsync()
}

[Fact]
public async void accountStatementWith33DaySpanIsNotAllowed()
public async void AccountStatementWith33DaySpanIsNotAllowed()
{
// Arrange
var startDate = DateTime.SpecifyKind(new DateTime(2020, 11, 1), DateTimeKind.Utc);
Expand All @@ -56,7 +60,7 @@ public async void accountStatementWith33DaySpanIsNotAllowed()
}

[Fact]
public async void transactionOutsideSpecifiedTimeStampsAreNotReturned()
public async void TransactionOutsideSpecifiedTimeStampsAreNotReturned()
{
// Arrange
var date1 = new DateTime(2020, 12, 1, 0, 0, 0, DateTimeKind.Utc);
Expand All @@ -68,6 +72,21 @@ public async void transactionOutsideSpecifiedTimeStampsAreNotReturned()
var controller = new ExportController(_testHelper.dbContextFactory);
var address = "3XSLuJcXg6xEua6iBPnWacc3iWh93yEDMCqX8FbE3RDSbEnT9P";

MemoryStream stream = new MemoryStream();
var _headers = new HeaderDictionary();

var httpResponseMock = new Mock<HttpResponse>();
httpResponseMock.Setup(mock => mock.Body).Returns(stream);
httpResponseMock.Setup(mock => mock.Headers).Returns(_headers);

var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(mock => mock.Response).Returns(httpResponseMock.Object);

var _controllerContext = new ControllerContext
{
HttpContext = httpContextMock.Object
};

_testHelper.DbContext.Accounts.Add(new AccountBuilder()
.WithId(42)
.WithCanonicalAddress(address, true)
Expand All @@ -82,15 +101,13 @@ public async void transactionOutsideSpecifiedTimeStampsAreNotReturned()

// Act
var actionResult = await controller.GetStatementExport(address, startDate, endDate);
var result = Assert.IsType<FileStreamResult>(actionResult);
);

using StreamReader reader = new(result.FileStream, System.Text.Encoding.UTF8);
string csv = reader.ReadToEnd();
var result = Assert.IsType<EnumerableFileResult<string>>(actionResult);
await result.ExecuteResultAsync(_controllerContext);
string csv = System.Text.Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine($"csv: {csv}");

// Assert
Regex.Matches(csv, "\n").Count.Should().Be(2);

}

}

0 comments on commit 43c9ff9

Please sign in to comment.