[ACS-6584] Added validation for illegal characters when creating a tag (#9251)

* [ACS-6584] added validation for illegal character on tag creation

* [ACS-6584] remove fdescribe

* [ACS-6584] cr fix

* [ACS-6584] missing semicolon
This commit is contained in:
Mykyta Maliarchuk
2024-01-18 18:30:28 +01:00
committed by GitHub
parent e1fcaed12d
commit ca38f05f30
3 changed files with 34 additions and 4 deletions

View File

@@ -155,7 +155,8 @@
"EMPTY_TAG": "Tag name can't contain only spaces",
"REQUIRED": "Tag name is required",
"FETCH_TAGS": "Error while fetching the tags",
"CREATE_TAGS": "Error while creating the tags"
"CREATE_TAGS": "Error while creating the tags",
"SPECIAL_CHARACTERS": "Tag name cannot contain prohibited characters"
},
"TOOLTIPS": {
"DELETE_TAG": "Delete tag"

View File

@@ -382,6 +382,14 @@ describe('TagsCreatorComponent', () => {
expect(getFirstError()).toBe('TAG.TAGS_CREATOR.ERRORS.ALREADY_ADDED_TAG');
}));
it('should show error for prohibited characters', fakeAsync(() => {
typeTag('tag*"<>\\/?:|');
component.tagNameControl.markAsTouched();
fixture.detectChanges();
const error = getFirstError();
expect(error).toBe('TAG.TAGS_CREATOR.ERRORS.SPECIAL_CHARACTERS');
}));
it('should show error when duplicated already existing tag', fakeAsync(() => {
const tag = 'Some tag';
@@ -518,6 +526,17 @@ describe('TagsCreatorComponent', () => {
expect(tagService.findTagByName).toHaveBeenCalledWith(name);
}));
it('should not perform search if an illegal character is specified', fakeAsync(() => {
spyOn(tagService, 'findTagByName').and.returnValue(EMPTY);
spyOn(tagService, 'searchTags').and.returnValue(EMPTY);
const name = 'Tag:"\'>';
typeTag(name);
expect(tagService.findTagByName).not.toHaveBeenCalled();
expect(tagService.searchTags).not.toHaveBeenCalled();
}));
it('should call searchTags on tagService using name set in input and correct params', fakeAsync(() => {
spyOn(tagService, 'searchTags').and.returnValue(EMPTY);

View File

@@ -41,6 +41,7 @@ interface TagNameControlErrors {
duplicatedAddedTag?: boolean;
emptyTag?: boolean;
required?: boolean;
specialCharacters?: boolean;
}
const DEFAULT_TAGS_SORTING = {
@@ -132,7 +133,8 @@ export class TagsCreatorComponent implements OnInit, OnDestroy {
['duplicatedExistingTag', 'EXISTING_TAG'],
['duplicatedAddedTag', 'ALREADY_ADDED_TAG'],
['emptyTag', 'EMPTY_TAG'],
['required', 'REQUIRED']
['required', 'REQUIRED'],
['specialCharacters', 'SPECIAL_CHARACTERS']
]);
private readonly existingTagsListLimit = 15;
@@ -144,7 +146,8 @@ export class TagsCreatorComponent implements OnInit, OnDestroy {
[
this.validateIfNotAlreadyAdded.bind(this),
Validators.required,
this.validateEmptyTag
this.validateEmptyTag,
this.validateSpecialCharacters
],
this.validateIfNotExistingTag.bind(this)
);
@@ -301,7 +304,7 @@ export class TagsCreatorComponent implements OnInit, OnDestroy {
}
private loadTags(name: string) {
if (name) {
if (name && !this.tagNameControl.hasError('specialCharacters')) {
forkJoin({
exactResult: this.tagService.findTagByName(name),
searchedResult: this.tagService.searchTags(name, DEFAULT_TAGS_SORTING, false, 0, this.existingTagsListLimit)
@@ -369,6 +372,13 @@ export class TagsCreatorComponent implements OnInit, OnDestroy {
: null;
}
private validateSpecialCharacters(tagNameControl: FormControl<string>): TagNameControlErrors | null {
const specialSymbolsRegex = /[':"\\|<>/?]/;
return tagNameControl.value.length && specialSymbolsRegex.test(tagNameControl.value)
? { specialCharacters: true }
: null;
}
private setTagNameControlErrorMessageKey(): void {
this._tagNameErrorMessageKey = this.tagNameControl.invalid
? `TAG.TAGS_CREATOR.ERRORS.${this.nameErrorMessagesByErrors.get(