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: use same regex for layoutsets in FE and BE #14327

Merged
Merged
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
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. Det må ha mellom 2 og 28 tegn og kan inneholde 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.",
standeren marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -16,10 +16,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) => StringUtils.areCaseInsensitiveEqual(set.id, newLayoutSetId)))
return 'process_editor.configuration_panel_layout_set_id_not_unique';
return null;
Expand Down
Loading