From f1f45c65fd17a1f6769c4135989725b0f29193e4 Mon Sep 17 00:00:00 2001 From: zoten Date: Wed, 19 Oct 2022 16:00:21 +0200 Subject: [PATCH] Task/redacting fallback (#16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [fix] add fallbacks for center redacter * [change] rename old 'mask' conceptù * [add] allow fallback value in base redacters * [change] update documentation * [add] tests for custom fallback fixed value * [change] total renaming of endemic typo --- README.md | 6 ++-- lib/redact_ex.ex | 16 +++++----- lib/redact_ex/algorithms/center.ex | 17 +++++++---- lib/redact_ex/algorithms/simple.ex | 11 ++++--- lib/redact_ex/configuration.ex | 42 ++++++++++++++------------ lib/redact_ex/configuration/context.ex | 11 ++++--- lib/redact_ex/redactable.ex | 16 +++++----- lib/redact_ex/redacter.ex | 41 +++++++++++++------------ test/base_redact_test.exs | 29 ++++++++++-------- test/center_redact_test.exs | 17 +++++++---- test/custom_redacter_test.exs | 6 ++-- test/env_redacter_test.exs | 10 +++--- test/overload_redacter_test.exs | 14 ++++----- test/redacter_test.exs | 4 +-- test/support/base_test_redacter.ex | 9 +++--- test/support/center_redacter.ex | 6 ++-- test/support/custom_redacter.ex | 6 ++-- test/support/derive/derive_structs.ex | 10 +++--- test/support/env_redacter.ex | 6 ++-- test/support/overload_test_redacter.ex | 6 ++-- 20 files changed, 152 insertions(+), 131 deletions(-) diff --git a/README.md b/README.md index 5994c32..b78b62c 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ to your understanding of the system, and at the same moment it does not expose ## Usage See [RedactEx](./lib/redact_ex.ex) for general information about this library and its intended use -See [RedactEx.Redacter](./lib/redact_ex/redacter.ex) for generating fast redacters for strings +See [RedactEx.Redactor](./lib/redact_ex/redactor.ex) for generating fast redactors for strings See [RedactEx.Redactable](./lib/redact_ex/redactable.ex) protocol for deriving redactable structs ## Contributing @@ -40,7 +40,7 @@ cp .tool-versions.recommended .tool-versions ## Open Points - * Other ways of masking (e.g. first X letters and last Y exposed) - * maybe-email masker (with fallback)? + * Other ways of redacting (e.g. first X letters and last Y exposed) + * maybe-email redactor (with fallback)? * define a default strict implementation for `redact` in the `Any` implementation? * some other default magic configuration in derive, e.g. a default module+function? diff --git a/lib/redact_ex.ex b/lib/redact_ex.ex index d24e23f..401a100 100644 --- a/lib/redact_ex.ex +++ b/lib/redact_ex.ex @@ -3,7 +3,7 @@ defmodule RedactEx do RedactEx provides protocols and derivatoin utilities to work with sensitive data that should be redacted in logs and external facing tools - * `RedactEx.Redacter` module gives you automagical generation of redacting + * `RedactEx.Redactor` module gives you automagical generation of redacting functions under a desired module namespace * `RedactEx.Redactable` is the protocol for which implementation and derivation should be created to ensure the best possible practices @@ -13,13 +13,13 @@ defmodule RedactEx do RedactEx usual protection consists in two or more steps: - ## Generate your redacter functions + ## Generate your redactor functions - You can generate functions to redact your specific data using `RedactEx.Redacter` module. + You can generate functions to redact your specific data using `RedactEx.Redactor` module. iex> defmodule MyApp.Redacting do ...> @moduledoc false - ...> use RedactEx.Redacter, redacters: [ + ...> use RedactEx.Redactor, redactors: [ ...> {"redact_three", length: 3, algorithm: :simple}, ...> {"redact", lengths: 1..3, algorithm: :simple} ...> ] @@ -42,17 +42,17 @@ defmodule RedactEx do You can use the `@derive` macro from the `RedactEx.Redactable` protocol, configured based on your needs and optionally using functions from a module generated with - `RedactEx.Redacter` helpers, or manually define an implementation of choice, e.g. + `RedactEx.Redactor` helpers, or manually define an implementation of choice, e.g. - iex> defmodule MyRedacterModule do + iex> defmodule MyRedactorModule do ...> def redact_function_one(_), do: "(redacted1)" ...> def redact_function_two(_), do: "(redacted2)" ...> end ...> defmodule MyApp.RedactStruct do ...> @derive {RedactEx.Redactable, ...> fields: [ - ...> myfield1: {MyRedacterModule, :redact_function_one}, - ...> myfield2: {MyRedacterModule, :redact_function_two}, + ...> myfield1: {MyRedactorModule, :redact_function_one}, + ...> myfield2: {MyRedactorModule, :redact_function_two}, ...> ]} ...> defstruct [:myfield1, :myfield2] ...> end diff --git a/lib/redact_ex/algorithms/center.ex b/lib/redact_ex/algorithms/center.ex index ea73b19..7b422eb 100644 --- a/lib/redact_ex/algorithms/center.ex +++ b/lib/redact_ex/algorithms/center.ex @@ -10,34 +10,39 @@ defmodule RedactEx.Algorithms.Center do """ @behaviour RedactEx.Algorithms.Algorithm + alias RedactEx.Configuration.Context + @impl RedactEx.Algorithms.Algorithm - def generate_ast(%{ + def generate_ast(%Context{ plaintext_length: nil, redacted_length: nil, keep: keep, name: name, - redacter: redacter + redactor: redactor, + fallback_value: fallback_value }) do quote do - def unquote(name)(value) do + def unquote(name)(value) when is_binary(value) do string_length = String.length(value) keep_size = string_length * unquote(keep) / 100 / 2 head_size = ceil(keep_size) tail_size = floor(keep_size) center_size = max(string_length - head_size - tail_size, 0) - center_content = for _ <- 1..center_size, do: unquote(redacter), into: "" + center_content = for _ <- 1..center_size, do: unquote(redactor), into: "" if center_size <= 0 do - unquote(redacter) + unquote(redactor) else String.slice(value, 0..(head_size - 1)) <> center_content <> String.slice(value, (string_length - tail_size)..string_length) end end + + def unquote(name)(_value), do: unquote(fallback_value) end end - def generate_ast(%{ + def generate_ast(%Context{ plaintext_length: plaintext_length, redacted_length: redacted_length, name: name, diff --git a/lib/redact_ex/algorithms/simple.ex b/lib/redact_ex/algorithms/simple.ex index 584238f..fe93b8c 100644 --- a/lib/redact_ex/algorithms/simple.ex +++ b/lib/redact_ex/algorithms/simple.ex @@ -17,8 +17,9 @@ defmodule RedactEx.Algorithms.Simple do length: :*, keep: keep, name: name, - redacter: redacter, - redacted_size: redacted_size + redactor: redactor, + redacted_size: redacted_size, + fallback_value: fallback_value }) do quote do def unquote(name)(value) when is_binary(value) do @@ -31,7 +32,7 @@ defmodule RedactEx.Algorithms.Simple do unquote(redacted_size) ) - redacted = Context.get_redacter_string(redacted_length, unquote(redacter)) + redacted = Context.get_redactor_string(redacted_length, unquote(redactor)) case max(0, plaintext_length - 1) do 0 -> @@ -42,11 +43,11 @@ defmodule RedactEx.Algorithms.Simple do end end - def unquote(name)(_value), do: "(fully redacted as not a string)" + def unquote(name)(_value), do: unquote(fallback_value) end end - def generate_ast(%{ + def generate_ast(%Context{ plaintext_length: plaintext_length, redacted_length: redacted_length, name: name, diff --git a/lib/redact_ex/configuration.ex b/lib/redact_ex/configuration.ex index fbb4209..de39117 100644 --- a/lib/redact_ex/configuration.ex +++ b/lib/redact_ex/configuration.ex @@ -5,18 +5,20 @@ defmodule RedactEx.Configuration do @default_redacting_keep 25 @default_redacted_size :auto - @default_redacter "*" + @default_redactor "*" @default_redacting_algorithm RedactEx.Algorithms.algorithm_simple() @default_lengths [:*] @default_except [] + @default_fallback_value "(redacted)" @default_configuration [ keep: @default_redacting_keep, redacted_size: @default_redacted_size, - redacter: @default_redacter, + redactor: @default_redactor, algorithm: @default_redacting_algorithm, lengths: @default_lengths, - except: @default_except + except: @default_except, + fallback_value: @default_fallback_value ] alias RedactEx.Algorithms @@ -32,43 +34,44 @@ defmodule RedactEx.Configuration do def parse(configuration, current_env, macro_env) when is_list(configuration), do: configuration - |> Keyword.fetch!(:redacters) - |> Enum.flat_map(&parse_redacter(&1, macro_env)) + |> Keyword.fetch!(:redactors) + |> Enum.flat_map(&parse_redactor(&1, macro_env)) |> reject_by_env(current_env) |> Enum.group_by(fn %{name: name} = _config -> name end) - |> Enum.map(&add_fallback_redacter!(&1, macro_env)) + |> Enum.map(&add_fallback_redactor!(&1, macro_env)) |> Enum.into(%{}) - defp reject_by_env(redacters, current_env), + defp reject_by_env(redactors, current_env), do: - Enum.reject(redacters, fn %Context{except: refute_envs} -> + Enum.reject(redactors, fn %Context{except: refute_envs} -> current_env in refute_envs end) - defp parse_redacter({aliases, config}, macro_env) when is_list(aliases), + defp parse_redactor({aliases, config}, macro_env) when is_list(aliases), do: Enum.flat_map(aliases, &map_lengths_and_parse(&1, config, macro_env)) # Single name: all defaults apply # Name will be normalized later - defp parse_redacter(name, macro_env) when is_atom(name) or is_binary(name), + defp parse_redactor(name, macro_env) when is_atom(name) or is_binary(name), do: map_lengths_and_parse(name, @default_configuration, macro_env) - defp parse_redacter({name, config}, macro_env), + defp parse_redactor({name, config}, macro_env), do: map_lengths_and_parse(name, config, macro_env) defp map_lengths_and_parse(name, config, macro_env) do config |> get_lengths_from_config(name) - |> Enum.map(&do_parse_redacter(&1, name, config, macro_env)) + |> Enum.map(&do_parse_redactor(&1, name, config, macro_env)) end - defp do_parse_redacter(string_length, given_name, given_config, macro_env) + defp do_parse_redactor(string_length, given_name, given_config, macro_env) when is_integer(string_length) or string_length == :* do config = Keyword.merge(@default_configuration, given_config) except = Keyword.fetch!(config, :except) keep = Keyword.fetch!(config, :keep) redacted_size = Keyword.fetch!(config, :redacted_size) + fallback_value = Keyword.fetch!(config, :fallback_value) needs_fallback_function = needs_fallback_function?(string_length) algorithm = @@ -76,19 +79,19 @@ defmodule RedactEx.Configuration do name = alias_name(given_name) - redacter = Keyword.fetch!(config, :redacter) + redactor = Keyword.fetch!(config, :redactor) {plaintext_length, redacted_length} = Context.get_plaintext_length_redacted_length(string_length, keep, redacted_size) - redacted = Context.get_redacter_string(redacted_length, redacter) + redacted = Context.get_redactor_string(redacted_length, redactor) extra = algorithm.parse_extra_configuration!(config) %Context{ redacted_size: redacted_size, length: string_length, - redacter: redacter, + redactor: redactor, keep: keep, plaintext_length: plaintext_length, redacted_length: redacted_length, @@ -98,7 +101,8 @@ defmodule RedactEx.Configuration do algorithm: algorithm, needs_fallback_function: needs_fallback_function, extra: extra, - except: except + except: except, + fallback_value: fallback_value } end @@ -143,12 +147,12 @@ defmodule RedactEx.Configuration do end end - defp add_fallback_redacter!({key, contexts}, macro_env) do + defp add_fallback_redactor!({key, contexts}, macro_env) do case Enum.split_with(contexts, fn %Context{length: len} -> len != :* end) do {non_fallback_contexts, []} -> {key, Enum.concat(non_fallback_contexts, [ - do_parse_redacter(:*, key, @default_configuration, macro_env) + do_parse_redactor(:*, key, @default_configuration, macro_env) ])} {non_fallback_contexts, [fallback_context]} -> diff --git a/lib/redact_ex/configuration/context.ex b/lib/redact_ex/configuration/context.ex index b9008de..1ec871f 100644 --- a/lib/redact_ex/configuration/context.ex +++ b/lib/redact_ex/configuration/context.ex @@ -6,7 +6,7 @@ defmodule RedactEx.Configuration.Context do alias __MODULE__ defstruct length: :*, - redacter: nil, + redactor: nil, keep: nil, plaintext_length: nil, redacted_size: nil, @@ -16,6 +16,7 @@ defmodule RedactEx.Configuration.Context do string_length: nil, algorithm: nil, needs_fallback_function: nil, + fallback_value: nil, extra: nil, except: [] @@ -42,12 +43,12 @@ defmodule RedactEx.Configuration.Context do @doc """ Gets the actual redacted part of the string given configuration """ - @spec get_redacter_string(size :: nil | integer(), redacter :: char() | any()) :: + @spec get_redactor_string(size :: nil | integer(), redactor :: char() | any()) :: nil | String.t() - def get_redacter_string(nil, _redacter), do: nil + def get_redactor_string(nil, _redactor), do: nil - def get_redacter_string(size, redacter) do - 1..size |> Enum.map(fn _ -> redacter end) |> List.to_string() + def get_redactor_string(size, redactor) do + 1..size |> Enum.map(fn _ -> redactor end) |> List.to_string() end # Get the length of the redacted part of data diff --git a/lib/redact_ex/redactable.ex b/lib/redact_ex/redactable.ex index 37a4e57..8b99968 100644 --- a/lib/redact_ex/redactable.ex +++ b/lib/redact_ex/redactable.ex @@ -2,7 +2,7 @@ defprotocol RedactEx.Redactable do @moduledoc """ # Redactable - Protocol for defining a redact-able item, e.g. an item whose internal elements could be masked + Protocol for defining a redact-able item, e.g. an item whose internal elements could be redacted It shall return a redact-ed item of the same type as the input item You can derive Redactable protocol for a struct by using @@ -98,7 +98,7 @@ defimpl RedactEx.Redactable, for: Any do fields = fields_to_redact(struct, opts) case fields do - {redacter_module, function} -> redact_all_ast(module, redacter_module, function) + {redactor_module, function} -> redact_all_ast(module, redactor_module, function) fields when is_list(fields) -> redact_ast(module, fields) end end @@ -110,11 +110,11 @@ defimpl RedactEx.Redactable, for: Any do description: "RedactEx.Redactable protocol must always be explicitly implemented" end - defp redact_all_ast(module, redacter_module, function) do + defp redact_all_ast(module, redactor_module, function) do quote do defimpl RedactEx.Redactable, for: unquote(module) do def redact(%_{} = value) do - :erlang.apply(unquote(redacter_module), unquote(function), [value]) + :erlang.apply(unquote(redactor_module), unquote(function), [value]) end end end @@ -162,10 +162,10 @@ defimpl RedactEx.Redactable, for: Any do {_, action} when action in [:redact, :drop] -> true - {_, {redacter_module, redacter_function}} -> - Code.ensure_loaded!(redacter_module) - # true = Module.defines?(redacter_module, {redacter_function, 1}) - true = Kernel.function_exported?(redacter_module, redacter_function, 1) + {_, {redactor_module, redactor_function}} -> + Code.ensure_loaded!(redactor_module) + # true = Module.defines?(redactor_module, {redactor_function, 1}) + true = Kernel.function_exported?(redactor_module, redactor_function, 1) end) end diff --git a/lib/redact_ex/redacter.ex b/lib/redact_ex/redacter.ex index 1d1bcf0..ef4f3b2 100644 --- a/lib/redact_ex/redacter.ex +++ b/lib/redact_ex/redacter.ex @@ -1,7 +1,7 @@ -defmodule RedactEx.Redacter do +defmodule RedactEx.Redactor do @default_redacting_keep 25 @default_redacted_size :auto - @default_redacter "*" + @default_redactor "*" @default_redacting_algorithm RedactEx.Algorithms.algorithm_simple() @@ -38,23 +38,23 @@ defmodule RedactEx.Redacter do | Algorithm | Description | | --------- | ----------- | - | `:simple` | keeps the first `keep`% of the string and sets the rest to `redacter`, according to `redacted_size` | - | `:center` | splits `keep`% of the string between head and tail, and sets the middle to `redacter`, according to `redacted_size` | + | `:simple` | keeps the first `keep`% of the string and sets the rest to `redactor`, according to `redacted_size` | + | `:center` | splits `keep`% of the string between head and tail, and sets the middle to `redactor`, according to `redacted_size` | ## Configuration Options: - `redacters` a list of redacting rules. Each element can be: - - a tuple in the form {redacter_function_name, configuration} - - a tuple in the form {[redacter_function_names], configuration} - - a single redacter_function_name - `redacter_function_name` can be an atom or string value in all cases + `redactors` a list of redacting rules. Each element can be: + - a tuple in the form {redactor_function_name, configuration} + - a tuple in the form {[redactor_function_names], configuration} + - a single redactor_function_name + `redactor_function_name` can be an atom or string value in all cases ### Common configuration - Configuration of a redacter is a keyword list containing + Configuration of a redactor is a keyword list containing - * `redacter` string character used to redact the hidden part of the. Defaults to `#{@default_redacter}` + * `redactor` string character used to redact the hidden part of the. Defaults to `#{@default_redactor}` * `redacted_size` integer | :auto length of the resulting string that will be set as redacted. Defaults to `#{@default_redacted_size}`, which will set it to `expected_string_length - keep` * `algorithm` atom | module algorithm used to redact the string. Defaults to `#{@default_redacting_algorithm}` @@ -66,12 +66,13 @@ defmodule RedactEx.Redacter do * `length` integer or `:*` length of the string to be considered. `:*` stands for the fallback function configuration. Is overridden by key `:lengths` if present * `lengths` [integer or `:*`] | min..max lengths of the strings to be considered. `:*` stands for the fallback function configuration. A function for each specific length will be generated * `except` list(atom()) list of environments for which this configuration will have effect + * `fallback_value` string fixed value for redacting non-string values ### Example iex> defmodule MyApp.Redacting do ...> @moduledoc false - ...> use RedactEx.Redacter, redacters: [ + ...> use RedactEx.Redactor, redactors: [ ...> {"redact_three", length: 3, algorithm: :simple}, ...> {"redact", lengths: 1..3, algorithm: :simple} ...> ] @@ -91,16 +92,16 @@ defmodule RedactEx.Redacter do @spec __using__(opts :: list()) :: any() defmacro __using__(opts) do - redacters = Configuration.parse(opts, Mix.env(), __CALLER__) + redactors = Configuration.parse(opts, Mix.env(), __CALLER__) - for {alias_name, alias_redacters} <- redacters do - lengths = Enum.map(alias_redacters, fn %{string_length: string_length} -> string_length end) + for {alias_name, alias_redactors} <- redactors do + lengths = Enum.map(alias_redactors, fn %{string_length: string_length} -> string_length end) doc_ast = quote do @doc """ - Redacter `#{unquote(alias_name)}` for strings of lengths #{unquote(inspect(lengths))} - This is a self-generated function from `#{RedactEx.Redacter}` + Redactor `#{unquote(alias_name)}` for strings of lengths #{unquote(inspect(lengths))} + This is a self-generated function from `#{RedactEx.Redactor}` """ end @@ -111,9 +112,9 @@ defmodule RedactEx.Redacter do end fun_ast = - for %Context{algorithm: algorithm} = redacter_configuration <- - alias_redacters do - Algorithms.generate_ast(algorithm, redacter_configuration) + for %Context{algorithm: algorithm} = redactor_configuration <- + alias_redactors do + Algorithms.generate_ast(algorithm, redactor_configuration) end List.flatten([doc_ast, function_spec_ast, fun_ast]) diff --git a/test/base_redact_test.exs b/test/base_redact_test.exs index 7def7af..995210b 100644 --- a/test/base_redact_test.exs +++ b/test/base_redact_test.exs @@ -1,31 +1,34 @@ defmodule RedactEx.BaseRedactTest do use ExUnit.Case, aync: true - alias Support.RedactEx.BaseTestRedacter + alias Support.RedactEx.BaseTestRedactor - test "1-length, 5-length and 8-length redacters are exported" do + test "1-length, 5-length and 8-length redactors are exported" do # length 1, 25% =~ 0 - assert BaseTestRedacter.base("1") == "*" - assert BaseTestRedacter.string_base("1") == "*" + assert BaseTestRedactor.base("1") == "*" + assert BaseTestRedactor.string_base("1") == "*" # length 5, 25% =~ 1 - assert BaseTestRedacter.base("12345") == "1****" - assert BaseTestRedacter.string_base("12345") == "1****" + assert BaseTestRedactor.base("12345") == "1****" + assert BaseTestRedactor.string_base("12345") == "1****" # length 8, 25% =~ 2 - assert BaseTestRedacter.base("12345678") == "12******" - assert BaseTestRedacter.string_base("12345678") == "12******" + assert BaseTestRedactor.base("12345678") == "12******" + assert BaseTestRedactor.string_base("12345678") == "12******" end test "fallback values work as expected" do # length 2, 25% =~ 0 - assert BaseTestRedacter.base("12") == "**" - assert BaseTestRedacter.string_base("12") == "**" + assert BaseTestRedactor.base("12") == "**" + assert BaseTestRedactor.string_base("12") == "**" # length 10, 25% =~ 2 - assert BaseTestRedacter.base("1234567890") == "12********" - assert BaseTestRedacter.string_base("1234567890") == "12********" + assert BaseTestRedactor.base("1234567890") == "12********" + assert BaseTestRedactor.string_base("1234567890") == "12********" - assert BaseTestRedacter.base(nil) == "(fully redacted as not a string)" + assert BaseTestRedactor.base(nil) == "(redacted)" + assert BaseTestRedactor.string_base(nil) == "(redacted)" + + assert BaseTestRedactor.fallback(nil) == "(fallback redact)" end end diff --git a/test/center_redact_test.exs b/test/center_redact_test.exs index 94f4a95..9328cad 100644 --- a/test/center_redact_test.exs +++ b/test/center_redact_test.exs @@ -1,16 +1,21 @@ defmodule RedactEx.CenterRedactTest do use ExUnit.Case, aync: true - alias Support.RedactEx.CenterTestRedacter + alias Support.RedactEx.CenterTestRedactor - test "center masker works as expected for fixed length strings" do - assert CenterTestRedacter.center_ten("1234567890") == "1********0" + test "center redactor works as expected for fixed length strings" do + assert CenterTestRedactor.center_ten("1234567890") == "1********0" end - test "center masker works as expected for unknown length strings" do - assert CenterTestRedacter.center_unknown("1") == "*" + test "center redactor works as expected for unknown length strings" do + assert CenterTestRedactor.center_unknown("1") == "*" - assert CenterTestRedacter.center_unknown("12345678901234567890") == + assert CenterTestRedactor.center_unknown("12345678901234567890") == "123***************90" end + + test "center redactor works as expected for fallback values" do + assert CenterTestRedactor.center_unknown(nil) == "(redacted)" + assert CenterTestRedactor.center_ten(nil) == "(redacted)" + end end diff --git a/test/custom_redacter_test.exs b/test/custom_redacter_test.exs index be1fd8d..6bde072 100644 --- a/test/custom_redacter_test.exs +++ b/test/custom_redacter_test.exs @@ -1,10 +1,10 @@ defmodule RedactEx.CustomRedactTest do use ExUnit.Case, aync: true - alias Support.RedactEx.CustomRedacter + alias Support.RedactEx.CustomRedactor - test "Redacter in an external module works as expected" do + test "Redactor in an external module works as expected" do value = "some_value" - assert CustomRedacter.redact(value) == "(custom redact) #{value}" + assert CustomRedactor.redact(value) == "(custom redact) #{value}" end end diff --git a/test/env_redacter_test.exs b/test/env_redacter_test.exs index adfafb8..c2d5740 100644 --- a/test/env_redacter_test.exs +++ b/test/env_redacter_test.exs @@ -1,11 +1,11 @@ -defmodule RedactEx.EnvRedacterTest do +defmodule RedactEx.EnvRedactorTest do use ExUnit.Case, aync: true - alias Support.RedactEx.EnvRedacter + alias Support.RedactEx.EnvRedactor test "functions are exported or not exported as expected" do - Code.ensure_compiled!(Support.RedactEx.EnvRedacter) - assert function_exported?(EnvRedacter, :in_test, 1) - refute function_exported?(EnvRedacter, :not_in_test, 1) + Code.ensure_compiled!(Support.RedactEx.EnvRedactor) + assert function_exported?(EnvRedactor, :in_test, 1) + refute function_exported?(EnvRedactor, :not_in_test, 1) end end diff --git a/test/overload_redacter_test.exs b/test/overload_redacter_test.exs index f260692..45ffaed 100644 --- a/test/overload_redacter_test.exs +++ b/test/overload_redacter_test.exs @@ -1,16 +1,16 @@ defmodule RedactEx.OverloadRedactTest do use ExUnit.Case, aync: true - alias Support.RedactEx.OverloadTestRedacter + alias Support.RedactEx.OverloadTestRedactor - test "1-length, 5-length and 8-length redacters are exported" do - assert OverloadTestRedacter.overload("1") == "*" - assert OverloadTestRedacter.overload("12") == "**" - assert OverloadTestRedacter.overload("123") == "***" - assert OverloadTestRedacter.overload("1234") == "1***" + test "1-length, 5-length and 8-length redactors are exported" do + assert OverloadTestRedactor.overload("1") == "*" + assert OverloadTestRedactor.overload("12") == "**" + assert OverloadTestRedactor.overload("123") == "***" + assert OverloadTestRedactor.overload("1234") == "1***" end test "fallback reload work as expected" do - assert OverloadTestRedacter.overload("1234567890") == "12********" + assert OverloadTestRedactor.overload("1234567890") == "12********" end end diff --git a/test/redacter_test.exs b/test/redacter_test.exs index 9e6b74b..891b37e 100644 --- a/test/redacter_test.exs +++ b/test/redacter_test.exs @@ -1,4 +1,4 @@ -defmodule RedactEx.RedacterTest do +defmodule RedactEx.RedactorTest do use ExUnit.Case, async: true - doctest RedactEx.Redacter + doctest RedactEx.Redactor end diff --git a/test/support/base_test_redacter.ex b/test/support/base_test_redacter.ex index fee5d2d..87e87cb 100644 --- a/test/support/base_test_redacter.ex +++ b/test/support/base_test_redacter.ex @@ -1,8 +1,9 @@ -defmodule Support.RedactEx.BaseTestRedacter do +defmodule Support.RedactEx.BaseTestRedactor do @moduledoc false - use RedactEx.Redacter, - redacters: [ - {[:base, "string_base"], lengths: [1, 5, 8]} + use RedactEx.Redactor, + redactors: [ + {[:base, "string_base"], lengths: [1, 5, 8]}, + {:fallback, length: :*, fallback_value: "(fallback redact)"} ] end diff --git a/test/support/center_redacter.ex b/test/support/center_redacter.ex index 41e3eb6..2436f03 100644 --- a/test/support/center_redacter.ex +++ b/test/support/center_redacter.ex @@ -1,8 +1,8 @@ -defmodule Support.RedactEx.CenterTestRedacter do +defmodule Support.RedactEx.CenterTestRedactor do @moduledoc false - use RedactEx.Redacter, - redacters: [ + use RedactEx.Redactor, + redactors: [ {:center_ten, length: 10, algorithm: :center}, {:center_unknown, length: :*, algorithm: :center} ] diff --git a/test/support/custom_redacter.ex b/test/support/custom_redacter.ex index fa0736a..724677c 100644 --- a/test/support/custom_redacter.ex +++ b/test/support/custom_redacter.ex @@ -1,10 +1,10 @@ -defmodule Support.RedactEx.CustomRedacter do +defmodule Support.RedactEx.CustomRedactor do @moduledoc false alias Support.RedactEx.Algorithms.CustomAlgorithm - use RedactEx.Redacter, - redacters: [ + use RedactEx.Redactor, + redactors: [ {:redact, length: :*, algorithm: CustomAlgorithm} ] end diff --git a/test/support/derive/derive_structs.ex b/test/support/derive/derive_structs.ex index 184a783..26da076 100644 --- a/test/support/derive/derive_structs.ex +++ b/test/support/derive/derive_structs.ex @@ -1,4 +1,4 @@ -defmodule Support.RedactEx.Derive.Redacter do +defmodule Support.RedactEx.Derive.Redactor do @moduledoc false def redact0(value) when is_binary(value), do: "#{value} redacted0" @@ -9,7 +9,7 @@ defmodule Support.RedactEx.Derive.SubDeriveMe do @moduledoc false @derive {RedactEx.Redactable, fields: [ - field: {Support.RedactEx.Derive.Redacter, :redact0} + field: {Support.RedactEx.Derive.Redactor, :redact0} ]} defstruct [:field] end @@ -30,8 +30,8 @@ defmodule Support.RedactEx.Derive.DeriveMe do @moduledoc false @derive {RedactEx.Redactable, fields: [ - field0: {Support.RedactEx.Derive.Redacter, :redact0}, - field1: {Support.RedactEx.Derive.Redacter, :redact1}, + field0: {Support.RedactEx.Derive.Redactor, :redact0}, + field1: {Support.RedactEx.Derive.Redactor, :redact1}, substruct: :redact ]} defstruct [:field0, :field1, :substruct, :keepme, :keepme_number] @@ -41,7 +41,7 @@ defmodule Support.RedactEx.Derive.DropMe do @moduledoc false @derive {RedactEx.Redactable, fields: [ - field0: {Support.RedactEx.Derive.Redacter, :redact0}, + field0: {Support.RedactEx.Derive.Redactor, :redact0}, field_drop: :drop ]} defstruct [:field0, :field_drop] diff --git a/test/support/env_redacter.ex b/test/support/env_redacter.ex index e38f553..710fd8c 100644 --- a/test/support/env_redacter.ex +++ b/test/support/env_redacter.ex @@ -1,8 +1,8 @@ -defmodule Support.RedactEx.EnvRedacter do +defmodule Support.RedactEx.EnvRedactor do @moduledoc false - use RedactEx.Redacter, - redacters: [ + use RedactEx.Redactor, + redactors: [ {"in_test", lengths: [1, 5, 8]}, {"not_in_test", lengths: [1, 5, 8], except: [:test]} ] diff --git a/test/support/overload_test_redacter.ex b/test/support/overload_test_redacter.ex index 3b14e61..b521a7a 100644 --- a/test/support/overload_test_redacter.ex +++ b/test/support/overload_test_redacter.ex @@ -1,8 +1,8 @@ -defmodule Support.RedactEx.OverloadTestRedacter do +defmodule Support.RedactEx.OverloadTestRedactor do @moduledoc false - use RedactEx.Redacter, - redacters: [ + use RedactEx.Redactor, + redactors: [ {:overload, length: 1}, {:overload, length: 2}, {:overload, lengths: 3..4}