mention_spec.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 '.mentionable?' do
  50. context 'with a ticket' do
  51. let(:other_ticket) { create(:ticket) }
  52. it 'retuns true if user has agent access' do
  53. expect(described_class).to be_mentionable(ticket, user)
  54. end
  55. it 'retuns true if user has limited agent access' do
  56. user.user_groups.create! group: other_ticket.group, access: 'read'
  57. expect(described_class).to be_mentionable(other_ticket, user)
  58. end
  59. it 'retuns false if agent has no access to this ticket' do
  60. expect(described_class).not_to be_mentionable(other_ticket, user)
  61. end
  62. it 'retuns false if user is customer' do
  63. ticket.update! customer: user
  64. expect(described_class).not_to be_mentionable(other_ticket, user)
  65. end
  66. end
  67. context 'with non-ticket' do
  68. it 'retuns false for non-Ticket' do
  69. expect(described_class).not_to be_mentionable(Ticket::Article.first, user)
  70. end
  71. end
  72. end
  73. end