attachments_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. require 'rails_helper'
  2. RSpec.describe 'KnowledgeBase attachments', type: :request do
  3. include_context 'basic Knowledge Base'
  4. let(:attachment) do
  5. attachment_file = File.open 'spec/fixtures/upload/hello_world.txt'
  6. Store.add(
  7. object: object.class.to_s,
  8. o_id: object.id,
  9. data: attachment_file.read,
  10. filename: 'hello_world.txt',
  11. preferences: {},
  12. created_by_id: 1,
  13. )
  14. end
  15. let(:endpoint) { "/api/v1/attachments/#{attachment.id}" }
  16. describe 'visible when attached to' do
  17. shared_examples 'a visible resource' do
  18. it 'and returns correct status code' do
  19. get endpoint
  20. expect(response).to have_http_status(:ok)
  21. end
  22. end
  23. shared_examples 'a non-existent resource' do
  24. it 'and returns correct status code' do
  25. get endpoint
  26. expect(response).to have_http_status(:not_found)
  27. end
  28. end
  29. describe 'draft answer' do
  30. let(:object) { draft_answer }
  31. describe 'as agent', authenticated_as: :agent_user do
  32. it_behaves_like 'a non-existent resource'
  33. end
  34. context 'as admin', authenticated_as: :admin_user do
  35. it_behaves_like 'a visible resource'
  36. end
  37. context 'as customer', authenticated_as: :customer_user do
  38. it_behaves_like 'a non-existent resource'
  39. end
  40. context 'as guest' do
  41. it_behaves_like 'a non-existent resource'
  42. end
  43. end
  44. describe 'internal answer' do
  45. let(:object) { internal_answer }
  46. describe 'as agent', authenticated_as: :agent_user do
  47. it_behaves_like 'a visible resource'
  48. end
  49. context 'as admin', authenticated_as: :admin_user do
  50. it_behaves_like 'a visible resource'
  51. end
  52. context 'as customer', authenticated_as: :customer_user do
  53. it_behaves_like 'a non-existent resource'
  54. end
  55. context 'as guest' do
  56. it_behaves_like 'a non-existent resource'
  57. end
  58. end
  59. describe 'published answer' do
  60. let(:object) { published_answer }
  61. describe 'as agent', authenticated_as: :agent_user do
  62. it_behaves_like 'a visible resource'
  63. end
  64. context 'as admin', authenticated_as: :admin_user do
  65. it_behaves_like 'a visible resource'
  66. end
  67. context 'as customer', authenticated_as: :customer_user do
  68. it_behaves_like 'a visible resource'
  69. end
  70. context 'as guest' do
  71. it_behaves_like 'a visible resource'
  72. end
  73. end
  74. describe 'archived answer' do
  75. let(:object) { archived_answer }
  76. describe 'as agent', authenticated_as: :agent_user do
  77. it_behaves_like 'a non-existent resource'
  78. end
  79. context 'as admin', authenticated_as: :admin_user do
  80. it_behaves_like 'a visible resource'
  81. end
  82. context 'as customer', authenticated_as: :customer_user do
  83. it_behaves_like 'a non-existent resource'
  84. end
  85. context 'as guest' do
  86. it_behaves_like 'a non-existent resource'
  87. end
  88. end
  89. end
  90. describe 'deletable when attached to' do
  91. shared_examples 'a deletable resource' do
  92. it { expect { delete endpoint }.to change { Store.exists? attachment.id }.from(true).to(false) }
  93. end
  94. shared_examples 'a non-deletable resource' do
  95. it { expect { delete endpoint }.not_to change { Store.exists? attachment.id }.from(true) }
  96. end
  97. describe 'draft answer' do
  98. let(:object) { draft_answer }
  99. describe 'as agent', authenticated_as: :agent_user do
  100. it_behaves_like 'a non-deletable resource'
  101. end
  102. context 'as admin', authenticated_as: :admin_user do
  103. it_behaves_like 'a deletable resource'
  104. end
  105. context 'as customer', authenticated_as: :customer_user do
  106. it_behaves_like 'a non-deletable resource'
  107. end
  108. context 'as guest' do
  109. it_behaves_like 'a non-deletable resource'
  110. end
  111. end
  112. describe 'internal answer' do
  113. let(:object) { internal_answer }
  114. describe 'as agent', authenticated_as: :agent_user do
  115. it_behaves_like 'a non-deletable resource'
  116. end
  117. context 'as admin', authenticated_as: :admin_user do
  118. it_behaves_like 'a deletable resource'
  119. end
  120. context 'as customer', authenticated_as: :customer_user do
  121. it_behaves_like 'a non-deletable resource'
  122. end
  123. context 'as guest' do
  124. it_behaves_like 'a non-deletable resource'
  125. end
  126. end
  127. describe 'published answer' do
  128. let(:object) { published_answer }
  129. describe 'as agent', authenticated_as: :agent_user do
  130. it_behaves_like 'a non-deletable resource'
  131. end
  132. context 'as admin', authenticated_as: :admin_user do
  133. it_behaves_like 'a deletable resource'
  134. end
  135. context 'as customer', authenticated_as: :customer_user do
  136. it_behaves_like 'a non-deletable resource'
  137. end
  138. context 'as guest' do
  139. it_behaves_like 'a non-deletable resource'
  140. end
  141. end
  142. describe 'archived answer' do
  143. let(:object) { archived_answer }
  144. describe 'as agent', authenticated_as: :agent_user do
  145. it_behaves_like 'a non-deletable resource'
  146. end
  147. context 'as admin', authenticated_as: :admin_user do
  148. it_behaves_like 'a deletable resource'
  149. end
  150. context 'as customer', authenticated_as: :customer_user do
  151. it_behaves_like 'a non-deletable resource'
  152. end
  153. context 'as guest' do
  154. it_behaves_like 'a non-deletable resource'
  155. end
  156. end
  157. end
  158. end