state_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Import::OTRS::State do
  4. def creates_with(zammad_structure)
  5. allow(import_object).to receive(:find_by).and_return(nil)
  6. allow(import_object).to receive(:new).with(zammad_structure).and_call_original
  7. expect_any_instance_of(import_object).to receive(:save)
  8. expect_any_instance_of(described_class).to receive(:reset_primary_key_sequence)
  9. start_import_test
  10. end
  11. def updates_with(zammad_structure)
  12. allow(import_object).to receive(:find_by).and_return(existing_object)
  13. expect(existing_object).to receive(:update!).with(zammad_structure)
  14. expect(import_object).not_to receive(:new)
  15. start_import_test
  16. end
  17. def load_state_json(file)
  18. json_fixture("import/otrs/state/#{file}")
  19. end
  20. let(:import_object) { Ticket::State }
  21. let(:existing_object) { instance_double(import_object) }
  22. let(:start_import_test) { described_class.new(object_structure) }
  23. context 'closed' do
  24. let(:object_structure) { load_state_json('default') }
  25. let(:zammad_structure) do
  26. {
  27. created_by_id: 1,
  28. updated_by_id: 1,
  29. active: '1',
  30. state_type_id: 5,
  31. updated_at: '2014-04-28 10:53:18',
  32. created_at: '2014-04-28 10:53:18',
  33. name: 'closed successful',
  34. id: '2',
  35. note: 'Ticket is closed successful.',
  36. ignore_escalation: true
  37. }
  38. end
  39. it 'creates' do
  40. creates_with(zammad_structure)
  41. end
  42. it 'updates' do
  43. updates_with(zammad_structure)
  44. end
  45. end
  46. context 'open' do
  47. let(:object_structure) { load_state_json('open') }
  48. let(:zammad_structure) do
  49. {
  50. created_by_id: 1,
  51. updated_by_id: 1,
  52. active: '1',
  53. state_type_id: 2,
  54. updated_at: '2014-04-28 10:53:18',
  55. created_at: '2014-04-28 10:53:18',
  56. name: 'open',
  57. id: '4',
  58. note: 'Open tickets.',
  59. ignore_escalation: false
  60. }
  61. end
  62. it 'creates' do
  63. creates_with(zammad_structure)
  64. end
  65. it 'updates' do
  66. updates_with(zammad_structure)
  67. end
  68. end
  69. context 'with removed state' do
  70. let(:object_structure) { load_state_json('removed') }
  71. it 'skips', :aggregate_failures do
  72. start_import_test
  73. expect(import_object).not_to receive(:new)
  74. expect(Ticket::State.find_by(name: 'removed')).to be_nil
  75. end
  76. end
  77. end