##// END OF EJS Templates
Removed some shoulda context....
Jean-Philippe Lang -
r11633:8f7b69f77e7f
parent child
Show More
@@ -1,297 +1,235
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
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, :issue_categories
24 24
25 25 def setup
26 26 Setting.rest_api_enabled = '1'
27 27 set_tmp_attachments_directory
28 28 end
29 29
30 context "GET /projects" do
31 context ".xml" do
32 should "return projects" do
30 # TODO: A private project is needed because should_allow_api_authentication
31 # actually tests that authentication is *required*, not just allowed
32 should_allow_api_authentication(:get, "/projects/2.xml")
33 should_allow_api_authentication(:get, "/projects/2.json")
34 should_allow_api_authentication(:post,
35 '/projects.xml',
36 {:project => {:name => 'API test', :identifier => 'api-test'}},
37 {:success_code => :created})
38 should_allow_api_authentication(:put,
39 '/projects/2.xml',
40 {:project => {:name => 'API update'}},
41 {:success_code => :ok})
42 should_allow_api_authentication(:delete,
43 '/projects/2.xml',
44 {},
45 {:success_code => :ok})
46
47 test "GET /projects.xml should return projects" do
33 48 get '/projects.xml'
34 49 assert_response :success
35 50 assert_equal 'application/xml', @response.content_type
36 51
37 52 assert_tag :tag => 'projects',
38 53 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
39 54 end
40 end
41 55
42 context ".json" do
43 should "return projects" do
56 test "GET /projects.json should return projects" do
44 57 get '/projects.json'
45 58 assert_response :success
46 59 assert_equal 'application/json', @response.content_type
47 60
48 61 json = ActiveSupport::JSON.decode(response.body)
49 62 assert_kind_of Hash, json
50 63 assert_kind_of Array, json['projects']
51 64 assert_kind_of Hash, json['projects'].first
52 65 assert json['projects'].first.has_key?('id')
53 66 end
54 end
55 end
56
57 context "GET /projects/:id" do
58 context ".xml" do
59 # TODO: A private project is needed because should_allow_api_authentication
60 # actually tests that authentication is *required*, not just allowed
61 should_allow_api_authentication(:get, "/projects/2.xml")
62 67
63 should "return requested project" do
68 test "GET /projects/:id.xml should return the project" do
64 69 get '/projects/1.xml'
65 70 assert_response :success
66 71 assert_equal 'application/xml', @response.content_type
67 72
68 73 assert_tag :tag => 'project',
69 74 :child => {:tag => 'id', :content => '1'}
70 75 assert_tag :tag => 'custom_field',
71 76 :attributes => {:name => 'Development status'}, :content => 'Stable'
72 77
73 78 assert_no_tag 'trackers'
74 79 assert_no_tag 'issue_categories'
75 80 end
76 81
77 context "with hidden custom fields" do
78 setup do
79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
82 test "GET /projects/:id.json should return the project" do
83 get '/projects/1.json'
84
85 json = ActiveSupport::JSON.decode(response.body)
86 assert_kind_of Hash, json
87 assert_kind_of Hash, json['project']
88 assert_equal 1, json['project']['id']
80 89 end
81 90
82 should "not display hidden custom fields" do
91 test "GET /projects/:id.xml with hidden custom fields should not display hidden custom fields" do
92 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
93
83 94 get '/projects/1.xml'
84 95 assert_response :success
85 96 assert_equal 'application/xml', @response.content_type
86 97
87 98 assert_no_tag 'custom_field',
88 99 :attributes => {:name => 'Development status'}
89 100 end
90 end
91 101
92 should "return categories with include=issue_categories" do
102 test "GET /projects/:id.xml with include=issue_categories should return categories" do
93 103 get '/projects/1.xml?include=issue_categories'
94 104 assert_response :success
95 105 assert_equal 'application/xml', @response.content_type
96 106
97 107 assert_tag 'issue_categories',
98 108 :attributes => {:type => 'array'},
99 109 :child => {
100 110 :tag => 'issue_category',
101 111 :attributes => {
102 112 :id => '2',
103 113 :name => 'Recipes'
104 114 }
105 115 }
106 116 end
107 117
108 should "return trackers with include=trackers" do
118 test "GET /projects/:id.xml with include=trackers should return trackers" do
109 119 get '/projects/1.xml?include=trackers'
110 120 assert_response :success
111 121 assert_equal 'application/xml', @response.content_type
112 122
113 123 assert_tag 'trackers',
114 124 :attributes => {:type => 'array'},
115 125 :child => {
116 126 :tag => 'tracker',
117 127 :attributes => {
118 128 :id => '2',
119 129 :name => 'Feature request'
120 130 }
121 131 }
122 132 end
123 end
124
125 context ".json" do
126 should_allow_api_authentication(:get, "/projects/2.json")
127
128 should "return requested project" do
129 get '/projects/1.json'
130 133
131 json = ActiveSupport::JSON.decode(response.body)
132 assert_kind_of Hash, json
133 assert_kind_of Hash, json['project']
134 assert_equal 1, json['project']['id']
135 end
136 end
137 end
138
139 context "POST /projects" do
140 context "with valid parameters" do
141 setup do
134 test "POST /projects.xml with valid parameters should create the project" do
142 135 Setting.default_projects_modules = ['issue_tracking', 'repository']
143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
144 end
145 136
146 context ".xml" do
147 should_allow_api_authentication(:post,
148 '/projects.xml',
149 {:project => {:name => 'API test', :identifier => 'api-test'}},
150 {:success_code => :created})
151
152
153 should "create a project with the attributes" do
154 137 assert_difference('Project.count') do
155 post '/projects.xml', @parameters, credentials('admin')
138 post '/projects.xml',
139 {:project => {:name => 'API test', :identifier => 'api-test'}},
140 credentials('admin')
156 141 end
157 142
158 143 project = Project.first(:order => 'id DESC')
159 144 assert_equal 'API test', project.name
160 145 assert_equal 'api-test', project.identifier
161 146 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
162 147 assert_equal Tracker.all.size, project.trackers.size
163 148
164 149 assert_response :created
165 150 assert_equal 'application/xml', @response.content_type
166 151 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
167 152 end
168 153
169 should "accept enabled_module_names attribute" do
170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
171
154 test "POST /projects.xml should accept enabled_module_names attribute" do
172 155 assert_difference('Project.count') do
173 post '/projects.xml', @parameters, credentials('admin')
156 post '/projects.xml',
157 {:project => {:name => 'API test', :identifier => 'api-test', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}},
158 credentials('admin')
174 159 end
175 160
176 161 project = Project.first(:order => 'id DESC')
177 162 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
178 163 end
179 164
180 should "accept tracker_ids attribute" do
181 @parameters[:project].merge!({:tracker_ids => [1, 3]})
182
165 test "POST /projects.xml should accept tracker_ids attribute" do
183 166 assert_difference('Project.count') do
184 post '/projects.xml', @parameters, credentials('admin')
167 post '/projects.xml',
168 {:project => {:name => 'API test', :identifier => 'api-test', :tracker_ids => [1, 3]}},
169 credentials('admin')
185 170 end
186 171
187 172 project = Project.first(:order => 'id DESC')
188 173 assert_equal [1, 3], project.trackers.map(&:id).sort
189 174 end
190 end
191 end
192
193 context "with invalid parameters" do
194 setup do
195 @parameters = {:project => {:name => 'API test'}}
196 end
197 175
198 context ".xml" do
199 should "return errors" do
176 test "POST /projects.xml with invalid parameters should return errors" do
200 177 assert_no_difference('Project.count') do
201 post '/projects.xml', @parameters, credentials('admin')
178 post '/projects.xml', {:project => {:name => 'API test'}}, credentials('admin')
202 179 end
203 180
204 181 assert_response :unprocessable_entity
205 182 assert_equal 'application/xml', @response.content_type
206 183 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
207 184 end
208 end
209 end
210 end
211
212 context "PUT /projects/:id" do
213 context "with valid parameters" do
214 setup do
215 @parameters = {:project => {:name => 'API update'}}
216 end
217 185
218 context ".xml" do
219 should_allow_api_authentication(:put,
220 '/projects/2.xml',
221 {:project => {:name => 'API update'}},
222 {:success_code => :ok})
223
224 should "update the project" do
186 test "PUT /projects/:id.xml with valid parameters should update the project" do
225 187 assert_no_difference 'Project.count' do
226 put '/projects/2.xml', @parameters, credentials('jsmith')
188 put '/projects/2.xml', {:project => {:name => 'API update'}}, credentials('jsmith')
227 189 end
228 190 assert_response :ok
229 191 assert_equal '', @response.body
230 192 assert_equal 'application/xml', @response.content_type
231 193 project = Project.find(2)
232 194 assert_equal 'API update', project.name
233 195 end
234 196
235 should "accept enabled_module_names attribute" do
236 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
237
197 test "PUT /projects/:id.xml should accept enabled_module_names attribute" do
238 198 assert_no_difference 'Project.count' do
239 put '/projects/2.xml', @parameters, credentials('admin')
199 put '/projects/2.xml', {:project => {:name => 'API update', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, credentials('admin')
240 200 end
241 201 assert_response :ok
242 202 assert_equal '', @response.body
243 203 project = Project.find(2)
244 204 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
245 205 end
246 206
247 should "accept tracker_ids attribute" do
248 @parameters[:project].merge!({:tracker_ids => [1, 3]})
249
207 test "PUT /projects/:id.xml should accept tracker_ids attribute" do
250 208 assert_no_difference 'Project.count' do
251 put '/projects/2.xml', @parameters, credentials('admin')
209 put '/projects/2.xml', {:project => {:name => 'API update', :tracker_ids => [1, 3]}}, credentials('admin')
252 210 end
253 211 assert_response :ok
254 212 assert_equal '', @response.body
255 213 project = Project.find(2)
256 214 assert_equal [1, 3], project.trackers.map(&:id).sort
257 215 end
258 end
259 end
260
261 context "with invalid parameters" do
262 setup do
263 @parameters = {:project => {:name => ''}}
264 end
265 216
266 context ".xml" do
267 should "return errors" do
217 test "PUT /projects/:id.xml with invalid parameters should return errors" do
268 218 assert_no_difference('Project.count') do
269 put '/projects/2.xml', @parameters, credentials('admin')
219 put '/projects/2.xml', {:project => {:name => ''}}, credentials('admin')
270 220 end
271 221
272 222 assert_response :unprocessable_entity
273 223 assert_equal 'application/xml', @response.content_type
274 224 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
275 225 end
276 end
277 end
278 end
279
280 context "DELETE /projects/:id" do
281 context ".xml" do
282 should_allow_api_authentication(:delete,
283 '/projects/2.xml',
284 {},
285 {:success_code => :ok})
286 226
287 should "delete the project" do
227 test "DELETE /projects/:id.xml should delete the project" do
288 228 assert_difference('Project.count',-1) do
289 229 delete '/projects/2.xml', {}, credentials('admin')
290 230 end
291 231 assert_response :ok
292 232 assert_equal '', @response.body
293 233 assert_nil Project.find_by_id(2)
294 234 end
295 235 end
296 end
297 end
@@ -1,58 +1,53
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::QueriesTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :queries
29 29
30 30 def setup
31 31 Setting.rest_api_enabled = '1'
32 32 end
33 33
34 context "/queries" do
35 context "GET" do
36
37 should "return queries" do
34 test "GET /queries.xml should return queries" do
38 35 get '/queries.xml'
39 36
40 37 assert_response :success
41 38 assert_equal 'application/xml', @response.content_type
42 39 assert_tag :tag => 'queries',
43 40 :attributes => {:type => 'array'},
44 41 :child => {
45 42 :tag => 'query',
46 43 :child => {
47 44 :tag => 'id',
48 45 :content => '4',
49 46 :sibling => {
50 47 :tag => 'name',
51 48 :content => 'Public query for all projects'
52 49 }
53 50 }
54 51 }
55 52 end
56 53 end
57 end
58 end
@@ -1,90 +1,76
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
21 21 fixtures :roles
22 22
23 23 def setup
24 24 Setting.rest_api_enabled = '1'
25 25 end
26 26
27 context "/roles" do
28 context "GET" do
29 context "xml" do
30 should "return the roles" do
27 test "GET /roles.xml should return the roles" do
31 28 get '/roles.xml'
32 29
33 30 assert_response :success
34 31 assert_equal 'application/xml', @response.content_type
35 32 assert_equal 3, assigns(:roles).size
36 33
37 34 assert_tag :tag => 'roles',
38 35 :attributes => {:type => 'array'},
39 36 :child => {
40 37 :tag => 'role',
41 38 :child => {
42 39 :tag => 'id',
43 40 :content => '2',
44 41 :sibling => {
45 42 :tag => 'name',
46 43 :content => 'Developer'
47 44 }
48 45 }
49 46 }
50 47 end
51 end
52 48
53 context "json" do
54 should "return the roles" do
49 test "GET /roles.json should return the roles" do
55 50 get '/roles.json'
56 51
57 52 assert_response :success
58 53 assert_equal 'application/json', @response.content_type
59 54 assert_equal 3, assigns(:roles).size
60 55
61 56 json = ActiveSupport::JSON.decode(response.body)
62 57 assert_kind_of Hash, json
63 58 assert_kind_of Array, json['roles']
64 59 assert_include({'id' => 2, 'name' => 'Developer'}, json['roles'])
65 60 end
66 end
67 end
68 end
69 61
70 context "/roles/:id" do
71 context "GET" do
72 context "xml" do
73 should "return the role" do
62 test "GET /roles/:id.xml should return the role" do
74 63 get '/roles/1.xml'
75 64
76 65 assert_response :success
77 66 assert_equal 'application/xml', @response.content_type
78 67
79 68 assert_select 'role' do
80 69 assert_select 'name', :text => 'Manager'
81 70 assert_select 'role permissions[type=array]' do
82 71 assert_select 'permission', Role.find(1).permissions.size
83 72 assert_select 'permission', :text => 'view_issues'
84 73 end
85 74 end
86 75 end
87 76 end
88 end
89 end
90 end
@@ -1,164 +1,142
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :time_entries
29 29
30 30 def setup
31 31 Setting.rest_api_enabled = '1'
32 32 end
33 33
34 context "GET /time_entries.xml" do
35 should "return time entries" do
34 test "GET /time_entries.xml should return time entries" do
36 35 get '/time_entries.xml', {}, credentials('jsmith')
37 36 assert_response :success
38 37 assert_equal 'application/xml', @response.content_type
39 38 assert_tag :tag => 'time_entries',
40 39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
41 40 end
42 41
43 context "with limit" do
44 should "return limited results" do
42 test "GET /time_entries.xml with limit should return limited results" do
45 43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
46 44 assert_response :success
47 45 assert_equal 'application/xml', @response.content_type
48 46 assert_tag :tag => 'time_entries',
49 47 :children => {:count => 2}
50 48 end
51 end
52 end
53 49
54 context "GET /time_entries/2.xml" do
55 should "return requested time entry" do
50 test "GET /time_entries/:id.xml should return the time entry" do
56 51 get '/time_entries/2.xml', {}, credentials('jsmith')
57 52 assert_response :success
58 53 assert_equal 'application/xml', @response.content_type
59 54 assert_tag :tag => 'time_entry',
60 55 :child => {:tag => 'id', :content => '2'}
61 56 end
62 end
63 57
64 context "POST /time_entries.xml" do
65 context "with issue_id" do
66 should "return create time entry" do
58 test "POST /time_entries.xml with issue_id should create time entry" do
67 59 assert_difference 'TimeEntry.count' do
68 60 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
69 61 end
70 62 assert_response :created
71 63 assert_equal 'application/xml', @response.content_type
72 64
73 65 entry = TimeEntry.first(:order => 'id DESC')
74 66 assert_equal 'jsmith', entry.user.login
75 67 assert_equal Issue.find(1), entry.issue
76 68 assert_equal Project.find(1), entry.project
77 69 assert_equal Date.parse('2010-12-02'), entry.spent_on
78 70 assert_equal 3.5, entry.hours
79 71 assert_equal TimeEntryActivity.find(11), entry.activity
80 72 end
81 73
82 should "accept custom fields" do
74 test "POST /time_entries.xml with issue_id should accept custom fields" do
83 75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
84 76
85 77 assert_difference 'TimeEntry.count' do
86 78 post '/time_entries.xml', {:time_entry => {
87 79 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
88 80 }}, credentials('jsmith')
89 81 end
90 82 assert_response :created
91 83 assert_equal 'application/xml', @response.content_type
92 84
93 85 entry = TimeEntry.first(:order => 'id DESC')
94 86 assert_equal 'accepted', entry.custom_field_value(field)
95 87 end
96 end
97 88
98 context "with project_id" do
99 should "return create time entry" do
89 test "POST /time_entries.xml with project_id should create time entry" do
100 90 assert_difference 'TimeEntry.count' do
101 91 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
102 92 end
103 93 assert_response :created
104 94 assert_equal 'application/xml', @response.content_type
105 95
106 96 entry = TimeEntry.first(:order => 'id DESC')
107 97 assert_equal 'jsmith', entry.user.login
108 98 assert_nil entry.issue
109 99 assert_equal Project.find(1), entry.project
110 100 assert_equal Date.parse('2010-12-02'), entry.spent_on
111 101 assert_equal 3.5, entry.hours
112 102 assert_equal TimeEntryActivity.find(11), entry.activity
113 103 end
114 end
115 104
116 context "with invalid parameters" do
117 should "return errors" do
105 test "POST /time_entries.xml with invalid parameters should return errors" do
118 106 assert_no_difference 'TimeEntry.count' do
119 107 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
120 108 end
121 109 assert_response :unprocessable_entity
122 110 assert_equal 'application/xml', @response.content_type
123 111
124 112 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
125 113 end
126 end
127 end
128 114
129 context "PUT /time_entries/2.xml" do
130 context "with valid parameters" do
131 should "update time entry" do
115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
132 116 assert_no_difference 'TimeEntry.count' do
133 117 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
134 118 end
135 119 assert_response :ok
136 120 assert_equal '', @response.body
137 121 assert_equal 'API Update', TimeEntry.find(2).comments
138 122 end
139 end
140 123
141 context "with invalid parameters" do
142 should "return errors" do
124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
143 125 assert_no_difference 'TimeEntry.count' do
144 126 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
145 127 end
146 128 assert_response :unprocessable_entity
147 129 assert_equal 'application/xml', @response.content_type
148 130
149 131 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
150 132 end
151 end
152 end
153 133
154 context "DELETE /time_entries/2.xml" do
155 should "destroy time entry" do
134 test "DELETE /time_entries/:id.xml should destroy time entry" do
156 135 assert_difference 'TimeEntry.count', -1 do
157 136 delete '/time_entries/2.xml', {}, credentials('jsmith')
158 137 end
159 138 assert_response :ok
160 139 assert_equal '', @response.body
161 140 assert_nil TimeEntry.find_by_id(2)
162 141 end
163 142 end
164 end
@@ -1,49 +1,42
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::TokenAuthenticationTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules
28 28
29 29 def setup
30 30 Setting.rest_api_enabled = '1'
31 31 Setting.login_required = '1'
32 32 end
33 33
34 34 def teardown
35 35 Setting.rest_api_enabled = '0'
36 36 Setting.login_required = '0'
37 37 end
38 38
39 39 # Using the NewsController because it's a simple API.
40 context "get /news" do
41 context "in :xml format" do
42 40 should_allow_key_based_auth(:get, "/news.xml")
43 end
44
45 context "in :json format" do
46 41 should_allow_key_based_auth(:get, "/news.json")
47 42 end
48 end
49 end
@@ -1,51 +1,46
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::TrackersTest < Redmine::ApiTest::Base
21 21 fixtures :trackers
22 22
23 23 def setup
24 24 Setting.rest_api_enabled = '1'
25 25 end
26 26
27 context "/trackers" do
28 context "GET" do
29
30 should "return trackers" do
27 test "GET /trackers.xml should return trackers" do
31 28 get '/trackers.xml'
32 29
33 30 assert_response :success
34 31 assert_equal 'application/xml', @response.content_type
35 32 assert_tag :tag => 'trackers',
36 33 :attributes => {:type => 'array'},
37 34 :child => {
38 35 :tag => 'tracker',
39 36 :child => {
40 37 :tag => 'id',
41 38 :content => '2',
42 39 :sibling => {
43 40 :tag => 'name',
44 41 :content => 'Feature request'
45 42 }
46 43 }
47 44 }
48 45 end
49 46 end
50 end
51 end
@@ -1,383 +1,327
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
21 21 fixtures :users, :members, :member_roles, :roles, :projects
22 22
23 23 def setup
24 24 Setting.rest_api_enabled = '1'
25 25 end
26 26
27 context "GET /users" do
28 27 should_allow_api_authentication(:get, "/users.xml")
29 28 should_allow_api_authentication(:get, "/users.json")
30 end
29 should_allow_api_authentication(:post,
30 '/users.xml',
31 {:user => {
32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
33 :mail => 'foo@example.net', :password => 'secret123'
34 }},
35 {:success_code => :created})
36 should_allow_api_authentication(:post,
37 '/users.json',
38 {:user => {
39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
40 :mail => 'foo@example.net'
41 }},
42 {:success_code => :created})
43 should_allow_api_authentication(:put,
44 '/users/2.xml',
45 {:user => {
46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
47 :mail => 'jsmith@somenet.foo'
48 }},
49 {:success_code => :ok})
50 should_allow_api_authentication(:put,
51 '/users/2.json',
52 {:user => {
53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
54 :mail => 'jsmith@somenet.foo'
55 }},
56 {:success_code => :ok})
57 should_allow_api_authentication(:delete,
58 '/users/2.xml',
59 {},
60 {:success_code => :ok})
61 should_allow_api_authentication(:delete,
62 '/users/2.xml',
63 {},
64 {:success_code => :ok})
31 65
32 context "GET /users/2" do
33 context ".xml" do
34 should "return requested user" do
66 test "GET /users/:id.xml should return the user" do
35 67 get '/users/2.xml'
36 68
37 69 assert_response :success
38 70 assert_tag :tag => 'user',
39 71 :child => {:tag => 'id', :content => '2'}
40 72 end
41 73
42 context "with include=memberships" do
43 should "include memberships" do
44 get '/users/2.xml?include=memberships'
45
46 assert_response :success
47 assert_tag :tag => 'memberships',
48 :parent => {:tag => 'user'},
49 :children => {:count => 1}
50 end
51 end
52 end
53
54 context ".json" do
55 should "return requested user" do
74 test "GET /users/:id.json should return the user" do
56 75 get '/users/2.json'
57 76
58 77 assert_response :success
59 78 json = ActiveSupport::JSON.decode(response.body)
60 79 assert_kind_of Hash, json
61 80 assert_kind_of Hash, json['user']
62 81 assert_equal 2, json['user']['id']
63 82 end
64 83
65 context "with include=memberships" do
66 should "include memberships" do
84 test "GET /users/:id.xml with include=memberships should include memberships" do
85 get '/users/2.xml?include=memberships'
86
87 assert_response :success
88 assert_tag :tag => 'memberships',
89 :parent => {:tag => 'user'},
90 :children => {:count => 1}
91 end
92
93 test "GET /users/:id.json with include=memberships should include memberships" do
67 94 get '/users/2.json?include=memberships'
68 95
69 96 assert_response :success
70 97 json = ActiveSupport::JSON.decode(response.body)
71 98 assert_kind_of Array, json['user']['memberships']
72 99 assert_equal [{
73 100 "id"=>1,
74 101 "project"=>{"name"=>"eCookbook", "id"=>1},
75 102 "roles"=>[{"name"=>"Manager", "id"=>1}]
76 103 }], json['user']['memberships']
77 104 end
78 end
79 end
80 end
81 105
82 context "GET /users/current" do
83 context ".xml" do
84 should "require authentication" do
106 test "GET /users/current.xml should require authentication" do
85 107 get '/users/current.xml'
86 108
87 109 assert_response 401
88 110 end
89 111
90 should "return current user" do
112 test "GET /users/current.xml should return current user" do
91 113 get '/users/current.xml', {}, credentials('jsmith')
92 114
93 115 assert_tag :tag => 'user',
94 116 :child => {:tag => 'id', :content => '2'}
95 117 end
96 end
97 end
98 118
99 119 test "GET /users/:id should not return login for other user" do
100 120 get '/users/3.xml', {}, credentials('jsmith')
101 121 assert_response :success
102 122 assert_no_tag 'user', :child => {:tag => 'login'}
103 123 end
104 124
105 125 test "GET /users/:id should return login for current user" do
106 126 get '/users/2.xml', {}, credentials('jsmith')
107 127 assert_response :success
108 128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
109 129 end
110 130
111 131 test "GET /users/:id should not return api_key for other user" do
112 132 get '/users/3.xml', {}, credentials('jsmith')
113 133 assert_response :success
114 134 assert_no_tag 'user', :child => {:tag => 'api_key'}
115 135 end
116 136
117 137 test "GET /users/:id should return api_key for current user" do
118 138 get '/users/2.xml', {}, credentials('jsmith')
119 139 assert_response :success
120 140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
121 141 end
122 142
123 143 test "GET /users/:id should not return status for standard user" do
124 144 get '/users/3.xml', {}, credentials('jsmith')
125 145 assert_response :success
126 146 assert_no_tag 'user', :child => {:tag => 'status'}
127 147 end
128 148
129 149 test "GET /users/:id should return status for administrators" do
130 150 get '/users/2.xml', {}, credentials('admin')
131 151 assert_response :success
132 152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
133 153 end
134 154
135 context "POST /users" do
136 context "with valid parameters" do
137 setup do
138 @parameters = {
155 test "POST /users.xml with valid parameters should create the user" do
156 assert_difference('User.count') do
157 post '/users.xml', {
139 158 :user => {
140 159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
141 160 :mail => 'foo@example.net', :password => 'secret123',
142 :mail_notification => 'only_assigned'
143 }
144 }
145 end
146
147 context ".xml" do
148 should_allow_api_authentication(:post,
149 '/users.xml',
150 {:user => {
151 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
152 :mail => 'foo@example.net', :password => 'secret123'
153 }},
154 {:success_code => :created})
155
156 should "create a user with the attributes" do
157 assert_difference('User.count') do
158 post '/users.xml', @parameters, credentials('admin')
161 :mail_notification => 'only_assigned'}
162 },
163 credentials('admin')
159 164 end
160 165
161 166 user = User.first(:order => 'id DESC')
162 167 assert_equal 'foo', user.login
163 168 assert_equal 'Firstname', user.firstname
164 169 assert_equal 'Lastname', user.lastname
165 170 assert_equal 'foo@example.net', user.mail
166 171 assert_equal 'only_assigned', user.mail_notification
167 172 assert !user.admin?
168 173 assert user.check_password?('secret123')
169 174
170 175 assert_response :created
171 176 assert_equal 'application/xml', @response.content_type
172 177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
173 178 end
174 end
175
176 context ".json" do
177 should_allow_api_authentication(:post,
178 '/users.json',
179 {:user => {
180 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
181 :mail => 'foo@example.net'
182 }},
183 {:success_code => :created})
184 179
185 should "create a user with the attributes" do
180 test "POST /users.json with valid parameters should create the user" do
186 181 assert_difference('User.count') do
187 post '/users.json', @parameters, credentials('admin')
182 post '/users.json', {
183 :user => {
184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
185 :mail => 'foo@example.net', :password => 'secret123',
186 :mail_notification => 'only_assigned'}
187 },
188 credentials('admin')
188 189 end
189 190
190 191 user = User.first(:order => 'id DESC')
191 192 assert_equal 'foo', user.login
192 193 assert_equal 'Firstname', user.firstname
193 194 assert_equal 'Lastname', user.lastname
194 195 assert_equal 'foo@example.net', user.mail
195 196 assert !user.admin?
196 197
197 198 assert_response :created
198 199 assert_equal 'application/json', @response.content_type
199 200 json = ActiveSupport::JSON.decode(response.body)
200 201 assert_kind_of Hash, json
201 202 assert_kind_of Hash, json['user']
202 203 assert_equal user.id, json['user']['id']
203 204 end
204 end
205 end
206 205
207 context "with invalid parameters" do
208 setup do
209 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
210 end
211
212 context ".xml" do
213 should "return errors" do
206 test "POST /users.xml with with invalid parameters should return errors" do
214 207 assert_no_difference('User.count') do
215 post '/users.xml', @parameters, credentials('admin')
208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
216 209 end
217 210
218 211 assert_response :unprocessable_entity
219 212 assert_equal 'application/xml', @response.content_type
220 213 assert_tag 'errors', :child => {
221 214 :tag => 'error',
222 215 :content => "First name can't be blank"
223 216 }
224 217 end
225 end
226 218
227 context ".json" do
228 should "return errors" do
219 test "POST /users.json with with invalid parameters should return errors" do
229 220 assert_no_difference('User.count') do
230 post '/users.json', @parameters, credentials('admin')
221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
231 222 end
232 223
233 224 assert_response :unprocessable_entity
234 225 assert_equal 'application/json', @response.content_type
235 226 json = ActiveSupport::JSON.decode(response.body)
236 227 assert_kind_of Hash, json
237 228 assert json.has_key?('errors')
238 229 assert_kind_of Array, json['errors']
239 230 end
240 end
241 end
242 end
243 231
244 context "PUT /users/2" do
245 context "with valid parameters" do
246 setup do
247 @parameters = {
232 test "PUT /users/:id.xml with valid parameters should update the user" do
233 assert_no_difference('User.count') do
234 put '/users/2.xml', {
248 235 :user => {
249 236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
250 :mail => 'jsmith@somenet.foo'
251 }
252 }
253 end
254
255 context ".xml" do
256 should_allow_api_authentication(:put,
257 '/users/2.xml',
258 {:user => {
259 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
260 :mail => 'jsmith@somenet.foo'
261 }},
262 {:success_code => :ok})
263
264 should "update user with the attributes" do
265 assert_no_difference('User.count') do
266 put '/users/2.xml', @parameters, credentials('admin')
237 :mail => 'jsmith@somenet.foo'}
238 },
239 credentials('admin')
267 240 end
268 241
269 242 user = User.find(2)
270 243 assert_equal 'jsmith', user.login
271 244 assert_equal 'John', user.firstname
272 245 assert_equal 'Renamed', user.lastname
273 246 assert_equal 'jsmith@somenet.foo', user.mail
274 247 assert !user.admin?
275 248
276 249 assert_response :ok
277 250 assert_equal '', @response.body
278 251 end
279 end
280 252
281 context ".json" do
282 should_allow_api_authentication(:put,
283 '/users/2.json',
284 {:user => {
285 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
286 :mail => 'jsmith@somenet.foo'
287 }},
288 {:success_code => :ok})
289
290 should "update user with the attributes" do
253 test "PUT /users/:id.json with valid parameters should update the user" do
291 254 assert_no_difference('User.count') do
292 put '/users/2.json', @parameters, credentials('admin')
255 put '/users/2.json', {
256 :user => {
257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
258 :mail => 'jsmith@somenet.foo'}
259 },
260 credentials('admin')
293 261 end
294 262
295 263 user = User.find(2)
296 264 assert_equal 'jsmith', user.login
297 265 assert_equal 'John', user.firstname
298 266 assert_equal 'Renamed', user.lastname
299 267 assert_equal 'jsmith@somenet.foo', user.mail
300 268 assert !user.admin?
301 269
302 270 assert_response :ok
303 271 assert_equal '', @response.body
304 272 end
305 end
306 end
307 273
308 context "with invalid parameters" do
309 setup do
310 @parameters = {
274 test "PUT /users/:id.xml with invalid parameters" do
275 assert_no_difference('User.count') do
276 put '/users/2.xml', {
311 277 :user => {
312 278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
313 :mail => 'foo'
314 }
315 }
316 end
317
318 context ".xml" do
319 should "return errors" do
320 assert_no_difference('User.count') do
321 put '/users/2.xml', @parameters, credentials('admin')
279 :mail => 'foo'}
280 },
281 credentials('admin')
322 282 end
323 283
324 284 assert_response :unprocessable_entity
325 285 assert_equal 'application/xml', @response.content_type
326 286 assert_tag 'errors', :child => {
327 287 :tag => 'error',
328 288 :content => "First name can't be blank"
329 289 }
330 290 end
331 end
332 291
333 context ".json" do
334 should "return errors" do
292 test "PUT /users/:id.json with invalid parameters" do
335 293 assert_no_difference('User.count') do
336 put '/users/2.json', @parameters, credentials('admin')
294 put '/users/2.json', {
295 :user => {
296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
297 :mail => 'foo'}
298 },
299 credentials('admin')
337 300 end
338 301
339 302 assert_response :unprocessable_entity
340 303 assert_equal 'application/json', @response.content_type
341 304 json = ActiveSupport::JSON.decode(response.body)
342 305 assert_kind_of Hash, json
343 306 assert json.has_key?('errors')
344 307 assert_kind_of Array, json['errors']
345 308 end
346 end
347 end
348 end
349
350 context "DELETE /users/2" do
351 context ".xml" do
352 should_allow_api_authentication(:delete,
353 '/users/2.xml',
354 {},
355 {:success_code => :ok})
356 309
357 should "delete user" do
310 test "DELETE /users/:id.xml should delete the user" do
358 311 assert_difference('User.count', -1) do
359 312 delete '/users/2.xml', {}, credentials('admin')
360 313 end
361 314
362 315 assert_response :ok
363 316 assert_equal '', @response.body
364 317 end
365 end
366 318
367 context ".json" do
368 should_allow_api_authentication(:delete,
369 '/users/2.xml',
370 {},
371 {:success_code => :ok})
372
373 should "delete user" do
319 test "DELETE /users/:id.json should delete the user" do
374 320 assert_difference('User.count', -1) do
375 321 delete '/users/2.json', {}, credentials('admin')
376 322 end
377 323
378 324 assert_response :ok
379 325 assert_equal '', @response.body
380 326 end
381 327 end
382 end
383 end
@@ -1,158 +1,142
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :versions
29 29
30 30 def setup
31 31 Setting.rest_api_enabled = '1'
32 32 end
33 33
34 context "/projects/:project_id/versions" do
35 context "GET" do
36 should "return project versions" do
34 test "GET /projects/:project_id/versions.xml should return project versions" do
37 35 get '/projects/1/versions.xml'
38 36
39 37 assert_response :success
40 38 assert_equal 'application/xml', @response.content_type
41 39 assert_tag :tag => 'versions',
42 40 :attributes => {:type => 'array'},
43 41 :child => {
44 42 :tag => 'version',
45 43 :child => {
46 44 :tag => 'id',
47 45 :content => '2',
48 46 :sibling => {
49 47 :tag => 'name',
50 48 :content => '1.0'
51 49 }
52 50 }
53 51 }
54 52 end
55 end
56 53
57 context "POST" do
58 should "create the version" do
54 test "POST /projects/:project_id/versions.xml should create the version" do
59 55 assert_difference 'Version.count' do
60 56 post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
61 57 end
62 58
63 59 version = Version.first(:order => 'id DESC')
64 60 assert_equal 'API test', version.name
65 61
66 62 assert_response :created
67 63 assert_equal 'application/xml', @response.content_type
68 64 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
69 65 end
70 66
71 should "create the version with due date" do
67 test "POST /projects/:project_id/versions.xml should create the version with due date" do
72 68 assert_difference 'Version.count' do
73 69 post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
74 70 end
75 71
76 72 version = Version.first(:order => 'id DESC')
77 73 assert_equal 'API test', version.name
78 74 assert_equal Date.parse('2012-01-24'), version.due_date
79 75
80 76 assert_response :created
81 77 assert_equal 'application/xml', @response.content_type
82 78 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
83 79 end
84 80
85 should "create the version with custom fields" do
81 test "POST /projects/:project_id/versions.xml should create the version with custom fields" do
86 82 field = VersionCustomField.generate!
87 83
88 84 assert_difference 'Version.count' do
89 85 post '/projects/1/versions.xml', {
90 86 :version => {
91 87 :name => 'API test',
92 88 :custom_fields => [
93 89 {'id' => field.id.to_s, 'value' => 'Some value'}
94 90 ]
95 91 }
96 92 }, credentials('jsmith')
97 93 end
98 94
99 95 version = Version.first(:order => 'id DESC')
100 96 assert_equal 'API test', version.name
101 97 assert_equal 'Some value', version.custom_field_value(field)
102 98
103 99 assert_response :created
104 100 assert_equal 'application/xml', @response.content_type
105 101 assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
106 102 end
107 103
108 context "with failure" do
109 should "return the errors" do
104 test "POST /projects/:project_id/versions.xml with failure should return the errors" do
110 105 assert_no_difference('Version.count') do
111 106 post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
112 107 end
113 108
114 109 assert_response :unprocessable_entity
115 110 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
116 111 end
117 end
118 end
119 end
120 112
121 context "/versions/:id" do
122 context "GET" do
123 should "return the version" do
113 test "GET /versions/:id.xml should return the version" do
124 114 get '/versions/2.xml'
125 115
126 116 assert_response :success
127 117 assert_equal 'application/xml', @response.content_type
128 118 assert_select 'version' do
129 119 assert_select 'id', :text => '2'
130 120 assert_select 'name', :text => '1.0'
131 121 assert_select 'sharing', :text => 'none'
132 122 end
133 123 end
134 end
135 124
136 context "PUT" do
137 should "update the version" do
125 test "PUT /versions/:id.xml should update the version" do
138 126 put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
139 127
140 128 assert_response :ok
141 129 assert_equal '', @response.body
142 130 assert_equal 'API update', Version.find(2).name
143 131 end
144 end
145 132
146 context "DELETE" do
147 should "destroy the version" do
133 test "DELETE /versions/:id.xml should destroy the version" do
148 134 assert_difference 'Version.count', -1 do
149 135 delete '/versions/3.xml', {}, credentials('jsmith')
150 136 end
151 137
152 138 assert_response :ok
153 139 assert_equal '', @response.body
154 140 assert_nil Version.find_by_id(3)
155 141 end
156 142 end
157 end
158 end
General Comments 0
You need to be logged in to leave comments. Login now