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