-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make endpoint to read images as stream from relative repo path (#11317)
* Make endpoint to read images as bytearray from relative repo path * Allow imagefilepath to be arbitrary long * Add tests * Use stream instead of read all bytes * Add tests for non-existing iamges
- Loading branch information
Showing
9 changed files
with
471 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
backend/tests/Designer.Tests/Controllers/PreviewController/GetImageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Services.Interfaces; | ||
using Designer.Tests.Utils; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Controllers.PreviewController | ||
{ | ||
public class GetImageTests : PreviewControllerTestsBase<GetImageTests>, IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
|
||
public GetImageTests(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Image_From_Wwww_Root_Folder_Ok() | ||
{ | ||
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, App, Developer, "App/wwwroot/AltinnD-logo.svg"); | ||
|
||
string dataPathWithData = $"{Org}/{App}/AltinnD-logo.svg"; | ||
|
||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
||
var result = await response.Content.ReadAsByteArrayAsync(); | ||
Assert.NotNull(result); | ||
Assert.Equal(expectedImageData, result); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Image_From_Sub_Folder_Ok() | ||
{ | ||
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, App, Developer, "App/wwwroot/images/AltinnD-logo.svg"); | ||
|
||
string dataPathWithData = $"{Org}/{App}/images/AltinnD-logo.svg"; | ||
|
||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
||
var result = await response.Content.ReadAsByteArrayAsync(); | ||
Assert.NotNull(result); | ||
Assert.Equal(expectedImageData, result); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Image_From_Sub_Sub_Folder_Ok() | ||
{ | ||
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, App, Developer, "App/wwwroot/images/subImagesFolder/AltinnD-logo.svg"); | ||
|
||
string dataPathWithData = $"{Org}/{App}/images/subImagesFolder/AltinnD-logo.svg"; | ||
|
||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
||
byte[] result = await response.Content.ReadAsByteArrayAsync(); | ||
Assert.NotNull(result); | ||
Assert.Equal(expectedImageData, result); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Image_Non_Existing_Folder_Returns_NotFound() | ||
{ | ||
string dataPathWithData = $"{Org}/{App}/images/subImagesFolder/SubSubImageFolder/AltinnD-logo.svg"; | ||
|
||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Image_Non_Existing_Image_Return_NotFound() | ||
{ | ||
string dataPathWithData = $"{Org}/{App}/images/subImagesFolder/non-existing-image.svg"; | ||
|
||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Call_To_Get_Designer_Iframe_Does_Not_Hit_Image_EndPoint() | ||
{ | ||
Mock<IAltinnGitRepositoryFactory> factMock = new(); | ||
ConfigureTestForSpecificTest = s => | ||
{ | ||
s.AddTransient(_ => factMock.Object); | ||
}; | ||
|
||
string dataPathWithData = "designer/html/path/some-file.jpg"; | ||
using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData); | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
factMock.Verify(x => x.GetAltinnAppGitRepository(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...ts/_TestData/Repositories/testUser/ttd/preview-app/App/wwwroot/AltinnD-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.