imap_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::Imap, required_envs: %w[IMAP_ASCII_8BIT_HOST IMAP_ASCII_8BIT_USER IMAP_ASCII_8BIT_PASSWORD] do
  4. # https://github.com/zammad/zammad/issues/2964
  5. context 'when connecting with a ASCII 8-Bit password' do
  6. it 'succeeds' do
  7. params = {
  8. host: ENV['IMAP_ASCII_8BIT_HOST'],
  9. user: ENV['IMAP_ASCII_8BIT_USER'],
  10. password: ENV['IMAP_ASCII_8BIT_PASSWORD'],
  11. }
  12. result = described_class.new.fetch(params, nil, 'check')
  13. expect(result[:result]).to eq 'ok'
  14. end
  15. end
  16. describe '.parse_rfc822_headers' do
  17. it 'parses simple header' do
  18. expect(described_class.parse_rfc822_headers('Key: Value')).to have_key('Key').and(have_value('Value'))
  19. end
  20. it 'parses header with no white space' do
  21. expect(described_class.parse_rfc822_headers('Key:Value')).to have_key('Key').and(have_value('Value'))
  22. end
  23. it 'parses multiline header' do
  24. expect(described_class.parse_rfc822_headers("Key: Value\r\n2nd-key: 2nd-value"))
  25. .to have_key('Key').and(have_value('Value')).and(have_key('2nd-key')).and(have_value('2nd-value'))
  26. end
  27. it 'parses value with semicolons' do
  28. expect(described_class.parse_rfc822_headers('Key: Val:ue')).to have_key('Key').and(have_value('Val:ue'))
  29. end
  30. it 'parses key-only lines' do
  31. expect(described_class.parse_rfc822_headers('Key')).to have_key('Key')
  32. end
  33. it 'handles empty line' do
  34. expect { described_class.parse_rfc822_headers("Key: Value\r\n") }.not_to raise_error
  35. end
  36. it 'handles tabbed value' do
  37. expect(described_class.parse_rfc822_headers("Key: \r\n\tValue")).to have_key('Key').and(have_value('Value'))
  38. end
  39. end
  40. describe '.extract_rfc822_headers' do
  41. it 'extracts header' do
  42. object = Net::IMAP::FetchData.new :id, { 'RFC822.HEADER' => 'Key: Value' }
  43. expect(described_class.extract_rfc822_headers(object)).to have_key('Key').and(have_value('Value'))
  44. end
  45. it 'returns nil when header attribute is missing' do
  46. object = Net::IMAP::FetchData.new :id, { 'Another' => 'Key: Value' }
  47. expect(described_class.extract_rfc822_headers(object)).to be_nil
  48. end
  49. it 'does not raise error when given nil' do
  50. expect { described_class.extract_rfc822_headers(nil) }.not_to raise_error
  51. end
  52. end
  53. end