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