macro_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Macro', authenticated_as: :user, type: :request do
  4. let(:successful_params) do
  5. {
  6. name: 'asd',
  7. perform: {
  8. 'ticket.state_id': {
  9. value: '2'
  10. }
  11. },
  12. ux_flow_next_up: 'none',
  13. note: '',
  14. group_ids: nil,
  15. active: true
  16. }
  17. end
  18. describe '#create' do
  19. before do
  20. post '/api/v1/macros', params: successful_params, as: :json
  21. end
  22. context 'when user is not allowed to create macro' do
  23. let(:user) { create(:agent) }
  24. it 'does not create macro' do
  25. expect(response).to have_http_status(:forbidden)
  26. end
  27. end
  28. context 'when user is allowed to create macros' do
  29. let(:user) { create(:admin) }
  30. it 'creates macro' do
  31. expect(response).to have_http_status(:created)
  32. end
  33. end
  34. end
  35. describe '#update' do
  36. let(:macro) { create(:macro, name: 'test') }
  37. before do
  38. put "/api/v1/macros/#{macro.id}", params: successful_params, as: :json
  39. end
  40. context 'when user is not allowed to update macro' do
  41. let(:user) { create(:agent) }
  42. it 'does not update macro' do
  43. expect(response).to have_http_status(:forbidden)
  44. end
  45. it 'macro is not changed' do
  46. expect(macro.reload.name).to eq 'test'
  47. end
  48. end
  49. context 'when user is allowed to update macros' do
  50. let(:user) { create(:admin) }
  51. it 'request is successful' do
  52. expect(response).to have_http_status(:ok)
  53. end
  54. it 'macro is changed' do
  55. expect(macro.reload.name).to eq 'asd'
  56. end
  57. end
  58. end
  59. describe '#destroy' do
  60. let(:macro) { create(:macro) }
  61. before do
  62. delete "/api/v1/macros/#{macro.id}", as: :json
  63. end
  64. context 'when user is not allowed to destroy macro' do
  65. let(:user) { create(:agent) }
  66. it 'does not destroy macro' do
  67. expect(response).to have_http_status(:forbidden)
  68. end
  69. it 'macro is not destroyed' do
  70. expect(macro).not_to be_destroyed
  71. end
  72. end
  73. context 'when user is allowed to create macros' do
  74. let(:user) { create(:admin) }
  75. it 'request is successful' do
  76. expect(response).to have_http_status(:ok)
  77. end
  78. it 'macro is destroyed' do
  79. expect(Macro).not_to be_exist(macro.id)
  80. end
  81. end
  82. end
  83. describe '#index' do
  84. before do
  85. get '/api/v1/macros', as: :json
  86. end
  87. context 'when user is not allowed to use macros' do
  88. let(:user) { create(:customer) }
  89. it 'returns exception' do
  90. expect(response).to have_http_status(:forbidden)
  91. end
  92. end
  93. context 'when user is allowed to use macros' do
  94. let(:user) { create(:agent) }
  95. it 'request is successful' do
  96. expect(response).to have_http_status(:ok)
  97. end
  98. it 'returns array of macros' do
  99. expect(json_response.pluck('id')).to eq [Macro.first.id]
  100. end
  101. end
  102. context 'when user is admin only' do
  103. let(:user) { create(:admin_only) }
  104. it 'returns array of macros' do
  105. expect(json_response.pluck('id')).to eq [Macro.first.id]
  106. end
  107. end
  108. end
  109. describe '#show' do
  110. let(:macro) { create(:macro, groups: create_list(:group, 1)) }
  111. before do
  112. get "/api/v1/macros/#{macro.id}", as: :json
  113. end
  114. context 'when user is not allowed to use macros' do
  115. let(:user) { create(:customer) }
  116. it 'returns exception' do
  117. expect(response).to have_http_status(:forbidden)
  118. end
  119. end
  120. context 'when user is allowed to use macros' do
  121. let(:user) { create(:agent) }
  122. it 'returns exception when user has no access to related group' do
  123. expect(response).to have_http_status(:not_found)
  124. end
  125. context 'when user has acess to this group' do
  126. let(:user) { create(:agent, groups: macro.groups) }
  127. it 'returns macro when user has access to related group' do
  128. expect(response).to have_http_status(:ok)
  129. end
  130. end
  131. end
  132. context 'when user is admin only' do
  133. let(:user) { create(:admin_only) }
  134. it 'returns array of macros' do
  135. expect(response).to have_http_status(:ok)
  136. end
  137. end
  138. end
  139. end