pseudonymisation_spec.rb 2.9 KB

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