template_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Template, type: :request do
  4. let(:agent) { create(:agent) }
  5. let(:admin) { create(:user, roles: Role.where(name: 'Admin')) }
  6. let(:customer) { create(:customer) }
  7. describe 'request handling', authenticated_as: :admin do
  8. before do
  9. allow(ActiveSupport::Deprecation).to receive(:warn)
  10. end
  11. context 'when listing templates' do
  12. let!(:templates) do
  13. create_list(:template, 10).tap do |templates|
  14. templates.each do |template|
  15. # Make all templates with even IDs inactive (total of 5).
  16. template.update!(active: false) if template.id.even?
  17. end
  18. end
  19. end
  20. it 'returns all' do
  21. get '/api/v1/templates.json'
  22. expect(json_response.length).to eq(10)
  23. end
  24. it 'returns options in a new format only' do
  25. get '/api/v1/templates.json'
  26. templates.each_with_index do |template, index|
  27. expect(json_response[index]['options']).to eq(template.options)
  28. end
  29. end
  30. context 'with agent permissions', authenticated_as: :agent do
  31. it 'returns active templates only' do
  32. get '/api/v1/templates.json'
  33. expect(json_response.length).to eq(5)
  34. end
  35. end
  36. end
  37. context 'when showing templates' do
  38. let!(:template) { create(:template) }
  39. it 'returns ok' do
  40. get "/api/v1/templates/#{template.id}.json"
  41. expect(response).to have_http_status(:ok)
  42. end
  43. it 'returns options in a new format only' do
  44. get "/api/v1/templates/#{template.id}.json"
  45. expect(json_response['options']).to eq(template.options)
  46. end
  47. context 'with inactive template' do
  48. let!(:inactive_template) { create(:template, active: false) }
  49. it 'returns ok' do
  50. get "/api/v1/templates/#{inactive_template.id}.json"
  51. expect(response).to have_http_status(:ok)
  52. end
  53. end
  54. context 'with agent permissions', authenticated_as: :agent do
  55. it 'returns ok' do
  56. get "/api/v1/templates/#{template.id}.json"
  57. expect(response).to have_http_status(:ok)
  58. end
  59. context 'with inactive template' do
  60. let!(:inactive_template) { create(:template, active: false) }
  61. it 'request is not found' do
  62. get "/api/v1/templates/#{inactive_template.id}.json"
  63. expect(response).to have_http_status(:not_found)
  64. end
  65. end
  66. end
  67. end
  68. context 'when creating template' do
  69. it 'returns created' do
  70. post '/api/v1/templates.json', params: { name: 'Foo', options: { 'ticket.title': { value: 'Bar' }, 'ticket.customer_id': { value: customer.id.to_s, value_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } } }
  71. expect(response).to have_http_status(:created)
  72. end
  73. it 'supports template options in an older format' do
  74. params = { name: 'Foo', options: { title: 'Bar', customer_id: customer.id.to_s, customer_id_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }
  75. post '/api/v1/templates.json', params: params
  76. expect(json_response['options']).to eq({ 'ticket.title': { value: 'Bar' }, 'ticket.customer_id': { value: customer.id.to_s, value_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }.deep_stringify_keys)
  77. end
  78. it 'throws deprecation warning' do
  79. post '/api/v1/templates.json', params: { name: 'Foo', options: { title: 'Bar', customer_id: customer.id.to_s, customer_id_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }
  80. expect(ActiveSupport::Deprecation).to have_received(:warn)
  81. end
  82. context 'with agent permissions', authenticated_as: :agent do
  83. it 'request is forbidden' do
  84. post '/api/v1/templates.json', params: { name: 'Foo', options: { 'ticket.title': { value: 'Bar' } } }
  85. expect(response).to have_http_status(:forbidden)
  86. end
  87. end
  88. end
  89. context 'when updating template' do
  90. let!(:template) { create(:template) }
  91. it 'returns ok' do
  92. put "/api/v1/templates/#{template.id}.json", params: { options: { 'ticket.title': { value: 'Foo' } } }
  93. expect(response).to have_http_status(:ok)
  94. end
  95. it 'supports template options in an older format' do
  96. params = { name: 'Foo', options: { title: 'Bar', customer_id: customer.id.to_s, customer_id_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }
  97. put "/api/v1/templates/#{template.id}.json", params: params
  98. expect(json_response['options']).to eq({ 'ticket.title': { value: 'Bar' }, 'ticket.customer_id': { value: customer.id.to_s, value_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }.deep_stringify_keys)
  99. end
  100. it 'throws deprecation warning' do
  101. put "/api/v1/templates/#{template.id}.json", params: { name: 'Foo', options: { title: 'Bar', customer_id: customer.id.to_s, customer_id_completion: "#{customer.firstname} #{customer.lastname} <#{customer.email}>" } }
  102. expect(ActiveSupport::Deprecation).to have_received(:warn)
  103. end
  104. context 'with agent permissions', authenticated_as: :agent do
  105. it 'request is forbidden' do
  106. put "/api/v1/templates/#{template.id}.json", params: { options: { 'ticket.title': { value: 'Foo' } } }
  107. expect(response).to have_http_status(:forbidden)
  108. end
  109. end
  110. end
  111. context 'when destroying template' do
  112. let!(:template) { create(:template) }
  113. it 'returns ok' do
  114. delete "/api/v1/templates/#{template.id}.json"
  115. expect(response).to have_http_status(:ok)
  116. end
  117. context 'with agent permissions', authenticated_as: :agent do
  118. it 'request is forbidden' do
  119. delete "/api/v1/templates/#{template.id}.json"
  120. expect(response).to have_http_status(:forbidden)
  121. end
  122. end
  123. end
  124. end
  125. end