##// 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 25 def setup
26 26 Setting.rest_api_enabled = '1'
27 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
36 # TODO: A private project is needed because should_allow_api_authentication
37 # actually tests that authentication is *required*, not just allowed
38 should_allow_api_authentication(:get, "/projects/2.xml")
29 context "GET /projects" do
30 context ".xml" do
31 should "return projects" do
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 54 end
40 55
41 def test_show
42 get '/projects/1.xml'
43 assert_response :success
44 assert_equal 'application/xml', @response.content_type
45 assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable'
46 end
56 context "GET /projects/:id" do
57 context ".xml" do
58 # TODO: A private project is needed because should_allow_api_authentication
59 # actually tests that authentication is *required*, not just allowed
60 should_allow_api_authentication(:get, "/projects/2.xml")
47 61
48 def test_show_should_not_display_hidden_custom_fields
49 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
50 get '/projects/1.xml'
51 assert_response :success
52 assert_equal 'application/xml', @response.content_type
53 assert_no_tag 'custom_field', :attributes => {:name => 'Development status'}
54 end
55
56 context "POST /projects.xml" do
57 should_allow_api_authentication(:post,
58 '/projects.xml',
59 {:project => {:name => 'API test', :identifier => 'api-test'}},
60 {:success_code => :created})
62 should "return requested project" do
63 get '/projects/1.xml'
64 assert_response :success
65 assert_equal 'application/xml', @response.content_type
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'
71 end
72
73 context "with hidden custom fields" do
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
63 assert_difference('Project.count') do
64 post '/projects.xml', {:project => {:name => 'API test', :identifier => 'api-test'}}, :authorization => credentials('admin')
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'
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 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 100 end
75 101 end
76 102
77 def test_create_failure
78 attributes = {:name => 'API test'}
79 assert_no_difference 'Project.count' do
80 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
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
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 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 149 end
86 150
87 context "PUT /projects/2.xml" do
88 should_allow_api_authentication(:put,
89 '/projects/2.xml',
90 {:project => {:name => 'API test'}},
91 {:success_code => :ok})
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
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
94 assert_no_difference 'Project.count' do
95 put '/projects/2.xml', {:project => {:name => 'API update'}}, :authorization => credentials('jsmith')
175 context "with invalid parameters" do
176 setup do
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 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 191 end
102 192 end
103 193
104 def test_update_failure
105 attributes = {:name => ''}
106 assert_no_difference 'Project.count' do
107 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
108 end
109 assert_response :unprocessable_entity
110 assert_equal 'application/xml', @response.content_type
111 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
112 end
194 context "DELETE /projects/:id" do
195 context ".xml" do
196 should_allow_api_authentication(:delete,
197 '/projects/2.xml',
198 {},
199 {:success_code => :ok})
113 200
114 context "DELETE /projects/2.xml" do
115 should_allow_api_authentication(:delete,
116 '/projects/2.xml',
117 {},
118 {:success_code => :ok})
119
120 should "delete the project" do
121 assert_difference('Project.count',-1) do
122 delete '/projects/2.xml', {}, :authorization => credentials('admin')
201 should "delete the project" do
202 assert_difference('Project.count',-1) do
203 delete '/projects/2.xml', {}, :authorization => credentials('admin')
204 end
205 assert_response :ok
206 assert_nil Project.find_by_id(2)
123 207 end
124 assert_response :ok
125 assert_nil Project.find_by_id(2)
126 208 end
127 209 end
128 210
General Comments 0
You need to be logged in to leave comments. Login now