Skip to content

Commit cc174c5

Browse files
authored
End-to-end happy path test (#199)
* Added full E2E test * Fixed our method for determining if we are in development or test * No need to pin versions * Deleting brittle tests in favor of E2E * Fix docker compose
1 parent 47052e6 commit cc174c5

File tree

10 files changed

+109
-232
lines changed

10 files changed

+109
-232
lines changed

Test/Altinn.Broker.Tests/Altinn.Broker.Tests.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
1415
<PackageReference Include="xunit" Version="2.6.2" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Net.Http.Headers;
2+
using System.Net.Http.Json;
3+
using System.Text;
4+
using System.Text.Json;
5+
6+
using Altinn.Broker.Core.Models;
7+
using Altinn.Broker.Enums;
8+
using Altinn.Broker.Models;
9+
10+
using Microsoft.AspNetCore.Mvc.Testing;
11+
12+
namespace Altinn.Broker.Tests;
13+
public class FileControllerTests : IClassFixture<WebApplicationFactory<Program>>
14+
{
15+
private readonly WebApplicationFactory<Program> _factory;
16+
private readonly HttpClient _client;
17+
private readonly JsonSerializerOptions _responseSerializerOptions;
18+
19+
public FileControllerTests(WebApplicationFactory<Program> factory)
20+
{
21+
_factory = factory;
22+
_client = factory.CreateClient();
23+
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIwMTkyOjk5MTgyNTgyNyJ9.exFSD-mL1fzoWhg8IKcVeCeEyJ5qpABPU9A1AXHDa_k");
24+
_responseSerializerOptions = new JsonSerializerOptions(new JsonSerializerOptions()
25+
{
26+
PropertyNameCaseInsensitive = true
27+
});
28+
_responseSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
29+
}
30+
31+
32+
[Fact]
33+
public async Task WhenAllIsOk_NormalFlow_Success()
34+
{
35+
var initializeFileResponse = await _client.PostAsJsonAsync("broker/api/v1/file", new FileInitalizeExt()
36+
{
37+
Checksum = null,
38+
FileName = "input.txt",
39+
PropertyList = [],
40+
Recipients = new List<string> { "974761076" },
41+
Sender = "991825827",
42+
SendersFileReference = "test-data"
43+
});
44+
var fileId = await initializeFileResponse.Content.ReadAsStringAsync();
45+
46+
var initializedFile = await _client.GetFromJsonAsync<FileOverviewExt>($"broker/api/v1/file/{fileId}", _responseSerializerOptions);
47+
Assert.NotNull(initializedFile);
48+
Assert.True(initializedFile.FileStatus == FileStatusExt.Initialized);
49+
50+
var uploadedFileBytes = Encoding.UTF8.GetBytes("This is the contents of the uploaded file");
51+
using (var content = new ByteArrayContent(uploadedFileBytes))
52+
{
53+
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
54+
var uploadResponse = await _client.PostAsync($"broker/api/v1/file/{fileId}/upload", content);
55+
Assert.True(uploadResponse.IsSuccessStatusCode);
56+
}
57+
58+
var uploadedFile = await _client.GetFromJsonAsync<FileOverviewExt>($"broker/api/v1/file/{fileId}", _responseSerializerOptions);
59+
Assert.NotNull(uploadedFile);
60+
Assert.True(uploadedFile.FileStatus == FileStatusExt.Published); // When running integration test this happens instantly as of now.
61+
62+
var downloadedFile = await _client.GetAsync($"broker/api/v1/file/{fileId}/download");
63+
var downloadedFileBytes = await downloadedFile.Content.ReadAsByteArrayAsync();
64+
Assert.Equal(uploadedFileBytes, downloadedFileBytes);
65+
66+
var downloadedFileDetails = await _client.GetFromJsonAsync<FileStatusDetailsExt>($"broker/api/v1/file/{fileId}/details", _responseSerializerOptions);
67+
Assert.NotNull(downloadedFileDetails);
68+
Assert.True(downloadedFileDetails.FileStatus == FileStatusExt.Published);
69+
Assert.Contains(downloadedFileDetails.RecipientFileStatusHistory, recipient => recipient.RecipientFileStatusCode == RecipientFileStatusExt.DownloadStarted);
70+
71+
await _client.PostAsync($"broker/api/v1/file/{fileId}/confirmdownload", null);
72+
73+
var confirmedFileDetails = await _client.GetFromJsonAsync<FileStatusDetailsExt>($"broker/api/v1/file/{fileId}/details", _responseSerializerOptions);
74+
Assert.NotNull(confirmedFileDetails);
75+
Assert.True(confirmedFileDetails.FileStatus == FileStatusExt.AllConfirmedDownloaded);
76+
Assert.Contains(confirmedFileDetails.RecipientFileStatusHistory, recipient => recipient.RecipientFileStatusCode == RecipientFileStatusExt.DownloadConfirmed);
77+
}
78+
}

Test/Altinn.Broker.Tests/Persistence/RepositoryTests.cs

-210
This file was deleted.

docker-compose.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ networks:
99

1010
services:
1111
storage:
12-
image: mcr.microsoft.com/azure-storage/azurite
12+
image: mcr.microsoft.com/azure-storage/azurite:latest
1313
ports:
1414
- "10000:10000"
1515
- "10001:10001"
@@ -38,7 +38,7 @@ services:
3838
POSTGRES_PASSWORD: postgres
3939
POSTGRES_DB: broker
4040
database_migration:
41-
image: flyway/flyway@sha256:54414197790fe7aa1da07b8034f82e40b7e33a8b3f67f27972c2f3f73271147f
41+
image: flyway/flyway:latest
4242
command: -url='jdbc:postgresql://database:5432/broker' -user=postgres -password=postgres -connectRetries=60 migrate -validateMigrationNaming='true'
4343
volumes:
4444
- ./src/Altinn.Broker.Persistence/Migrations:/flyway/sql
@@ -49,7 +49,7 @@ services:
4949
"flyway",
5050
]
5151
prepare_test_data:
52-
image: flyway/flyway@sha256:54414197790fe7aa1da07b8034f82e40b7e33a8b3f67f27972c2f3f73271147f
52+
image: flyway/flyway:latest
5353
command: -url='jdbc:postgresql://database:5432/broker' -user=postgres -password=postgres -connectRetries=60 migrate
5454
volumes:
5555
- ./Test/Altinn.Broker.Tests/Data:/flyway/sql

src/Altinn.Broker.Integrations/Azure/AzureBrokerStorageService.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
using Altinn.Broker.Core.Services;
33
using Altinn.Broker.Repositories;
44

5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
57
using Microsoft.Extensions.Options;
68

79
namespace Altinn.Broker.Integrations.Azure;
810
public class AzureBrokerStorageService : IBrokerStorageService
911
{
1012
private readonly IFileStore _fileStore;
1113
private readonly IResourceManager _resourceManager;
14+
private readonly IHostEnvironment _hostEnvironment;
1215
private readonly AzureStorageOptions _azureStorageOptions;
16+
private readonly ILogger<AzureBrokerStorageService> _logger;
1317

14-
public AzureBrokerStorageService(IFileStore fileStore, IResourceManager resourceManager, IOptions<AzureStorageOptions> options)
18+
public AzureBrokerStorageService(IFileStore fileStore, IResourceManager resourceManager, IHostEnvironment hostEnvironment, IOptions<AzureStorageOptions> options, ILogger<AzureBrokerStorageService> logger)
1519
{
1620
_fileStore = fileStore;
1721
_resourceManager = resourceManager;
22+
_hostEnvironment = hostEnvironment;
1823
_azureStorageOptions = options.Value;
24+
_logger = logger;
1925
}
2026

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

3339
private async Task<string> GetConnectionString(ServiceOwnerEntity serviceOwnerEntity)
3440
{
35-
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
41+
if (_hostEnvironment.IsDevelopment())
3642
{
43+
_logger.LogInformation("Running in development. Using local development storage.");
3744
return _azureStorageOptions.ConnectionString;
3845
}
3946
return await _resourceManager.GetStorageConnectionString(serviceOwnerEntity);

0 commit comments

Comments
 (0)