annotate test/integration/api_test/.svn/text-base/projects_test.rb.svn-base @ 507:0c939c159af4 redmine-1.2

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