You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repositories_controller_test.rb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class RepositoriesControllerTest < Redmine::RepositoryControllerTest
  20. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :enabled_modules,
  21. :repositories, :issues, :issue_statuses, :changesets, :changes,
  22. :issue_categories, :enumerations, :custom_fields, :custom_values, :trackers
  23. def setup
  24. super
  25. User.current = nil
  26. end
  27. def test_new
  28. @request.session[:user_id] = 1
  29. get(
  30. :new,
  31. :params => {
  32. :project_id => 'subproject1'
  33. }
  34. )
  35. assert_response :success
  36. assert_select 'select[name=?]', 'repository_scm' do
  37. assert_select 'option[value=?][selected=selected]', 'Subversion'
  38. end
  39. assert_select 'input[name=?]:not([disabled])', 'repository[url]'
  40. end
  41. def test_new_should_propose_enabled_scm_only
  42. @request.session[:user_id] = 1
  43. with_settings :enabled_scm => ['Mercurial', 'Git'] do
  44. get(
  45. :new,
  46. :params => {
  47. :project_id => 'subproject1'
  48. }
  49. )
  50. end
  51. assert_response :success
  52. assert_select 'select[name=repository_scm]' do
  53. assert_select 'option', 3
  54. assert_select 'option[value=Mercurial][selected=selected]'
  55. assert_select 'option[value=Git]:not([selected])'
  56. end
  57. end
  58. def test_get_new_with_type
  59. @request.session[:user_id] = 1
  60. get(
  61. :new,
  62. :params => {
  63. :project_id => 'subproject1',
  64. :repository_scm => 'Git'
  65. }
  66. )
  67. assert_response :success
  68. assert_select 'select[name=?]', 'repository_scm' do
  69. assert_select 'option[value=?][selected=selected]', 'Git'
  70. end
  71. end
  72. def test_create
  73. @request.session[:user_id] = 1
  74. assert_difference 'Repository.count' do
  75. post(
  76. :create,
  77. :params => {
  78. :project_id => 'subproject1',
  79. :repository_scm => 'Subversion',
  80. :repository => {
  81. :url => 'file:///test',
  82. :is_default => '1',
  83. :identifier => ''
  84. }
  85. }
  86. )
  87. end
  88. assert_response 302
  89. repository = Repository.order('id DESC').first
  90. assert_kind_of Repository::Subversion, repository
  91. assert_equal 'file:///test', repository.url
  92. end
  93. def test_create_with_failure
  94. @request.session[:user_id] = 1
  95. assert_no_difference 'Repository.count' do
  96. post(
  97. :create,
  98. :params => {
  99. :project_id => 'subproject1',
  100. :repository_scm => 'Subversion',
  101. :repository => {
  102. :url => 'invalid'
  103. }
  104. }
  105. )
  106. end
  107. assert_response :success
  108. assert_select_error /URL is invalid/
  109. assert_select 'select[name=?]', 'repository_scm' do
  110. assert_select 'option[value=?][selected=selected]', 'Subversion'
  111. end
  112. end
  113. def test_edit
  114. @request.session[:user_id] = 1
  115. get(:edit, :params => {:id => 11})
  116. assert_response :success
  117. assert_select 'input[name=?][value=?][disabled=disabled]', 'repository[url]', 'svn://localhost/test'
  118. end
  119. def test_update
  120. @request.session[:user_id] = 1
  121. put(
  122. :update,
  123. :params => {
  124. :id => 11,
  125. :repository => {
  126. :password => 'test_update'
  127. }
  128. }
  129. )
  130. assert_response 302
  131. assert_equal 'test_update', Repository.find(11).password
  132. end
  133. def test_update_with_failure
  134. @request.session[:user_id] = 1
  135. put(
  136. :update,
  137. :params => {
  138. :id => 11,
  139. :repository => {
  140. :password => 'x'*260
  141. }
  142. }
  143. )
  144. assert_response :success
  145. assert_select_error /Password is too long/
  146. end
  147. def test_destroy
  148. @request.session[:user_id] = 1
  149. assert_difference 'Repository.count', -1 do
  150. delete(:destroy, :params => {:id => 11})
  151. end
  152. assert_response 302
  153. assert_nil Repository.find_by_id(11)
  154. end
  155. def test_show_with_autofetch_changesets_enabled_should_fetch_changesets
  156. Repository::Subversion.any_instance.expects(:fetch_changesets).once
  157. with_settings :autofetch_changesets => '1' do
  158. get(:show, :params => {:id => 1})
  159. end
  160. end
  161. def test_show_with_autofetch_changesets_disabled_should_not_fetch_changesets
  162. Repository::Subversion.any_instance.expects(:fetch_changesets).never
  163. with_settings :autofetch_changesets => '0' do
  164. get(:show, :params => {:id => 1})
  165. end
  166. end
  167. def test_show_with_closed_project_should_not_fetch_changesets
  168. Repository::Subversion.any_instance.expects(:fetch_changesets).never
  169. Project.find(1).close
  170. with_settings :autofetch_changesets => '1' do
  171. get(:show, :params => {:id => 1})
  172. end
  173. end
  174. def test_show_without_main_repository_should_display_first_repository
  175. skip unless repository_configured?('subversion')
  176. project = Project.find(1)
  177. repos = project.repositories
  178. repos << Repository::Subversion.create(:identifier => 'test', :url => 'svn://valid')
  179. assert_equal true, repos.exists?(:is_default => true)
  180. repos.update_all(:is_default => false)
  181. repos.reload
  182. assert_equal false, repos.exists?(:is_default => true)
  183. repository = repos.sort.first # rubocop:disable Style/RedundantSort
  184. @request.session[:user_id] = 2
  185. get(:show, :params => {:id => 1})
  186. assert_response :success
  187. assert_select '#sidebar' do
  188. assert_select 'a.repository.selected[href=?]', "/projects/#{project.identifier}/repository/#{repository.identifier_param}"
  189. end
  190. end
  191. def test_show_should_show_diff_button_depending_on_browse_repository_permission
  192. skip unless repository_configured?('subversion')
  193. @request.session[:user_id] = 2
  194. role = Role.find(1)
  195. role.add_permission! :browse_repository
  196. get(:show, :params => {:id => 1})
  197. assert_response :success
  198. assert_select 'input[value="View differences"]'
  199. role.remove_permission! :browse_repository
  200. get(:show, :params => {:id => 1})
  201. assert_response :success
  202. assert_select 'input[value="View differences"]', :count => 0
  203. end
  204. def test_fetch_changesets
  205. skip unless repository_configured?('subversion')
  206. @request.session[:user_id] = 2
  207. role = Role.find(1)
  208. with_settings :autofetch_changesets => '0' do
  209. role.add_permission! :manage_repository
  210. Repository::Subversion.any_instance.expects(:fetch_changesets).once
  211. post(:fetch_changesets, :params => {:id => 1, :repository_id => 10})
  212. assert_response :success
  213. role.remove_permission! :manage_repository
  214. Repository::Subversion.any_instance.expects(:fetch_changesets).never
  215. post(:fetch_changesets, :params => {:id => 1, :repository_id => 10})
  216. assert_response :forbidden
  217. end
  218. end
  219. def test_revisions
  220. get(
  221. :revisions,
  222. :params => {
  223. :id => 1,
  224. :repository_id => 10
  225. }
  226. )
  227. assert_response :success
  228. assert_select 'table.changesets'
  229. end
  230. def test_revisions_for_other_repository
  231. repository = Repository::Subversion.create!(:project_id => 1, :identifier => 'foo', :url => 'file:///foo')
  232. get(
  233. :revisions,
  234. :params => {
  235. :id => 1,
  236. :repository_id => 'foo'
  237. }
  238. )
  239. assert_response :success
  240. assert_select 'table.changesets'
  241. end
  242. def test_revisions_for_invalid_repository
  243. get(
  244. :revisions,
  245. :params => {
  246. :id => 1,
  247. :repository_id => 'foo'
  248. }
  249. )
  250. assert_response 404
  251. end
  252. def test_revision
  253. get(
  254. :revision,
  255. :params => {
  256. :id => 1,
  257. :repository_id => 10,
  258. :rev => 1
  259. }
  260. )
  261. assert_response :success
  262. assert_select 'h2', :text => 'Revision 1'
  263. end
  264. def test_revision_should_not_format_comments_when_disabled
  265. Changeset.where(:id => 100).update_all(:comments => 'Simple *text*')
  266. with_settings :commit_logs_formatting => '0' do
  267. get(
  268. :revision,
  269. :params => {
  270. :id => 1,
  271. :repository_id => 10,
  272. :rev => 1
  273. }
  274. )
  275. assert_response :success
  276. assert_select '.changeset-comments', :text => 'Simple *text*'
  277. end
  278. end
  279. def test_revision_should_show_add_related_issue_form
  280. Role.find(1).add_permission! :manage_related_issues
  281. @request.session[:user_id] = 2
  282. get(
  283. :revision,
  284. :params => {
  285. :id => 1,
  286. :repository_id => 10,
  287. :rev => 1
  288. }
  289. )
  290. assert_response :success
  291. assert_select 'form[action=?]', '/projects/ecookbook/repository/10/revisions/1/issues' do
  292. assert_select 'input[name=?]', 'issue_id'
  293. end
  294. end
  295. def test_revision_should_not_change_the_project_menu_link
  296. get(
  297. :revision,
  298. :params => {
  299. :id => 1,
  300. :repository_id => 10,
  301. :rev => 1
  302. }
  303. )
  304. assert_response :success
  305. assert_select '#main-menu a.repository[href=?]', '/projects/ecookbook/repository'
  306. end
  307. def test_revision_with_before_nil_and_afer_normal
  308. get(
  309. :revision,
  310. :params => {
  311. :id => 1,
  312. :repository_id => 10,
  313. :rev => 1
  314. }
  315. )
  316. assert_response :success
  317. assert_select 'div.contextual' do
  318. assert_select 'a[href=?]', '/projects/ecookbook/repository/10/revisions/0', 0
  319. assert_select 'a[href=?]', '/projects/ecookbook/repository/10/revisions/2'
  320. end
  321. end
  322. def test_add_related_issue
  323. @request.session[:user_id] = 2
  324. assert_difference 'Changeset.find(103).issues.size' do
  325. post(
  326. :add_related_issue,
  327. :params => {
  328. :id => 1,
  329. :repository_id => 10,
  330. :rev => 4,
  331. :issue_id => 2,
  332. :format => 'js'
  333. },
  334. :xhr => true
  335. )
  336. assert_response :success
  337. assert_equal 'text/javascript', response.media_type
  338. end
  339. assert_equal [2], Changeset.find(103).issue_ids
  340. assert_include 'related-issues', response.body
  341. assert_include 'Feature request #2', response.body
  342. end
  343. def test_add_related_issue_should_accept_issue_id_with_sharp
  344. @request.session[:user_id] = 2
  345. assert_difference 'Changeset.find(103).issues.size' do
  346. post(
  347. :add_related_issue,
  348. :params => {
  349. :id => 1,
  350. :repository_id => 10,
  351. :rev => 4,
  352. :issue_id => "#2",
  353. :format => 'js'
  354. },
  355. :xhr => true
  356. )
  357. end
  358. assert_equal [2], Changeset.find(103).issue_ids
  359. end
  360. def test_add_related_issue_with_invalid_issue_id
  361. @request.session[:user_id] = 2
  362. assert_no_difference 'Changeset.find(103).issues.size' do
  363. post(
  364. :add_related_issue,
  365. :params => {
  366. :id => 1,
  367. :repository_id => 10,
  368. :rev => 4,
  369. :issue_id => 9999,
  370. :format => 'js'
  371. },
  372. :xhr => true
  373. )
  374. assert_response :success
  375. assert_equal 'text/javascript', response.media_type
  376. end
  377. assert_include 'alert("Issue is invalid")', response.body
  378. end
  379. def test_remove_related_issue
  380. Changeset.find(103).issues << Issue.find(1)
  381. Changeset.find(103).issues << Issue.find(2)
  382. @request.session[:user_id] = 2
  383. assert_difference 'Changeset.find(103).issues.size', -1 do
  384. delete(
  385. :remove_related_issue,
  386. :params => {
  387. :id => 1,
  388. :repository_id => 10,
  389. :rev => 4,
  390. :issue_id => 2,
  391. :format => 'js'
  392. },
  393. :xhr => true
  394. )
  395. assert_response :success
  396. assert_equal 'text/javascript', response.media_type
  397. end
  398. assert_equal [1], Changeset.find(103).issue_ids
  399. assert_include 'related-issue-2', response.body
  400. end
  401. def test_graph_commits_per_month
  402. # Make sure there's some data to display
  403. latest = Project.find(1).repository.changesets.maximum(:commit_date)
  404. assert_not_nil latest
  405. Date.stubs(:today).returns(latest.to_date + 10)
  406. get(
  407. :graph,
  408. :params => {
  409. :id => 1,
  410. :repository_id => 10,
  411. :graph => 'commits_per_month'
  412. }
  413. )
  414. assert_response :success
  415. assert_equal 'application/json', response.media_type
  416. data = ActiveSupport::JSON.decode(response.body)
  417. assert_not_nil data['labels']
  418. assert_not_nil data['commits']
  419. assert_not_nil data['changes']
  420. end
  421. def test_graph_commits_per_author
  422. get(
  423. :graph,
  424. :params => {
  425. :id => 1,
  426. :repository_id => 10,
  427. :graph => 'commits_per_author'
  428. }
  429. )
  430. assert_response :success
  431. assert_equal 'application/json', response.media_type
  432. data = ActiveSupport::JSON.decode(response.body)
  433. assert_not_nil data['labels']
  434. assert_not_nil data['commits']
  435. assert_not_nil data['changes']
  436. end
  437. def test_get_committers
  438. @request.session[:user_id] = 2
  439. # add a commit with an unknown user
  440. Changeset.
  441. create!(
  442. :repository => Project.find(1).repository,
  443. :committer => 'foo',
  444. :committed_on => Time.now,
  445. :revision => 100,
  446. :comments => 'Committed by foo.'
  447. )
  448. get(:committers, :params => {:id => 10})
  449. assert_response :success
  450. assert_select 'input[value=dlopper] + select option[value="3"][selected=selected]', :text => 'Dave Lopper'
  451. assert_select 'input[value=foo] + select option[selected=selected]', 0 # no option selected
  452. end
  453. def test_get_committers_without_changesets
  454. Changeset.delete_all
  455. @request.session[:user_id] = 2
  456. get(:committers, :params => {:id => 10})
  457. assert_response :success
  458. end
  459. def test_post_committers
  460. @request.session[:user_id] = 2
  461. # add a commit with an unknown user
  462. c = Changeset.
  463. create!(
  464. :repository => Project.find(1).repository,
  465. :committer => 'foo',
  466. :committed_on => Time.now,
  467. :revision => 100,
  468. :comments => 'Committed by foo.'
  469. )
  470. assert_no_difference "Changeset.where(:user_id => 3).count" do
  471. post(
  472. :committers,
  473. :params => {
  474. :id => 10,
  475. :committers => {
  476. '0' => ['foo', '2'], '1' => ['dlopper', '3']
  477. }
  478. }
  479. )
  480. assert_response 302
  481. assert_equal User.find(2), c.reload.user
  482. end
  483. end
  484. end