annotate .svn/pristine/4c/4c87209f1f933df211a435d5f5082b69a9cf19f5.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@909 19
Chris@909 20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
Chris@909 21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
Chris@909 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
Chris@909 23 :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories
Chris@909 24
Chris@909 25 def setup
Chris@909 26 Setting.rest_api_enabled = '1'
Chris@909 27 set_tmp_attachments_directory
Chris@909 28 end
Chris@909 29
Chris@909 30 context "GET /projects" do
Chris@909 31 context ".xml" do
Chris@909 32 should "return projects" do
Chris@909 33 get '/projects.xml'
Chris@909 34 assert_response :success
Chris@909 35 assert_equal 'application/xml', @response.content_type
Chris@909 36
Chris@909 37 assert_tag :tag => 'projects',
Chris@909 38 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
Chris@909 39 end
Chris@909 40 end
Chris@909 41
Chris@909 42 context ".json" do
Chris@909 43 should "return projects" do
Chris@909 44 get '/projects.json'
Chris@909 45 assert_response :success
Chris@909 46 assert_equal 'application/json', @response.content_type
Chris@909 47
Chris@909 48 json = ActiveSupport::JSON.decode(response.body)
Chris@909 49 assert_kind_of Hash, json
Chris@909 50 assert_kind_of Array, json['projects']
Chris@909 51 assert_kind_of Hash, json['projects'].first
Chris@909 52 assert json['projects'].first.has_key?('id')
Chris@909 53 end
Chris@909 54 end
Chris@909 55 end
Chris@909 56
Chris@909 57 context "GET /projects/:id" do
Chris@909 58 context ".xml" do
Chris@909 59 # TODO: A private project is needed because should_allow_api_authentication
Chris@909 60 # actually tests that authentication is *required*, not just allowed
Chris@909 61 should_allow_api_authentication(:get, "/projects/2.xml")
Chris@909 62
Chris@909 63 should "return requested project" do
Chris@909 64 get '/projects/1.xml'
Chris@909 65 assert_response :success
Chris@909 66 assert_equal 'application/xml', @response.content_type
Chris@909 67
Chris@909 68 assert_tag :tag => 'project',
Chris@909 69 :child => {:tag => 'id', :content => '1'}
Chris@909 70 assert_tag :tag => 'custom_field',
Chris@909 71 :attributes => {:name => 'Development status'}, :content => 'Stable'
Chris@909 72
Chris@909 73 assert_no_tag 'trackers'
Chris@909 74 assert_no_tag 'issue_categories'
Chris@909 75 end
Chris@909 76
Chris@909 77 context "with hidden custom fields" do
Chris@909 78 setup do
Chris@909 79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
Chris@909 80 end
Chris@909 81
Chris@909 82 should "not display hidden custom fields" do
Chris@909 83 get '/projects/1.xml'
Chris@909 84 assert_response :success
Chris@909 85 assert_equal 'application/xml', @response.content_type
Chris@909 86
Chris@909 87 assert_no_tag 'custom_field',
Chris@909 88 :attributes => {:name => 'Development status'}
Chris@909 89 end
Chris@909 90 end
Chris@909 91
Chris@909 92 should "return categories with include=issue_categories" do
Chris@909 93 get '/projects/1.xml?include=issue_categories'
Chris@909 94 assert_response :success
Chris@909 95 assert_equal 'application/xml', @response.content_type
Chris@909 96
Chris@909 97 assert_tag 'issue_categories',
Chris@909 98 :attributes => {:type => 'array'},
Chris@909 99 :child => {
Chris@909 100 :tag => 'issue_category',
Chris@909 101 :attributes => {
Chris@909 102 :id => '2',
Chris@909 103 :name => 'Recipes'
Chris@909 104 }
Chris@909 105 }
Chris@909 106 end
Chris@909 107
Chris@909 108 should "return trackers with include=trackers" do
Chris@909 109 get '/projects/1.xml?include=trackers'
Chris@909 110 assert_response :success
Chris@909 111 assert_equal 'application/xml', @response.content_type
Chris@909 112
Chris@909 113 assert_tag 'trackers',
Chris@909 114 :attributes => {:type => 'array'},
Chris@909 115 :child => {
Chris@909 116 :tag => 'tracker',
Chris@909 117 :attributes => {
Chris@909 118 :id => '2',
Chris@909 119 :name => 'Feature request'
Chris@909 120 }
Chris@909 121 }
Chris@909 122 end
Chris@909 123 end
Chris@909 124
Chris@909 125 context ".json" do
Chris@909 126 should_allow_api_authentication(:get, "/projects/2.json")
Chris@909 127
Chris@909 128 should "return requested project" do
Chris@909 129 get '/projects/1.json'
Chris@909 130
Chris@909 131 json = ActiveSupport::JSON.decode(response.body)
Chris@909 132 assert_kind_of Hash, json
Chris@909 133 assert_kind_of Hash, json['project']
Chris@909 134 assert_equal 1, json['project']['id']
Chris@909 135 end
Chris@909 136 end
Chris@909 137 end
Chris@909 138
Chris@909 139 context "POST /projects" do
Chris@909 140 context "with valid parameters" do
Chris@909 141 setup do
Chris@909 142 Setting.default_projects_modules = ['issue_tracking', 'repository']
Chris@909 143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
Chris@909 144 end
Chris@909 145
Chris@909 146 context ".xml" do
Chris@909 147 should_allow_api_authentication(:post,
Chris@909 148 '/projects.xml',
Chris@909 149 {:project => {:name => 'API test', :identifier => 'api-test'}},
Chris@909 150 {:success_code => :created})
Chris@909 151
Chris@909 152
Chris@909 153 should "create a project with the attributes" do
Chris@909 154 assert_difference('Project.count') do
Chris@909 155 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@909 156 end
Chris@909 157
Chris@909 158 project = Project.first(:order => 'id DESC')
Chris@909 159 assert_equal 'API test', project.name
Chris@909 160 assert_equal 'api-test', project.identifier
Chris@909 161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
Chris@909 162 assert_equal Tracker.all.size, project.trackers.size
Chris@909 163
Chris@909 164 assert_response :created
Chris@909 165 assert_equal 'application/xml', @response.content_type
Chris@909 166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
Chris@909 167 end
Chris@909 168
Chris@909 169 should "accept enabled_module_names attribute" do
Chris@909 170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@909 171
Chris@909 172 assert_difference('Project.count') do
Chris@909 173 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@909 174 end
Chris@909 175
Chris@909 176 project = Project.first(:order => 'id DESC')
Chris@909 177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@909 178 end
Chris@909 179
Chris@909 180 should "accept tracker_ids attribute" do
Chris@909 181 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@909 182
Chris@909 183 assert_difference('Project.count') do
Chris@909 184 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@909 185 end
Chris@909 186
Chris@909 187 project = Project.first(:order => 'id DESC')
Chris@909 188 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@909 189 end
Chris@909 190 end
Chris@909 191 end
Chris@909 192
Chris@909 193 context "with invalid parameters" do
Chris@909 194 setup do
Chris@909 195 @parameters = {:project => {:name => 'API test'}}
Chris@909 196 end
Chris@909 197
Chris@909 198 context ".xml" do
Chris@909 199 should "return errors" do
Chris@909 200 assert_no_difference('Project.count') do
Chris@909 201 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@909 202 end
Chris@909 203
Chris@909 204 assert_response :unprocessable_entity
Chris@909 205 assert_equal 'application/xml', @response.content_type
Chris@909 206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
Chris@909 207 end
Chris@909 208 end
Chris@909 209 end
Chris@909 210 end
Chris@909 211
Chris@909 212 context "PUT /projects/:id" do
Chris@909 213 context "with valid parameters" do
Chris@909 214 setup do
Chris@909 215 @parameters = {:project => {:name => 'API update'}}
Chris@909 216 end
Chris@909 217
Chris@909 218 context ".xml" do
Chris@909 219 should_allow_api_authentication(:put,
Chris@909 220 '/projects/2.xml',
Chris@909 221 {:project => {:name => 'API update'}},
Chris@909 222 {:success_code => :ok})
Chris@909 223
Chris@909 224 should "update the project" do
Chris@909 225 assert_no_difference 'Project.count' do
Chris@909 226 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
Chris@909 227 end
Chris@909 228 assert_response :ok
Chris@909 229 assert_equal 'application/xml', @response.content_type
Chris@909 230 project = Project.find(2)
Chris@909 231 assert_equal 'API update', project.name
Chris@909 232 end
Chris@909 233
Chris@909 234 should "accept enabled_module_names attribute" do
Chris@909 235 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@909 236
Chris@909 237 assert_no_difference 'Project.count' do
Chris@909 238 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@909 239 end
Chris@909 240 assert_response :ok
Chris@909 241 project = Project.find(2)
Chris@909 242 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@909 243 end
Chris@909 244
Chris@909 245 should "accept tracker_ids attribute" do
Chris@909 246 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@909 247
Chris@909 248 assert_no_difference 'Project.count' do
Chris@909 249 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@909 250 end
Chris@909 251 assert_response :ok
Chris@909 252 project = Project.find(2)
Chris@909 253 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@909 254 end
Chris@909 255 end
Chris@909 256 end
Chris@909 257
Chris@909 258 context "with invalid parameters" do
Chris@909 259 setup do
Chris@909 260 @parameters = {:project => {:name => ''}}
Chris@909 261 end
Chris@909 262
Chris@909 263 context ".xml" do
Chris@909 264 should "return errors" do
Chris@909 265 assert_no_difference('Project.count') do
Chris@909 266 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@909 267 end
Chris@909 268
Chris@909 269 assert_response :unprocessable_entity
Chris@909 270 assert_equal 'application/xml', @response.content_type
Chris@909 271 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
Chris@909 272 end
Chris@909 273 end
Chris@909 274 end
Chris@909 275 end
Chris@909 276
Chris@909 277 context "DELETE /projects/:id" do
Chris@909 278 context ".xml" do
Chris@909 279 should_allow_api_authentication(:delete,
Chris@909 280 '/projects/2.xml',
Chris@909 281 {},
Chris@909 282 {:success_code => :ok})
Chris@909 283
Chris@909 284 should "delete the project" do
Chris@909 285 assert_difference('Project.count',-1) do
Chris@909 286 delete '/projects/2.xml', {}, :authorization => credentials('admin')
Chris@909 287 end
Chris@909 288 assert_response :ok
Chris@909 289 assert_nil Project.find_by_id(2)
Chris@909 290 end
Chris@909 291 end
Chris@909 292 end
Chris@909 293
Chris@909 294 def credentials(user, password=nil)
Chris@909 295 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
Chris@909 296 end
Chris@909 297 end