article_customer_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Import::OTRS::ArticleCustomer do
  4. def load_article_json(file)
  5. json_fixture("import/otrs/article/#{file}")
  6. end
  7. let(:instance_id) { 1337 }
  8. let(:existing_object) { instance_double(import_object) }
  9. let(:import_object) { User }
  10. let(:object_structure) { load_article_json('customer_phone') }
  11. let(:start_import_test) { described_class.new(object_structure) }
  12. it 'finds customers by email' do
  13. allow(import_object).to receive(:find_by).with(email: 'kunde2@kunde.de').and_return(existing_object)
  14. expect(import_object).not_to receive(:create)
  15. start_import_test
  16. end
  17. it 'finds customers by login' do
  18. allow(import_object).to receive(:find_by)
  19. allow(import_object).to receive(:find_by).with(login: 'kunde2@kunde.de').and_return(existing_object)
  20. expect(import_object).not_to receive(:create)
  21. start_import_test
  22. end
  23. it 'creates customers' do
  24. allow(import_object).to receive(:create).and_return(existing_object)
  25. expect(import_object).to receive(:find_by).at_least(:once)
  26. start_import_test
  27. end
  28. it 'creates customers with special encoding in name' do
  29. expect { described_class.new(load_article_json('customer_special_chars')) }.to change(User, :count).by(1)
  30. expect(User.last.login).to eq('user.hernandez@example.com')
  31. end
  32. it 'creates customers with special from email syntax' do
  33. expect { described_class.new(load_article_json('from_bracket_email_syntax')) }.to change(User, :count).by(1)
  34. expect(User.last.login).to eq('user@example.com')
  35. end
  36. it 'converts emails to downcase' do
  37. Setting.set('import_mode', true)
  38. expect { described_class.new(load_article_json('from_capital_case')) }.to change(User, :count).by(1)
  39. expect(User.last.email).to eq('user@example.com')
  40. expect(User.last.login).to eq('user@example.com')
  41. end
  42. describe '.find' do
  43. it 'returns nil if no email could be found' do
  44. expect(described_class.find({})).to be_nil
  45. end
  46. end
  47. describe '.local_email' do
  48. it 'returns nil if no email could be found' do
  49. expect(described_class.local_email(nil)).to be_nil
  50. end
  51. it 'returns the parameter if no email could be found' do
  52. not_an_email = 'thisisnotanemail'
  53. expect(described_class.local_email(not_an_email)).to eq(not_an_email)
  54. end
  55. end
  56. end