checklist_templates_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'ChecklistTemplates', type: :request do
  4. let(:unauthorized_user) { create(:agent) }
  5. let(:authorized_user) { create(:admin) }
  6. describe '#index' do
  7. before do
  8. create_list(:checklist_template, 10)
  9. get '/api/v1/checklist_templates'
  10. end
  11. context 'when user is not authenticated', authenticated_as: :unauthorized_user do
  12. let(:unauthorized_user) { create(:customer) }
  13. it 'returns forbidden status' do
  14. expect(response).to have_http_status(:forbidden)
  15. end
  16. end
  17. context 'when user is authenticated', authenticated_as: :authorized_user do
  18. it 'returns ok status' do
  19. expect(response).to have_http_status(:ok)
  20. end
  21. context 'when no template was created' do
  22. it 'returns checklist templates' do
  23. expect(json_response.length).to eq(10)
  24. end
  25. end
  26. end
  27. end
  28. describe '#show' do
  29. let(:checklist_template) { create(:checklist_template) }
  30. before do
  31. get "/api/v1/checklist_templates/#{checklist_template.id}"
  32. end
  33. context 'when user is not authenticated', authenticated_as: :unauthorized_user do
  34. let(:unauthorized_user) { create(:customer) }
  35. it 'returns forbidden status' do
  36. expect(response).to have_http_status(:forbidden)
  37. end
  38. end
  39. context 'when user is authenticated', authenticated_as: :authorized_user do
  40. it 'returns ok status' do
  41. expect(response).to have_http_status(:ok)
  42. end
  43. context 'when checklist template was created' do
  44. it 'returns checklist template' do
  45. expect(json_response.except(:created_at, :updated_at)).to include(checklist_template.attributes_with_association_ids.except(:created_at, :updated_at))
  46. end
  47. end
  48. context 'when checklist template was not found' do
  49. let(:checklist_template) { create(:checklist) }
  50. it 'returns not found status' do
  51. expect(response).to have_http_status(:not_found)
  52. end
  53. end
  54. end
  55. end
  56. describe '#create' do
  57. let(:checklist_template_params) do
  58. {
  59. name: Faker::Name.unique.name,
  60. items: [
  61. Faker::Lorem.unique.sentence,
  62. Faker::Lorem.unique.sentence,
  63. Faker::Lorem.unique.sentence,
  64. ],
  65. active: true,
  66. }
  67. end
  68. before do
  69. post '/api/v1/checklist_templates', params: checklist_template_params
  70. end
  71. context 'when user is not authenticated', authenticated_as: :unauthorized_user do
  72. it 'returns forbidden status' do
  73. expect(response).to have_http_status(:forbidden)
  74. end
  75. end
  76. context 'when user is authenticated', authenticated_as: :authorized_user do
  77. it 'returns created status' do
  78. expect(response).to have_http_status(:created)
  79. end
  80. context 'when checklist template was created' do
  81. it 'returns checklist template' do
  82. expect(json_response.except(:created_at, :updated_at)).to include(ChecklistTemplate.last.attributes_with_association_ids.except(:created_at, :updated_at))
  83. end
  84. end
  85. end
  86. end
  87. describe '#update' do
  88. let(:checklist_template) { create(:checklist_template) }
  89. let(:checklist_template_params) do
  90. {
  91. 'name' => 'Updated Checklist',
  92. 'active' => false,
  93. }
  94. end
  95. before do
  96. put "/api/v1/checklist_templates/#{checklist_template.id}", params: checklist_template_params
  97. end
  98. context 'when user is not authenticated', authenticated_as: :unauthorized_user do
  99. it 'returns forbidden status' do
  100. expect(response).to have_http_status(:forbidden)
  101. end
  102. end
  103. context 'when user is authenticated', authenticated_as: :authorized_user do
  104. it 'returns ok status' do
  105. expect(response).to have_http_status(:ok)
  106. end
  107. context 'when checklist template was updated' do
  108. it 'returns updated checklist template' do
  109. expect(json_response.except(:created_at, :updated_at)).to include(checklist_template_params)
  110. end
  111. end
  112. context 'when checklist template was not found' do
  113. let(:checklist_template) { create(:checklist) }
  114. it 'returns not found status' do
  115. expect(response).to have_http_status(:not_found)
  116. end
  117. end
  118. end
  119. end
  120. describe '#destroy' do
  121. let(:checklist_template) { create(:checklist_template) }
  122. before do
  123. delete "/api/v1/checklist_templates/#{checklist_template.id}"
  124. end
  125. context 'when user is not authenticated', authenticated_as: :unauthorized_user do
  126. it 'returns forbidden status' do
  127. expect(response).to have_http_status(:forbidden)
  128. end
  129. end
  130. context 'when user is authenticated', authenticated_as: :authorized_user do
  131. it 'returns no content status' do
  132. expect(response).to have_http_status(:ok)
  133. end
  134. context 'when checklist template was destroyed' do
  135. it 'returns no content status' do
  136. expect { checklist_template.reload }.to raise_error(ActiveRecord::RecordNotFound)
  137. end
  138. end
  139. context 'when checklist template was not found' do
  140. let(:checklist_template) { create(:checklist) }
  141. it 'returns not found status' do
  142. expect(response).to have_http_status(:not_found)
  143. end
  144. end
  145. end
  146. end
  147. end