chris@37: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 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@909: # 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@909: # 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@119: require File.expand_path('../../../test_helper', __FILE__) 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@909: :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories chris@37: chris@37: def setup chris@37: Setting.rest_api_enabled = '1' Chris@909: set_tmp_attachments_directory chris@37: end Chris@909: Chris@119: context "GET /projects" do Chris@119: context ".xml" do Chris@119: should "return projects" do Chris@119: get '/projects.xml' Chris@119: assert_response :success Chris@119: assert_equal 'application/xml', @response.content_type Chris@909: Chris@119: assert_tag :tag => 'projects', Chris@119: :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} Chris@119: end chris@37: end chris@37: Chris@119: context ".json" do Chris@119: should "return projects" do Chris@119: get '/projects.json' Chris@119: assert_response :success Chris@119: assert_equal 'application/json', @response.content_type Chris@909: Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert_kind_of Array, json['projects'] Chris@119: assert_kind_of Hash, json['projects'].first Chris@119: assert json['projects'].first.has_key?('id') Chris@119: end chris@37: end chris@37: end Chris@909: Chris@119: context "GET /projects/:id" do Chris@119: context ".xml" do Chris@119: # TODO: A private project is needed because should_allow_api_authentication Chris@119: # actually tests that authentication is *required*, not just allowed Chris@119: should_allow_api_authentication(:get, "/projects/2.xml") Chris@909: Chris@119: should "return requested project" do Chris@119: get '/projects/1.xml' Chris@119: assert_response :success Chris@119: assert_equal 'application/xml', @response.content_type Chris@909: Chris@119: assert_tag :tag => 'project', Chris@119: :child => {:tag => 'id', :content => '1'} Chris@119: assert_tag :tag => 'custom_field', Chris@119: :attributes => {:name => 'Development status'}, :content => 'Stable' Chris@909: Chris@909: assert_no_tag 'trackers' Chris@909: assert_no_tag 'issue_categories' Chris@119: end Chris@909: Chris@119: context "with hidden custom fields" do Chris@119: setup do Chris@119: ProjectCustomField.find_by_name('Development status').update_attribute :visible, false Chris@119: end Chris@909: Chris@119: should "not display hidden custom fields" do Chris@119: get '/projects/1.xml' Chris@119: assert_response :success Chris@119: assert_equal 'application/xml', @response.content_type Chris@909: Chris@119: assert_no_tag 'custom_field', Chris@119: :attributes => {:name => 'Development status'} Chris@119: end Chris@119: end Chris@909: Chris@909: should "return categories with include=issue_categories" do Chris@909: get '/projects/1.xml?include=issue_categories' Chris@909: assert_response :success Chris@909: assert_equal 'application/xml', @response.content_type Chris@909: Chris@909: assert_tag 'issue_categories', Chris@909: :attributes => {:type => 'array'}, Chris@909: :child => { Chris@909: :tag => 'issue_category', Chris@909: :attributes => { Chris@909: :id => '2', Chris@909: :name => 'Recipes' Chris@909: } Chris@909: } Chris@909: end Chris@909: Chris@909: should "return trackers with include=trackers" do Chris@909: get '/projects/1.xml?include=trackers' Chris@909: assert_response :success Chris@909: assert_equal 'application/xml', @response.content_type Chris@909: Chris@909: assert_tag 'trackers', Chris@909: :attributes => {:type => 'array'}, Chris@909: :child => { Chris@909: :tag => 'tracker', Chris@909: :attributes => { Chris@909: :id => '2', Chris@909: :name => 'Feature request' Chris@909: } Chris@909: } Chris@909: end chris@37: end Chris@119: Chris@119: context ".json" do Chris@119: should_allow_api_authentication(:get, "/projects/2.json") Chris@909: Chris@119: should "return requested project" do Chris@119: get '/projects/1.json' Chris@909: Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert_kind_of Hash, json['project'] Chris@119: assert_equal 1, json['project']['id'] Chris@119: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "POST /projects" do Chris@119: context "with valid parameters" do Chris@119: setup do Chris@119: Setting.default_projects_modules = ['issue_tracking', 'repository'] Chris@119: @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should_allow_api_authentication(:post, Chris@119: '/projects.xml', Chris@119: {:project => {:name => 'API test', :identifier => 'api-test'}}, Chris@119: {:success_code => :created}) Chris@909: Chris@909: Chris@119: should "create a project with the attributes" do Chris@119: assert_difference('Project.count') do Chris@119: post '/projects.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@909: Chris@119: project = Project.first(:order => 'id DESC') Chris@119: assert_equal 'API test', project.name Chris@119: assert_equal 'api-test', project.identifier Chris@119: assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort Chris@119: assert_equal Tracker.all.size, project.trackers.size Chris@909: Chris@119: assert_response :created Chris@119: assert_equal 'application/xml', @response.content_type Chris@119: assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} Chris@119: end Chris@909: Chris@119: should "accept enabled_module_names attribute" do Chris@119: @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) Chris@909: Chris@119: assert_difference('Project.count') do Chris@119: post '/projects.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@909: Chris@119: project = Project.first(:order => 'id DESC') Chris@119: assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort Chris@119: end Chris@909: Chris@119: should "accept tracker_ids attribute" do Chris@119: @parameters[:project].merge!({:tracker_ids => [1, 3]}) Chris@909: Chris@119: assert_difference('Project.count') do Chris@119: post '/projects.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@909: Chris@119: project = Project.first(:order => 'id DESC') Chris@119: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@119: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "with invalid parameters" do Chris@119: setup do Chris@119: @parameters = {:project => {:name => 'API test'}} Chris@119: end Chris@119: Chris@119: context ".xml" do Chris@119: should "return errors" do Chris@119: assert_no_difference('Project.count') do Chris@119: post '/projects.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/xml', @response.content_type Chris@119: assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} Chris@119: end Chris@119: end Chris@119: end chris@37: end Chris@909: Chris@119: context "PUT /projects/:id" do Chris@119: context "with valid parameters" do Chris@119: setup do Chris@119: @parameters = {:project => {:name => 'API update'}} Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should_allow_api_authentication(:put, Chris@119: '/projects/2.xml', Chris@119: {:project => {:name => 'API update'}}, Chris@119: {:success_code => :ok}) Chris@909: Chris@119: should "update the project" do Chris@119: assert_no_difference 'Project.count' do Chris@119: put '/projects/2.xml', @parameters, :authorization => credentials('jsmith') Chris@119: end Chris@119: assert_response :ok Chris@119: assert_equal 'application/xml', @response.content_type Chris@119: project = Project.find(2) Chris@119: assert_equal 'API update', project.name Chris@119: end Chris@909: Chris@119: should "accept enabled_module_names attribute" do Chris@119: @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) Chris@909: Chris@119: assert_no_difference 'Project.count' do Chris@119: put '/projects/2.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@119: assert_response :ok Chris@119: project = Project.find(2) Chris@119: assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort Chris@119: end Chris@909: Chris@119: should "accept tracker_ids attribute" do Chris@119: @parameters[:project].merge!({:tracker_ids => [1, 3]}) Chris@909: Chris@119: assert_no_difference 'Project.count' do Chris@119: put '/projects/2.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@119: assert_response :ok Chris@119: project = Project.find(2) Chris@119: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@119: end Chris@119: end chris@37: end Chris@909: Chris@119: context "with invalid parameters" do Chris@119: setup do Chris@119: @parameters = {:project => {:name => ''}} Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should "return errors" do Chris@119: assert_no_difference('Project.count') do Chris@119: put '/projects/2.xml', @parameters, :authorization => credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/xml', @response.content_type Chris@119: assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} Chris@119: end Chris@119: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "DELETE /projects/:id" do Chris@119: context ".xml" do Chris@119: should_allow_api_authentication(:delete, Chris@119: '/projects/2.xml', Chris@119: {}, Chris@119: {:success_code => :ok}) Chris@909: Chris@119: should "delete the project" do Chris@119: assert_difference('Project.count',-1) do Chris@119: delete '/projects/2.xml', {}, :authorization => credentials('admin') Chris@119: end Chris@119: assert_response :ok Chris@119: assert_nil Project.find_by_id(2) Chris@119: end Chris@119: end chris@37: end Chris@909: chris@37: def credentials(user, password=nil) chris@37: ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) chris@37: end chris@37: end