Skip to content

Commit

Permalink
Validate cleaner params
Browse files Browse the repository at this point in the history
  • Loading branch information
borama committed Jun 6, 2024
1 parent 8b5a853 commit 13c8701
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/dump_cleaner/cleanup/cleaning_steps/randomize_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module DumpCleaner
module Cleanup
module CleaningSteps
class RandomizeEmail < Base
def run(domains_to_keep_key: "domains_to_keep", words_key: "words")
def run(domains_to_keep_data_key: "domains_to_keep", words_data_key: "words")
validate_params(domains_to_keep_data_key:, words_data_key:)

mailbox, domain = current_value.split("@", 2)

if !mailbox || !domain || mailbox.empty? || domain.empty? || !domain.include?(".")
Expand All @@ -13,9 +15,9 @@ def run(domains_to_keep_key: "domains_to_keep", words_key: "words")
return step_context
end

new_mailbox = new_mailbox(mailbox, words: cleanup_data[words_key])
new_domain = new_domain(domain, domains: cleanup_data[domains_to_keep_key],
words: cleanup_data[words_key])
new_mailbox = new_mailbox(mailbox, words: cleanup_data[words_data_key])
new_domain = new_domain(domain, domains: cleanup_data[domains_to_keep_data_key],
words: cleanup_data[words_data_key])

step_context.current_value = "#{new_mailbox}@#{new_domain}"
step_context
Expand Down Expand Up @@ -51,6 +53,14 @@ def random_word_instead_of(word)
GenerateRandomString.new(StepContext.new_from(step_context, current_value: word))
.run(character_set: :lowercase).current_value
end

def validate_params(domains_to_keep_data_key:, words_data_key:)
unless cleanup_data.respond_to?(:key) &&
cleanup_data.key?(domains_to_keep_data_key) && cleanup_data.key?(words_data_key)
raise_params_error("The cleanup_data does not contain the dictionary keys
\"#{domains_to_keep_data_key}\" and \"#{words_data_key})\"".gsub(/\s+/, " "))
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,18 @@ def cleaner(step_context)
expect(cleaner(step_context(orig_value: "[email protected]")).run.current_value)
.to eq("[email protected]")
end

it "allows specifying custom dictionary data keys" do
step_context = step_context(orig_value: "[email protected]",
cleanup_data: { "domains" => %w[gmail.com],
"dictionary" => { "7-7" => %w[willful foobars] } })
expect(cleaner(step_context).run(domains_to_keep_data_key: "domains", words_data_key: "dictionary").current_value)
.to eq("[email protected]")
end

it "raises error if custom dictionary key not found in data" do
step_context = step_context(orig_value: "[email protected]", cleanup_data: {})
expect { cleaner(step_context).run }.to raise_error(ArgumentError, /does not contain the dictionary keys/)
end
end
end

0 comments on commit 13c8701

Please sign in to comment.