選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

repositories_controller_test.rb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 File.expand_path('../../test_helper', __FILE__)
  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. if repository_configured?('subversion')
  175. def test_show_should_show_diff_button_depending_on_browse_repository_permission
  176. @request.session[:user_id] = 2
  177. role = Role.find(1)
  178. role.add_permission! :browse_repository
  179. get(:show, :params => {:id => 1})
  180. assert_response :success
  181. assert_select 'input[value="View differences"]'
  182. role.remove_permission! :browse_repository
  183. get(:show, :params => {:id => 1})
  184. assert_response :success
  185. assert_select 'input[value="View differences"]', :count => 0
  186. end
  187. end
  188. def test_revisions
  189. get(
  190. :revisions,
  191. :params => {
  192. :id => 1,
  193. :repository_id => 10
  194. }
  195. )
  196. assert_response :success
  197. assert_select 'table.changesets'
  198. end
  199. def test_revisions_for_other_repository
  200. repository = Repository::Subversion.create!(:project_id => 1, :identifier => 'foo', :url => 'file:///foo')
  201. get(
  202. :revisions,
  203. :params => {
  204. :id => 1,
  205. :repository_id => 'foo'
  206. }
  207. )
  208. assert_response :success
  209. assert_select 'table.changesets'
  210. end
  211. def test_revisions_for_invalid_repository
  212. get(
  213. :revisions,
  214. :params => {
  215. :id => 1,
  216. :repository_id => 'foo'
  217. }
  218. )
  219. assert_response 404
  220. end
  221. def test_revision
  222. get(
  223. :revision,
  224. :params => {
  225. :id => 1,
  226. :repository_id => 10,
  227. :rev => 1
  228. }
  229. )
  230. assert_response :success
  231. assert_select 'h2', :text => 'Revision 1'
  232. end
  233. def test_revision_should_not_format_comments_when_disabled
  234. Changeset.where(:id => 100).update_all(:comments => 'Simple *text*')
  235. with_settings :commit_logs_formatting => '0' do
  236. get(
  237. :revision,
  238. :params => {
  239. :id => 1,
  240. :repository_id => 10,
  241. :rev => 1
  242. }
  243. )
  244. assert_response :success
  245. assert_select '.changeset-comments', :text => 'Simple *text*'
  246. end
  247. end
  248. def test_revision_should_show_add_related_issue_form
  249. Role.find(1).add_permission! :manage_related_issues
  250. @request.session[:user_id] = 2
  251. get(
  252. :revision,
  253. :params => {
  254. :id => 1,
  255. :repository_id => 10,
  256. :rev => 1
  257. }
  258. )
  259. assert_response :success
  260. assert_select 'form[action=?]', '/projects/ecookbook/repository/10/revisions/1/issues' do
  261. assert_select 'input[name=?]', 'issue_id'
  262. end
  263. end
  264. def test_revision_should_not_change_the_project_menu_link
  265. get(
  266. :revision,
  267. :params => {
  268. :id => 1,
  269. :repository_id => 10,
  270. :rev => 1
  271. }
  272. )
  273. assert_response :success
  274. assert_select '#main-menu a.repository[href=?]', '/projects/ecookbook/repository'
  275. end
  276. def test_revision_with_before_nil_and_afer_normal
  277. get(
  278. :revision,
  279. :params => {
  280. :id => 1,
  281. :repository_id => 10,
  282. :rev => 1
  283. }
  284. )
  285. assert_response :success
  286. assert_select 'div.contextual' do
  287. assert_select 'a[href=?]', '/projects/ecookbook/repository/10/revisions/0', 0
  288. assert_select 'a[href=?]', '/projects/ecookbook/repository/10/revisions/2'
  289. end
  290. end
  291. def test_add_related_issue
  292. @request.session[:user_id] = 2
  293. assert_difference 'Changeset.find(103).issues.size' do
  294. post(
  295. :add_related_issue,
  296. :params => {
  297. :id => 1,
  298. :repository_id => 10,
  299. :rev => 4,
  300. :issue_id => 2,
  301. :format => 'js'
  302. },
  303. :xhr => true
  304. )
  305. assert_response :success
  306. assert_equal 'text/javascript', response.media_type
  307. end
  308. assert_equal [2], Changeset.find(103).issue_ids
  309. assert_include 'related-issues', response.body
  310. assert_include 'Feature request #2', response.body
  311. end
  312. def test_add_related_issue_should_accept_issue_id_with_sharp
  313. @request.session[:user_id] = 2
  314. assert_difference 'Changeset.find(103).issues.size' do
  315. post(
  316. :add_related_issue,
  317. :params => {
  318. :id => 1,
  319. :repository_id => 10,
  320. :rev => 4,
  321. :issue_id => "#2",
  322. :format => 'js'
  323. },
  324. :xhr => true
  325. )
  326. end
  327. assert_equal [2], Changeset.find(103).issue_ids
  328. end
  329. def test_add_related_issue_with_invalid_issue_id
  330. @request.session[:user_id] = 2
  331. assert_no_difference 'Changeset.find(103).issues.size' do
  332. post(
  333. :add_related_issue,
  334. :params => {
  335. :id => 1,
  336. :repository_id => 10,
  337. :rev => 4,
  338. :issue_id => 9999,
  339. :format => 'js'
  340. },
  341. :xhr => true
  342. )
  343. assert_response :success
  344. assert_equal 'text/javascript', response.media_type
  345. end
  346. assert_include 'alert("Issue is invalid")', response.body
  347. end
  348. def test_remove_related_issue
  349. Changeset.find(103).issues << Issue.find(1)
  350. Changeset.find(103).issues << Issue.find(2)
  351. @request.session[:user_id] = 2
  352. assert_difference 'Changeset.find(103).issues.size', -1 do
  353. delete(
  354. :remove_related_issue,
  355. :params => {
  356. :id => 1,
  357. :repository_id => 10,
  358. :rev => 4,
  359. :issue_id => 2,
  360. :format => 'js'
  361. },
  362. :xhr => true
  363. )
  364. assert_response :success
  365. assert_equal 'text/javascript', response.media_type
  366. end
  367. assert_equal [1], Changeset.find(103).issue_ids
  368. assert_include 'related-issue-2', response.body
  369. end
  370. def test_graph_commits_per_month
  371. # Make sure there's some data to display
  372. latest = Project.find(1).repository.changesets.maximum(:commit_date)
  373. assert_not_nil latest
  374. Date.stubs(:today).returns(latest.to_date + 10)
  375. get(
  376. :graph,
  377. :params => {
  378. :id => 1,
  379. :repository_id => 10,
  380. :graph => 'commits_per_month'
  381. }
  382. )
  383. assert_response :success
  384. assert_equal 'application/json', response.media_type
  385. data = ActiveSupport::JSON.decode(response.body)
  386. assert_not_nil data['labels']
  387. assert_not_nil data['commits']
  388. assert_not_nil data['changes']
  389. end
  390. def test_graph_commits_per_author
  391. get(
  392. :graph,
  393. :params => {
  394. :id => 1,
  395. :repository_id => 10,
  396. :graph => 'commits_per_author'
  397. }
  398. )
  399. assert_response :success
  400. assert_equal 'application/json', response.media_type
  401. data = ActiveSupport::JSON.decode(response.body)
  402. assert_not_nil data['labels']
  403. assert_not_nil data['commits']
  404. assert_not_nil data['changes']
  405. end
  406. def test_get_committers
  407. @request.session[:user_id] = 2
  408. # add a commit with an unknown user
  409. Changeset.
  410. create!(
  411. :repository => Project.find(1).repository,
  412. :committer => 'foo',
  413. :committed_on => Time.now,
  414. :revision => 100,
  415. :comments => 'Committed by foo.'
  416. )
  417. get(:committers, :params => {:id => 10})
  418. assert_response :success
  419. assert_select 'input[value=dlopper] + select option[value="3"][selected=selected]', :text => 'Dave Lopper'
  420. assert_select 'input[value=foo] + select option[selected=selected]', 0 # no option selected
  421. end
  422. def test_get_committers_without_changesets
  423. Changeset.delete_all
  424. @request.session[:user_id] = 2
  425. get(:committers, :params => {:id => 10})
  426. assert_response :success
  427. end
  428. def test_post_committers
  429. @request.session[:user_id] = 2
  430. # add a commit with an unknown user
  431. c = Changeset.
  432. create!(
  433. :repository => Project.find(1).repository,
  434. :committer => 'foo',
  435. :committed_on => Time.now,
  436. :revision => 100,
  437. :comments => 'Committed by foo.'
  438. )
  439. assert_no_difference "Changeset.where(:user_id => 3).count" do
  440. post(
  441. :committers,
  442. :params => {
  443. :id => 10,
  444. :committers => {
  445. '0' => ['foo', '2'], '1' => ['dlopper', '3']
  446. }
  447. }
  448. )
  449. assert_response 302
  450. assert_equal User.find(2), c.reload.user
  451. end
  452. end
  453. end