mention_spec.rb 3.0 KB

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