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