annotate test/integration/api_test/projects_test.rb @ 1271:cf4cc816278a live last_rails2_version

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