Skip to content

Commit

Permalink
Add tag validation (#855)
Browse files Browse the repository at this point in the history
* Add regex frontend and backend tag validation fix #464
  • Loading branch information
EMaksy authored Oct 4, 2022
1 parent 6336b87 commit c11cf42
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 6 additions & 4 deletions assets/js/components/Tags/Tags.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useState, useEffect, useRef } from 'react';
import classNames from 'classnames';

import { EOS_NEW_LABEL, EOS_CLOSE } from 'eos-icons-react';

import Pill from '@components/Pill';
import useOnClickOutside from '@hooks/useOnClickOutside';
//eslint-disable-next-line
const tagRegexValidation = /^[\+\-=.,_:@\p{L}\w]*$/u;
const tagValidation = (char) => tagRegexValidation.test(char);

const Tags = ({ className, tags, onChange, onAdd, onRemove }) => {
const [renderedTags, setTags] = useState(tags);
Expand Down Expand Up @@ -61,7 +62,6 @@ const Tags = ({ className, tags, onChange, onAdd, onRemove }) => {
(acc, current) => (current === tag ? acc : [...acc, current]),
[]
);

setTags(newTagsList);
onChange(newTagsList);
onRemove(tag);
Expand All @@ -77,7 +77,9 @@ const Tags = ({ className, tags, onChange, onAdd, onRemove }) => {
ref={inputRef}
className="bg-green-100"
onChange={({ target: { value } }) => {
setNewTagValue(value);
if (tagValidation(value)) {
setNewTagValue(value);
}
}}
onKeyDown={({ key }) => {
if (key === 'Enter') {
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/application/usecases/tags/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Trento.Tag do

import Ecto.Changeset

@forbidden_tag_chars_regex ~r/^[\+\-=.,_:@\p{L}\w]*$/u
@type t :: %__MODULE__{}

@derive {Jason.Encoder, except: [:__meta__, :__struct__]}
Expand All @@ -20,6 +21,7 @@ defmodule Trento.Tag do
tag
|> cast(attrs, [:value, :resource_id, :resource_type])
|> validate_required([:value, :resource_id, :resource_type])
|> validate_format(:value, @forbidden_tag_chars_regex)
|> unique_constraint([:resource_id, :value])
end
end
25 changes: 19 additions & 6 deletions test/trento_web/controllers/tags_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ defmodule TrentoWeb.TagsControllerTest do
use TrentoWeb.ConnCase, async: true

import Trento.Factory

alias Faker.Color
alias Trento.Tag

describe "Tag Validation" do
test "should validate incoming tags", %{conn: conn} do
test "should decline tag with whitespace", %{conn: conn} do
conn =
post(conn, Routes.hosts_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => " "
Expand All @@ -18,13 +18,26 @@ defmodule TrentoWeb.TagsControllerTest do
}
} = json_response(conn, 400)
end

test "should decline tag with forbidden characters", %{conn: conn} do
conn =
post(conn, Routes.hosts_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => "This / is a \ wrong #tag"
})

assert %{
"errors" => %{
"value" => ["has invalid format"]
}
} = json_response(conn, 400)
end
end

describe "tagging sap systems and databases" do
test "should add a tag to a sap system", %{conn: conn} do
conn =
post(conn, Routes.sap_systems_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => Faker.Beer.style()
"value" => Color.En.name()
})

assert 201 == conn.status
Expand Down Expand Up @@ -65,7 +78,7 @@ defmodule TrentoWeb.TagsControllerTest do
test "should add a tag to a database", %{conn: conn} do
conn =
post(conn, Routes.databases_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => Faker.Beer.style()
"value" => Color.En.name()
})

assert 201 == conn.status
Expand All @@ -89,7 +102,7 @@ defmodule TrentoWeb.TagsControllerTest do
test "should add a tag to a cluster", %{conn: conn} do
conn =
post(conn, Routes.clusters_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => tag_value = Faker.Beer.style()
"value" => tag_value = Color.En.name()
})

assert json_response(conn, 201)["value"] == tag_value
Expand Down Expand Up @@ -130,7 +143,7 @@ defmodule TrentoWeb.TagsControllerTest do
test "should add a tag to a host", %{conn: conn} do
conn =
post(conn, Routes.hosts_tagging_path(conn, :add_tag, Faker.UUID.v4()), %{
"value" => Faker.Beer.style()
"value" => Color.En.name()
})

assert 201 == conn.status
Expand Down

0 comments on commit c11cf42

Please sign in to comment.