mention_spec.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2023 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. expect { create(:mention, mentionable: ticket, user: create(:customer)) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: User has no agent access to this ticket')
  9. end
  10. end
  11. describe '.subscribed?', current_user_id: 1 do
  12. it 'returns true when subscribed' do
  13. described_class.subscribe! ticket, user
  14. expect(described_class).to be_subscribed(ticket, user)
  15. end
  16. it 'returns false when subscribed' do
  17. expect(described_class).not_to be_subscribed(ticket, user)
  18. end
  19. end
  20. describe '.subscribe!', current_user_id: 1 do
  21. it 'subscribes to a object' do
  22. expect { described_class.subscribe! ticket, user }
  23. .to change { ticket.mentions.where(user: user).count }
  24. .from(0)
  25. .to(1)
  26. end
  27. it 'ignores if re-subscribing to a object' do
  28. described_class.subscribe! ticket, user
  29. expect { described_class.subscribe! ticket, user }
  30. .not_to change { ticket.mentions.where(user: user).count }
  31. .from(1)
  32. end
  33. end
  34. describe '.unsubscribe!', current_user_id: 1 do
  35. it 'unsubscribes from a object' do
  36. described_class.subscribe! ticket, user
  37. expect { described_class.unsubscribe! ticket, user }
  38. .to change { ticket.mentions.where(user: user).count }
  39. .from(1)
  40. .to(0)
  41. end
  42. it 'ignores if unsubscribing from a not-subscribed object' do
  43. expect(described_class.unsubscribe!(ticket, user))
  44. .to be_truthy
  45. end
  46. end
  47. describe '.mentionable?' do
  48. context 'with a ticket' do
  49. let(:other_ticket) { create(:ticket) }
  50. it 'retuns true if user has agent access' do
  51. expect(described_class).to be_mentionable(ticket, user)
  52. end
  53. it 'retuns true if user has limited agent access' do
  54. user.user_groups.create! group: other_ticket.group, access: 'read'
  55. expect(described_class).to be_mentionable(other_ticket, user)
  56. end
  57. it 'retuns false if agent has no access to this ticket' do
  58. expect(described_class).not_to be_mentionable(other_ticket, user)
  59. end
  60. it 'retuns false if user is customer' do
  61. ticket.update! customer: user
  62. expect(described_class).not_to be_mentionable(other_ticket, user)
  63. end
  64. end
  65. context 'with non-ticket' do
  66. it 'retuns false for non-Ticket' do
  67. expect(described_class).not_to be_mentionable(Ticket::Article.first, user)
  68. end
  69. end
  70. end
  71. end