mention_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Mention, type: :model do
  4. let(:ticket) { create(:ticket) }
  5. let(:user) { create(:agent_and_customer, groups: [ticket.group]) }
  6. describe 'validation' do
  7. it 'does not allow mentions for customers' do
  8. record = build(:mention, mentionable: ticket, user: create(:customer))
  9. record.save
  10. expect(record.errors.full_messages).to include('A mentioned user has no agent access to this ticket')
  11. end
  12. end
  13. describe '.subscribed?', current_user_id: 1 do
  14. it 'returns true when subscribed' do
  15. described_class.subscribe! ticket, user
  16. expect(described_class).to be_subscribed(ticket, user)
  17. end
  18. it 'returns false when subscribed' do
  19. expect(described_class).not_to be_subscribed(ticket, user)
  20. end
  21. end
  22. describe '.subscribe!', current_user_id: 1 do
  23. it 'subscribes to a object' do
  24. expect { described_class.subscribe! ticket, user }
  25. .to change { ticket.mentions.where(user: user).count }
  26. .from(0)
  27. .to(1)
  28. end
  29. it 'ignores if re-subscribing to a object' do
  30. described_class.subscribe! ticket, user
  31. expect { described_class.subscribe! ticket, user }
  32. .not_to change { ticket.mentions.where(user: user).count }
  33. .from(1)
  34. end
  35. end
  36. describe '.unsubscribe!', current_user_id: 1 do
  37. it 'unsubscribes from a object' do
  38. described_class.subscribe! ticket, user
  39. expect { described_class.unsubscribe! ticket, user }
  40. .to change { ticket.mentions.where(user: user).count }
  41. .from(1)
  42. .to(0)
  43. end
  44. it 'ignores if unsubscribing from a not-subscribed object' do
  45. expect(described_class.unsubscribe!(ticket, user))
  46. .to be_truthy
  47. end
  48. end
  49. describe '.unsubscribe_all!', current_user_id: 1 do
  50. it 'unsubscribes all users from a object' do
  51. described_class.subscribe! ticket, user
  52. described_class.subscribe! ticket, create(:agent_and_customer, groups: [ticket.group])
  53. expect { described_class.unsubscribe_all! ticket }
  54. .to change { ticket.mentions.count }
  55. .by(-2)
  56. end
  57. it 'ignores if unsubscribing from a not-subscribed object' do
  58. expect(described_class.unsubscribe!(ticket, user))
  59. .to be_truthy
  60. end
  61. end
  62. describe '.mentionable?' do
  63. context 'with a ticket' do
  64. let(:other_ticket) { create(:ticket) }
  65. it 'retuns true if user has agent access' do
  66. expect(described_class).to be_mentionable(ticket, user)
  67. end
  68. it 'retuns true if user has limited agent access' do
  69. user.user_groups.create! group: other_ticket.group, access: 'read'
  70. expect(described_class).to be_mentionable(other_ticket, user)
  71. end
  72. it 'retuns false if agent has no access to this ticket' do
  73. expect(described_class).not_to be_mentionable(other_ticket, user)
  74. end
  75. it 'retuns false if user is customer' do
  76. ticket.update! customer: user
  77. expect(described_class).not_to be_mentionable(other_ticket, user)
  78. end
  79. end
  80. context 'with non-ticket' do
  81. it 'retuns false for non-Ticket' do
  82. expect(described_class).not_to be_mentionable(Ticket::Article.first, user)
  83. end
  84. end
  85. end
  86. end