@@ -1,132 +1,187 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2010 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2010 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require "#{File.dirname(__FILE__)}/../test_helper" |
|
18 | require "#{File.dirname(__FILE__)}/../test_helper" | |
19 |
|
19 | |||
20 | class IssuesApiTest < ActionController::IntegrationTest |
|
20 | class IssuesApiTest < ActionController::IntegrationTest | |
21 | fixtures :projects, |
|
21 | fixtures :projects, | |
22 | :users, |
|
22 | :users, | |
23 | :roles, |
|
23 | :roles, | |
24 | :members, |
|
24 | :members, | |
25 | :member_roles, |
|
25 | :member_roles, | |
26 | :issues, |
|
26 | :issues, | |
27 | :issue_statuses, |
|
27 | :issue_statuses, | |
28 | :versions, |
|
28 | :versions, | |
29 | :trackers, |
|
29 | :trackers, | |
30 | :projects_trackers, |
|
30 | :projects_trackers, | |
31 | :issue_categories, |
|
31 | :issue_categories, | |
32 | :enabled_modules, |
|
32 | :enabled_modules, | |
33 | :enumerations, |
|
33 | :enumerations, | |
34 | :attachments, |
|
34 | :attachments, | |
35 | :workflows, |
|
35 | :workflows, | |
36 | :custom_fields, |
|
36 | :custom_fields, | |
37 | :custom_values, |
|
37 | :custom_values, | |
38 | :custom_fields_projects, |
|
38 | :custom_fields_projects, | |
39 | :custom_fields_trackers, |
|
39 | :custom_fields_trackers, | |
40 | :time_entries, |
|
40 | :time_entries, | |
41 | :journals, |
|
41 | :journals, | |
42 | :journal_details, |
|
42 | :journal_details, | |
43 | :queries |
|
43 | :queries | |
44 |
|
44 | |||
45 | def setup |
|
45 | def setup | |
46 | Setting.rest_api_enabled = '1' |
|
46 | Setting.rest_api_enabled = '1' | |
47 | end |
|
47 | end | |
48 |
|
48 | |||
49 | def test_index |
|
49 | context "/index.xml" do | |
50 | get '/issues.xml' |
|
50 | setup do | |
51 | assert_response :success |
|
51 | get '/issues.xml' | |
52 | assert_equal 'application/xml', @response.content_type |
|
52 | end | |
|
53 | ||||
|
54 | should_respond_with :success | |||
|
55 | should_respond_with_content_type 'application/xml' | |||
53 | end |
|
56 | end | |
54 |
|
57 | |||
55 |
|
|
58 | context "/index.xml with filter" do | |
56 | get '/issues.xml?status_id=5' |
|
59 | setup do | |
57 | assert_response :success |
|
60 | get '/issues.xml?status_id=5' | |
58 | assert_equal 'application/xml', @response.content_type |
|
61 | end | |
59 | assert_tag :tag => 'issues', |
|
|||
60 | :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), |
|
|||
61 | :only => { :tag => 'issue' } } |
|
|||
62 | end |
|
|||
63 |
|
62 | |||
64 | def test_show |
|
63 | should_respond_with :success | |
65 | get '/issues/1.xml' |
|
64 | should_respond_with_content_type 'application/xml' | |
66 | assert_response :success |
|
65 | should "show only issues with the status_id" do | |
67 | assert_equal 'application/xml', @response.content_type |
|
66 | assert_tag :tag => 'issues', | |
|
67 | :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), | |||
|
68 | :only => { :tag => 'issue' } } | |||
|
69 | end | |||
68 | end |
|
70 | end | |
|
71 | ||||
|
72 | context "/issues/1.xml" do | |||
|
73 | setup do | |||
|
74 | get '/issues/1.xml' | |||
|
75 | end | |||
69 |
|
76 | |||
70 | def test_create |
|
77 | should_respond_with :success | |
71 | attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3} |
|
78 | should_respond_with_content_type 'application/xml' | |
72 | assert_difference 'Issue.count' do |
|
79 | end | |
73 | post '/issues.xml', {:issue => attributes}, :authorization => credentials('jsmith') |
|
80 | ||
|
81 | context "POST /issues.xml" do | |||
|
82 | setup do | |||
|
83 | @issue_count = Issue.count | |||
|
84 | @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3} | |||
|
85 | post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |||
74 | end |
|
86 | end | |
75 | assert_response :created |
|
87 | ||
76 | assert_equal 'application/xml', @response.content_type |
|
88 | should_respond_with :created | |
77 | issue = Issue.first(:order => 'id DESC') |
|
89 | should_respond_with_content_type 'application/xml' | |
78 | attributes.each do |attribute, value| |
|
90 | ||
79 | assert_equal value, issue.send(attribute) |
|
91 | should "create an issue with the attributes" do | |
|
92 | assert_equal Issue.count, @issue_count + 1 | |||
|
93 | ||||
|
94 | issue = Issue.first(:order => 'id DESC') | |||
|
95 | @attributes.each do |attribute, value| | |||
|
96 | assert_equal value, issue.send(attribute) | |||
|
97 | end | |||
80 | end |
|
98 | end | |
81 | end |
|
99 | end | |
82 |
|
100 | |||
83 | def test_create_failure |
|
101 | context "POST /issues.xml with failure" do | |
84 | attributes = {:project_id => 1} |
|
102 | setup do | |
85 | assert_no_difference 'Issue.count' do |
|
103 | @attributes = {:project_id => 1} | |
86 | post '/issues.xml', {:issue => attributes}, :authorization => credentials('jsmith') |
|
104 | post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |
87 | end |
|
105 | end | |
88 | assert_response :unprocessable_entity |
|
106 | ||
89 | assert_equal 'application/xml', @response.content_type |
|
107 | should_respond_with :unprocessable_entity | |
90 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} |
|
108 | should_respond_with_content_type 'application/xml' | |
|
109 | ||||
|
110 | should "have an errors tag" do | |||
|
111 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |||
|
112 | end | |||
91 | end |
|
113 | end | |
92 |
|
114 | |||
93 | def test_update |
|
115 | context "PUT /issues/1.xml" do | |
94 | attributes = {:subject => 'API update'} |
|
116 | setup do | |
95 | assert_no_difference 'Issue.count' do |
|
117 | @issue_count = Issue.count | |
96 | assert_difference 'Journal.count' do |
|
118 | @journal_count = Journal.count | |
97 | put '/issues/1.xml', {:issue => attributes}, :authorization => credentials('jsmith') |
|
119 | @attributes = {:subject => 'API update'} | |
98 | end |
|
120 | ||
|
121 | put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |||
99 | end |
|
122 | end | |
100 | assert_response :ok |
|
123 | ||
101 | assert_equal 'application/xml', @response.content_type |
|
124 | should_respond_with :ok | |
102 | issue = Issue.find(1) |
|
125 | should_respond_with_content_type 'application/xml' | |
103 | attributes.each do |attribute, value| |
|
126 | ||
104 | assert_equal value, issue.send(attribute) |
|
127 | should "not create a new issue" do | |
|
128 | assert_equal Issue.count, @issue_count | |||
105 | end |
|
129 | end | |
106 | end |
|
130 | ||
107 |
|
131 | should "create a new journal" do | ||
108 | def test_update_failure |
|
132 | assert_equal Journal.count, @journal_count + 1 | |
109 | attributes = {:subject => ''} |
|
133 | end | |
110 | assert_no_difference 'Issue.count' do |
|
134 | ||
111 | assert_no_difference 'Journal.count' do |
|
135 | should "update the issue" do | |
112 | put '/issues/1.xml', {:issue => attributes}, :authorization => credentials('jsmith') |
|
136 | issue = Issue.find(1) | |
|
137 | @attributes.each do |attribute, value| | |||
|
138 | assert_equal value, issue.send(attribute) | |||
113 | end |
|
139 | end | |
114 | end |
|
140 | end | |
115 | assert_response :unprocessable_entity |
|
141 | ||
116 | assert_equal 'application/xml', @response.content_type |
|
|||
117 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} |
|
|||
118 | end |
|
142 | end | |
|
143 | ||||
|
144 | context "PUT /issues/1.xml with failed update" do | |||
|
145 | setup do | |||
|
146 | @attributes = {:subject => ''} | |||
|
147 | @issue_count = Issue.count | |||
|
148 | @journal_count = Journal.count | |||
|
149 | ||||
|
150 | put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |||
|
151 | end | |||
119 |
|
152 | |||
120 | def test_destroy |
|
153 | should_respond_with :unprocessable_entity | |
121 | assert_difference 'Issue.count', -1 do |
|
154 | should_respond_with_content_type 'application/xml' | |
|
155 | ||||
|
156 | should "not create a new issue" do | |||
|
157 | assert_equal Issue.count, @issue_count | |||
|
158 | end | |||
|
159 | ||||
|
160 | should "not create a new journal" do | |||
|
161 | assert_equal Journal.count, @journal_count | |||
|
162 | end | |||
|
163 | ||||
|
164 | should "have an errors tag" do | |||
|
165 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |||
|
166 | end | |||
|
167 | end | |||
|
168 | ||||
|
169 | context "DELETE /issues/1.xml" do | |||
|
170 | setup do | |||
|
171 | @issue_count = Issue.count | |||
122 | delete '/issues/1.xml', {}, :authorization => credentials('jsmith') |
|
172 | delete '/issues/1.xml', {}, :authorization => credentials('jsmith') | |
123 | end |
|
173 | end | |
124 | assert_response :ok |
|
174 | ||
125 | assert_equal 'application/xml', @response.content_type |
|
175 | should_respond_with :ok | |
126 | assert_nil Issue.find_by_id(1) |
|
176 | should_respond_with_content_type 'application/xml' | |
|
177 | ||||
|
178 | should "delete the issue" do | |||
|
179 | assert_equal Issue.count, @issue_count -1 | |||
|
180 | assert_nil Issue.find_by_id(1) | |||
|
181 | end | |||
127 | end |
|
182 | end | |
128 |
|
183 | |||
129 | def credentials(user, password=nil) |
|
184 | def credentials(user, password=nil) | |
130 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
|
185 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | |
131 | end |
|
186 | end | |
132 | end |
|
187 | end |
General Comments 0
You need to be logged in to leave comments.
Login now