Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require File.expand_path('../../test_helper', __FILE__) Chris@1494: Chris@1494: class RepositoriesControllerTest < ActionController::TestCase Chris@1494: fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, Chris@1494: :repositories, :issues, :issue_statuses, :changesets, :changes, Chris@1494: :issue_categories, :enumerations, :custom_fields, :custom_values, :trackers Chris@1494: Chris@1494: def setup Chris@1494: User.current = nil Chris@1494: end Chris@1494: Chris@1494: def test_new Chris@1494: @request.session[:user_id] = 1 Chris@1494: get :new, :project_id => 'subproject1' Chris@1494: assert_response :success Chris@1494: assert_template 'new' Chris@1494: assert_kind_of Repository::Subversion, assigns(:repository) Chris@1494: assert assigns(:repository).new_record? Chris@1494: assert_tag 'input', :attributes => {:name => 'repository[url]', :disabled => nil} Chris@1494: end Chris@1494: Chris@1494: def test_new_should_propose_enabled_scm_only Chris@1494: @request.session[:user_id] = 1 Chris@1494: with_settings :enabled_scm => ['Mercurial', 'Git'] do Chris@1494: get :new, :project_id => 'subproject1' Chris@1494: end Chris@1494: assert_response :success Chris@1494: assert_template 'new' Chris@1494: assert_kind_of Repository::Mercurial, assigns(:repository) Chris@1494: assert_tag 'select', :attributes => {:name => 'repository_scm'}, Chris@1494: :children => {:count => 3} Chris@1494: assert_tag 'select', :attributes => {:name => 'repository_scm'}, Chris@1494: :child => {:tag => 'option', :attributes => {:value => 'Mercurial', :selected => 'selected'}} Chris@1494: assert_tag 'select', :attributes => {:name => 'repository_scm'}, Chris@1494: :child => {:tag => 'option', :attributes => {:value => 'Git', :selected => nil}} Chris@1494: end Chris@1494: Chris@1494: def test_create Chris@1494: @request.session[:user_id] = 1 Chris@1494: assert_difference 'Repository.count' do Chris@1494: post :create, :project_id => 'subproject1', Chris@1494: :repository_scm => 'Subversion', Chris@1494: :repository => {:url => 'file:///test', :is_default => '1', :identifier => ''} Chris@1494: end Chris@1494: assert_response 302 Chris@1494: repository = Repository.first(:order => 'id DESC') Chris@1494: assert_kind_of Repository::Subversion, repository Chris@1494: assert_equal 'file:///test', repository.url Chris@1494: end Chris@1494: Chris@1494: def test_create_with_failure Chris@1494: @request.session[:user_id] = 1 Chris@1494: assert_no_difference 'Repository.count' do Chris@1494: post :create, :project_id => 'subproject1', Chris@1494: :repository_scm => 'Subversion', Chris@1494: :repository => {:url => 'invalid'} Chris@1494: end Chris@1494: assert_response :success Chris@1494: assert_template 'new' Chris@1494: assert_kind_of Repository::Subversion, assigns(:repository) Chris@1494: assert assigns(:repository).new_record? Chris@1494: end Chris@1494: Chris@1494: def test_edit Chris@1494: @request.session[:user_id] = 1 Chris@1494: get :edit, :id => 11 Chris@1494: assert_response :success Chris@1494: assert_template 'edit' Chris@1494: assert_equal Repository.find(11), assigns(:repository) Chris@1494: assert_tag 'input', :attributes => {:name => 'repository[url]', :value => 'svn://localhost/test', :disabled => 'disabled'} Chris@1494: end Chris@1494: Chris@1494: def test_update Chris@1494: @request.session[:user_id] = 1 Chris@1494: put :update, :id => 11, :repository => {:password => 'test_update'} Chris@1494: assert_response 302 Chris@1494: assert_equal 'test_update', Repository.find(11).password Chris@1494: end Chris@1494: Chris@1494: def test_update_with_failure Chris@1494: @request.session[:user_id] = 1 Chris@1494: put :update, :id => 11, :repository => {:password => 'x'*260} Chris@1494: assert_response :success Chris@1494: assert_template 'edit' Chris@1494: assert_equal Repository.find(11), assigns(:repository) Chris@1494: end Chris@1494: Chris@1494: def test_destroy Chris@1494: @request.session[:user_id] = 1 Chris@1494: assert_difference 'Repository.count', -1 do Chris@1494: delete :destroy, :id => 11 Chris@1494: end Chris@1494: assert_response 302 Chris@1494: assert_nil Repository.find_by_id(11) Chris@1494: end Chris@1494: Chris@1494: def test_show_with_autofetch_changesets_enabled_should_fetch_changesets Chris@1494: Repository::Subversion.any_instance.expects(:fetch_changesets).once Chris@1494: Chris@1494: with_settings :autofetch_changesets => '1' do Chris@1494: get :show, :id => 1 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def test_show_with_autofetch_changesets_disabled_should_not_fetch_changesets Chris@1494: Repository::Subversion.any_instance.expects(:fetch_changesets).never Chris@1494: Chris@1494: with_settings :autofetch_changesets => '0' do Chris@1494: get :show, :id => 1 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def test_show_with_closed_project_should_not_fetch_changesets Chris@1494: Repository::Subversion.any_instance.expects(:fetch_changesets).never Chris@1494: Project.find(1).close Chris@1494: Chris@1494: with_settings :autofetch_changesets => '1' do Chris@1494: get :show, :id => 1 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def test_revisions Chris@1494: get :revisions, :id => 1 Chris@1494: assert_response :success Chris@1494: assert_template 'revisions' Chris@1494: assert_equal Repository.find(10), assigns(:repository) Chris@1494: assert_not_nil assigns(:changesets) Chris@1494: end Chris@1494: Chris@1494: def test_revisions_for_other_repository Chris@1494: repository = Repository::Subversion.create!(:project_id => 1, :identifier => 'foo', :url => 'file:///foo') Chris@1494: Chris@1494: get :revisions, :id => 1, :repository_id => 'foo' Chris@1494: assert_response :success Chris@1494: assert_template 'revisions' Chris@1494: assert_equal repository, assigns(:repository) Chris@1494: assert_not_nil assigns(:changesets) Chris@1494: end Chris@1494: Chris@1494: def test_revisions_for_invalid_repository Chris@1494: get :revisions, :id => 1, :repository_id => 'foo' Chris@1494: assert_response 404 Chris@1494: end Chris@1494: Chris@1494: def test_revision Chris@1494: get :revision, :id => 1, :rev => 1 Chris@1494: assert_response :success Chris@1494: assert_not_nil assigns(:changeset) Chris@1494: assert_equal "1", assigns(:changeset).revision Chris@1494: end Chris@1494: Chris@1494: def test_revision_should_not_change_the_project_menu_link Chris@1494: get :revision, :id => 1, :rev => 1 Chris@1494: assert_response :success Chris@1494: Chris@1494: assert_tag 'a', :attributes => {:href => '/projects/ecookbook/repository', :class => /repository/}, Chris@1494: :ancestor => {:attributes => {:id => 'main-menu'}} Chris@1494: end Chris@1494: Chris@1494: def test_revision_with_before_nil_and_afer_normal Chris@1494: get :revision, {:id => 1, :rev => 1} Chris@1494: assert_response :success Chris@1494: assert_template 'revision' Chris@1494: assert_no_tag :tag => "div", :attributes => { :class => "contextual" }, Chris@1494: :child => { :tag => "a", :attributes => { :href => '/projects/ecookbook/repository/revisions/0'} Chris@1494: } Chris@1494: assert_tag :tag => "div", :attributes => { :class => "contextual" }, Chris@1494: :child => { :tag => "a", :attributes => { :href => '/projects/ecookbook/repository/revisions/2'} Chris@1494: } Chris@1494: end Chris@1494: Chris@1494: def test_add_related_issue Chris@1494: @request.session[:user_id] = 2 Chris@1494: assert_difference 'Changeset.find(103).issues.size' do Chris@1494: xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js' Chris@1494: assert_response :success Chris@1494: assert_template 'add_related_issue' Chris@1494: assert_equal 'text/javascript', response.content_type Chris@1494: end Chris@1494: assert_equal [2], Changeset.find(103).issue_ids Chris@1494: assert_include 'related-issues', response.body Chris@1494: assert_include 'Feature request #2', response.body Chris@1494: end Chris@1494: Chris@1494: def test_add_related_issue_should_accept_issue_id_with_sharp Chris@1494: @request.session[:user_id] = 2 Chris@1494: assert_difference 'Changeset.find(103).issues.size' do Chris@1494: xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => "#2", :format => 'js' Chris@1494: end Chris@1494: assert_equal [2], Changeset.find(103).issue_ids Chris@1494: end Chris@1494: Chris@1494: def test_add_related_issue_with_invalid_issue_id Chris@1494: @request.session[:user_id] = 2 Chris@1494: assert_no_difference 'Changeset.find(103).issues.size' do Chris@1494: xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 9999, :format => 'js' Chris@1494: assert_response :success Chris@1494: assert_template 'add_related_issue' Chris@1494: assert_equal 'text/javascript', response.content_type Chris@1494: end Chris@1494: assert_include 'alert("Issue is invalid")', response.body Chris@1494: end Chris@1494: Chris@1494: def test_remove_related_issue Chris@1494: Changeset.find(103).issues << Issue.find(1) Chris@1494: Changeset.find(103).issues << Issue.find(2) Chris@1494: Chris@1494: @request.session[:user_id] = 2 Chris@1494: assert_difference 'Changeset.find(103).issues.size', -1 do Chris@1494: xhr :delete, :remove_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js' Chris@1494: assert_response :success Chris@1494: assert_template 'remove_related_issue' Chris@1494: assert_equal 'text/javascript', response.content_type Chris@1494: end Chris@1494: assert_equal [1], Changeset.find(103).issue_ids Chris@1494: assert_include 'related-issue-2', response.body Chris@1494: end Chris@1494: Chris@1494: def test_graph_commits_per_month Chris@1494: # Make sure there's some data to display Chris@1494: latest = Project.find(1).repository.changesets.maximum(:commit_date) Chris@1494: assert_not_nil latest Chris@1494: Date.stubs(:today).returns(latest.to_date + 10) Chris@1494: Chris@1494: get :graph, :id => 1, :graph => 'commits_per_month' Chris@1494: assert_response :success Chris@1494: assert_equal 'image/svg+xml', @response.content_type Chris@1494: end Chris@1494: Chris@1494: def test_graph_commits_per_author Chris@1494: get :graph, :id => 1, :graph => 'commits_per_author' Chris@1494: assert_response :success Chris@1494: assert_equal 'image/svg+xml', @response.content_type Chris@1494: end Chris@1494: Chris@1494: def test_get_committers Chris@1494: @request.session[:user_id] = 2 Chris@1494: # add a commit with an unknown user Chris@1494: Changeset.create!( Chris@1494: :repository => Project.find(1).repository, Chris@1494: :committer => 'foo', Chris@1494: :committed_on => Time.now, Chris@1494: :revision => 100, Chris@1494: :comments => 'Committed by foo.' Chris@1494: ) Chris@1494: Chris@1494: get :committers, :id => 10 Chris@1494: assert_response :success Chris@1494: assert_template 'committers' Chris@1494: Chris@1494: assert_tag :td, :content => 'dlopper', Chris@1494: :sibling => { :tag => 'td', Chris@1494: :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} }, Chris@1494: :child => { :tag => 'option', :content => 'Dave Lopper', Chris@1494: :attributes => { :value => '3', :selected => 'selected' }}}} Chris@1494: assert_tag :td, :content => 'foo', Chris@1494: :sibling => { :tag => 'td', Chris@1494: :child => { :tag => 'select', :attributes => { :name => %r{^committers\[\d+\]\[\]$} }}} Chris@1494: assert_no_tag :td, :content => 'foo', Chris@1494: :sibling => { :tag => 'td', Chris@1494: :descendant => { :tag => 'option', :attributes => { :selected => 'selected' }}} Chris@1494: end Chris@1494: Chris@1494: def test_post_committers Chris@1494: @request.session[:user_id] = 2 Chris@1494: # add a commit with an unknown user Chris@1494: c = Changeset.create!( Chris@1494: :repository => Project.find(1).repository, Chris@1494: :committer => 'foo', Chris@1494: :committed_on => Time.now, Chris@1494: :revision => 100, Chris@1494: :comments => 'Committed by foo.' Chris@1494: ) Chris@1494: assert_no_difference "Changeset.where(:user_id => 3).count" do Chris@1494: post :committers, :id => 10, :committers => { '0' => ['foo', '2'], '1' => ['dlopper', '3']} Chris@1494: assert_response 302 Chris@1494: assert_equal User.find(2), c.reload.user Chris@1494: end Chris@1494: end Chris@1494: end