Skip to content

Commit

Permalink
fix: use same regex for layoutsets in FE and BE
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Dec 23, 2024
1 parent cc8754c commit 4bcf361
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ await _mediator.Publish(new LayoutSetCreatedEvent
[Route("layout-set/{layoutSetIdToUpdate}")]
public async Task<ActionResult> UpdateLayoutSetName(string org, string app, [FromRoute] string layoutSetIdToUpdate, [FromBody] string newLayoutSetName, CancellationToken cancellationToken)
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);
LayoutSets layoutSets = await _appDevelopmentService.UpdateLayoutSetName(editingContext, layoutSetIdToUpdate, newLayoutSetName, cancellationToken);
return Ok(layoutSets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class AppDevelopmentErrorCodes
{
public const string NonUniqueLayoutSetIdError = "AD_01";
public const string NonUniqueTaskForLayoutSetError = "AD_02";
public const string EmptyLayoutSetIdError = "AD_03";
public const string InvalidLayoutSetIdError = "AD_03";
public const string ConflictingFileNameError = "AD_04";
public const string UploadedImageNotValid = nameof(UploadedImageNotValid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void OnException(ExceptionContext context)
}
if (context.Exception is InvalidLayoutSetIdException)
{
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, AppDevelopmentErrorCodes.EmptyLayoutSetIdError, HttpStatusCode.BadRequest)) { StatusCode = (int)HttpStatusCode.BadRequest };
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, AppDevelopmentErrorCodes.InvalidLayoutSetIdError, HttpStatusCode.BadRequest)) { StatusCode = (int)HttpStatusCode.BadRequest };
}
if (context.Exception is ConflictingFileNameException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AppDevelopmentService : IAppDevelopmentService
{
private readonly IAltinnGitRepositoryFactory _altinnGitRepositoryFactory;
private readonly ISchemaModelService _schemaModelService;
private readonly string _layoutSetNameRegEx = "[a-zA-Z0-9-]{2,28}";
private readonly string _layoutSetNameRegEx = @"^[a-zA-Z0-9_\-]{2,28}$";

/// <summary>
/// Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ public async Task UpdateLayoutSet_NewLayoutSetNameIsEmpty_ReturnsBadRequest(stri
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData("ttd", "app-with-layoutsets", "testUser", "layoutSet1")]
public async Task UpdateLayoutSet_NewLayoutSetNameDoesNotMatchRegex_ReturnsBadRequest(string org, string app, string developer,
string oldLayoutSetName)
{
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest(org, app, developer, targetRepository);
string newLayoutSetName = ".abc";

string url = $"{VersionPrefix(org, targetRepository)}/layout-set/{oldLayoutSetName}";

using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, url)
{
Content = new StringContent($"\"{newLayoutSetName}\"", Encoding.UTF8, MediaTypeNames.Application.Json)
};

using var response = await HttpClient.SendAsync(httpRequestMessage);
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData("ttd", "app-without-layoutsets", "testUser", null)]
public async Task UpdateLayoutSetName_AppWithoutLayoutSets_ReturnsNotFound(string org, string app, string developer,
Expand Down
2 changes: 1 addition & 1 deletion frontend/language/src/nb.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"api_errors.AD_01": "En annen sidegruppe har allerede dette navnet.",
"api_errors.AD_02": "Det finnes allerede en sidegruppe med denne oppgaven.",
"api_errors.AD_03": "Navnet på sidegruppen kan ikke være tomt.",
"api_errors.AD_03": "Navnet på sidegruppen er ugyldig. Du kan bruke tall, understrek, bindestrek, og store/små bokstaver fra det engelske alfabetet.",
"api_errors.DM_01": "Kunne ikke bygge datamodellen.",
"api_errors.DM_05": "Kunne ikke lese ugyldig XML.",
"api_errors.DM_CsharpCompiler_NameCollision": "Navnet <bold>{{nodeName}}</bold> er det samme som på et overordnet element. Gi ett av elementene et annet navn.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ describe('FileNameUtils', () => {
expect(fileNameError).toBe(FileNameErrorResult.NoRegExMatch);
});

it('Returns "NoRegExMatch" when file name does not match file name regex in terms of length', () => {
const fileName: string = '12345678901234567890123456789';
const fileNameError: FileNameErrorResult = FileNameUtils.findFileNameError(fileName, []);
expect(fileNameError).toBe(FileNameErrorResult.NoRegExMatch);
});

it('Returns "FileExists" when file name matches regEx and name exists in list', () => {
const fileName: string = 'fileName1';
const invalidFileNames: string[] = [fileName, 'fileName2', 'fileName3'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StringUtils } from '@studio/pure-functions';

export const FILE_NAME_REGEX = /^[a-zA-Z0-9_\-]{1,50}$/;
export const FILE_NAME_REGEX = /^[a-zA-Z0-9_\-]{2,28}$/;

export enum FileNameErrorResult {
FileNameIsEmpty = 'fileNameIsEmpty',
Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/shared/src/utils/layoutSetsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const getLayoutSetIdValidationErrorKey = (
): string => {
if (oldLayoutSetId === newLayoutSetId) return null;
if (!newLayoutSetId || newLayoutSetId.trim() === '') return 'validation_errors.required';
if (!validateLayoutNameAndLayoutSetName(newLayoutSetId))
return 'validation_errors.file_name_invalid';
if (newLayoutSetId.length === 1)
return 'process_editor.configuration_panel_custom_receipt_layout_set_name_validation';
if (!validateLayoutNameAndLayoutSetName(newLayoutSetId))
return 'validation_errors.file_name_invalid';
if (layoutSets.sets.some((set) => set.id === newLayoutSetId))
return 'process_editor.configuration_panel_layout_set_id_not_unique';
return null;
Expand Down

0 comments on commit 4bcf361

Please sign in to comment.