summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/timelog_controller.rb82
-rw-r--r--app/views/timelog/index.apit16
-rw-r--r--app/views/timelog/show.apit12
-rw-r--r--lib/redmine.rb2
-rw-r--r--test/integration/api_test/time_entries_test.rb134
5 files changed, 228 insertions, 18 deletions
diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb
index 1ac7c9a5c..4bec02f35 100644
--- a/app/controllers/timelog_controller.rb
+++ b/app/controllers/timelog_controller.rb
@@ -1,5 +1,5 @@
-# redMine - project management software
-# Copyright (C) 2006-2007 Jean-Philippe Lang
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -18,10 +18,11 @@
class TimelogController < ApplicationController
menu_item :issues
before_filter :find_project, :only => [:new, :create]
- before_filter :find_time_entry, :only => [:edit, :update, :destroy]
+ before_filter :find_time_entry, :only => [:show, :edit, :update, :destroy]
before_filter :authorize, :except => [:index]
before_filter :find_optional_project, :only => [:index]
-
+ accept_key_auth :index, :show, :create, :update, :destroy
+
helper :sort
include SortHelper
helper :issues
@@ -66,6 +67,18 @@ class TimelogController < ApplicationController
render :layout => !request.xhr?
}
+ format.api {
+ @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
+ @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
+ @entries = TimeEntry.find(:all,
+ :include => [:project, :activity, :user, {:issue => :tracker}],
+ :conditions => cond.conditions,
+ :order => sort_clause,
+ :limit => @entry_pages.items_per_page,
+ :offset => @entry_pages.current.offset)
+
+ render :template => 'timelog/index.apit'
+ }
format.atom {
entries = TimeEntry.find(:all,
:include => [:project, :activity, :user, {:issue => :tracker}],
@@ -85,6 +98,14 @@ class TimelogController < ApplicationController
end
end
end
+
+ def show
+ respond_to do |format|
+ # TODO: Implement html response
+ format.html { render :nothing => true, :status => 406 }
+ format.api { render :template => 'timelog/show.apit' }
+ end
+ end
def new
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
@@ -102,10 +123,18 @@ class TimelogController < ApplicationController
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
if @time_entry.save
- flash[:notice] = l(:notice_successful_update)
- redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+ respond_to do |format|
+ format.html {
+ flash[:notice] = l(:notice_successful_update)
+ redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+ }
+ format.api { render :template => 'timelog/show.apit', :status => :created, :location => time_entry_url(@time_entry) }
+ end
else
- render :action => 'edit'
+ respond_to do |format|
+ format.html { render :action => 'edit' }
+ format.api { render_validation_errors(@time_entry) }
+ end
end
end
@@ -122,21 +151,40 @@ class TimelogController < ApplicationController
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
if @time_entry.save
- flash[:notice] = l(:notice_successful_update)
- redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+ respond_to do |format|
+ format.html {
+ flash[:notice] = l(:notice_successful_update)
+ redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+ }
+ format.api { head :ok }
+ end
else
- render :action => 'edit'
+ respond_to do |format|
+ format.html { render :action => 'edit' }
+ format.api { render_validation_errors(@time_entry) }
+ end
end
end
verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
def destroy
if @time_entry.destroy && @time_entry.destroyed?
- flash[:notice] = l(:notice_successful_delete)
+ respond_to do |format|
+ format.html {
+ flash[:notice] = l(:notice_successful_delete)
+ redirect_to :back
+ }
+ format.api { head :ok }
+ end
else
- flash[:error] = l(:notice_unable_delete_time_entry)
+ respond_to do |format|
+ format.html {
+ flash[:notice] = l(:notice_unable_delete_time_entry)
+ redirect_to :back
+ }
+ format.api { render_validation_errors(@time_entry) }
+ end
end
- redirect_to :back
rescue ::ActionController::RedirectBackError
redirect_to :action => 'index', :project_id => @time_entry.project
end
@@ -154,11 +202,11 @@ private
end
def find_project
- if params[:issue_id]
- @issue = Issue.find(params[:issue_id])
+ if issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])
+ @issue = Issue.find(issue_id)
@project = @issue.project
- elsif params[:project_id]
- @project = Project.find(params[:project_id])
+ elsif project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])
+ @project = Project.find(project_id)
else
render_404
return false
diff --git a/app/views/timelog/index.apit b/app/views/timelog/index.apit
new file mode 100644
index 000000000..0367e0666
--- /dev/null
+++ b/app/views/timelog/index.apit
@@ -0,0 +1,16 @@
+api.array :time_entries do
+ @entries.each do |time_entry|
+ api.time_entry do
+ api.id time_entry.id
+ api.project(:id => time_entry.project_id, :name => time_entry.project.name) unless time_entry.project.nil?
+ api.issue(:id => time_entry.issue_id) unless time_entry.issue.nil?
+ api.user(:id => time_entry.user_id, :name => time_entry.user.name) unless time_entry.user.nil?
+ api.activity(:id => time_entry.activity_id, :name => time_entry.activity.name) unless time_entry.activity.nil?
+ api.hours time_entry.hours
+ api.comments time_entry.comments
+ api.spent_on time_entry.spent_on
+ api.created_on time_entry.created_on
+ api.updated_on time_entry.updated_on
+ end
+ end
+end
diff --git a/app/views/timelog/show.apit b/app/views/timelog/show.apit
new file mode 100644
index 000000000..d5d3c5d26
--- /dev/null
+++ b/app/views/timelog/show.apit
@@ -0,0 +1,12 @@
+api.time_entry do
+ api.id @time_entry.id
+ api.project(:id => @time_entry.project_id, :name => @time_entry.project.name) unless @time_entry.project.nil?
+ api.issue(:id => @time_entry.issue_id) unless @time_entry.issue.nil?
+ api.user(:id => @time_entry.user_id, :name => @time_entry.user.name) unless @time_entry.user.nil?
+ api.activity(:id => @time_entry.activity_id, :name => @time_entry.activity.name) unless @time_entry.activity.nil?
+ api.hours @time_entry.hours
+ api.comments @time_entry.comments
+ api.spent_on @time_entry.spent_on
+ api.created_on @time_entry.created_on
+ api.updated_on @time_entry.updated_on
+end
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 2b01f25e8..b5a9a57c5 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -85,7 +85,7 @@ Redmine::AccessControl.map do |map|
map.project_module :time_tracking do |map|
map.permission :log_time, {:timelog => [:new, :create, :edit, :update]}, :require => :loggedin
- map.permission :view_time_entries, :timelog => [:index], :time_entry_reports => [:report]
+ map.permission :view_time_entries, :timelog => [:index, :show], :time_entry_reports => [:report]
map.permission :edit_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :member
map.permission :edit_own_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :loggedin
map.permission :manage_project_activities, {:project_enumerations => [:update, :destroy]}, :require => :member
diff --git a/test/integration/api_test/time_entries_test.rb b/test/integration/api_test/time_entries_test.rb
new file mode 100644
index 000000000..317baba67
--- /dev/null
+++ b/test/integration/api_test/time_entries_test.rb
@@ -0,0 +1,134 @@
+# Redmine - project management software
+# Copyright (C) 2006-2010 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+require "#{File.dirname(__FILE__)}/../../test_helper"
+
+class ApiTest::TimeEntriesTest < ActionController::IntegrationTest
+ fixtures :all
+
+ def setup
+ Setting.rest_api_enabled = '1'
+ end
+
+ context "GET /time_entries.xml" do
+ should "return time entries" do
+ get '/time_entries.xml', {}, :authorization => credentials('jsmith')
+ assert_response :success
+ assert_equal 'application/xml', @response.content_type
+ assert_tag :tag => 'time_entries',
+ :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
+ end
+ end
+
+ context "GET /time_entries/2.xml" do
+ should "return requested time entry" do
+ get '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
+ assert_response :success
+ assert_equal 'application/xml', @response.content_type
+ assert_tag :tag => 'time_entry',
+ :child => {:tag => 'id', :content => '2'}
+ end
+ end
+
+ context "POST /time_entries.xml" do
+ context "with issue_id" do
+ should "return create time entry" do
+ assert_difference 'TimeEntry.count' do
+ post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
+ end
+ assert_response :created
+ assert_equal 'application/xml', @response.content_type
+
+ entry = TimeEntry.first(:order => 'id DESC')
+ assert_equal 'jsmith', entry.user.login
+ assert_equal Issue.find(1), entry.issue
+ assert_equal Project.find(1), entry.project
+ assert_equal Date.parse('2010-12-02'), entry.spent_on
+ assert_equal 3.5, entry.hours
+ assert_equal TimeEntryActivity.find(11), entry.activity
+ end
+ end
+
+ context "with project_id" do
+ should "return create time entry" do
+ assert_difference 'TimeEntry.count' do
+ post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
+ end
+ assert_response :created
+ assert_equal 'application/xml', @response.content_type
+
+ entry = TimeEntry.first(:order => 'id DESC')
+ assert_equal 'jsmith', entry.user.login
+ assert_nil entry.issue
+ assert_equal Project.find(1), entry.project
+ assert_equal Date.parse('2010-12-02'), entry.spent_on
+ assert_equal 3.5, entry.hours
+ assert_equal TimeEntryActivity.find(11), entry.activity
+ end
+ end
+
+ context "with invalid parameters" do
+ should "return errors" do
+ assert_no_difference 'TimeEntry.count' do
+ post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith')
+ end
+ assert_response :unprocessable_entity
+ assert_equal 'application/xml', @response.content_type
+
+ assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
+ end
+ end
+ end
+
+ context "PUT /time_entries/2.xml" do
+ context "with valid parameters" do
+ should "update time entry" do
+ assert_no_difference 'TimeEntry.count' do
+ put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith')
+ end
+ assert_response :ok
+ assert_equal 'API Update', TimeEntry.find(2).comments
+ end
+ end
+
+ context "with invalid parameters" do
+ should "return errors" do
+ assert_no_difference 'TimeEntry.count' do
+ put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith')
+ end
+ assert_response :unprocessable_entity
+ assert_equal 'application/xml', @response.content_type
+
+ assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
+ end
+ end
+ end
+
+ context "DELETE /time_entries/2.xml" do
+ should "destroy time entry" do
+ assert_difference 'TimeEntry.count', -1 do
+ delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
+ end
+ assert_response :ok
+ assert_nil TimeEntry.find_by_id(2)
+ end
+ end
+
+ def credentials(user, password=nil)
+ ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
+ end
+end