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

fix: accept no options-folder when fetching usage #14409

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions backend/src/Designer/Controllers/AppDevelopmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,14 @@ public ActionResult GetWidgetSettings(string org, string app)
[Route("option-list-ids")]
public ActionResult GetOptionListIds(string org, string app)
{
try
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
string[] optionListIds = altinnAppGitRepository.GetOptionsListIds();
return Ok(optionListIds);
}
catch (LibGit2Sharp.NotFoundException)
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
string[] optionListIds = altinnAppGitRepository.GetOptionsListIds();
if (optionListIds.Length == 0)
{
return NoContent();
}
return Ok(optionListIds);
}

[HttpGet("app-version")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ public string[] GetOptionsListIds()
string optionsFolder = Path.Combine(OptionsFolderPath);
if (!DirectoryExistsByRelativePath(optionsFolder))
{
throw new NotFoundException("Options folder not found.");
return [];
}

string[] fileNames = GetFilesByRelativeDirectoryAscSorted(optionsFolder, "*.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Models.Dto;
using Designer.Tests.Controllers.ApiTests;
using Designer.Tests.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
Expand All @@ -14,6 +16,7 @@ public class GetOptionListsReferencesTests : DesignerEndpointsTestsBase<GetOptio
{
const string RepoWithUsedOptions = "app-with-options";
const string RepoWithUnusedOptions = "app-with-layoutsets";
const string RepoWithoutOptions = "empty-app";

public GetOptionListsReferencesTests(WebApplicationFactory<Program> factory) : base(factory)
{
Expand Down Expand Up @@ -85,4 +88,25 @@ public async Task GetOptionListsReferences_Returns200Ok_WithEmptyOptionsReferenc
Assert.Equal(StatusCodes.Status200OK, (int)response.StatusCode);
Assert.Equivalent("[]", responseBody);
}

[Fact]
public async Task GetOptionListsReferences_Returns200Ok_WithEmptyOptionsReferences_WhenAppDoesNotHaveOptions()
{
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest("ttd", RepoWithoutOptions, "testUser", targetRepository);
string repoPath = TestDataHelper.GetTestDataRepositoryDirectory("ttd", targetRepository, "testUser");
string exampleLayout = @"{ ""data"": {""layout"": []}}";
string filePath = Path.Combine(repoPath, "App/ui/form/layouts");
Directory.CreateDirectory(filePath);
await File.WriteAllTextAsync(Path.Combine(filePath, "exampleLayout.json"), exampleLayout);

string apiUrl = $"/designer/api/ttd/{targetRepository}/options/usage";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, apiUrl);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
string responseBody = await response.Content.ReadAsStringAsync();

Assert.Equal(StatusCodes.Status200OK, (int)response.StatusCode);
Assert.Equivalent("[]", responseBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ public Task GetOptionListIds_WithAppThatHasOptionLists_ShouldReturnOptionListPat
}

[Fact]
public Task GetOptionListIds_WithAppThatHasNoOptionLists_ShouldThrowNotFoundException()
public void GetOptionListIds_WithAppThatHasNoOptionLists_ShouldReturnEmptyList()
{
string org = "ttd";
string repository = "empty-app";
string developer = "testUser";
AltinnAppGitRepository altinnAppGitRepository = PrepareRepositoryForTest(org, repository, developer);
Assert.Throws<LibGit2Sharp.NotFoundException>(altinnAppGitRepository.GetOptionsListIds);
return Task.CompletedTask;
var optionsIds = altinnAppGitRepository.GetOptionsListIds();
Assert.Equal([], optionsIds);
}

[Fact]
Expand Down
Loading