PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/spec/controllers/projects/snippets_controller_spec.rb

https://gitlab.com/klml/gitlab-ee
Ruby | 325 lines | 249 code | 75 blank | 1 comment | 0 complexity | 353b79fd954cd5fa1372b0eaf123fa3e MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Projects::SnippetsController do
  4. include Gitlab::Routing
  5. let_it_be(:user) { create(:user) }
  6. let_it_be(:other_user) { create(:user) }
  7. let_it_be(:project) { create(:project_empty_repo, :public) }
  8. before do
  9. project.add_maintainer(user)
  10. project.add_developer(other_user)
  11. end
  12. describe 'GET #index' do
  13. let(:base_params) do
  14. {
  15. namespace_id: project.namespace,
  16. project_id: project
  17. }
  18. end
  19. subject { get :index, params: base_params }
  20. it_behaves_like 'paginated collection' do
  21. let(:collection) { project.snippets }
  22. let(:params) { base_params }
  23. before do
  24. create(:project_snippet, :public, project: project, author: user)
  25. end
  26. end
  27. it 'fetches snippet counts via the snippet count service' do
  28. service = double(:count_service, execute: {})
  29. expect(Snippets::CountService)
  30. .to receive(:new).with(nil, project: project)
  31. .and_return(service)
  32. subject
  33. end
  34. it_behaves_like 'snippets sort order' do
  35. let(:params) { base_params }
  36. end
  37. it_behaves_like 'snippets views' do
  38. let(:params) { base_params }
  39. end
  40. context 'when the project snippet is private' do
  41. let_it_be(:project_snippet) { create(:project_snippet, :private, project: project, author: user) }
  42. context 'when anonymous' do
  43. it 'does not include the private snippet' do
  44. subject
  45. expect(assigns(:snippets)).not_to include(project_snippet)
  46. expect(response).to have_gitlab_http_status(:ok)
  47. end
  48. end
  49. context 'when signed in as the author' do
  50. it 'renders the snippet' do
  51. sign_in(user)
  52. subject
  53. expect(assigns(:snippets)).to include(project_snippet)
  54. expect(response).to have_gitlab_http_status(:ok)
  55. end
  56. end
  57. context 'when signed in as a project member' do
  58. it 'renders the snippet' do
  59. sign_in(other_user)
  60. subject
  61. expect(assigns(:snippets)).to include(project_snippet)
  62. expect(response).to have_gitlab_http_status(:ok)
  63. end
  64. end
  65. end
  66. end
  67. describe 'POST #mark_as_spam' do
  68. let_it_be(:snippet) { create(:project_snippet, :private, project: project, author: user) }
  69. before do
  70. allow_next_instance_of(Spam::AkismetService) do |instance|
  71. allow(instance).to receive_messages(submit_spam: true)
  72. end
  73. stub_application_setting(akismet_enabled: true)
  74. end
  75. def mark_as_spam
  76. admin = create(:admin)
  77. create(:user_agent_detail, subject: snippet)
  78. project.add_maintainer(admin)
  79. sign_in(admin)
  80. post :mark_as_spam,
  81. params: {
  82. namespace_id: project.namespace,
  83. project_id: project,
  84. id: snippet.id
  85. }
  86. end
  87. it 'updates the snippet' do
  88. mark_as_spam
  89. expect(snippet.reload).not_to be_submittable_as_spam
  90. end
  91. end
  92. shared_examples 'successful response' do
  93. it 'renders the snippet' do
  94. subject
  95. expect(assigns(:snippet)).to eq(project_snippet)
  96. expect(response).to have_gitlab_http_status(:ok)
  97. end
  98. end
  99. %w[show raw].each do |action|
  100. describe "GET ##{action}" do
  101. context 'when the project snippet is private' do
  102. let_it_be(:project_snippet) { create(:project_snippet, :private, :repository, project: project, author: user) }
  103. subject { get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } }
  104. context 'when anonymous' do
  105. it 'responds with status 404' do
  106. subject
  107. expect(response).to have_gitlab_http_status(:not_found)
  108. end
  109. end
  110. context 'when signed in as the author' do
  111. before do
  112. sign_in(user)
  113. end
  114. it_behaves_like 'successful response'
  115. end
  116. context 'when signed in as a project member' do
  117. before do
  118. sign_in(other_user)
  119. end
  120. it_behaves_like 'successful response'
  121. end
  122. end
  123. context 'when the project snippet does not exist' do
  124. subject { get action, params: { namespace_id: project.namespace, project_id: project, id: 42 } }
  125. context 'when anonymous' do
  126. it 'responds with status 404' do
  127. subject
  128. expect(response).to have_gitlab_http_status(:not_found)
  129. end
  130. end
  131. context 'when signed in' do
  132. before do
  133. sign_in(user)
  134. end
  135. it 'responds with status 404' do
  136. subject
  137. expect(response).to have_gitlab_http_status(:not_found)
  138. end
  139. end
  140. end
  141. end
  142. end
  143. describe "GET #show for embeddable content" do
  144. let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) }
  145. let(:extra_params) { {} }
  146. before do
  147. sign_in(user)
  148. end
  149. subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param, **extra_params }, format: :js }
  150. context 'when snippet is private' do
  151. let(:snippet_permission) { :private }
  152. it 'responds with status 404' do
  153. subject
  154. expect(response).to have_gitlab_http_status(:not_found)
  155. end
  156. end
  157. context 'when snippet is public' do
  158. let(:snippet_permission) { :public }
  159. it 'renders the blob from the repository' do
  160. subject
  161. expect(assigns(:snippet)).to eq(project_snippet)
  162. expect(assigns(:blobs).map(&:name)).to eq(project_snippet.blobs.map(&:name))
  163. expect(response).to have_gitlab_http_status(:ok)
  164. end
  165. it 'does not show the blobs expanded by default' do
  166. subject
  167. expect(assigns(:blobs).map(&:expanded?)).to be_all(false)
  168. end
  169. context 'when param expanded is set' do
  170. let(:extra_params) { { expanded: true } }
  171. it 'shows all blobs expanded' do
  172. subject
  173. expect(assigns(:blobs).map(&:expanded?)).to be_all(true)
  174. end
  175. end
  176. end
  177. context 'when the project is private' do
  178. let(:project) { create(:project_empty_repo, :private) }
  179. context 'when snippet is public' do
  180. let(:project_snippet) { create(:project_snippet, :public, project: project, author: user) }
  181. it 'responds with status 404' do
  182. subject
  183. expect(assigns(:snippet)).to eq(project_snippet)
  184. expect(response).to have_gitlab_http_status(:not_found)
  185. end
  186. end
  187. end
  188. end
  189. describe 'GET #raw' do
  190. let(:inline) { nil }
  191. let(:line_ending) { nil }
  192. let(:params) do
  193. {
  194. namespace_id: project.namespace,
  195. project_id: project,
  196. id: project_snippet.to_param,
  197. inline: inline,
  198. line_ending: line_ending
  199. }
  200. end
  201. subject { get :raw, params: params }
  202. context 'when repository is empty' do
  203. let_it_be(:content) { "first line\r\nsecond line\r\nthird line" }
  204. let_it_be(:project_snippet) do
  205. create(
  206. :project_snippet, :public, :empty_repo,
  207. project: project,
  208. author: user,
  209. content: content
  210. )
  211. end
  212. let(:formatted_content) { content.gsub(/\r\n/, "\n") }
  213. context 'CRLF line ending' do
  214. before do
  215. allow_next_instance_of(Blob) do |instance|
  216. allow(instance).to receive(:data).and_return(content)
  217. end
  218. end
  219. it 'returns LF line endings by default' do
  220. subject
  221. expect(response.body).to eq(formatted_content)
  222. end
  223. context 'when line_ending parameter present' do
  224. let(:line_ending) { :raw }
  225. it 'does not convert line endings' do
  226. subject
  227. expect(response.body).to eq(content)
  228. end
  229. end
  230. end
  231. end
  232. context 'when repository is not empty' do
  233. let_it_be(:project_snippet) do
  234. create(
  235. :project_snippet, :public, :repository,
  236. project: project,
  237. author: user
  238. )
  239. end
  240. it 'sends the blob' do
  241. subject
  242. expect(response).to have_gitlab_http_status(:ok)
  243. expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
  244. expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
  245. end
  246. it_behaves_like 'project cache control headers'
  247. it_behaves_like 'content disposition headers'
  248. end
  249. end
  250. end