Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-to-end happy path test #199

Merged
merged 13 commits into from
Nov 27, 2023
3 changes: 2 additions & 1 deletion Test/Altinn.Broker.Tests/Altinn.Broker.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
Expand Down
78 changes: 78 additions & 0 deletions Test/Altinn.Broker.Tests/FileControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;

using Altinn.Broker.Core.Models;
using Altinn.Broker.Enums;
using Altinn.Broker.Models;

using Microsoft.AspNetCore.Mvc.Testing;

namespace Altinn.Broker.Tests;
public class FileControllerTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _responseSerializerOptions;

public FileControllerTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
_client = factory.CreateClient();
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIwMTkyOjk5MTgyNTgyNyJ9.exFSD-mL1fzoWhg8IKcVeCeEyJ5qpABPU9A1AXHDa_k");
_responseSerializerOptions = new JsonSerializerOptions(new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});
_responseSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
}


[Fact]
public async Task WhenAllIsOk_NormalFlow_Success()
{
var initializeFileResponse = await _client.PostAsJsonAsync("broker/api/v1/file", new FileInitalizeExt()
{
Checksum = null,
FileName = "input.txt",
PropertyList = [],
Recipients = new List<string> { "974761076" },
Sender = "991825827",
SendersFileReference = "test-data"
});
var fileId = await initializeFileResponse.Content.ReadAsStringAsync();

var initializedFile = await _client.GetFromJsonAsync<FileOverviewExt>($"broker/api/v1/file/{fileId}", _responseSerializerOptions);
Assert.NotNull(initializedFile);
Assert.True(initializedFile.FileStatus == FileStatusExt.Initialized);

var uploadedFileBytes = Encoding.UTF8.GetBytes("This is the contents of the uploaded file");
using (var content = new ByteArrayContent(uploadedFileBytes))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var uploadResponse = await _client.PostAsync($"broker/api/v1/file/{fileId}/upload", content);
Assert.True(uploadResponse.IsSuccessStatusCode);
}

var uploadedFile = await _client.GetFromJsonAsync<FileOverviewExt>($"broker/api/v1/file/{fileId}", _responseSerializerOptions);
Assert.NotNull(uploadedFile);
Assert.True(uploadedFile.FileStatus == FileStatusExt.Published); // When running integration test this happens instantly as of now.

var downloadedFile = await _client.GetAsync($"broker/api/v1/file/{fileId}/download");
var downloadedFileBytes = await downloadedFile.Content.ReadAsByteArrayAsync();
Assert.Equal(uploadedFileBytes, downloadedFileBytes);

var downloadedFileDetails = await _client.GetFromJsonAsync<FileStatusDetailsExt>($"broker/api/v1/file/{fileId}/details", _responseSerializerOptions);
Assert.NotNull(downloadedFileDetails);
Assert.True(downloadedFileDetails.FileStatus == FileStatusExt.Published);
Assert.Contains(downloadedFileDetails.RecipientFileStatusHistory, recipient => recipient.RecipientFileStatusCode == RecipientFileStatusExt.DownloadStarted);

await _client.PostAsync($"broker/api/v1/file/{fileId}/confirmdownload", null);

var confirmedFileDetails = await _client.GetFromJsonAsync<FileStatusDetailsExt>($"broker/api/v1/file/{fileId}/details", _responseSerializerOptions);
Assert.NotNull(confirmedFileDetails);
Assert.True(confirmedFileDetails.FileStatus == FileStatusExt.AllConfirmedDownloaded);
Assert.Contains(confirmedFileDetails.RecipientFileStatusHistory, recipient => recipient.RecipientFileStatusCode == RecipientFileStatusExt.DownloadConfirmed);
}
}
210 changes: 0 additions & 210 deletions Test/Altinn.Broker.Tests/Persistence/RepositoryTests.cs

This file was deleted.

6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ networks:

services:
storage:
image: mcr.microsoft.com/azure-storage/azurite
image: mcr.microsoft.com/azure-storage/azurite:latest
ports:
- "10000:10000"
- "10001:10001"
Expand Down Expand Up @@ -38,7 +38,7 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: broker
database_migration:
image: flyway/flyway@sha256:54414197790fe7aa1da07b8034f82e40b7e33a8b3f67f27972c2f3f73271147f
image: flyway/flyway:latest
command: -url='jdbc:postgresql://database:5432/broker' -user=postgres -password=postgres -connectRetries=60 migrate -validateMigrationNaming='true'
volumes:
- ./src/Altinn.Broker.Persistence/Migrations:/flyway/sql
Expand All @@ -49,7 +49,7 @@ services:
"flyway",
]
prepare_test_data:
image: flyway/flyway@sha256:54414197790fe7aa1da07b8034f82e40b7e33a8b3f67f27972c2f3f73271147f
image: flyway/flyway:latest
command: -url='jdbc:postgresql://database:5432/broker' -user=postgres -password=postgres -connectRetries=60 migrate
volumes:
- ./Test/Altinn.Broker.Tests/Data:/flyway/sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
using Altinn.Broker.Core.Services;
using Altinn.Broker.Repositories;

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Altinn.Broker.Integrations.Azure;
public class AzureBrokerStorageService : IBrokerStorageService
{
private readonly IFileStore _fileStore;
private readonly IResourceManager _resourceManager;
private readonly IHostEnvironment _hostEnvironment;
private readonly AzureStorageOptions _azureStorageOptions;
private readonly ILogger<AzureBrokerStorageService> _logger;

public AzureBrokerStorageService(IFileStore fileStore, IResourceManager resourceManager, IOptions<AzureStorageOptions> options)
public AzureBrokerStorageService(IFileStore fileStore, IResourceManager resourceManager, IHostEnvironment hostEnvironment, IOptions<AzureStorageOptions> options, ILogger<AzureBrokerStorageService> logger)
{
_fileStore = fileStore;
_resourceManager = resourceManager;
_hostEnvironment = hostEnvironment;
_azureStorageOptions = options.Value;
_logger = logger;
}

public async Task UploadFile(ServiceOwnerEntity serviceOwnerEntity, FileEntity fileEntity, Stream stream)
Expand All @@ -32,8 +38,9 @@ public async Task<Stream> DownloadFile(ServiceOwnerEntity serviceOwnerEntity, Fi

private async Task<string> GetConnectionString(ServiceOwnerEntity serviceOwnerEntity)
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
if (_hostEnvironment.IsDevelopment())
{
_logger.LogInformation("Running in development. Using local development storage.");
return _azureStorageOptions.ConnectionString;
}
return await _resourceManager.GetStorageConnectionString(serviceOwnerEntity);
Expand Down
Loading