Chris@117: # Redmine - project management software Chris@117: # Copyright (C) 2006-2010 Jean-Philippe Lang Chris@117: # Chris@117: # This program is free software; you can redistribute it and/or Chris@117: # modify it under the terms of the GNU General Public License Chris@117: # as published by the Free Software Foundation; either version 2 Chris@117: # of the License, or (at your option) any later version. Chris@117: # Chris@117: # This program is distributed in the hope that it will be useful, Chris@117: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@117: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@117: # GNU General Public License for more details. Chris@117: # Chris@117: # You should have received a copy of the GNU General Public License Chris@117: # along with this program; if not, write to the Free Software Chris@117: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@117: Chris@117: require File.expand_path('../../../test_helper', __FILE__) Chris@117: Chris@117: class ApiTest::TimeEntriesTest < ActionController::IntegrationTest Chris@117: fixtures :all Chris@117: Chris@117: def setup Chris@117: Setting.rest_api_enabled = '1' Chris@117: end Chris@117: Chris@117: context "GET /time_entries.xml" do Chris@117: should "return time entries" do Chris@117: get '/time_entries.xml', {}, :authorization => credentials('jsmith') Chris@117: assert_response :success Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: assert_tag :tag => 'time_entries', Chris@117: :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}} Chris@117: end Chris@117: end Chris@117: Chris@117: context "GET /time_entries/2.xml" do Chris@117: should "return requested time entry" do Chris@117: get '/time_entries/2.xml', {}, :authorization => credentials('jsmith') Chris@117: assert_response :success Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: assert_tag :tag => 'time_entry', Chris@117: :child => {:tag => 'id', :content => '2'} Chris@117: end Chris@117: end Chris@117: Chris@117: context "POST /time_entries.xml" do Chris@117: context "with issue_id" do Chris@117: should "return create time entry" do Chris@117: assert_difference 'TimeEntry.count' do Chris@117: post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :created Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: Chris@117: entry = TimeEntry.first(:order => 'id DESC') Chris@117: assert_equal 'jsmith', entry.user.login Chris@117: assert_equal Issue.find(1), entry.issue Chris@117: assert_equal Project.find(1), entry.project Chris@117: assert_equal Date.parse('2010-12-02'), entry.spent_on Chris@117: assert_equal 3.5, entry.hours Chris@117: assert_equal TimeEntryActivity.find(11), entry.activity Chris@117: end Chris@117: end Chris@117: Chris@117: context "with project_id" do Chris@117: should "return create time entry" do Chris@117: assert_difference 'TimeEntry.count' do Chris@117: post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :created Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: Chris@117: entry = TimeEntry.first(:order => 'id DESC') Chris@117: assert_equal 'jsmith', entry.user.login Chris@117: assert_nil entry.issue Chris@117: assert_equal Project.find(1), entry.project Chris@117: assert_equal Date.parse('2010-12-02'), entry.spent_on Chris@117: assert_equal 3.5, entry.hours Chris@117: assert_equal TimeEntryActivity.find(11), entry.activity Chris@117: end Chris@117: end Chris@117: Chris@117: context "with invalid parameters" do Chris@117: should "return errors" do Chris@117: assert_no_difference 'TimeEntry.count' do Chris@117: post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :unprocessable_entity Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: Chris@117: assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} Chris@117: end Chris@117: end Chris@117: end Chris@117: Chris@117: context "PUT /time_entries/2.xml" do Chris@117: context "with valid parameters" do Chris@117: should "update time entry" do Chris@117: assert_no_difference 'TimeEntry.count' do Chris@117: put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :ok Chris@117: assert_equal 'API Update', TimeEntry.find(2).comments Chris@117: end Chris@117: end Chris@117: Chris@117: context "with invalid parameters" do Chris@117: should "return errors" do Chris@117: assert_no_difference 'TimeEntry.count' do Chris@117: put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :unprocessable_entity Chris@117: assert_equal 'application/xml', @response.content_type Chris@117: Chris@117: assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} Chris@117: end Chris@117: end Chris@117: end Chris@117: Chris@117: context "DELETE /time_entries/2.xml" do Chris@117: should "destroy time entry" do Chris@117: assert_difference 'TimeEntry.count', -1 do Chris@117: delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith') Chris@117: end Chris@117: assert_response :ok Chris@117: assert_nil TimeEntry.find_by_id(2) Chris@117: end Chris@117: end Chris@117: Chris@117: def credentials(user, password=nil) Chris@117: ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) Chris@117: end Chris@117: end