| 
									
										
										
										
											2019-03-16 15:03:47 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2023-01-01 06:19:35 +00:00
										 |  |  | # Copyright (C) 2006-2023  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-12 23:24:34 +00:00
										 |  |  | require File.expand_path('../../test_helper', __FILE__) | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 09:35:49 +00:00
										 |  |  | class SysControllerTest < Redmine::ControllerTest | 
					
						
							| 
									
										
										
										
											2011-03-16 20:31:01 +00:00
										 |  |  |   fixtures :projects, :repositories, :enabled_modules | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |   def setup | 
					
						
							|  |  |  |     Setting.sys_api_enabled = '1' | 
					
						
							| 
									
										
										
										
											2009-03-01 10:55:01 +00:00
										 |  |  |     Setting.enabled_scm = %w(Subversion Git) | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-08 12:23:47 +00:00
										 |  |  |   def teardown | 
					
						
							|  |  |  |     Setting.clear_cache | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |   def test_projects_with_repository_enabled | 
					
						
							|  |  |  |     get :projects | 
					
						
							|  |  |  |     assert_response :success | 
					
						
							| 
									
										
										
										
											2020-01-27 03:54:25 +00:00
										 |  |  |     assert_equal 'application/json', @response.media_type | 
					
						
							| 
									
										
										
										
											2017-06-01 20:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     data = ActiveSupport::JSON.decode(response.body) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_equal Project.active.has_module(:repository).count, data.size | 
					
						
							|  |  |  |     project = data.first | 
					
						
							|  |  |  |     assert project['identifier'] | 
					
						
							|  |  |  |     assert project['is_public'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_not_include 'extra-info', response.body | 
					
						
							|  |  |  |     assert_not_include 'extra_info', response.body | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_create_project_repository | 
					
						
							|  |  |  |     assert_nil Project.find(4).repository | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 13:35:17 +00:00
										 |  |  |     post( | 
					
						
							|  |  |  |       :create_project_repository, | 
					
						
							|  |  |  |       :params => { | 
					
						
							|  |  |  |         :id => 4, | 
					
						
							|  |  |  |         :vendor => 'Subversion', | 
					
						
							|  |  |  |         :repository => {:url => 'file:///create/project/repository/subproject2'} | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |     assert_response :created | 
					
						
							| 
									
										
										
										
											2020-01-27 03:54:25 +00:00
										 |  |  |     assert_equal 'application/json', @response.media_type | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |     r = Project.find(4).repository | 
					
						
							|  |  |  |     assert r.is_a?(Repository::Subversion) | 
					
						
							|  |  |  |     assert_equal 'file:///create/project/repository/subproject2', r.url | 
					
						
							| 
									
										
										
										
											2017-06-01 20:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     data = ActiveSupport::JSON.decode(response.body) | 
					
						
							|  |  |  |     assert data['repository-subversion'] | 
					
						
							|  |  |  |     assert_equal r.id, data['repository-subversion']['id'] | 
					
						
							|  |  |  |     assert_equal r.url, data['repository-subversion']['url'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_not_include 'extra-info', response.body | 
					
						
							|  |  |  |     assert_not_include 'extra_info', response.body | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-03 13:59:17 +00:00
										 |  |  |   def test_create_already_existing | 
					
						
							| 
									
										
										
										
											2020-10-26 13:35:17 +00:00
										 |  |  |     post( | 
					
						
							|  |  |  |       :create_project_repository, | 
					
						
							|  |  |  |       :params => { | 
					
						
							|  |  |  |         :id => 1, | 
					
						
							|  |  |  |         :vendor => 'Subversion', | 
					
						
							|  |  |  |         :repository => {:url => 'file:///create/project/repository/subproject2'} | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2011-12-03 13:59:17 +00:00
										 |  |  |     assert_response :conflict | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_create_with_failure | 
					
						
							| 
									
										
										
										
											2020-10-26 13:35:17 +00:00
										 |  |  |     post( | 
					
						
							|  |  |  |       :create_project_repository, | 
					
						
							|  |  |  |       :params => { | 
					
						
							|  |  |  |         :id => 4, | 
					
						
							|  |  |  |         :vendor => 'Subversion', | 
					
						
							|  |  |  |         :repository => {:url => 'invalid url'} | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2011-12-03 13:59:17 +00:00
										 |  |  |     assert_response :unprocessable_entity | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-28 16:50:26 +00:00
										 |  |  |   def test_fetch_changesets | 
					
						
							| 
									
										
										
										
											2012-01-15 14:23:06 +00:00
										 |  |  |     Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true) | 
					
						
							| 
									
										
										
										
											2009-11-28 16:50:26 +00:00
										 |  |  |     get :fetch_changesets | 
					
						
							|  |  |  |     assert_response :success | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-25 12:57:30 +00:00
										 |  |  |   def test_fetch_changesets_one_project_by_identifier | 
					
						
							| 
									
										
										
										
											2012-01-15 14:23:06 +00:00
										 |  |  |     Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true) | 
					
						
							| 
									
										
										
										
											2016-07-16 07:43:16 +00:00
										 |  |  |     get :fetch_changesets, :params => {:id => 'ecookbook'} | 
					
						
							| 
									
										
										
										
											2009-11-28 16:50:26 +00:00
										 |  |  |     assert_response :success | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-25 12:57:30 +00:00
										 |  |  |   def test_fetch_changesets_one_project_by_id | 
					
						
							|  |  |  |     Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true) | 
					
						
							| 
									
										
										
										
											2016-07-16 07:43:16 +00:00
										 |  |  |     get :fetch_changesets, :params => {:id => '1'} | 
					
						
							| 
									
										
										
										
											2012-02-25 12:57:30 +00:00
										 |  |  |     assert_response :success | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-28 16:50:26 +00:00
										 |  |  |   def test_fetch_changesets_unknown_project | 
					
						
							| 
									
										
										
										
											2016-07-16 07:43:16 +00:00
										 |  |  |     get :fetch_changesets, :params => {:id => 'unknown'} | 
					
						
							| 
									
										
										
										
											2009-11-28 16:50:26 +00:00
										 |  |  |     assert_response 404
 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |   def test_disabled_ws_should_respond_with_403_error | 
					
						
							|  |  |  |     with_settings :sys_api_enabled => '0' do | 
					
						
							|  |  |  |       get :projects | 
					
						
							|  |  |  |       assert_response 403
 | 
					
						
							| 
									
										
										
										
											2016-07-21 20:49:14 +00:00
										 |  |  |       assert_include 'Access denied', response.body | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |   def test_api_key | 
					
						
							|  |  |  |     with_settings :sys_api_key => 'my_secret_key' do | 
					
						
							| 
									
										
										
										
											2016-07-16 07:43:16 +00:00
										 |  |  |       get :projects, :params => {:key => 'my_secret_key'} | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |       assert_response :success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-25 04:40:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |   def test_wrong_key_should_respond_with_403_error | 
					
						
							|  |  |  |     with_settings :sys_api_enabled => 'my_secret_key' do | 
					
						
							| 
									
										
										
										
											2016-07-16 07:43:16 +00:00
										 |  |  |       get :projects, :params => {:key => 'wrong_key'} | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |       assert_response 403
 | 
					
						
							| 
									
										
										
										
											2016-07-21 20:49:14 +00:00
										 |  |  |       assert_include 'Access denied', response.body | 
					
						
							| 
									
										
										
										
											2009-12-20 09:44:28 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-08-10 01:34:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_should_skip_verify_authenticity_token | 
					
						
							|  |  |  |     ActionController::Base.allow_forgery_protection = true | 
					
						
							|  |  |  |     assert_nothing_raised {test_create_project_repository} | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     ActionController::Base.allow_forgery_protection = false | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2009-02-10 22:03:25 +00:00
										 |  |  | end |