template_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. context 'with agent permissions', authenticated_as: :agent do
  74. it 'request is forbidden' do
  75. post '/api/v1/templates.json', params: { name: 'Foo', options: { 'ticket.title': { value: 'Bar' } } }
  76. expect(response).to have_http_status(:forbidden)
  77. end
  78. end
  79. end
  80. context 'when updating template' do
  81. let!(:template) { create(:template) }
  82. it 'returns ok' do
  83. put "/api/v1/templates/#{template.id}.json", params: { options: { 'ticket.title': { value: 'Foo' } } }
  84. expect(response).to have_http_status(:ok)
  85. end
  86. context 'with agent permissions', authenticated_as: :agent do
  87. it 'request is forbidden' do
  88. put "/api/v1/templates/#{template.id}.json", params: { options: { 'ticket.title': { value: 'Foo' } } }
  89. expect(response).to have_http_status(:forbidden)
  90. end
  91. end
  92. end
  93. context 'when destroying template' do
  94. let!(:template) { create(:template) }
  95. it 'returns ok' do
  96. delete "/api/v1/templates/#{template.id}.json"
  97. expect(response).to have_http_status(:ok)
  98. end
  99. context 'with agent permissions', authenticated_as: :agent do
  100. it 'request is forbidden' do
  101. delete "/api/v1/templates/#{template.id}.json"
  102. expect(response).to have_http_status(:forbidden)
  103. end
  104. end
  105. end
  106. end
  107. end