|
| 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 | +} |
0 commit comments