sla_spec.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/sla/has_escalation_calculation_impact_examples'
  5. RSpec.describe Sla, type: :model do
  6. it_behaves_like 'ApplicationModel', can_assets: { associations: :calendar, selectors: :condition }
  7. it_behaves_like 'HasEscalationCalculationImpact'
  8. context 'when matching Ticket' do
  9. let(:sla) { create(:sla, :condition_title, condition_title: 'matching') }
  10. let(:sla_blank) { create(:sla, :condition_blank) }
  11. let(:ticket_matching) { create(:ticket, title: 'matching title') }
  12. let(:ticket_not_matching) { create(:ticket, title: 'nope') }
  13. describe '#condition_matches?' do
  14. it 'returns true when condition matches ticket' do
  15. expect(sla).to be_condition_matches(ticket_matching)
  16. end
  17. it 'returns false when condition does not match ticket' do
  18. expect(sla).not_to be_condition_matches(ticket_not_matching)
  19. end
  20. it 'returns false when condition does not match ticket while matching tickets exist' do
  21. ticket_matching
  22. expect(sla).not_to be_condition_matches(ticket_not_matching)
  23. end
  24. it 'returns true when SLA condition is blank ticket' do
  25. expect(sla_blank).to be_condition_matches(ticket_not_matching)
  26. end
  27. end
  28. describe '#cannot_have_response_and_update' do
  29. it 'allows neither #response_time nor #update_time' do
  30. instance = build(:sla, response_time: nil, update_time: nil)
  31. expect(instance).to be_valid
  32. end
  33. it 'allows #response_time' do
  34. instance = build(:sla, response_time: 180, update_time: nil)
  35. expect(instance).to be_valid
  36. end
  37. it 'allows #update_time' do
  38. instance = build(:sla, response_time: nil, update_time: 180)
  39. expect(instance).to be_valid
  40. end
  41. it 'denies both #response_time and #update_time' do
  42. instance = build(:sla, response_time: 180, update_time: 180)
  43. expect(instance).not_to be_valid
  44. end
  45. end
  46. describe '.for_ticket' do
  47. it 'returns matching SLA for the ticket' do
  48. sla
  49. expect(described_class.for_ticket(ticket_matching)).to eq sla
  50. end
  51. it 'returns nil when no SLA matches ticket' do
  52. sla
  53. expect(described_class.for_ticket(ticket_not_matching)).to be_nil
  54. end
  55. it 'returns blank SLA for the ticket' do
  56. sla_blank
  57. expect(described_class.for_ticket(ticket_matching)).to eq sla_blank
  58. end
  59. it 'returns non-blank SLA over blank SLA for the ticket' do
  60. sla
  61. sla_blank
  62. expect(described_class.for_ticket(ticket_matching)).to eq sla
  63. end
  64. context 'when multiple SLAs are matching' do
  65. let(:sla) { create(:sla, :condition_title, condition_title: 'matching', name: 'ZZZ 1') }
  66. let(:sla2) { create(:sla, :condition_title, condition_title: 'matching', name: 'AAA 1') }
  67. before do
  68. sla
  69. sla2
  70. end
  71. it 'returns the AAA 1 sla as matching' do
  72. expect(described_class.for_ticket(ticket_matching)).to eq sla2
  73. end
  74. end
  75. end
  76. end
  77. end