pseudonymisation_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. RSpec.describe Pseudonymisation do
  3. describe '.of_hash' do
  4. let(:source) do
  5. {
  6. firstname: 'John',
  7. lastname: nil,
  8. email: 'john.doe@example.com',
  9. organization: 'Example Inc.',
  10. }
  11. end
  12. let(:result) do
  13. {
  14. firstname: 'J*n',
  15. lastname: '*',
  16. email: 'j*e@e*e.com',
  17. organization: 'E*e I*.',
  18. }
  19. end
  20. it 'creates pseudonymous hash' do
  21. expect(described_class.of_hash(source)).to eq(result)
  22. end
  23. end
  24. describe '.of_value' do
  25. context 'when email address is given' do
  26. let(:source) { 'test@example.com' }
  27. it 'creates pseudonymous email_address' do
  28. expect(described_class.of_value(source)).to eq('t*t@e*e.com')
  29. end
  30. end
  31. context 'when string is given' do
  32. let(:source) { 'Zammad' }
  33. it 'creates pseudonymous string' do
  34. expect(described_class.of_value(source)).to eq('Z*d')
  35. end
  36. end
  37. context 'when nil is given' do
  38. let(:source) { nil }
  39. it 'returns *' do
  40. expect(described_class.of_value(source)).to eq('*')
  41. end
  42. end
  43. end
  44. describe '.of_email_address' do
  45. let(:source) { 'test@example.com' }
  46. it 'creates pseudonymous email_address' do
  47. expect(described_class.of_email_address(source)).to eq('t*t@e*e.com')
  48. end
  49. context 'when address is invalid' do
  50. it 'raises ArgumentError for parsing errors' do
  51. expect { described_class.of_email_address('i_m_no_address@') }.to raise_exception(ArgumentError)
  52. end
  53. it 'raises ArgumentError for string argument' do
  54. expect { described_class.of_email_address('i_m_no_address') }.to raise_exception(ArgumentError)
  55. end
  56. end
  57. end
  58. describe '.of_domain' do
  59. let(:source) { 'zammad.com' }
  60. it 'creates pseudonymous string with TLD' do
  61. expect(described_class.of_domain(source)).to eq('z*d.com')
  62. end
  63. context 'when no TLD is present' do
  64. let(:source) { 'localhost' }
  65. it 'creates pseudonymous string' do
  66. expect(described_class.of_domain(source)).to eq('l*t')
  67. end
  68. end
  69. end
  70. describe '.of_string' do
  71. let(:source) { 'Zammad' }
  72. it 'creates pseudonymous string' do
  73. expect(described_class.of_string(source)).to eq('Z*d')
  74. end
  75. context 'when only one char long' do
  76. let(:source) { 'a' }
  77. it 'returns *' do
  78. expect(described_class.of_string(source)).to eq('*')
  79. end
  80. end
  81. context 'when multiple sub-strings are given' do
  82. let(:source) { 'Zammad Foundation' }
  83. it 'create pseudonymous string for each' do
  84. expect(described_class.of_string(source)).to eq('Z*d F*n')
  85. end
  86. end
  87. context 'when nil are given' do
  88. let(:source) { nil }
  89. it 'returns *' do
  90. expect(described_class.of_string(source)).to eq('*')
  91. end
  92. end
  93. end
  94. end