Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

repositories_controller_test.rb 12KB

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