mention_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'rails_helper'
  2. RSpec.describe 'Mention', type: :request, authenticated_as: -> { user } do
  3. let(:group) { create(:group) }
  4. let(:ticket1) { create(:ticket, group: group) }
  5. let(:ticket2) { create(:ticket, group: group) }
  6. let(:ticket3) { create(:ticket, group: group) }
  7. let(:ticket4) { create(:ticket, group: group) }
  8. let(:user) { create(:agent, groups: [group]) }
  9. describe 'GET /api/v1/mentions' do
  10. before do
  11. create(:mention, mentionable: ticket1, user: user)
  12. create(:mention, mentionable: ticket2, user: user)
  13. create(:mention, mentionable: ticket3, user: user)
  14. end
  15. it 'returns good status code' do
  16. get '/api/v1/mentions', params: {}, as: :json
  17. expect(response).to have_http_status(:ok)
  18. end
  19. it 'returns mentions by user' do
  20. get '/api/v1/mentions', params: {}, as: :json
  21. expect(json_response['mentions'].count).to eq(3)
  22. end
  23. it 'returns mentions by mentionable' do
  24. get '/api/v1/mentions', params: { mentionable_type: 'Ticket', mentionable_id: ticket3.id }, as: :json
  25. expect(json_response['mentions'].count).to eq(1)
  26. end
  27. it 'returns mentions by id' do
  28. mention = create(:mention, mentionable: ticket4, user: user)
  29. get '/api/v1/mentions', params: { id: mention.id }, as: :json
  30. expect(json_response['mentions'].count).to eq(1)
  31. end
  32. end
  33. describe 'POST /api/v1/mentions' do
  34. let(:params) do
  35. {
  36. mentionable_type: 'Ticket',
  37. mentionable_id: ticket1.id
  38. }
  39. end
  40. it 'returns good status code for subscribe' do
  41. post '/api/v1/mentions', params: params, as: :json
  42. expect(response).to have_http_status(:created)
  43. end
  44. it 'updates mention count' do
  45. expect { post '/api/v1/mentions', params: params, as: :json }.to change(Mention, :count).from(0).to(1)
  46. end
  47. describe 'when agent with read permissions' do
  48. before do
  49. user.group_names_access_map = {
  50. ticket1.group.name => 'read',
  51. }
  52. end
  53. it 'updates mention count of read only agent' do
  54. expect { post '/api/v1/mentions', params: params, as: :json }.to change(Mention, :count).from(0).to(1)
  55. end
  56. end
  57. end
  58. describe 'DELETE /api/v1/mentions/:id' do
  59. let!(:mention) { create(:mention, user: user) }
  60. it 'returns good status code' do
  61. delete "/api/v1/mentions/#{mention.id}", params: {}, as: :json
  62. expect(response).to have_http_status(:ok)
  63. end
  64. it 'clears mention count' do
  65. expect { delete "/api/v1/mentions/#{mention.id}", params: {}, as: :json }.to change(Mention, :count).from(1).to(0)
  66. end
  67. describe 'when agent with read permissions' do
  68. before do
  69. user.group_names_access_map = {
  70. ticket1.group.name => 'read',
  71. }
  72. end
  73. it 'clears mention count for read only agent' do
  74. expect { delete "/api/v1/mentions/#{mention.id}", params: {}, as: :json }.to change(Mention, :count).from(1).to(0)
  75. end
  76. end
  77. end
  78. end