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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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 < ActionController::TestCase
  19. fixtures :projects, :users, :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. User.current = nil
  24. end
  25. def test_new
  26. @request.session[:user_id] = 1
  27. get :new, :project_id => 'subproject1'
  28. assert_response :success
  29. assert_template 'new'
  30. assert_kind_of Repository::Subversion, assigns(:repository)
  31. assert assigns(:repository).new_record?
  32. assert_tag 'input', :attributes => {:name => 'repository[url]', :disabled => nil}
  33. end
  34. def test_new_should_propose_enabled_scm_only
  35. @request.session[:user_id] = 1
  36. with_settings :enabled_scm => ['Mercurial', 'Git'] do
  37. get :new, :project_id => 'subproject1'
  38. end
  39. assert_response :success
  40. assert_template 'new'
  41. assert_kind_of Repository::Mercurial, assigns(:repository)
  42. assert_tag 'select', :attributes => {:name => 'repository_scm'},
  43. :children => {:count => 3}
  44. assert_tag 'select', :attributes => {:name => 'repository_scm'},
  45. :child => {:tag => 'option', :attributes => {:value => 'Mercurial', :selected => 'selected'}}
  46. assert_tag 'select', :attributes => {:name => 'repository_scm'},
  47. :child => {:tag => 'option', :attributes => {:value => 'Git', :selected => nil}}
  48. end
  49. def test_create
  50. @request.session[:user_id] = 1
  51. assert_difference 'Repository.count' do
  52. post :create, :project_id => 'subproject1',
  53. :repository_scm => 'Subversion',
  54. :repository => {:url => 'file:///test', :is_default => '1', :identifier => ''}
  55. end
  56. assert_response 302
  57. repository = Repository.first(:order => 'id DESC')
  58. assert_kind_of Repository::Subversion, repository
  59. assert_equal 'file:///test', repository.url
  60. end
  61. def test_create_with_failure
  62. @request.session[:user_id] = 1
  63. assert_no_difference 'Repository.count' do
  64. post :create, :project_id => 'subproject1',
  65. :repository_scm => 'Subversion',
  66. :repository => {:url => 'invalid'}
  67. end
  68. assert_response :success
  69. assert_template 'new'
  70. assert_kind_of Repository::Subversion, assigns(:repository)
  71. assert assigns(:repository).new_record?
  72. end
  73. def test_edit
  74. @request.session[:user_id] = 1
  75. get :edit, :id => 11
  76. assert_response :success
  77. assert_template 'edit'
  78. assert_equal Repository.find(11), assigns(:repository)
  79. assert_tag 'input', :attributes => {:name => 'repository[url]', :value => 'svn://localhost/test', :disabled => 'disabled'}
  80. end
  81. def test_update
  82. @request.session[:user_id] = 1
  83. put :update, :id => 11, :repository => {:password => 'test_update'}
  84. assert_response 302
  85. assert_equal 'test_update', Repository.find(11).password
  86. end
  87. def test_update_with_failure
  88. @request.session[:user_id] = 1
  89. put :update, :id => 11, :repository => {:password => 'x'*260}
  90. assert_response :success
  91. assert_template 'edit'
  92. assert_equal Repository.find(11), assigns(:repository)
  93. end
  94. def test_destroy
  95. @request.session[:user_id] = 1
  96. assert_difference 'Repository.count', -1 do
  97. delete :destroy, :id => 11
  98. end
  99. assert_response 302
  100. assert_nil Repository.find_by_id(11)
  101. end
  102. def test_show_with_autofetch_changesets_enabled_should_fetch_changesets
  103. Repository::Subversion.any_instance.expects(:fetch_changesets).once
  104. with_settings :autofetch_changesets => '1' do
  105. get :show, :id => 1
  106. end
  107. end
  108. def test_show_with_autofetch_changesets_disabled_should_not_fetch_changesets
  109. Repository::Subversion.any_instance.expects(:fetch_changesets).never
  110. with_settings :autofetch_changesets => '0' do
  111. get :show, :id => 1
  112. end
  113. end
  114. def test_show_with_closed_project_should_not_fetch_changesets
  115. Repository::Subversion.any_instance.expects(:fetch_changesets).never
  116. Project.find(1).close
  117. with_settings :autofetch_changesets => '1' do
  118. get :show, :id => 1
  119. end
  120. end
  121. def test_revisions
  122. get :revisions, :id => 1
  123. assert_response :success
  124. assert_template 'revisions'
  125. assert_equal Repository.find(10), assigns(:repository)
  126. assert_not_nil assigns(:changesets)
  127. end
  128. def test_revisions_for_other_repository
  129. repository = Repository::Subversion.create!(:project_id => 1, :identifier => 'foo', :url => 'file:///foo')
  130. get :revisions, :id => 1, :repository_id => 'foo'
  131. assert_response :success
  132. assert_template 'revisions'
  133. assert_equal repository, assigns(:repository)
  134. assert_not_nil assigns(:changesets)
  135. end
  136. def test_revisions_for_invalid_repository
  137. get :revisions, :id => 1, :repository_id => 'foo'
  138. assert_response 404
  139. end
  140. def test_revision
  141. get :revision, :id => 1, :rev => 1
  142. assert_response :success
  143. assert_not_nil assigns(:changeset)
  144. assert_equal "1", assigns(:changeset).revision
  145. end
  146. def test_revision_should_not_change_the_project_menu_link
  147. get :revision, :id => 1, :rev => 1
  148. assert_response :success
  149. assert_tag 'a', :attributes => {:href => '/projects/ecookbook/repository', :class => /repository/},
  150. :ancestor => {:attributes => {:id => 'main-menu'}}
  151. end
  152. def test_revision_with_before_nil_and_afer_normal
  153. get :revision, {:id => 1, :rev => 1}
  154. assert_response :success
  155. assert_template 'revision'
  156. assert_no_tag :tag => "div", :attributes => { :class => "contextual" },
  157. :child => { :tag => "a", :attributes => { :href => '/projects/ecookbook/repository/revisions/0'}
  158. }
  159. assert_tag :tag => "div", :attributes => { :class => "contextual" },
  160. :child => { :tag => "a", :attributes => { :href => '/projects/ecookbook/repository/revisions/2'}
  161. }
  162. end
  163. def test_add_related_issue
  164. @request.session[:user_id] = 2
  165. assert_difference 'Changeset.find(103).issues.size' do
  166. xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js'
  167. assert_response :success
  168. assert_template 'add_related_issue'
  169. assert_equal 'text/javascript', response.content_type
  170. end
  171. assert_equal [2], Changeset.find(103).issue_ids
  172. assert_include 'related-issues', response.body
  173. assert_include 'Feature request #2', response.body
  174. end
  175. def test_add_related_issue_should_accept_issue_id_with_sharp
  176. @request.session[:user_id] = 2
  177. assert_difference 'Changeset.find(103).issues.size' do
  178. xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => "#2", :format => 'js'
  179. end
  180. assert_equal [2], Changeset.find(103).issue_ids
  181. end
  182. def test_add_related_issue_with_invalid_issue_id
  183. @request.session[:user_id] = 2
  184. assert_no_difference 'Changeset.find(103).issues.size' do
  185. xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 9999, :format => 'js'
  186. assert_response :success
  187. assert_template 'add_related_issue'
  188. assert_equal 'text/javascript', response.content_type
  189. end
  190. assert_include 'alert("Issue is invalid")', response.body
  191. end
  192. def test_remove_related_issue
  193. Changeset.find(103).issues << Issue.find(1)
  194. Changeset.find(103).issues << Issue.find(2)
  195. @request.session[:user_id] = 2
  196. assert_difference 'Changeset.find(103).issues.size', -1 do
  197. xhr :delete, :remove_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js'
  198. assert_response :success
  199. assert_template 'remove_related_issue'
  200. assert_equal 'text/javascript', response.content_type
  201. end
  202. assert_equal [1], Changeset.find(103).issue_ids
  203. assert_include 'related-issue-2', response.body
  204. end
  205. def test_graph_commits_per_month
  206. # Make sure there's some data to display
  207. latest = Project.find(1).repository.changesets.maximum(:commit_date)
  208. assert_not_nil latest
  209. Date.stubs(:today).returns(latest.to_date + 10)
  210. get :graph, :id => 1, :graph => 'commits_per_month'
  211. assert_response :success
  212. assert_equal 'image/svg+xml', @response.content_type
  213. end
  214. def test_graph_commits_per_author
  215. get :graph, :id => 1, :graph => 'commits_per_author'
  216. assert_response :success
  217. assert_equal 'image/svg+xml', @response.content_type
  218. end
  219. def test_get_committers
  220. @request.session[:user_id] = 2
  221. # add a commit with an unknown user
  222. Changeset.create!(
  223. :repository => Project.find(1).repository,
  224. :committer => 'foo',
  225. :committed_on => Time.now,
  226. :revision => 100,
  227. :comments => 'Committed by foo.'
  228. )
  229. get :committers, :id => 10
  230. assert_response :success
  231. assert_template 'committers'
  232. assert_tag :td, :content => 'dlopper',
  233. :sibling => { :tag => 'td',
  234. :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} },
  235. :child => { :tag => 'option', :content => 'Dave Lopper',
  236. :attributes => { :value => '3', :selected => 'selected' }}}}
  237. assert_tag :td, :content => 'foo',
  238. :sibling => { :tag => 'td',
  239. :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} }}}
  240. assert_no_tag :td, :content => 'foo',
  241. :sibling => { :tag => 'td',
  242. :descendant => { :tag => 'option', :attributes => { :selected => 'selected' }}}
  243. end
  244. def test_post_committers
  245. @request.session[:user_id] = 2
  246. # add a commit with an unknown user
  247. c = Changeset.create!(
  248. :repository => Project.find(1).repository,
  249. :committer => 'foo',
  250. :committed_on => Time.now,
  251. :revision => 100,
  252. :comments => 'Committed by foo.'
  253. )
  254. assert_no_difference "Changeset.where(:user_id => 3).count" do
  255. post :committers, :id => 10, :committers => { '0' => ['foo', '2'], '1' => ['dlopper', '3']}
  256. assert_response 302
  257. assert_equal User.find(2), c.reload.user
  258. end
  259. end
  260. end