chris@37: # Redmine - project management software chris@37: # Copyright (C) 2006-2010 Jean-Philippe Lang chris@37: # chris@37: # This program is free software; you can redistribute it and/or chris@37: # modify it under the terms of the GNU General Public License chris@37: # as published by the Free Software Foundation; either version 2 chris@37: # of the License, or (at your option) any later version. chris@37: # chris@37: # This program is distributed in the hope that it will be useful, chris@37: # but WITHOUT ANY WARRANTY; without even the implied warranty of chris@37: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@37: # GNU General Public License for more details. chris@37: # chris@37: # You should have received a copy of the GNU General Public License chris@37: # along with this program; if not, write to the Free Software chris@37: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. chris@37: chris@37: require "#{File.dirname(__FILE__)}/../../test_helper" chris@37: chris@37: class ApiTest::ProjectsTest < ActionController::IntegrationTest chris@37: fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, chris@37: :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, chris@37: :attachments, :custom_fields, :custom_values, :time_entries chris@37: chris@37: def setup chris@37: Setting.rest_api_enabled = '1' chris@37: end chris@37: chris@37: def test_index chris@37: get '/projects.xml' chris@37: assert_response :success chris@37: assert_equal 'application/xml', @response.content_type chris@37: end chris@37: chris@37: def test_show chris@37: get '/projects/1.xml' chris@37: assert_response :success chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable' chris@37: end chris@37: chris@37: def test_show_should_not_display_hidden_custom_fields chris@37: ProjectCustomField.find_by_name('Development status').update_attribute :visible, false chris@37: get '/projects/1.xml' chris@37: assert_response :success chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_no_tag 'custom_field', :attributes => {:name => 'Development status'} chris@37: end chris@37: chris@37: def test_create chris@37: attributes = {:name => 'API test', :identifier => 'api-test'} chris@37: assert_difference 'Project.count' do chris@37: post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') chris@37: end chris@37: chris@37: project = Project.first(:order => 'id DESC') chris@37: attributes.each do |attribute, value| chris@37: assert_equal value, project.send(attribute) chris@37: end chris@37: chris@37: assert_response :created chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} chris@37: end chris@37: chris@37: def test_create_failure chris@37: attributes = {:name => 'API test'} chris@37: assert_no_difference 'Project.count' do chris@37: post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') chris@37: end chris@37: assert_response :unprocessable_entity chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"} chris@37: end chris@37: chris@37: def test_update chris@37: attributes = {:name => 'API update'} chris@37: assert_no_difference 'Project.count' do chris@37: put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') chris@37: end chris@37: assert_response :ok chris@37: assert_equal 'application/xml', @response.content_type chris@37: project = Project.find(1) chris@37: attributes.each do |attribute, value| chris@37: assert_equal value, project.send(attribute) chris@37: end chris@37: end chris@37: chris@37: def test_update_failure chris@37: attributes = {:name => ''} chris@37: assert_no_difference 'Project.count' do chris@37: put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') chris@37: end chris@37: assert_response :unprocessable_entity chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"} chris@37: end chris@37: chris@37: def test_destroy chris@37: assert_difference 'Project.count', -1 do chris@37: delete '/projects/2.xml', {}, :authorization => credentials('admin') chris@37: end chris@37: assert_response :ok chris@37: assert_equal 'application/xml', @response.content_type chris@37: assert_nil Project.find_by_id(2) chris@37: end chris@37: chris@37: def credentials(user, password=nil) chris@37: ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) chris@37: end chris@37: end