##// END OF EJS Templates
Projects API tests rewriting....
Jean-Philippe Lang -
r4343:eaf6bb1e9bc1
parent child
Show More
@@ -1,132 +1,214
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 ApiTest::ProjectsTest < ActionController::IntegrationTest
20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries
23 :attachments, :custom_fields, :custom_values, :time_entries
24
24
25 def setup
25 def setup
26 Setting.rest_api_enabled = '1'
26 Setting.rest_api_enabled = '1'
27 end
27 end
28
28
29 def test_index
29 context "GET /projects" do
30 context ".xml" do
31 should "return projects" do
30 get '/projects.xml'
32 get '/projects.xml'
31 assert_response :success
33 assert_response :success
32 assert_equal 'application/xml', @response.content_type
34 assert_equal 'application/xml', @response.content_type
35
36 assert_tag :tag => 'projects',
37 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
38 end
33 end
39 end
34
40
35 context "GET /projects/2.xml" do
41 context ".json" do
42 should "return projects" do
43 get '/projects.json'
44 assert_response :success
45 assert_equal 'application/json', @response.content_type
46
47 json = ActiveSupport::JSON.decode(response.body)
48 assert_kind_of Hash, json
49 assert_kind_of Array, json['projects']
50 assert_kind_of Hash, json['projects'].first
51 assert json['projects'].first.has_key?('id')
52 end
53 end
54 end
55
56 context "GET /projects/:id" do
57 context ".xml" do
36 # TODO: A private project is needed because should_allow_api_authentication
58 # TODO: A private project is needed because should_allow_api_authentication
37 # actually tests that authentication is *required*, not just allowed
59 # actually tests that authentication is *required*, not just allowed
38 should_allow_api_authentication(:get, "/projects/2.xml")
60 should_allow_api_authentication(:get, "/projects/2.xml")
39 end
40
61
41 def test_show
62 should "return requested project" do
42 get '/projects/1.xml'
63 get '/projects/1.xml'
43 assert_response :success
64 assert_response :success
44 assert_equal 'application/xml', @response.content_type
65 assert_equal 'application/xml', @response.content_type
45 assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable'
66
67 assert_tag :tag => 'project',
68 :child => {:tag => 'id', :content => '1'}
69 assert_tag :tag => 'custom_field',
70 :attributes => {:name => 'Development status'}, :content => 'Stable'
46 end
71 end
47
72
48 def test_show_should_not_display_hidden_custom_fields
73 context "with hidden custom fields" do
74 setup do
49 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
75 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
76 end
77
78 should "not display hidden custom fields" do
50 get '/projects/1.xml'
79 get '/projects/1.xml'
51 assert_response :success
80 assert_response :success
52 assert_equal 'application/xml', @response.content_type
81 assert_equal 'application/xml', @response.content_type
53 assert_no_tag 'custom_field', :attributes => {:name => 'Development status'}
82
83 assert_no_tag 'custom_field',
84 :attributes => {:name => 'Development status'}
85 end
54 end
86 end
87 end
88
89 context ".json" do
90 should_allow_api_authentication(:get, "/projects/2.json")
91
92 should "return requested project" do
93 get '/projects/1.json'
55
94
56 context "POST /projects.xml" do
95 json = ActiveSupport::JSON.decode(response.body)
96 assert_kind_of Hash, json
97 assert_kind_of Hash, json['project']
98 assert_equal 1, json['project']['id']
99 end
100 end
101 end
102
103 context "POST /projects" do
104 context "with valid parameters" do
105 setup do
106 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
107 end
108
109 context ".xml" do
57 should_allow_api_authentication(:post,
110 should_allow_api_authentication(:post,
58 '/projects.xml',
111 '/projects.xml',
59 {:project => {:name => 'API test', :identifier => 'api-test'}},
112 {:project => {:name => 'API test', :identifier => 'api-test'}},
60 {:success_code => :created})
113 {:success_code => :created})
61
114
115
62 should "create a project with the attributes" do
116 should "create a project with the attributes" do
63 assert_difference('Project.count') do
117 assert_difference('Project.count') do
64 post '/projects.xml', {:project => {:name => 'API test', :identifier => 'api-test'}}, :authorization => credentials('admin')
118 post '/projects.xml', @parameters, :authorization => credentials('admin')
65 end
119 end
66
120
67 project = Project.first(:order => 'id DESC')
121 project = Project.first(:order => 'id DESC')
68 assert_equal 'API test', project.name
122 assert_equal 'API test', project.name
69 assert_equal 'api-test', project.identifier
123 assert_equal 'api-test', project.identifier
70
124
71 assert_response :created
125 assert_response :created
72 assert_equal 'application/xml', @response.content_type
126 assert_equal 'application/xml', @response.content_type
73 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
127 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
74 end
128 end
75 end
129 end
130 end
76
131
77 def test_create_failure
132 context "with invalid parameters" do
78 attributes = {:name => 'API test'}
133 setup do
79 assert_no_difference 'Project.count' do
134 @parameters = {:project => {:name => 'API test'}}
80 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
135 end
136
137 context ".xml" do
138 should "return errors" do
139 assert_no_difference('Project.count') do
140 post '/projects.xml', @parameters, :authorization => credentials('admin')
81 end
141 end
142
82 assert_response :unprocessable_entity
143 assert_response :unprocessable_entity
83 assert_equal 'application/xml', @response.content_type
144 assert_equal 'application/xml', @response.content_type
84 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
145 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
146 end
147 end
148 end
85 end
149 end
86
150
87 context "PUT /projects/2.xml" do
151 context "PUT /projects/:id" do
152 context "with valid parameters" do
153 setup do
154 @parameters = {:project => {:name => 'API update'}}
155 end
156
157 context ".xml" do
88 should_allow_api_authentication(:put,
158 should_allow_api_authentication(:put,
89 '/projects/2.xml',
159 '/projects/2.xml',
90 {:project => {:name => 'API test'}},
160 {:project => {:name => 'API update'}},
91 {:success_code => :ok})
161 {:success_code => :ok})
92
162
93 should "update the project" do
163 should "update the project" do
94 assert_no_difference 'Project.count' do
164 assert_no_difference 'Project.count' do
95 put '/projects/2.xml', {:project => {:name => 'API update'}}, :authorization => credentials('jsmith')
165 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
96 end
166 end
97 assert_response :ok
167 assert_response :ok
98 assert_equal 'application/xml', @response.content_type
168 assert_equal 'application/xml', @response.content_type
99 project = Project.find(2)
169 project = Project.find(2)
100 assert_equal 'API update', project.name
170 assert_equal 'API update', project.name
101 end
171 end
102 end
172 end
173 end
103
174
104 def test_update_failure
175 context "with invalid parameters" do
105 attributes = {:name => ''}
176 setup do
106 assert_no_difference 'Project.count' do
177 @parameters = {:project => {:name => ''}}
107 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
178 end
179
180 context ".xml" do
181 should "return errors" do
182 assert_no_difference('Project.count') do
183 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
108 end
184 end
185
109 assert_response :unprocessable_entity
186 assert_response :unprocessable_entity
110 assert_equal 'application/xml', @response.content_type
187 assert_equal 'application/xml', @response.content_type
111 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
188 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
189 end
190 end
191 end
112 end
192 end
113
193
114 context "DELETE /projects/2.xml" do
194 context "DELETE /projects/:id" do
195 context ".xml" do
115 should_allow_api_authentication(:delete,
196 should_allow_api_authentication(:delete,
116 '/projects/2.xml',
197 '/projects/2.xml',
117 {},
198 {},
118 {:success_code => :ok})
199 {:success_code => :ok})
119
200
120 should "delete the project" do
201 should "delete the project" do
121 assert_difference('Project.count',-1) do
202 assert_difference('Project.count',-1) do
122 delete '/projects/2.xml', {}, :authorization => credentials('admin')
203 delete '/projects/2.xml', {}, :authorization => credentials('admin')
123 end
204 end
124 assert_response :ok
205 assert_response :ok
125 assert_nil Project.find_by_id(2)
206 assert_nil Project.find_by_id(2)
126 end
207 end
127 end
208 end
209 end
128
210
129 def credentials(user, password=nil)
211 def credentials(user, password=nil)
130 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
212 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
131 end
213 end
132 end
214 end
General Comments 0
You need to be logged in to leave comments. Login now