Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require File.expand_path('../../../test_helper', __FILE__) Chris@1494: Chris@1494: class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base Chris@1494: fixtures :projects, :trackers, :issue_statuses, :issues, Chris@1494: :enumerations, :users, :issue_categories, Chris@1494: :projects_trackers, Chris@1494: :roles, Chris@1494: :member_roles, Chris@1494: :members, Chris@1494: :enabled_modules, Chris@1494: :time_entries Chris@1494: Chris@1494: def setup Chris@1494: Setting.rest_api_enabled = '1' Chris@1494: end Chris@1494: Chris@1494: test "GET /time_entries.xml should return time entries" do Chris@1494: get '/time_entries.xml', {}, credentials('jsmith') Chris@1494: assert_response :success Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag :tag => 'time_entries', Chris@1494: :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}} Chris@1494: end Chris@1494: Chris@1494: test "GET /time_entries.xml with limit should return limited results" do Chris@1494: get '/time_entries.xml?limit=2', {}, credentials('jsmith') Chris@1494: assert_response :success Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag :tag => 'time_entries', Chris@1494: :children => {:count => 2} Chris@1494: end Chris@1494: Chris@1494: test "GET /time_entries/:id.xml should return the time entry" do Chris@1494: get '/time_entries/2.xml', {}, credentials('jsmith') Chris@1494: assert_response :success Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag :tag => 'time_entry', Chris@1494: :child => {:tag => 'id', :content => '2'} Chris@1494: end Chris@1494: Chris@1494: test "POST /time_entries.xml with issue_id should create time entry" do Chris@1494: assert_difference 'TimeEntry.count' do Chris@1494: post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :created Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: Chris@1494: entry = TimeEntry.first(:order => 'id DESC') Chris@1494: assert_equal 'jsmith', entry.user.login Chris@1494: assert_equal Issue.find(1), entry.issue Chris@1494: assert_equal Project.find(1), entry.project Chris@1494: assert_equal Date.parse('2010-12-02'), entry.spent_on Chris@1494: assert_equal 3.5, entry.hours Chris@1494: assert_equal TimeEntryActivity.find(11), entry.activity Chris@1494: end Chris@1494: Chris@1494: test "POST /time_entries.xml with issue_id should accept custom fields" do Chris@1494: field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string') Chris@1494: Chris@1494: assert_difference 'TimeEntry.count' do Chris@1494: post '/time_entries.xml', {:time_entry => { Chris@1494: :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}] Chris@1494: }}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :created Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: Chris@1494: entry = TimeEntry.first(:order => 'id DESC') Chris@1494: assert_equal 'accepted', entry.custom_field_value(field) Chris@1494: end Chris@1494: Chris@1494: test "POST /time_entries.xml with project_id should create time entry" do Chris@1494: assert_difference 'TimeEntry.count' do Chris@1494: post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :created Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: Chris@1494: entry = TimeEntry.first(:order => 'id DESC') Chris@1494: assert_equal 'jsmith', entry.user.login Chris@1494: assert_nil entry.issue Chris@1494: assert_equal Project.find(1), entry.project Chris@1494: assert_equal Date.parse('2010-12-02'), entry.spent_on Chris@1494: assert_equal 3.5, entry.hours Chris@1494: assert_equal TimeEntryActivity.find(11), entry.activity Chris@1494: end Chris@1494: Chris@1494: test "POST /time_entries.xml with invalid parameters should return errors" do Chris@1494: assert_no_difference 'TimeEntry.count' do Chris@1494: post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :unprocessable_entity Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: Chris@1494: assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} Chris@1494: end Chris@1494: Chris@1494: test "PUT /time_entries/:id.xml with valid parameters should update time entry" do Chris@1494: assert_no_difference 'TimeEntry.count' do Chris@1494: put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :ok Chris@1494: assert_equal '', @response.body Chris@1494: assert_equal 'API Update', TimeEntry.find(2).comments Chris@1494: end Chris@1494: Chris@1494: test "PUT /time_entries/:id.xml with invalid parameters should return errors" do Chris@1494: assert_no_difference 'TimeEntry.count' do Chris@1494: put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :unprocessable_entity Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: Chris@1494: assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} Chris@1494: end Chris@1494: Chris@1494: test "DELETE /time_entries/:id.xml should destroy time entry" do Chris@1494: assert_difference 'TimeEntry.count', -1 do Chris@1494: delete '/time_entries/2.xml', {}, credentials('jsmith') Chris@1494: end Chris@1494: assert_response :ok Chris@1494: assert_equal '', @response.body Chris@1494: assert_nil TimeEntry.find_by_id(2) Chris@1494: end Chris@1494: end