diff --git a/lib/pseudonymisation.rb b/lib/pseudonymisation.rb index 846e131b8..b4a02dbd5 100644 --- a/lib/pseudonymisation.rb +++ b/lib/pseudonymisation.rb @@ -4,7 +4,7 @@ class Pseudonymisation return if source.blank? source.transform_values do |value| - of_value(value) + of_value(value.to_s) end end @@ -33,7 +33,7 @@ class Pseudonymisation end def self.of_string(source) - return '*' if source.length == 1 + return '*' if source.to_s.length <= 1 return "#{source.first}*#{source.last}" if source.exclude?(' ') source.split.map do |sub_string| diff --git a/spec/lib/pseudonymisation_spec.rb b/spec/lib/pseudonymisation_spec.rb index 183ed360e..672e0e4b4 100644 --- a/spec/lib/pseudonymisation_spec.rb +++ b/spec/lib/pseudonymisation_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Pseudonymisation do let(:source) do { firstname: 'John', - lastname: 'Doe', + lastname: nil, email: 'john.doe@example.com', organization: 'Example Inc.', } @@ -16,7 +16,7 @@ RSpec.describe Pseudonymisation do let(:result) do { firstname: 'J*n', - lastname: 'D*e', + lastname: '*', email: 'j*e@e*e.com', organization: 'E*e I*.', } @@ -44,6 +44,14 @@ RSpec.describe Pseudonymisation do expect(described_class.of_value(source)).to eq('Z*d') end end + + context 'when nil is given' do + let(:source) { nil } + + it 'returns *' do + expect(described_class.of_value(source)).to eq('*') + end + end end describe '.of_email_address' do @@ -107,5 +115,13 @@ RSpec.describe Pseudonymisation do expect(described_class.of_string(source)).to eq('Z*d F*n') end end + + context 'when nil are given' do + let(:source) { nil } + + it 'returns *' do + expect(described_class.of_string(source)).to eq('*') + end + end end end