diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index af6e5f9..b1564a1 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -64,6 +64,7 @@ module Noosfero expose :profile, :using => Profile expose :categories, :using => Category expose :image, :using => Image + #TODO Apply vote stuff in core and make this test expose :votes_for expose :votes_against expose :setting diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 14ab098..489ff1c 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -2,10 +2,10 @@ module Noosfero module API module APIHelpers PRIVATE_TOKEN_PARAM = :private_token - ALLOWED_PARAMETERS = ['parent_id', 'from', 'until', 'content_type'] + ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type] def current_user - private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token'] || cookies['_noosfero_api_session']).to_s if params + private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s @current_user ||= User.find_by_private_token(private_token) @current_user = nil if !@current_user.nil? && @current_user.private_token_expired? @current_user @@ -53,10 +53,10 @@ module Noosfero def make_conditions_with_parameter(params = {}) parsed_params = parser_params(params) conditions = {} - from_date = DateTime.parse(parsed_params.delete('from')) if parsed_params['from'] - until_date = DateTime.parse(parsed_params.delete('until')) if parsed_params['until'] + from_date = DateTime.parse(parsed_params.delete(:from)) if parsed_params[:from] + until_date = DateTime.parse(parsed_params.delete(:until)) if parsed_params[:until] - conditions[:type] = parse_content_type(parsed_params.delete('content_type')) unless parsed_params['content_type'].nil? + conditions[:type] = parse_content_type(parsed_params.delete(:content_type)) unless parsed_params[:content_type].nil? conditions[:created_at] = period(from_date, until_date) if from_date || until_date conditions.merge!(parsed_params) @@ -167,7 +167,11 @@ module Noosfero private def parser_params(params) - params.select{|k,v| ALLOWED_PARAMETERS.include?(k)} + parsed_params = {} + params.map do |k,v| + parsed_params[k.to_sym] = v if ALLOWED_PARAMETERS.include?(k.to_sym) + end + parsed_params end def default_limit diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 0e20aa4..157155e 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -32,10 +32,13 @@ module Noosfero get ':id/children' do article = find_article(environment.articles, params[:id]) + #TODO make tests for this situation votes_order = params.delete(:order) if params[:order]=='votes_score' articles = select_filtered_collection_of(article, 'children', params) articles = articles.display_filter(current_person, nil) + + #TODO make tests for this situation if votes_order articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') end diff --git a/test/unit/api/helpers_test.rb b/test/unit/api/helpers_test.rb index 945fa02..676bf96 100644 --- a/test/unit/api/helpers_test.rb +++ b/test/unit/api/helpers_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/test_helper' require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") -class APITest < ActiveSupport::TestCase +class APIHelpersTest < ActiveSupport::TestCase include Noosfero::API::APIHelpers @@ -126,11 +126,18 @@ class APITest < ActiveSupport::TestCase assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at] end + should 'make_conditions_with_parameter return created_at parameter if from period is defined as string' do + assert_not_nil make_conditions_with_parameter('from' => '2010-10-10')[:created_at] + end + should 'make_conditions_with_parameter return created_at parameter if until period is defined' do assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at] end -# should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do + should 'make_conditions_with_parameter return created_at parameter if until period is defined as string' do + assert_not_nil make_conditions_with_parameter('until' => '2010-10-10')[:created_at] + end + should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min end diff --git a/test/unit/api/session_test.rb b/test/unit/api/session_test.rb index 1695d3e..84e4712 100644 --- a/test/unit/api/session_test.rb +++ b/test/unit/api/session_test.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + '/test_helper' -class APITest < ActiveSupport::TestCase +class SessionTest < ActiveSupport::TestCase def setup login_api -- libgit2 0.21.2