@@ -1,261 +1,262 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class ApiTest::ProjectsTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, |
|
22 | 22 | :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, |
|
23 | 23 | :attachments, :custom_fields, :custom_values, :time_entries |
|
24 | 24 | |
|
25 | 25 | def setup |
|
26 | 26 | Setting.rest_api_enabled = '1' |
|
27 | set_tmp_attachments_directory | |
|
27 | 28 | end |
|
28 | 29 | |
|
29 | 30 | context "GET /projects" do |
|
30 | 31 | context ".xml" do |
|
31 | 32 | should "return projects" do |
|
32 | 33 | get '/projects.xml' |
|
33 | 34 | assert_response :success |
|
34 | 35 | assert_equal 'application/xml', @response.content_type |
|
35 | 36 | |
|
36 | 37 | assert_tag :tag => 'projects', |
|
37 | 38 | :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} |
|
38 | 39 | end |
|
39 | 40 | end |
|
40 | 41 | |
|
41 | 42 | context ".json" do |
|
42 | 43 | should "return projects" do |
|
43 | 44 | get '/projects.json' |
|
44 | 45 | assert_response :success |
|
45 | 46 | assert_equal 'application/json', @response.content_type |
|
46 | 47 | |
|
47 | 48 | json = ActiveSupport::JSON.decode(response.body) |
|
48 | 49 | assert_kind_of Hash, json |
|
49 | 50 | assert_kind_of Array, json['projects'] |
|
50 | 51 | assert_kind_of Hash, json['projects'].first |
|
51 | 52 | assert json['projects'].first.has_key?('id') |
|
52 | 53 | end |
|
53 | 54 | end |
|
54 | 55 | end |
|
55 | 56 | |
|
56 | 57 | context "GET /projects/:id" do |
|
57 | 58 | context ".xml" do |
|
58 | 59 | # TODO: A private project is needed because should_allow_api_authentication |
|
59 | 60 | # actually tests that authentication is *required*, not just allowed |
|
60 | 61 | should_allow_api_authentication(:get, "/projects/2.xml") |
|
61 | 62 | |
|
62 | 63 | should "return requested project" do |
|
63 | 64 | get '/projects/1.xml' |
|
64 | 65 | assert_response :success |
|
65 | 66 | assert_equal 'application/xml', @response.content_type |
|
66 | 67 | |
|
67 | 68 | assert_tag :tag => 'project', |
|
68 | 69 | :child => {:tag => 'id', :content => '1'} |
|
69 | 70 | assert_tag :tag => 'custom_field', |
|
70 | 71 | :attributes => {:name => 'Development status'}, :content => 'Stable' |
|
71 | 72 | end |
|
72 | 73 | |
|
73 | 74 | context "with hidden custom fields" do |
|
74 | 75 | setup do |
|
75 | 76 | ProjectCustomField.find_by_name('Development status').update_attribute :visible, false |
|
76 | 77 | end |
|
77 | 78 | |
|
78 | 79 | should "not display hidden custom fields" do |
|
79 | 80 | get '/projects/1.xml' |
|
80 | 81 | assert_response :success |
|
81 | 82 | assert_equal 'application/xml', @response.content_type |
|
82 | 83 | |
|
83 | 84 | assert_no_tag 'custom_field', |
|
84 | 85 | :attributes => {:name => 'Development status'} |
|
85 | 86 | end |
|
86 | 87 | end |
|
87 | 88 | end |
|
88 | 89 | |
|
89 | 90 | context ".json" do |
|
90 | 91 | should_allow_api_authentication(:get, "/projects/2.json") |
|
91 | 92 | |
|
92 | 93 | should "return requested project" do |
|
93 | 94 | get '/projects/1.json' |
|
94 | 95 | |
|
95 | 96 | json = ActiveSupport::JSON.decode(response.body) |
|
96 | 97 | assert_kind_of Hash, json |
|
97 | 98 | assert_kind_of Hash, json['project'] |
|
98 | 99 | assert_equal 1, json['project']['id'] |
|
99 | 100 | end |
|
100 | 101 | end |
|
101 | 102 | end |
|
102 | 103 | |
|
103 | 104 | context "POST /projects" do |
|
104 | 105 | context "with valid parameters" do |
|
105 | 106 | setup do |
|
106 | 107 | Setting.default_projects_modules = ['issue_tracking', 'repository'] |
|
107 | 108 | @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} |
|
108 | 109 | end |
|
109 | 110 | |
|
110 | 111 | context ".xml" do |
|
111 | 112 | should_allow_api_authentication(:post, |
|
112 | 113 | '/projects.xml', |
|
113 | 114 | {:project => {:name => 'API test', :identifier => 'api-test'}}, |
|
114 | 115 | {:success_code => :created}) |
|
115 | 116 | |
|
116 | 117 | |
|
117 | 118 | should "create a project with the attributes" do |
|
118 | 119 | assert_difference('Project.count') do |
|
119 | 120 | post '/projects.xml', @parameters, :authorization => credentials('admin') |
|
120 | 121 | end |
|
121 | 122 | |
|
122 | 123 | project = Project.first(:order => 'id DESC') |
|
123 | 124 | assert_equal 'API test', project.name |
|
124 | 125 | assert_equal 'api-test', project.identifier |
|
125 | 126 | assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort |
|
126 | 127 | assert_equal Tracker.all.size, project.trackers.size |
|
127 | 128 | |
|
128 | 129 | assert_response :created |
|
129 | 130 | assert_equal 'application/xml', @response.content_type |
|
130 | 131 | assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} |
|
131 | 132 | end |
|
132 | 133 | |
|
133 | 134 | should "accept enabled_module_names attribute" do |
|
134 | 135 | @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) |
|
135 | 136 | |
|
136 | 137 | assert_difference('Project.count') do |
|
137 | 138 | post '/projects.xml', @parameters, :authorization => credentials('admin') |
|
138 | 139 | end |
|
139 | 140 | |
|
140 | 141 | project = Project.first(:order => 'id DESC') |
|
141 | 142 | assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
|
142 | 143 | end |
|
143 | 144 | |
|
144 | 145 | should "accept tracker_ids attribute" do |
|
145 | 146 | @parameters[:project].merge!({:tracker_ids => [1, 3]}) |
|
146 | 147 | |
|
147 | 148 | assert_difference('Project.count') do |
|
148 | 149 | post '/projects.xml', @parameters, :authorization => credentials('admin') |
|
149 | 150 | end |
|
150 | 151 | |
|
151 | 152 | project = Project.first(:order => 'id DESC') |
|
152 | 153 | assert_equal [1, 3], project.trackers.map(&:id).sort |
|
153 | 154 | end |
|
154 | 155 | end |
|
155 | 156 | end |
|
156 | 157 | |
|
157 | 158 | context "with invalid parameters" do |
|
158 | 159 | setup do |
|
159 | 160 | @parameters = {:project => {:name => 'API test'}} |
|
160 | 161 | end |
|
161 | 162 | |
|
162 | 163 | context ".xml" do |
|
163 | 164 | should "return errors" do |
|
164 | 165 | assert_no_difference('Project.count') do |
|
165 | 166 | post '/projects.xml', @parameters, :authorization => credentials('admin') |
|
166 | 167 | end |
|
167 | 168 | |
|
168 | 169 | assert_response :unprocessable_entity |
|
169 | 170 | assert_equal 'application/xml', @response.content_type |
|
170 | 171 | assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} |
|
171 | 172 | end |
|
172 | 173 | end |
|
173 | 174 | end |
|
174 | 175 | end |
|
175 | 176 | |
|
176 | 177 | context "PUT /projects/:id" do |
|
177 | 178 | context "with valid parameters" do |
|
178 | 179 | setup do |
|
179 | 180 | @parameters = {:project => {:name => 'API update'}} |
|
180 | 181 | end |
|
181 | 182 | |
|
182 | 183 | context ".xml" do |
|
183 | 184 | should_allow_api_authentication(:put, |
|
184 | 185 | '/projects/2.xml', |
|
185 | 186 | {:project => {:name => 'API update'}}, |
|
186 | 187 | {:success_code => :ok}) |
|
187 | 188 | |
|
188 | 189 | should "update the project" do |
|
189 | 190 | assert_no_difference 'Project.count' do |
|
190 | 191 | put '/projects/2.xml', @parameters, :authorization => credentials('jsmith') |
|
191 | 192 | end |
|
192 | 193 | assert_response :ok |
|
193 | 194 | assert_equal 'application/xml', @response.content_type |
|
194 | 195 | project = Project.find(2) |
|
195 | 196 | assert_equal 'API update', project.name |
|
196 | 197 | end |
|
197 | 198 | |
|
198 | 199 | should "accept enabled_module_names attribute" do |
|
199 | 200 | @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) |
|
200 | 201 | |
|
201 | 202 | assert_no_difference 'Project.count' do |
|
202 | 203 | put '/projects/2.xml', @parameters, :authorization => credentials('admin') |
|
203 | 204 | end |
|
204 | 205 | assert_response :ok |
|
205 | 206 | project = Project.find(2) |
|
206 | 207 | assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
|
207 | 208 | end |
|
208 | 209 | |
|
209 | 210 | should "accept tracker_ids attribute" do |
|
210 | 211 | @parameters[:project].merge!({:tracker_ids => [1, 3]}) |
|
211 | 212 | |
|
212 | 213 | assert_no_difference 'Project.count' do |
|
213 | 214 | put '/projects/2.xml', @parameters, :authorization => credentials('admin') |
|
214 | 215 | end |
|
215 | 216 | assert_response :ok |
|
216 | 217 | project = Project.find(2) |
|
217 | 218 | assert_equal [1, 3], project.trackers.map(&:id).sort |
|
218 | 219 | end |
|
219 | 220 | end |
|
220 | 221 | end |
|
221 | 222 | |
|
222 | 223 | context "with invalid parameters" do |
|
223 | 224 | setup do |
|
224 | 225 | @parameters = {:project => {:name => ''}} |
|
225 | 226 | end |
|
226 | 227 | |
|
227 | 228 | context ".xml" do |
|
228 | 229 | should "return errors" do |
|
229 | 230 | assert_no_difference('Project.count') do |
|
230 | 231 | put '/projects/2.xml', @parameters, :authorization => credentials('admin') |
|
231 | 232 | end |
|
232 | 233 | |
|
233 | 234 | assert_response :unprocessable_entity |
|
234 | 235 | assert_equal 'application/xml', @response.content_type |
|
235 | 236 | assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} |
|
236 | 237 | end |
|
237 | 238 | end |
|
238 | 239 | end |
|
239 | 240 | end |
|
240 | 241 | |
|
241 | 242 | context "DELETE /projects/:id" do |
|
242 | 243 | context ".xml" do |
|
243 | 244 | should_allow_api_authentication(:delete, |
|
244 | 245 | '/projects/2.xml', |
|
245 | 246 | {}, |
|
246 | 247 | {:success_code => :ok}) |
|
247 | 248 | |
|
248 | 249 | should "delete the project" do |
|
249 | 250 | assert_difference('Project.count',-1) do |
|
250 | 251 | delete '/projects/2.xml', {}, :authorization => credentials('admin') |
|
251 | 252 | end |
|
252 | 253 | assert_response :ok |
|
253 | 254 | assert_nil Project.find_by_id(2) |
|
254 | 255 | end |
|
255 | 256 | end |
|
256 | 257 | end |
|
257 | 258 | |
|
258 | 259 | def credentials(user, password=nil) |
|
259 | 260 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
|
260 | 261 | end |
|
261 | 262 | end |
General Comments 0
You need to be logged in to leave comments.
Login now