To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / integration / api_test / time_entries_test.rb @ 441:cbce1fd3b1b7

History | View | Annotate | Download (5.48 KB)

1 119:8661b858af72 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2010  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
require File.expand_path('../../../test_helper', __FILE__)
19
20
class ApiTest::TimeEntriesTest < ActionController::IntegrationTest
21
  fixtures :all
22
23
  def setup
24
    Setting.rest_api_enabled = '1'
25
  end
26
27
  context "GET /time_entries.xml" do
28
    should "return time entries" do
29
      get '/time_entries.xml', {}, :authorization => credentials('jsmith')
30
      assert_response :success
31
      assert_equal 'application/xml', @response.content_type
32
      assert_tag :tag => 'time_entries',
33
        :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
34
    end
35 441:cbce1fd3b1b7 Chris
36
    context "with limit" do
37
      should "return limited results" do
38
        get '/time_entries.xml?limit=2', {}, :authorization => credentials('jsmith')
39
        assert_response :success
40
        assert_equal 'application/xml', @response.content_type
41
        assert_tag :tag => 'time_entries',
42
          :children => {:count => 2}
43
      end
44
    end
45 119:8661b858af72 Chris
  end
46
47
  context "GET /time_entries/2.xml" do
48
    should "return requested time entry" do
49
      get '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
50
      assert_response :success
51
      assert_equal 'application/xml', @response.content_type
52
      assert_tag :tag => 'time_entry',
53
        :child => {:tag => 'id', :content => '2'}
54
    end
55
  end
56
57
  context "POST /time_entries.xml" do
58
    context "with issue_id" do
59
      should "return create time entry" do
60
        assert_difference 'TimeEntry.count' do
61
          post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
62
        end
63
        assert_response :created
64
        assert_equal 'application/xml', @response.content_type
65
66
        entry = TimeEntry.first(:order => 'id DESC')
67
        assert_equal 'jsmith', entry.user.login
68
        assert_equal Issue.find(1), entry.issue
69
        assert_equal Project.find(1), entry.project
70
        assert_equal Date.parse('2010-12-02'), entry.spent_on
71
        assert_equal 3.5, entry.hours
72
        assert_equal TimeEntryActivity.find(11), entry.activity
73
      end
74
    end
75
76
    context "with project_id" do
77
      should "return create time entry" do
78
        assert_difference 'TimeEntry.count' do
79
          post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
80
        end
81
        assert_response :created
82
        assert_equal 'application/xml', @response.content_type
83
84
        entry = TimeEntry.first(:order => 'id DESC')
85
        assert_equal 'jsmith', entry.user.login
86
        assert_nil entry.issue
87
        assert_equal Project.find(1), entry.project
88
        assert_equal Date.parse('2010-12-02'), entry.spent_on
89
        assert_equal 3.5, entry.hours
90
        assert_equal TimeEntryActivity.find(11), entry.activity
91
      end
92
    end
93
94
    context "with invalid parameters" do
95
      should "return errors" do
96
        assert_no_difference 'TimeEntry.count' do
97
          post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith')
98
        end
99
        assert_response :unprocessable_entity
100
        assert_equal 'application/xml', @response.content_type
101
102
        assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
103
      end
104
    end
105
  end
106
107
  context "PUT /time_entries/2.xml" do
108
    context "with valid parameters" do
109
      should "update time entry" do
110
        assert_no_difference 'TimeEntry.count' do
111
          put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith')
112
        end
113
        assert_response :ok
114
        assert_equal 'API Update', TimeEntry.find(2).comments
115
      end
116
    end
117
118
    context "with invalid parameters" do
119
      should "return errors" do
120
        assert_no_difference 'TimeEntry.count' do
121
          put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith')
122
        end
123
        assert_response :unprocessable_entity
124
        assert_equal 'application/xml', @response.content_type
125
126
        assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
127
      end
128
    end
129
  end
130
131
  context "DELETE /time_entries/2.xml" do
132
    should "destroy time entry" do
133
      assert_difference 'TimeEntry.count', -1 do
134
        delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
135
      end
136
      assert_response :ok
137
      assert_nil TimeEntry.find_by_id(2)
138
    end
139
  end
140
141
  def credentials(user, password=nil)
142
    ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
143
  end
144
end