Skip to content

Commit a7bd89e

Browse files
committed
Refactor thumbnail handling in tests: replace IFileAttachmentStreamProvider with IFileContentProvider and update related methods
1 parent 2fbb546 commit a7bd89e

4 files changed

Lines changed: 144 additions & 82 deletions

File tree

source/AAS.TwinEngine.DataEngine.ModuleTests/Api/Services/AasRepository/AasRepositoryControllerTests.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Net;
1+
using System.Net;
22
using System.Net.Http.Json;
33
using System.Text;
44
using System.Text.Json.Nodes;
@@ -27,14 +27,14 @@ public abstract class AasRepositoryControllerTests : IDisposable
2727
{
2828
private readonly ConfigTestFactory _factory;
2929
private readonly ITemplateProvider _mockTemplateProvider;
30-
private readonly IFileAttachmentStreamProvider _fileAttachmentStreamProvider;
30+
private readonly IFileContentProvider _fileContentProvider;
3131
private readonly HttpClient _client;
3232
private readonly ICreateClient _httpClientFactory;
3333

3434
protected AasRepositoryControllerTests(string configDir)
3535
{
3636
_mockTemplateProvider = Substitute.For<ITemplateProvider>();
37-
_fileAttachmentStreamProvider = Substitute.For<IFileAttachmentStreamProvider>();
37+
_fileContentProvider = Substitute.For<IFileContentProvider>();
3838
var mockPluginManifestProvider = Substitute.For<IPluginManifestProvider>();
3939
var mockPluginManifestConflictHandler = Substitute.For<IPluginManifestConflictHandler>();
4040
_httpClientFactory = Substitute.For<ICreateClient>();
@@ -45,7 +45,7 @@ protected AasRepositoryControllerTests(string configDir)
4545
_ = services.AddSingleton(mockPluginManifestConflictHandler);
4646
_ = services.AddSingleton(_httpClientFactory);
4747
_ = services.AddSingleton(_mockTemplateProvider);
48-
_ = services.AddSingleton(_fileAttachmentStreamProvider);
48+
_ = services.AddSingleton(_fileContentProvider);
4949
});
5050

5151
_client = _factory.CreateClient();
@@ -592,10 +592,7 @@ public async Task GetThumbnailAsync_ShouldReturn200OKWithStream_WhenThumbnailExi
592592
};
593593
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
594594

595-
_ = _fileAttachmentStreamProvider.GetResponseHeadersAsync("https://example.com/share/img/10080308_DE.jpg", Arg.Any<CancellationToken>())
596-
.Returns(httpResponseMessage);
597-
_ = _fileAttachmentStreamProvider.ReadStreamAsync(httpResponseMessage, Arg.Any<CancellationToken>())
598-
.Returns(new MemoryStream("test-bytes"u8.ToArray()));
595+
_ = _fileContentProvider.GetFileContentAsync("https://example.com/share/img/10080308_DE.jpg", Arg.Any<CancellationToken>());
599596

600597
var response = await _client.GetAsync($"/shells/{AasIdentifier}/asset-information/thumbnail");
601598

source/AAS.TwinEngine.DataEngine.UnitTests/Api/AasRepository/AasRepositoryControllerTests.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using AAS.TwinEngine.DataEngine.Api.AasRepository.Requests;
88
using AAS.TwinEngine.DataEngine.Api.AasRepository.Responses;
99
using AAS.TwinEngine.DataEngine.Api.Shared;
10+
using AAS.TwinEngine.DataEngine.Api.Shared.Results;
1011
using AAS.TwinEngine.DataEngine.ApplicationLogic.Extensions;
12+
using AAS.TwinEngine.DataEngine.DomainModel.Shared;
1113
using AAS.TwinEngine.DataEngine.DomainModel.SubmodelRepository;
1214

1315
using AasCore.Aas3_1;
@@ -276,18 +278,30 @@ private static SubmodelRefDto CreateSubmodelRefDto()
276278
}
277279

278280
[Fact]
279-
public async Task GetThumbnailAsync_ShouldReturnFileStreamResult()
281+
public async Task GetThumbnailAsync_ShouldReturnFileContentStreamResult_WhenHandlerCompletes()
280282
{
281283
var expectedStream = new MemoryStream("test-image"u8.ToArray());
282-
var attachmentResult = new FileAttachmentResult(expectedStream, "image/png", "thumbnail.png");
284+
var attachmentResult = new FileAttachmentResult(expectedStream, "image/png", "thumbnail.png", 100 * 1024 * 1024);
283285
_handler.GetThumbnailAsync(Arg.Any<GetThumbnailRequest>(), Arg.Any<CancellationToken>())
284286
.Returns(attachmentResult);
285287

286288
var response = await _sut.GetThumbnailAsync(AasIdentifier, CancellationToken.None);
287289

288-
var fileResult = Assert.IsType<FileStreamResult>(response);
289-
Assert.Equal("image/png", fileResult.ContentType);
290-
Assert.Equal("thumbnail.png", fileResult.FileDownloadName);
290+
Assert.IsType<FileContentStreamResult>(response);
291+
}
292+
293+
[Fact]
294+
public async Task GetThumbnailAsync_PassesRouteValuesToHandler()
295+
{
296+
_handler.GetThumbnailAsync(Arg.Any<GetThumbnailRequest>(), Arg.Any<CancellationToken>())
297+
.Returns(new FileAttachmentResult(Stream.Null, "image/png", "thumbnail.png", 100 * 1024 * 1024));
298+
299+
await _sut.GetThumbnailAsync(AasIdentifier, CancellationToken.None);
300+
301+
await _handler.Received(1)
302+
.GetThumbnailAsync(
303+
Arg.Is<GetThumbnailRequest>(r => r.AasIdentifier == AasIdentifier),
304+
Arg.Any<CancellationToken>());
291305
}
292306
}
293307

source/AAS.TwinEngine.DataEngine.UnitTests/Api/AasRepository/Handler/AasRepositoryHandlerTests.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public async Task GetSubmodelRefByIdAsync_InvalidBase64_ThrowsInvalidUserInputEx
260260
[Fact]
261261
public async Task GetThumbnailAsync_ShouldReturnFileAttachmentResult()
262262
{
263-
var expectedResult = new FileAttachmentResult(new MemoryStream(), "image/png", "test.png");
263+
var expectedResult = new FileAttachmentResult(new MemoryStream(), "image/png", "test.png", 100 * 1024 * 1024);
264264
_aasRepositoryService.GetThumbnailAsync("https://example.com/aas", Arg.Any<CancellationToken>())
265265
.Returns(expectedResult);
266266

@@ -270,6 +270,59 @@ public async Task GetThumbnailAsync_ShouldReturnFileAttachmentResult()
270270
Assert.Equal(expectedResult, result);
271271
}
272272

273+
[Fact]
274+
public async Task GetThumbnailAsync_CallsServiceWithDecodedAasIdentifier_WhenInputIsValid()
275+
{
276+
const string aasIdentifier = "https://example.com/aas";
277+
var encodedId = aasIdentifier.EncodeBase64Url();
278+
var request = new GetThumbnailRequest(encodedId);
279+
var expectedResult = new FileAttachmentResult(Stream.Null, "image/png", "thumbnail.png", 100 * 1024 * 1024);
280+
281+
_aasRepositoryService
282+
.GetThumbnailAsync(aasIdentifier, Arg.Any<CancellationToken>())
283+
.Returns(expectedResult);
284+
285+
await _sut.GetThumbnailAsync(request, CancellationToken.None);
286+
287+
await _aasRepositoryService.Received(1)
288+
.GetThumbnailAsync(aasIdentifier, Arg.Any<CancellationToken>());
289+
}
290+
291+
[Fact]
292+
public async Task GetThumbnailAsync_InvalidBase64AasIdentifier_ThrowsInvalidUserInputException()
293+
{
294+
const string invalidEncodedId = "!!invalid_base64@@";
295+
var request = new GetThumbnailRequest(invalidEncodedId);
296+
297+
await Assert.ThrowsAsync<InvalidUserInputException>(() =>
298+
_sut.GetThumbnailAsync(request, CancellationToken.None));
299+
}
300+
301+
[Theory]
302+
[InlineData("../../../etc/passwd")]
303+
[InlineData("..\\..\\..\\windows\\system32")]
304+
public async Task GetThumbnailAsync_MaliciousDecodedIdentifier_ThrowsInvalidUserInputException(string maliciousIdentifier)
305+
{
306+
var request = new GetThumbnailRequest(maliciousIdentifier.EncodeBase64Url());
307+
308+
await Assert.ThrowsAsync<InvalidUserInputException>(() =>
309+
_sut.GetThumbnailAsync(request, CancellationToken.None));
310+
}
311+
312+
[Fact]
313+
public async Task GetThumbnailAsync_ServiceReturnsNull_ThrowsTemplateNotFoundException()
314+
{
315+
const string aasIdentifier = "https://example.com/aas";
316+
var request = new GetThumbnailRequest(aasIdentifier.EncodeBase64Url());
317+
318+
_aasRepositoryService
319+
.GetThumbnailAsync(aasIdentifier, Arg.Any<CancellationToken>())!
320+
.Returns((FileAttachmentResult)null!);
321+
322+
await Assert.ThrowsAsync<TemplateNotFoundException>(() =>
323+
_sut.GetThumbnailAsync(request, CancellationToken.None));
324+
}
325+
273326
private static AssetInformation CreateAssetInformation()
274327
{
275328
var thumbnail = Substitute.For<IResource>();

0 commit comments

Comments
 (0)