##// END OF EJS Templates
Can not use Project#find on scopes with Rails 3.1....
Jean-Philippe Lang -
r8881:cf9154f924ff
parent child
Show More
@@ -1,75 +1,83
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class SysController < ActionController::Base
18 class SysController < ActionController::Base
19 before_filter :check_enabled
19 before_filter :check_enabled
20
20
21 def projects
21 def projects
22 p = Project.active.has_module(:repository).find(
22 p = Project.active.has_module(:repository).find(
23 :all,
23 :all,
24 :include => :repository,
24 :include => :repository,
25 :order => "#{Project.table_name}.identifier"
25 :order => "#{Project.table_name}.identifier"
26 )
26 )
27 # extra_info attribute from repository breaks activeresource client
27 # extra_info attribute from repository breaks activeresource client
28 render :xml => p.to_xml(
28 render :xml => p.to_xml(
29 :only => [:id, :identifier, :name, :is_public, :status],
29 :only => [:id, :identifier, :name, :is_public, :status],
30 :include => {:repository => {:only => [:id, :url]}}
30 :include => {:repository => {:only => [:id, :url]}}
31 )
31 )
32 end
32 end
33
33
34 def create_project_repository
34 def create_project_repository
35 project = Project.find(params[:id])
35 project = Project.find(params[:id])
36 if project.repository
36 if project.repository
37 render :nothing => true, :status => 409
37 render :nothing => true, :status => 409
38 else
38 else
39 logger.info "Repository for #{project.name} was reported to be created by #{request.remote_ip}."
39 logger.info "Repository for #{project.name} was reported to be created by #{request.remote_ip}."
40 project.repository = Repository.factory(params[:vendor], params[:repository])
40 project.repository = Repository.factory(params[:vendor], params[:repository])
41 if project.repository && project.repository.save
41 if project.repository && project.repository.save
42 render :xml => {project.repository.class.name.underscore.gsub('/', '-') => {:id => project.repository.id, :url => project.repository.url}}, :status => 201
42 render :xml => {project.repository.class.name.underscore.gsub('/', '-') => {:id => project.repository.id, :url => project.repository.url}}, :status => 201
43 else
43 else
44 render :nothing => true, :status => 422
44 render :nothing => true, :status => 422
45 end
45 end
46 end
46 end
47 end
47 end
48
48
49 def fetch_changesets
49 def fetch_changesets
50 projects = []
50 projects = []
51 scope = Project.active.has_module(:repository)
51 if params[:id]
52 if params[:id]
52 projects << Project.active.has_module(:repository).find(params[:id])
53 project = nil
54 if params[:id].to_s =~ /^\d*$/
55 project = scope.find(params[:id])
56 else
57 project = scope.find_by_identifier(params[:id])
58 end
59 raise ActiveRecord::RecordNotFound unless project
60 projects << project
53 else
61 else
54 projects = Project.active.has_module(:repository).all
62 projects = scope.all
55 end
63 end
56 projects.each do |project|
64 projects.each do |project|
57 project.repositories.each do |repository|
65 project.repositories.each do |repository|
58 repository.fetch_changesets
66 repository.fetch_changesets
59 end
67 end
60 end
68 end
61 render :nothing => true, :status => 200
69 render :nothing => true, :status => 200
62 rescue ActiveRecord::RecordNotFound
70 rescue ActiveRecord::RecordNotFound
63 render :nothing => true, :status => 404
71 render :nothing => true, :status => 404
64 end
72 end
65
73
66 protected
74 protected
67
75
68 def check_enabled
76 def check_enabled
69 User.current = nil
77 User.current = nil
70 unless Setting.sys_api_enabled? && params[:key].to_s == Setting.sys_api_key
78 unless Setting.sys_api_enabled? && params[:key].to_s == Setting.sys_api_key
71 render :text => 'Access denied. Repository management WS is disabled or key is invalid.', :status => 403
79 render :text => 'Access denied. Repository management WS is disabled or key is invalid.', :status => 403
72 return false
80 return false
73 end
81 end
74 end
82 end
75 end
83 end
@@ -1,123 +1,129
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'sys_controller'
19 require 'sys_controller'
20 require 'mocha'
20 require 'mocha'
21
21
22 # Re-raise errors caught by the controller.
22 # Re-raise errors caught by the controller.
23 class SysController; def rescue_action(e) raise e end; end
23 class SysController; def rescue_action(e) raise e end; end
24
24
25 class SysControllerTest < ActionController::TestCase
25 class SysControllerTest < ActionController::TestCase
26 fixtures :projects, :repositories, :enabled_modules
26 fixtures :projects, :repositories, :enabled_modules
27
27
28 def setup
28 def setup
29 @controller = SysController.new
29 @controller = SysController.new
30 @request = ActionController::TestRequest.new
30 @request = ActionController::TestRequest.new
31 @response = ActionController::TestResponse.new
31 @response = ActionController::TestResponse.new
32 Setting.sys_api_enabled = '1'
32 Setting.sys_api_enabled = '1'
33 Setting.enabled_scm = %w(Subversion Git)
33 Setting.enabled_scm = %w(Subversion Git)
34 end
34 end
35
35
36 def test_projects_with_repository_enabled
36 def test_projects_with_repository_enabled
37 get :projects
37 get :projects
38 assert_response :success
38 assert_response :success
39 assert_equal 'application/xml', @response.content_type
39 assert_equal 'application/xml', @response.content_type
40 with_options :tag => 'projects' do |test|
40 with_options :tag => 'projects' do |test|
41 test.assert_tag :children => { :count => Project.active.has_module(:repository).count }
41 test.assert_tag :children => { :count => Project.active.has_module(:repository).count }
42 test.assert_tag 'project', :child => {:tag => 'identifier', :sibling => {:tag => 'is-public'}}
42 test.assert_tag 'project', :child => {:tag => 'identifier', :sibling => {:tag => 'is-public'}}
43 end
43 end
44 assert_no_tag 'extra-info'
44 assert_no_tag 'extra-info'
45 assert_no_tag 'extra_info'
45 assert_no_tag 'extra_info'
46 end
46 end
47
47
48 def test_create_project_repository
48 def test_create_project_repository
49 assert_nil Project.find(4).repository
49 assert_nil Project.find(4).repository
50
50
51 post :create_project_repository, :id => 4,
51 post :create_project_repository, :id => 4,
52 :vendor => 'Subversion',
52 :vendor => 'Subversion',
53 :repository => { :url => 'file:///create/project/repository/subproject2'}
53 :repository => { :url => 'file:///create/project/repository/subproject2'}
54 assert_response :created
54 assert_response :created
55 assert_equal 'application/xml', @response.content_type
55 assert_equal 'application/xml', @response.content_type
56
56
57 r = Project.find(4).repository
57 r = Project.find(4).repository
58 assert r.is_a?(Repository::Subversion)
58 assert r.is_a?(Repository::Subversion)
59 assert_equal 'file:///create/project/repository/subproject2', r.url
59 assert_equal 'file:///create/project/repository/subproject2', r.url
60
60
61 assert_tag 'repository-subversion',
61 assert_tag 'repository-subversion',
62 :child => {
62 :child => {
63 :tag => 'id', :content => r.id.to_s,
63 :tag => 'id', :content => r.id.to_s,
64 :sibling => {:tag => 'url', :content => r.url}
64 :sibling => {:tag => 'url', :content => r.url}
65 }
65 }
66 assert_no_tag 'extra-info'
66 assert_no_tag 'extra-info'
67 assert_no_tag 'extra_info'
67 assert_no_tag 'extra_info'
68 end
68 end
69
69
70 def test_create_already_existing
70 def test_create_already_existing
71 post :create_project_repository, :id => 1,
71 post :create_project_repository, :id => 1,
72 :vendor => 'Subversion',
72 :vendor => 'Subversion',
73 :repository => { :url => 'file:///create/project/repository/subproject2'}
73 :repository => { :url => 'file:///create/project/repository/subproject2'}
74
74
75 assert_response :conflict
75 assert_response :conflict
76 end
76 end
77
77
78 def test_create_with_failure
78 def test_create_with_failure
79 post :create_project_repository, :id => 4,
79 post :create_project_repository, :id => 4,
80 :vendor => 'Subversion',
80 :vendor => 'Subversion',
81 :repository => { :url => 'invalid url'}
81 :repository => { :url => 'invalid url'}
82
82
83 assert_response :unprocessable_entity
83 assert_response :unprocessable_entity
84 end
84 end
85
85
86 def test_fetch_changesets
86 def test_fetch_changesets
87 Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true)
87 Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true)
88 get :fetch_changesets
88 get :fetch_changesets
89 assert_response :success
89 assert_response :success
90 end
90 end
91
91
92 def test_fetch_changesets_one_project
92 def test_fetch_changesets_one_project_by_identifier
93 Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true)
93 Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true)
94 get :fetch_changesets, :id => 'ecookbook'
94 get :fetch_changesets, :id => 'ecookbook'
95 assert_response :success
95 assert_response :success
96 end
96 end
97
97
98 def test_fetch_changesets_one_project_by_id
99 Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true)
100 get :fetch_changesets, :id => '1'
101 assert_response :success
102 end
103
98 def test_fetch_changesets_unknown_project
104 def test_fetch_changesets_unknown_project
99 get :fetch_changesets, :id => 'unknown'
105 get :fetch_changesets, :id => 'unknown'
100 assert_response 404
106 assert_response 404
101 end
107 end
102
108
103 def test_disabled_ws_should_respond_with_403_error
109 def test_disabled_ws_should_respond_with_403_error
104 with_settings :sys_api_enabled => '0' do
110 with_settings :sys_api_enabled => '0' do
105 get :projects
111 get :projects
106 assert_response 403
112 assert_response 403
107 end
113 end
108 end
114 end
109
115
110 def test_api_key
116 def test_api_key
111 with_settings :sys_api_key => 'my_secret_key' do
117 with_settings :sys_api_key => 'my_secret_key' do
112 get :projects, :key => 'my_secret_key'
118 get :projects, :key => 'my_secret_key'
113 assert_response :success
119 assert_response :success
114 end
120 end
115 end
121 end
116
122
117 def test_wrong_key_should_respond_with_403_error
123 def test_wrong_key_should_respond_with_403_error
118 with_settings :sys_api_enabled => 'my_secret_key' do
124 with_settings :sys_api_enabled => 'my_secret_key' do
119 get :projects, :key => 'wrong_key'
125 get :projects, :key => 'wrong_key'
120 assert_response 403
126 assert_response 403
121 end
127 end
122 end
128 end
123 end
129 end
General Comments 0
You need to be logged in to leave comments. Login now