From 8b7475adc83779f0a44318ea37c34ae6b16cd033 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Wed, 27 Jul 2016 08:58:19 -0300 Subject: [PATCH] considering timezone in api time filters --- app/api/helpers.rb | 2 +- test/api/activities_test.rb | 17 +++++++++++++++++ test/api/task_test.rb | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/api/helpers.rb b/app/api/helpers.rb index 127ed7a..f6b21a2 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -248,7 +248,7 @@ module Api def make_timestamp_with_parameters_and_method(object, method_or_relation, params) timestamp = nil if params[:timestamp] - datetime = DateTime.parse(params[:timestamp]) + datetime = DateTime.parse(params[:timestamp]).utc table_name = extract_associated_tablename(object, method_or_relation) assoc_class = extract_associated_classname(object, method_or_relation) date_atrr = assoc_class.attribute_names.include?('updated_at') ? 'updated_at' : 'created_at' diff --git a/test/api/activities_test.rb b/test/api/activities_test.rb index 303ad68..15e9f43 100644 --- a/test/api/activities_test.rb +++ b/test/api/activities_test.rb @@ -182,7 +182,24 @@ class ActivitiesTest < ActiveSupport::TestCase assert_not_includes json["activities"].map { |a| a["id"] }, a2.id end + should 'list activities with timestamp considering timezone' do + ActionTracker::Record.destroy_all + a1 = create_activity(:target => person) + a2 = create_activity(:target => person) + a2.updated_at = ActiveSupport::TimeZone.new('Brasilia').now + a2.save + + a1.updated_at = ActiveSupport::TimeZone.new('Brasilia').now + 3.hours + a1.save! + + params[:timestamp] = ActiveSupport::TimeZone.new('Brasilia').now + 1.hours + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_includes json["activities"].map { |a| a["id"] }, a1.id + assert_not_includes json["activities"].map { |a| a["id"] }, a2.id + end def create_activity(params = {}) params[:verb] ||= 'create_article' diff --git a/test/api/task_test.rb b/test/api/task_test.rb index bdccbb6..33de5d2 100644 --- a/test/api/task_test.rb +++ b/test/api/task_test.rb @@ -157,6 +157,22 @@ class TasksTest < ActiveSupport::TestCase assert_not_includes json["tasks"].map { |a| a["id"] }, t2.id end + should 'list tasks with timestamp considering timezone' do + t1 = create(Task, :requestor => person, :target => person) + t2 = create(Task, :requestor => person, :target => person, :created_at => ActiveSupport::TimeZone.new('Brasilia').now) + + t1.created_at = ActiveSupport::TimeZone.new('Brasilia').now + 3.hours + t1.save! + + + params[:timestamp] = ActiveSupport::TimeZone.new('Brasilia').now + 1.hours + get "/api/v1/tasks/?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_includes json["tasks"].map { |a| a["id"] }, t1.id + assert_not_includes json["tasks"].map { |a| a["id"] }, t2.id + end + task_actions=%w[finish cancel] task_actions_state={"finish"=>"FINISHED","cancel"=>"CANCELLED"} task_actions.each do |action| -- libgit2 0.21.2