PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/presenters/ci/pipeline_presenter_spec.rb

https://gitlab.com/gitlab-com/git-lab-playground
Ruby | 399 lines | 305 code | 93 blank | 1 comment | 0 complexity | 5f905d9d36e52178a676840ec14ef610 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Ci::PipelinePresenter do
  4. include Gitlab::Routing
  5. let_it_be(:user) { create(:user) }
  6. let_it_be_with_reload(:project) { create(:project, :test_repo) }
  7. let_it_be_with_reload(:pipeline) { create(:ci_pipeline, project: project) }
  8. let(:current_user) { user }
  9. subject(:presenter) do
  10. described_class.new(pipeline)
  11. end
  12. before_all do
  13. project.add_developer(user)
  14. end
  15. before do
  16. allow(presenter).to receive(:current_user) { current_user }
  17. end
  18. it 'inherits from Gitlab::View::Presenter::Delegated' do
  19. expect(described_class.superclass).to eq(Gitlab::View::Presenter::Delegated)
  20. end
  21. describe '#initialize' do
  22. it 'takes a pipeline and optional params' do
  23. expect { presenter }.not_to raise_error
  24. end
  25. it 'exposes pipeline' do
  26. expect(presenter.pipeline).to eq(pipeline)
  27. end
  28. it 'forwards missing methods to pipeline' do
  29. expect(presenter.ref).to eq(pipeline.ref)
  30. end
  31. end
  32. describe '#status_title' do
  33. context 'when pipeline is auto-canceled' do
  34. before do
  35. expect(pipeline).to receive(:auto_canceled?).and_return(true)
  36. expect(pipeline).to receive(:auto_canceled_by_id).and_return(1)
  37. end
  38. it 'shows that the pipeline is auto-canceled' do
  39. status_title = presenter.status_title
  40. expect(status_title).to include('auto-canceled')
  41. expect(status_title).to include('Pipeline #1')
  42. end
  43. end
  44. context 'when pipeline is not auto-canceled' do
  45. before do
  46. expect(pipeline).to receive(:auto_canceled?).and_return(false)
  47. end
  48. it 'does not have a status title' do
  49. expect(presenter.status_title).to be_nil
  50. end
  51. end
  52. end
  53. describe '#failure_reason' do
  54. context 'when pipeline has a failure reason' do
  55. Enums::Ci::Pipeline.failure_reasons.keys.each do |failure_reason|
  56. context "when failure reason is #{failure_reason}" do
  57. before do
  58. pipeline.failure_reason = failure_reason
  59. end
  60. it 'represents a failure reason sentence' do
  61. expect(presenter.failure_reason).to be_an_instance_of(String)
  62. expect(presenter.failure_reason).not_to eq(failure_reason.to_s)
  63. end
  64. end
  65. end
  66. end
  67. context 'when pipeline does not have failure reason' do
  68. it 'returns nil' do
  69. expect(presenter.failure_reason).to be_nil
  70. end
  71. end
  72. end
  73. describe '#name' do
  74. before do
  75. allow(pipeline).to receive(:merge_request_event_type) { event_type }
  76. end
  77. subject { presenter.name }
  78. context 'for a detached merge request pipeline' do
  79. let(:event_type) { :detached }
  80. it { is_expected.to eq('Detached merge request pipeline') }
  81. end
  82. context 'for a merged result pipeline' do
  83. let(:event_type) { :merged_result }
  84. it { is_expected.to eq('Merged result pipeline') }
  85. end
  86. context 'for a merge train pipeline' do
  87. let(:event_type) { :merge_train }
  88. it { is_expected.to eq('Merge train pipeline') }
  89. end
  90. context 'when pipeline is branch pipeline' do
  91. let(:event_type) { nil }
  92. it { is_expected.to eq('Pipeline') }
  93. end
  94. end
  95. describe '#coverage' do
  96. subject { presenter.coverage }
  97. context 'when pipeline has coverage' do
  98. before do
  99. allow(pipeline).to receive(:coverage).and_return(35.0)
  100. end
  101. it 'formats coverage into 2 decimal points' do
  102. expect(subject).to eq('35.00')
  103. end
  104. end
  105. context 'when pipeline does not have coverage' do
  106. before do
  107. allow(pipeline).to receive(:coverage).and_return(nil)
  108. end
  109. it 'returns nil' do
  110. expect(subject).to be_nil
  111. end
  112. end
  113. end
  114. describe '#ref_text' do
  115. subject { presenter.ref_text }
  116. context 'when pipeline is detached merge request pipeline' do
  117. let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
  118. let(:pipeline) { merge_request.all_pipelines.last }
  119. it 'returns a correct ref text' do
  120. is_expected.to eq("for <a class=\"mr-iid\" href=\"#{project_merge_request_path(merge_request.project, merge_request)}\">#{merge_request.to_reference}</a> " \
  121. "with <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.source_project, merge_request.source_branch)}\">#{merge_request.source_branch}</a>")
  122. end
  123. end
  124. context 'when pipeline is merge request pipeline' do
  125. let(:merge_request) { create(:merge_request, :with_merge_request_pipeline) }
  126. let(:pipeline) { merge_request.all_pipelines.last }
  127. it 'returns a correct ref text' do
  128. is_expected.to eq("for <a class=\"mr-iid\" href=\"#{project_merge_request_path(merge_request.project, merge_request)}\">#{merge_request.to_reference}</a> " \
  129. "with <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.source_project, merge_request.source_branch)}\">#{merge_request.source_branch}</a> " \
  130. "into <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.target_project, merge_request.target_branch)}\">#{merge_request.target_branch}</a>")
  131. end
  132. end
  133. context 'when pipeline is branch pipeline' do
  134. context 'when ref exists in the repository' do
  135. before do
  136. allow(pipeline).to receive(:ref_exists?) { true }
  137. end
  138. it 'returns a correct ref text' do
  139. is_expected.to eq("for <a class=\"ref-name\" href=\"#{project_commits_path(pipeline.project, pipeline.ref)}\">#{pipeline.ref}</a>")
  140. end
  141. context 'when ref contains malicious script' do
  142. let(:pipeline) { create(:ci_pipeline, ref: "<script>alter('1')</script>", project: project) }
  143. it 'does not include the malicious script' do
  144. is_expected.not_to include("<script>alter('1')</script>")
  145. end
  146. end
  147. end
  148. context 'when ref does not exist in the repository' do
  149. before do
  150. allow(pipeline).to receive(:ref_exists?) { false }
  151. end
  152. it 'returns a correct ref text' do
  153. is_expected.to eq("for <span class=\"ref-name\">#{pipeline.ref}</span>")
  154. end
  155. context 'when ref contains malicious script' do
  156. let(:pipeline) { create(:ci_pipeline, ref: "<script>alter('1')</script>", project: project) }
  157. it 'does not include the malicious script' do
  158. is_expected.not_to include("<script>alter('1')</script>")
  159. end
  160. end
  161. end
  162. end
  163. end
  164. describe '#all_related_merge_request_text' do
  165. subject { presenter.all_related_merge_request_text }
  166. let_it_be(:mr_1) { create(:merge_request) }
  167. let_it_be(:mr_2) { create(:merge_request) }
  168. context 'with zero related merge requests (branch pipeline)' do
  169. it { is_expected.to eq('No related merge requests found.') }
  170. end
  171. context 'with one related merge request' do
  172. before do
  173. allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: mr_1.id))
  174. end
  175. it {
  176. is_expected.to eq("1 related merge request: " \
  177. "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
  178. }
  179. end
  180. context 'with two related merge requests' do
  181. before do
  182. allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: [mr_1.id, mr_2.id]))
  183. end
  184. it {
  185. is_expected.to eq("2 related merge requests: " \
  186. "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_2)}\">#{mr_2.to_reference} #{mr_2.title}</a>, " \
  187. "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
  188. }
  189. context 'with a limit passed' do
  190. subject { presenter.all_related_merge_request_text(limit: 1) }
  191. it {
  192. is_expected.to eq("2 related merge requests: " \
  193. "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_2)}\">#{mr_2.to_reference} #{mr_2.title}</a>")
  194. }
  195. end
  196. end
  197. end
  198. describe '#all_related_merge_requests' do
  199. subject(:all_related_merge_requests) do
  200. presenter.send(:all_related_merge_requests)
  201. end
  202. it 'memoizes the returned relation' do
  203. expect(pipeline).to receive(:all_merge_requests_by_recency).exactly(1).time.and_call_original
  204. 2.times { presenter.send(:all_related_merge_requests).count }
  205. end
  206. context 'for a branch pipeline with two open MRs' do
  207. let!(:one) { create(:merge_request, source_project: project, source_branch: pipeline.ref) }
  208. let!(:two) { create(:merge_request, source_project: project, source_branch: pipeline.ref, target_branch: 'fix') }
  209. it { is_expected.to contain_exactly(one, two) }
  210. end
  211. context 'permissions' do
  212. let_it_be_with_refind(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
  213. let(:pipeline) { merge_request.all_pipelines.take }
  214. shared_examples 'private merge requests' do
  215. context 'when not logged in' do
  216. let(:current_user) {}
  217. it { is_expected.to be_empty }
  218. end
  219. context 'when logged in as a non_member' do
  220. let(:current_user) { create(:user) }
  221. it { is_expected.to be_empty }
  222. end
  223. context 'when logged in as a guest' do
  224. let(:current_user) { create(:user) }
  225. before do
  226. project.add_guest(current_user)
  227. end
  228. it { is_expected.to be_empty }
  229. end
  230. context 'when logged in as a developer' do
  231. it { is_expected.to contain_exactly(merge_request) }
  232. end
  233. context 'when logged in as a maintainer' do
  234. let(:current_user) { create(:user) }
  235. before do
  236. project.add_maintainer(current_user)
  237. end
  238. it { is_expected.to contain_exactly(merge_request) }
  239. end
  240. end
  241. context 'with a private project' do
  242. it_behaves_like 'private merge requests'
  243. end
  244. context 'with a public project with private merge requests' do
  245. before do
  246. project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
  247. project
  248. .project_feature
  249. .update!(merge_requests_access_level: ProjectFeature::PRIVATE)
  250. end
  251. it_behaves_like 'private merge requests'
  252. end
  253. context 'with a public project with public merge requests' do
  254. before do
  255. project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
  256. project
  257. .project_feature
  258. .update!(merge_requests_access_level: ProjectFeature::ENABLED)
  259. end
  260. context 'when not logged in' do
  261. let(:current_user) {}
  262. it { is_expected.to contain_exactly(merge_request) }
  263. end
  264. end
  265. end
  266. end
  267. describe '#link_to_merge_request' do
  268. subject { presenter.link_to_merge_request }
  269. context 'with a related merge request' do
  270. let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
  271. let(:pipeline) { merge_request.all_pipelines.take }
  272. it 'returns a correct link' do
  273. is_expected.to include(project_merge_request_path(project, merge_request))
  274. end
  275. end
  276. context 'when pipeline is branch pipeline' do
  277. it { is_expected.to be_nil }
  278. end
  279. end
  280. describe '#link_to_merge_request_source_branch' do
  281. subject { presenter.link_to_merge_request_source_branch }
  282. context 'with a related merge request' do
  283. let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
  284. let(:pipeline) { merge_request.all_pipelines.take }
  285. it 'returns a correct link' do
  286. is_expected.to include(project_commits_path(project, merge_request.source_branch))
  287. end
  288. end
  289. context 'when pipeline is branch pipeline' do
  290. it { is_expected.to be_nil }
  291. end
  292. end
  293. describe '#link_to_merge_request_target_branch' do
  294. subject { presenter.link_to_merge_request_target_branch }
  295. context 'with a related merge request' do
  296. let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
  297. let(:pipeline) { merge_request.all_pipelines.take }
  298. it 'returns a correct link' do
  299. is_expected.to include(project_commits_path(project, merge_request.target_branch))
  300. end
  301. end
  302. context 'when pipeline is branch pipeline' do
  303. it { is_expected.to be_nil }
  304. end
  305. end
  306. end