taskbar_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Taskbars', type: :request do
  4. let(:agent) do
  5. create(:agent)
  6. end
  7. let(:customer) do
  8. create(:customer)
  9. end
  10. describe 'request handling' do
  11. it 'does task ownership' do
  12. params = {
  13. user_id: customer.id,
  14. key: 'Ticket-5',
  15. callback: 'TicketZoom',
  16. state: {
  17. ticket: {
  18. owner_id: agent.id,
  19. },
  20. article: {},
  21. },
  22. params: {
  23. ticket_id: 5,
  24. shown: true,
  25. },
  26. prio: 3,
  27. notify: false,
  28. active: false,
  29. }
  30. authenticated_as(agent)
  31. post '/api/v1/taskbar', params: params, as: :json
  32. expect(response).to have_http_status(:created)
  33. expect(json_response).to be_a(Hash)
  34. expect(json_response['user_id']).to eq(agent.id)
  35. expect(json_response['params']['ticket_id']).to eq(5)
  36. expect(json_response['params']['shown']).to be(true)
  37. taskbar_id = json_response['id']
  38. params[:user_id] = customer.id
  39. params[:params] = {
  40. ticket_id: 5,
  41. shown: false,
  42. }
  43. put "/api/v1/taskbar/#{taskbar_id}", params: params, as: :json
  44. expect(response).to have_http_status(:ok)
  45. expect(json_response).to be_a(Hash)
  46. expect(json_response['user_id']).to eq(agent.id)
  47. expect(json_response['params']['ticket_id']).to eq(5)
  48. expect(json_response['params']['shown']).to be(false)
  49. # try to access with other user
  50. params = {
  51. active: true,
  52. }
  53. authenticated_as(customer)
  54. put "/api/v1/taskbar/#{taskbar_id}", params: params, as: :json
  55. expect(response).to have_http_status(:unprocessable_entity)
  56. expect(json_response).to be_a(Hash)
  57. expect(json_response['error']).to eq('Not allowed to access this task.')
  58. delete "/api/v1/taskbar/#{taskbar_id}", params: {}, as: :json
  59. expect(response).to have_http_status(:unprocessable_entity)
  60. expect(json_response).to be_a(Hash)
  61. expect(json_response['error']).to eq('Not allowed to access this task.')
  62. # delete with correct user
  63. authenticated_as(agent)
  64. delete "/api/v1/taskbar/#{taskbar_id}", params: {}, as: :json
  65. expect(response).to have_http_status(:ok)
  66. expect(json_response).to be_a(Hash)
  67. expect(json_response).to be_blank
  68. end
  69. end
  70. describe 'Init', authenticated_as: :agent do
  71. let(:tickets) { create_list(:ticket, 3, group: Group.first) }
  72. let(:customers) { create_list(:customer, 3) }
  73. let(:organizations) { create_list(:organization, 3) }
  74. let(:agent) { create(:agent, groups: [Group.first]) }
  75. before do
  76. tickets.each do |ticket|
  77. create(:taskbar, key: "Ticket-#{ticket.id}", params: { ticket_id: ticket.id }, user_id: agent.id)
  78. end
  79. customers.each do |customer|
  80. create(:taskbar, key: "User-#{customer.id}", callback: 'UserProfile', params: { user_id: customer.id }, user_id: agent.id)
  81. end
  82. organizations.each do |organization|
  83. create(:taskbar, key: "Organization-#{organization.id}", callback: 'OrganizationProfile', params: { organization_id: organization.id }, user_id: agent.id)
  84. end
  85. end
  86. it 'does return ticket create data' do
  87. get '/api/v1/taskbar/init', params: {}, as: :json
  88. expect(response).to have_http_status(:ok)
  89. expect(json_response['ticket_create']).to be_present
  90. end
  91. it 'does return ticket all data' do
  92. get '/api/v1/taskbar/init', params: {}, as: :json
  93. expect(response).to have_http_status(:ok)
  94. expect(json_response['ticket_all'].keys.map(&:to_i)).to include(*tickets.pluck(:id))
  95. end
  96. it 'does return user profiles' do
  97. get '/api/v1/taskbar/init', params: {}, as: :json
  98. expect(response).to have_http_status(:ok)
  99. expect(json_response['assets']).to include_assets_of(*customers)
  100. end
  101. it 'does return user profile stats' do
  102. get '/api/v1/taskbar/init', params: {}, as: :json
  103. expect(response).to have_http_status(:ok)
  104. expect(json_response['ticket_stats_user'].keys.map(&:to_i)).to include(*customers.pluck(:id))
  105. end
  106. it 'does return organization profiles' do
  107. get '/api/v1/taskbar/init', params: {}, as: :json
  108. expect(response).to have_http_status(:ok)
  109. expect(json_response['assets']).to include_assets_of(*organizations)
  110. end
  111. it 'does return organization profile stats' do
  112. get '/api/v1/taskbar/init', params: {}, as: :json
  113. expect(response).to have_http_status(:ok)
  114. expect(json_response['ticket_stats_organization'].keys.map(&:to_i)).to include(*organizations.pluck(:id))
  115. end
  116. end
  117. end