comparison test/integration/api_test/.svn/text-base/projects_test.rb.svn-base @ 117:af80e5618e9b redmine-1.1

* Update to Redmine 1.1-stable branch (Redmine SVN rev 4707)
author Chris Cannam
date Thu, 13 Jan 2011 12:53:21 +0000
parents 94944d00e43c
children
comparison
equal deleted inserted replaced
39:150ceac17a8d 117:af80e5618e9b
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.dirname(__FILE__)}/../../test_helper" 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 class ApiTest::ProjectsTest < ActionController::IntegrationTest 20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, 21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries 23 :attachments, :custom_fields, :custom_values, :time_entries
24 24
25 def setup 25 def setup
26 Setting.rest_api_enabled = '1' 26 Setting.rest_api_enabled = '1'
27 end 27 end
28 28
29 def test_index 29 context "GET /projects" do
30 get '/projects.xml' 30 context ".xml" do
31 assert_response :success 31 should "return projects" do
32 assert_equal 'application/xml', @response.content_type 32 get '/projects.xml'
33 end 33 assert_response :success
34 34 assert_equal 'application/xml', @response.content_type
35 def test_show 35
36 get '/projects/1.xml' 36 assert_tag :tag => 'projects',
37 assert_response :success 37 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
38 assert_equal 'application/xml', @response.content_type 38 end
39 assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable' 39 end
40 end 40
41 41 context ".json" do
42 def test_show_should_not_display_hidden_custom_fields 42 should "return projects" do
43 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false 43 get '/projects.json'
44 get '/projects/1.xml' 44 assert_response :success
45 assert_response :success 45 assert_equal 'application/json', @response.content_type
46 assert_equal 'application/xml', @response.content_type 46
47 assert_no_tag 'custom_field', :attributes => {:name => 'Development status'} 47 json = ActiveSupport::JSON.decode(response.body)
48 end 48 assert_kind_of Hash, json
49 49 assert_kind_of Array, json['projects']
50 def test_create 50 assert_kind_of Hash, json['projects'].first
51 attributes = {:name => 'API test', :identifier => 'api-test'} 51 assert json['projects'].first.has_key?('id')
52 assert_difference 'Project.count' do 52 end
53 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') 53 end
54 end 54 end
55 55
56 project = Project.first(:order => 'id DESC') 56 context "GET /projects/:id" do
57 attributes.each do |attribute, value| 57 context ".xml" do
58 assert_equal value, project.send(attribute) 58 # TODO: A private project is needed because should_allow_api_authentication
59 end 59 # actually tests that authentication is *required*, not just allowed
60 60 should_allow_api_authentication(:get, "/projects/2.xml")
61 assert_response :created 61
62 assert_equal 'application/xml', @response.content_type 62 should "return requested project" do
63 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} 63 get '/projects/1.xml'
64 end 64 assert_response :success
65 65 assert_equal 'application/xml', @response.content_type
66 def test_create_failure 66
67 attributes = {:name => 'API test'} 67 assert_tag :tag => 'project',
68 assert_no_difference 'Project.count' do 68 :child => {:tag => 'id', :content => '1'}
69 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') 69 assert_tag :tag => 'custom_field',
70 end 70 :attributes => {:name => 'Development status'}, :content => 'Stable'
71 assert_response :unprocessable_entity 71 end
72 assert_equal 'application/xml', @response.content_type 72
73 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"} 73 context "with hidden custom fields" do
74 end 74 setup do
75 75 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
76 def test_update 76 end
77 attributes = {:name => 'API update'} 77
78 assert_no_difference 'Project.count' do 78 should "not display hidden custom fields" do
79 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') 79 get '/projects/1.xml'
80 end 80 assert_response :success
81 assert_response :ok 81 assert_equal 'application/xml', @response.content_type
82 assert_equal 'application/xml', @response.content_type 82
83 project = Project.find(1) 83 assert_no_tag 'custom_field',
84 attributes.each do |attribute, value| 84 :attributes => {:name => 'Development status'}
85 assert_equal value, project.send(attribute) 85 end
86 end 86 end
87 end 87 end
88 88
89 def test_update_failure 89 context ".json" do
90 attributes = {:name => ''} 90 should_allow_api_authentication(:get, "/projects/2.json")
91 assert_no_difference 'Project.count' do 91
92 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') 92 should "return requested project" do
93 end 93 get '/projects/1.json'
94 assert_response :unprocessable_entity 94
95 assert_equal 'application/xml', @response.content_type 95 json = ActiveSupport::JSON.decode(response.body)
96 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"} 96 assert_kind_of Hash, json
97 end 97 assert_kind_of Hash, json['project']
98 98 assert_equal 1, json['project']['id']
99 def test_destroy 99 end
100 assert_difference 'Project.count', -1 do 100 end
101 delete '/projects/2.xml', {}, :authorization => credentials('admin') 101 end
102 end 102
103 assert_response :ok 103 context "POST /projects" do
104 assert_equal 'application/xml', @response.content_type 104 context "with valid parameters" do
105 assert_nil Project.find_by_id(2) 105 setup do
106 Setting.default_projects_modules = ['issue_tracking', 'repository']
107 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
108 end
109
110 context ".xml" do
111 should_allow_api_authentication(:post,
112 '/projects.xml',
113 {:project => {:name => 'API test', :identifier => 'api-test'}},
114 {:success_code => :created})
115
116
117 should "create a project with the attributes" do
118 assert_difference('Project.count') do
119 post '/projects.xml', @parameters, :authorization => credentials('admin')
120 end
121
122 project = Project.first(:order => 'id DESC')
123 assert_equal 'API test', project.name
124 assert_equal 'api-test', project.identifier
125 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
126 assert_equal Tracker.all.size, project.trackers.size
127
128 assert_response :created
129 assert_equal 'application/xml', @response.content_type
130 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
131 end
132
133 should "accept enabled_module_names attribute" do
134 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
135
136 assert_difference('Project.count') do
137 post '/projects.xml', @parameters, :authorization => credentials('admin')
138 end
139
140 project = Project.first(:order => 'id DESC')
141 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
142 end
143
144 should "accept tracker_ids attribute" do
145 @parameters[:project].merge!({:tracker_ids => [1, 3]})
146
147 assert_difference('Project.count') do
148 post '/projects.xml', @parameters, :authorization => credentials('admin')
149 end
150
151 project = Project.first(:order => 'id DESC')
152 assert_equal [1, 3], project.trackers.map(&:id).sort
153 end
154 end
155 end
156
157 context "with invalid parameters" do
158 setup do
159 @parameters = {:project => {:name => 'API test'}}
160 end
161
162 context ".xml" do
163 should "return errors" do
164 assert_no_difference('Project.count') do
165 post '/projects.xml', @parameters, :authorization => credentials('admin')
166 end
167
168 assert_response :unprocessable_entity
169 assert_equal 'application/xml', @response.content_type
170 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
171 end
172 end
173 end
174 end
175
176 context "PUT /projects/:id" do
177 context "with valid parameters" do
178 setup do
179 @parameters = {:project => {:name => 'API update'}}
180 end
181
182 context ".xml" do
183 should_allow_api_authentication(:put,
184 '/projects/2.xml',
185 {:project => {:name => 'API update'}},
186 {:success_code => :ok})
187
188 should "update the project" do
189 assert_no_difference 'Project.count' do
190 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
191 end
192 assert_response :ok
193 assert_equal 'application/xml', @response.content_type
194 project = Project.find(2)
195 assert_equal 'API update', project.name
196 end
197
198 should "accept enabled_module_names attribute" do
199 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
200
201 assert_no_difference 'Project.count' do
202 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
203 end
204 assert_response :ok
205 project = Project.find(2)
206 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
207 end
208
209 should "accept tracker_ids attribute" do
210 @parameters[:project].merge!({:tracker_ids => [1, 3]})
211
212 assert_no_difference 'Project.count' do
213 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
214 end
215 assert_response :ok
216 project = Project.find(2)
217 assert_equal [1, 3], project.trackers.map(&:id).sort
218 end
219 end
220 end
221
222 context "with invalid parameters" do
223 setup do
224 @parameters = {:project => {:name => ''}}
225 end
226
227 context ".xml" do
228 should "return errors" do
229 assert_no_difference('Project.count') do
230 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
231 end
232
233 assert_response :unprocessable_entity
234 assert_equal 'application/xml', @response.content_type
235 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
236 end
237 end
238 end
239 end
240
241 context "DELETE /projects/:id" do
242 context ".xml" do
243 should_allow_api_authentication(:delete,
244 '/projects/2.xml',
245 {},
246 {:success_code => :ok})
247
248 should "delete the project" do
249 assert_difference('Project.count',-1) do
250 delete '/projects/2.xml', {}, :authorization => credentials('admin')
251 end
252 assert_response :ok
253 assert_nil Project.find_by_id(2)
254 end
255 end
106 end 256 end
107 257
108 def credentials(user, password=nil) 258 def credentials(user, password=nil)
109 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) 259 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
110 end 260 end