annotate test/integration/api_test/projects_test.rb @ 1172:60d42b9850d2 bug_367

Close obsolete branch bug_367
author Chris Cannam
date Fri, 03 Feb 2012 15:20:50 +0000
parents af80e5618e9b
children cbb26bc654de
rev   line source
chris@37 1 # Redmine - project management software
chris@37 2 # Copyright (C) 2006-2010 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@37 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@37 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@117 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@37 23 :attachments, :custom_fields, :custom_values, :time_entries
chris@37 24
chris@37 25 def setup
chris@37 26 Setting.rest_api_enabled = '1'
chris@37 27 end
Chris@117 28
Chris@117 29 context "GET /projects" do
Chris@117 30 context ".xml" do
Chris@117 31 should "return projects" do
Chris@117 32 get '/projects.xml'
Chris@117 33 assert_response :success
Chris@117 34 assert_equal 'application/xml', @response.content_type
Chris@117 35
Chris@117 36 assert_tag :tag => 'projects',
Chris@117 37 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
Chris@117 38 end
chris@37 39 end
chris@37 40
Chris@117 41 context ".json" do
Chris@117 42 should "return projects" do
Chris@117 43 get '/projects.json'
Chris@117 44 assert_response :success
Chris@117 45 assert_equal 'application/json', @response.content_type
Chris@117 46
Chris@117 47 json = ActiveSupport::JSON.decode(response.body)
Chris@117 48 assert_kind_of Hash, json
Chris@117 49 assert_kind_of Array, json['projects']
Chris@117 50 assert_kind_of Hash, json['projects'].first
Chris@117 51 assert json['projects'].first.has_key?('id')
Chris@117 52 end
chris@37 53 end
chris@37 54 end
chris@37 55
Chris@117 56 context "GET /projects/:id" do
Chris@117 57 context ".xml" do
Chris@117 58 # TODO: A private project is needed because should_allow_api_authentication
Chris@117 59 # actually tests that authentication is *required*, not just allowed
Chris@117 60 should_allow_api_authentication(:get, "/projects/2.xml")
Chris@117 61
Chris@117 62 should "return requested project" do
Chris@117 63 get '/projects/1.xml'
Chris@117 64 assert_response :success
Chris@117 65 assert_equal 'application/xml', @response.content_type
Chris@117 66
Chris@117 67 assert_tag :tag => 'project',
Chris@117 68 :child => {:tag => 'id', :content => '1'}
Chris@117 69 assert_tag :tag => 'custom_field',
Chris@117 70 :attributes => {:name => 'Development status'}, :content => 'Stable'
Chris@117 71 end
Chris@117 72
Chris@117 73 context "with hidden custom fields" do
Chris@117 74 setup do
Chris@117 75 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
Chris@117 76 end
Chris@117 77
Chris@117 78 should "not display hidden custom fields" do
Chris@117 79 get '/projects/1.xml'
Chris@117 80 assert_response :success
Chris@117 81 assert_equal 'application/xml', @response.content_type
Chris@117 82
Chris@117 83 assert_no_tag 'custom_field',
Chris@117 84 :attributes => {:name => 'Development status'}
Chris@117 85 end
Chris@117 86 end
chris@37 87 end
Chris@117 88
Chris@117 89 context ".json" do
Chris@117 90 should_allow_api_authentication(:get, "/projects/2.json")
Chris@117 91
Chris@117 92 should "return requested project" do
Chris@117 93 get '/projects/1.json'
Chris@117 94
Chris@117 95 json = ActiveSupport::JSON.decode(response.body)
Chris@117 96 assert_kind_of Hash, json
Chris@117 97 assert_kind_of Hash, json['project']
Chris@117 98 assert_equal 1, json['project']['id']
Chris@117 99 end
Chris@117 100 end
Chris@117 101 end
Chris@117 102
Chris@117 103 context "POST /projects" do
Chris@117 104 context "with valid parameters" do
Chris@117 105 setup do
Chris@117 106 Setting.default_projects_modules = ['issue_tracking', 'repository']
Chris@117 107 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
Chris@117 108 end
Chris@117 109
Chris@117 110 context ".xml" do
Chris@117 111 should_allow_api_authentication(:post,
Chris@117 112 '/projects.xml',
Chris@117 113 {:project => {:name => 'API test', :identifier => 'api-test'}},
Chris@117 114 {:success_code => :created})
Chris@117 115
Chris@117 116
Chris@117 117 should "create a project with the attributes" do
Chris@117 118 assert_difference('Project.count') do
Chris@117 119 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@117 120 end
Chris@117 121
Chris@117 122 project = Project.first(:order => 'id DESC')
Chris@117 123 assert_equal 'API test', project.name
Chris@117 124 assert_equal 'api-test', project.identifier
Chris@117 125 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
Chris@117 126 assert_equal Tracker.all.size, project.trackers.size
Chris@117 127
Chris@117 128 assert_response :created
Chris@117 129 assert_equal 'application/xml', @response.content_type
Chris@117 130 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
Chris@117 131 end
Chris@117 132
Chris@117 133 should "accept enabled_module_names attribute" do
Chris@117 134 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@117 135
Chris@117 136 assert_difference('Project.count') do
Chris@117 137 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@117 138 end
Chris@117 139
Chris@117 140 project = Project.first(:order => 'id DESC')
Chris@117 141 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@117 142 end
Chris@117 143
Chris@117 144 should "accept tracker_ids attribute" do
Chris@117 145 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@117 146
Chris@117 147 assert_difference('Project.count') do
Chris@117 148 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@117 149 end
Chris@117 150
Chris@117 151 project = Project.first(:order => 'id DESC')
Chris@117 152 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@117 153 end
Chris@117 154 end
Chris@117 155 end
Chris@117 156
Chris@117 157 context "with invalid parameters" do
Chris@117 158 setup do
Chris@117 159 @parameters = {:project => {:name => 'API test'}}
Chris@117 160 end
Chris@117 161
Chris@117 162 context ".xml" do
Chris@117 163 should "return errors" do
Chris@117 164 assert_no_difference('Project.count') do
Chris@117 165 post '/projects.xml', @parameters, :authorization => credentials('admin')
Chris@117 166 end
Chris@117 167
Chris@117 168 assert_response :unprocessable_entity
Chris@117 169 assert_equal 'application/xml', @response.content_type
Chris@117 170 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
Chris@117 171 end
Chris@117 172 end
Chris@117 173 end
chris@37 174 end
chris@37 175
Chris@117 176 context "PUT /projects/:id" do
Chris@117 177 context "with valid parameters" do
Chris@117 178 setup do
Chris@117 179 @parameters = {:project => {:name => 'API update'}}
Chris@117 180 end
Chris@117 181
Chris@117 182 context ".xml" do
Chris@117 183 should_allow_api_authentication(:put,
Chris@117 184 '/projects/2.xml',
Chris@117 185 {:project => {:name => 'API update'}},
Chris@117 186 {:success_code => :ok})
Chris@117 187
Chris@117 188 should "update the project" do
Chris@117 189 assert_no_difference 'Project.count' do
Chris@117 190 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
Chris@117 191 end
Chris@117 192 assert_response :ok
Chris@117 193 assert_equal 'application/xml', @response.content_type
Chris@117 194 project = Project.find(2)
Chris@117 195 assert_equal 'API update', project.name
Chris@117 196 end
Chris@117 197
Chris@117 198 should "accept enabled_module_names attribute" do
Chris@117 199 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@117 200
Chris@117 201 assert_no_difference 'Project.count' do
Chris@117 202 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@117 203 end
Chris@117 204 assert_response :ok
Chris@117 205 project = Project.find(2)
Chris@117 206 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@117 207 end
Chris@117 208
Chris@117 209 should "accept tracker_ids attribute" do
Chris@117 210 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@117 211
Chris@117 212 assert_no_difference 'Project.count' do
Chris@117 213 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@117 214 end
Chris@117 215 assert_response :ok
Chris@117 216 project = Project.find(2)
Chris@117 217 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@117 218 end
Chris@117 219 end
chris@37 220 end
Chris@117 221
Chris@117 222 context "with invalid parameters" do
Chris@117 223 setup do
Chris@117 224 @parameters = {:project => {:name => ''}}
Chris@117 225 end
Chris@117 226
Chris@117 227 context ".xml" do
Chris@117 228 should "return errors" do
Chris@117 229 assert_no_difference('Project.count') do
Chris@117 230 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
Chris@117 231 end
Chris@117 232
Chris@117 233 assert_response :unprocessable_entity
Chris@117 234 assert_equal 'application/xml', @response.content_type
Chris@117 235 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
Chris@117 236 end
Chris@117 237 end
Chris@117 238 end
Chris@117 239 end
Chris@117 240
Chris@117 241 context "DELETE /projects/:id" do
Chris@117 242 context ".xml" do
Chris@117 243 should_allow_api_authentication(:delete,
Chris@117 244 '/projects/2.xml',
Chris@117 245 {},
Chris@117 246 {:success_code => :ok})
Chris@117 247
Chris@117 248 should "delete the project" do
Chris@117 249 assert_difference('Project.count',-1) do
Chris@117 250 delete '/projects/2.xml', {}, :authorization => credentials('admin')
Chris@117 251 end
Chris@117 252 assert_response :ok
Chris@117 253 assert_nil Project.find_by_id(2)
Chris@117 254 end
Chris@117 255 end
chris@37 256 end
chris@37 257
chris@37 258 def credentials(user, password=nil)
chris@37 259 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
chris@37 260 end
chris@37 261 end