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