"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/integration/api_test/time_entries_test.rb" (6 Apr 2020, 8404 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "time_entries_test.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 require File.expand_path('../../../test_helper', __FILE__)
   21 
   22 class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
   23   fixtures :projects, :trackers, :issue_statuses, :issues,
   24            :enumerations, :users, :issue_categories,
   25            :projects_trackers,
   26            :roles,
   27            :member_roles,
   28            :members,
   29            :enabled_modules,
   30            :time_entries
   31 
   32   test "GET /time_entries.xml should return time entries" do
   33     get '/time_entries.xml', :headers => credentials('jsmith')
   34     assert_response :success
   35     assert_equal 'application/xml', @response.content_type
   36     assert_select 'time_entries[type=array] time_entry id', :text => '2'
   37   end
   38 
   39   test "GET /time_entries.xml with limit should return limited results" do
   40     get '/time_entries.xml?limit=2', :headers => credentials('jsmith')
   41     assert_response :success
   42     assert_equal 'application/xml', @response.content_type
   43     assert_select 'time_entries[type=array] time_entry', 2
   44   end
   45 
   46   test "GET /time_entries/:id.xml should return the time entry" do
   47     get '/time_entries/2.xml', :headers => credentials('jsmith')
   48     assert_response :success
   49     assert_equal 'application/xml', @response.content_type
   50     assert_select 'time_entry id', :text => '2'
   51   end
   52 
   53   test "GET /time_entries/:id.xml on closed project should return the time entry" do
   54     project = TimeEntry.find(2).project
   55     project.close
   56     project.save!
   57 
   58     get '/time_entries/2.xml', :headers => credentials('jsmith')
   59     assert_response :success
   60     assert_equal 'application/xml', @response.content_type
   61     assert_select 'time_entry id', :text => '2'
   62   end
   63 
   64   test "GET /time_entries/:id.xml with invalid id should 404" do
   65     get '/time_entries/999.xml', :headers => credentials('jsmith')
   66     assert_response 404
   67   end
   68 
   69   test "POST /time_entries.xml with issue_id should create time entry" do
   70     assert_difference 'TimeEntry.count' do
   71       post(
   72         '/time_entries.xml',
   73         :params =>
   74           {:time_entry =>
   75             {:issue_id => '1', :spent_on => '2010-12-02',
   76              :hours => '3.5', :activity_id => '11'}},
   77         :headers => credentials('jsmith'))
   78     end
   79     assert_response :created
   80     assert_equal 'application/xml', @response.content_type
   81 
   82     entry = TimeEntry.order('id DESC').first
   83     assert_equal 'jsmith', entry.user.login
   84     assert_equal Issue.find(1), entry.issue
   85     assert_equal Project.find(1), entry.project
   86     assert_equal Date.parse('2010-12-02'), entry.spent_on
   87     assert_equal 3.5, entry.hours
   88     assert_equal TimeEntryActivity.find(11), entry.activity
   89   end
   90 
   91   test "POST /time_entries.xml with issue_id should accept custom fields" do
   92     field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
   93 
   94     assert_difference 'TimeEntry.count' do
   95       post(
   96         '/time_entries.xml',
   97         :params =>
   98           {:time_entry =>
   99             {:issue_id => '1', :spent_on => '2010-12-02',
  100              :hours => '3.5', :activity_id => '11',
  101              :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
  102         }},
  103         :headers => credentials('jsmith'))
  104     end
  105     assert_response :created
  106     assert_equal 'application/xml', @response.content_type
  107 
  108     entry = TimeEntry.order('id DESC').first
  109     assert_equal 'accepted', entry.custom_field_value(field)
  110   end
  111 
  112   test "POST /time_entries.xml with project_id should create time entry" do
  113     assert_difference 'TimeEntry.count' do
  114       post(
  115         '/time_entries.xml',
  116         :params =>
  117           {:time_entry =>
  118             {:project_id => '1', :spent_on => '2010-12-02',
  119              :hours => '3.5', :activity_id => '11'}},
  120         :headers => credentials('jsmith'))
  121     end
  122     assert_response :created
  123     assert_equal 'application/xml', @response.content_type
  124 
  125     entry = TimeEntry.order('id DESC').first
  126     assert_equal 'jsmith', entry.user.login
  127     assert_nil entry.issue
  128     assert_equal Project.find(1), entry.project
  129     assert_equal Date.parse('2010-12-02'), entry.spent_on
  130     assert_equal 3.5, entry.hours
  131     assert_equal TimeEntryActivity.find(11), entry.activity
  132   end
  133 
  134   test "POST /time_entries.xml with invalid parameters should return errors" do
  135     assert_no_difference 'TimeEntry.count' do
  136       post(
  137         '/time_entries.xml',
  138         :params => {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}},
  139         :headers => credentials('jsmith'))
  140     end
  141     assert_response :unprocessable_entity
  142     assert_equal 'application/xml', @response.content_type
  143 
  144     assert_select 'errors error', :text => "Hours cannot be blank"
  145   end
  146 
  147   test "POST /time_entries.xml with :project_id for other user" do
  148     Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  149 
  150     entry = new_record(TimeEntry) do
  151       post(
  152         '/time_entries.xml',
  153         :params =>
  154           {:time_entry =>
  155             {:project_id => '1', :spent_on => '2010-12-02', :user_id => '3',
  156              :hours => '3.5', :activity_id => '11'}},
  157         :headers => credentials('jsmith'))
  158     end
  159     assert_response :created
  160     assert_equal 3, entry.user_id
  161     assert_equal 2, entry.author_id
  162   end
  163 
  164   test "POST /time_entries.xml with :issue_id for other user" do
  165     Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  166 
  167     entry = new_record(TimeEntry) do
  168       post(
  169         '/time_entries.xml',
  170         :params =>
  171           {:time_entry =>
  172             {:issue_id => '1', :spent_on => '2010-12-02', :user_id => '3',
  173              :hours => '3.5', :activity_id => '11'}},
  174         :headers => credentials('jsmith'))
  175     end
  176     assert_response :created
  177     assert_equal 3, entry.user_id
  178     assert_equal 2, entry.author_id
  179   end
  180 
  181   test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
  182     assert_no_difference 'TimeEntry.count' do
  183       put(
  184         '/time_entries/2.xml',
  185         :params => {:time_entry => {:comments => 'API Update'}},
  186         :headers => credentials('jsmith'))
  187     end
  188     assert_response :no_content
  189     assert_equal '', @response.body
  190     assert_equal 'API Update', TimeEntry.find(2).comments
  191   end
  192 
  193   test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
  194     assert_no_difference 'TimeEntry.count' do
  195       put(
  196         '/time_entries/2.xml',
  197         :params => {:time_entry => {:hours => '', :comments => 'API Update'}},
  198         :headers => credentials('jsmith'))
  199     end
  200     assert_response :unprocessable_entity
  201     assert_equal 'application/xml', @response.content_type
  202 
  203     assert_select 'errors error', :text => "Hours cannot be blank"
  204   end
  205 
  206   test "PUT /time_entries/:id.xml without permissions should fail" do
  207     put(
  208       '/time_entries/2.xml',
  209       :params => {:time_entry => {:hours => '2.3', :comments => 'API Update'}},
  210       :headers => credentials('dlopper'))
  211     assert_response 403
  212   end
  213 
  214   test "DELETE /time_entries/:id.xml should destroy time entry" do
  215     assert_difference 'TimeEntry.count', -1 do
  216       delete '/time_entries/2.xml', :headers => credentials('jsmith')
  217     end
  218     assert_response :no_content
  219     assert_equal '', @response.body
  220     assert_nil TimeEntry.find_by_id(2)
  221   end
  222 
  223   test "DELETE /time_entries/:id.xml with failure should return errors" do
  224     TimeEntry.any_instance.stubs(:destroy).returns(false)
  225 
  226     assert_no_difference 'TimeEntry.count' do
  227       delete '/time_entries/2.xml', :headers => credentials('jsmith')
  228     end
  229     assert_response :unprocessable_entity
  230     assert_equal 'application/xml', @response.content_type
  231     assert_select 'errors'
  232   end
  233 end