selector_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Selector', authenticated_as: :admin, type: :request do
  4. let(:admin) { create(:admin) }
  5. describe 'Ticket' do
  6. let(:ticket_1) { create(:ticket) }
  7. let(:ticket_2) { create(:ticket) }
  8. let(:ticket_3) { create(:ticket) }
  9. before do
  10. ticket_1 && ticket_2 && ticket_3
  11. end
  12. it 'does find tickets by title', :aggregate_failures do
  13. condition = {
  14. operator: 'OR',
  15. conditions: [
  16. {
  17. name: 'ticket.title',
  18. operator: 'contains',
  19. value: ticket_1.title,
  20. },
  21. {
  22. name: 'ticket.title',
  23. operator: 'contains',
  24. value: ticket_2.title,
  25. },
  26. {
  27. name: 'ticket.title',
  28. operator: 'contains',
  29. value: ticket_3.title,
  30. },
  31. ]
  32. }
  33. params = {
  34. condition: condition
  35. }
  36. post '/api/v1/tickets/selector', params: params, as: :json
  37. expect(response).to have_http_status(:ok)
  38. expect(json_response['object_ids'].count).to eq(3)
  39. end
  40. end
  41. describe 'User' do
  42. let(:user_1) { create(:user, firstname: 'User-1') }
  43. let(:user_2) { create(:user, firstname: 'User-2') }
  44. let(:user_3) { create(:user, firstname: 'User-3') }
  45. before do
  46. user_1 && user_2 && user_3
  47. end
  48. it 'does find users by firstname', :aggregate_failures do
  49. condition = {
  50. operator: 'OR',
  51. conditions: [
  52. {
  53. name: 'user.firstname',
  54. operator: 'contains',
  55. value: user_1.firstname,
  56. },
  57. {
  58. name: 'user.firstname',
  59. operator: 'contains',
  60. value: user_2.firstname,
  61. },
  62. {
  63. name: 'user.firstname',
  64. operator: 'contains',
  65. value: user_3.firstname,
  66. },
  67. ]
  68. }
  69. params = {
  70. condition: condition
  71. }
  72. post '/api/v1/users/selector', params: params, as: :json
  73. expect(response).to have_http_status(:ok)
  74. expect(json_response['object_ids'].count).to eq(3)
  75. end
  76. end
  77. describe 'Organization' do
  78. let(:organization_1) { create(:organization, name: 'Org-1') }
  79. let(:organization_2) { create(:organization, name: 'Org-2') }
  80. let(:organization_3) { create(:organization, name: 'Org-3') }
  81. before do
  82. organization_1 && organization_2 && organization_3
  83. end
  84. it 'does find organizations by name', :aggregate_failures do
  85. condition = {
  86. operator: 'OR',
  87. conditions: [
  88. {
  89. name: 'organization.name',
  90. operator: 'contains',
  91. value: organization_1.name,
  92. },
  93. {
  94. name: 'organization.name',
  95. operator: 'contains',
  96. value: organization_2.name,
  97. },
  98. {
  99. name: 'organization.name',
  100. operator: 'contains',
  101. value: organization_3.name,
  102. },
  103. ]
  104. }
  105. params = {
  106. condition: condition
  107. }
  108. post '/api/v1/organizations/selector', params: params, as: :json
  109. expect(response).to have_http_status(:ok)
  110. expect(json_response['object_ids'].count).to eq(3)
  111. end
  112. end
  113. describe 'handling wrong parameters', aggregate_failures: true do
  114. it 'returns error for non existant type' do
  115. post '/api/v1/blablabla/selector', params: nil, as: :json
  116. expect(response).to have_http_status(:unprocessable_entity)
  117. expect(json_response['error_human']).to eq('Given object does not support selector')
  118. end
  119. it 'returns error for object that does not support selectors' do
  120. post '/api/v1/groups/selector', params: nil, as: :json
  121. expect(response).to have_http_status(:unprocessable_entity)
  122. expect(json_response['error_human']).to eq('Given object does not support selector')
  123. end
  124. end
  125. end