Commit 4fb04c74348ee79d15255aac9df8ab5f75b89a08

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent 8e6799ff

fixing unit tests

lib/noosfero/api/entities.rb
@@ -64,6 +64,7 @@ module Noosfero @@ -64,6 +64,7 @@ module Noosfero
64 expose :profile, :using => Profile 64 expose :profile, :using => Profile
65 expose :categories, :using => Category 65 expose :categories, :using => Category
66 expose :image, :using => Image 66 expose :image, :using => Image
  67 + #TODO Apply vote stuff in core and make this test
67 expose :votes_for 68 expose :votes_for
68 expose :votes_against 69 expose :votes_against
69 expose :setting 70 expose :setting
lib/noosfero/api/helpers.rb
@@ -2,10 +2,10 @@ module Noosfero @@ -2,10 +2,10 @@ module Noosfero
2 module API 2 module API
3 module APIHelpers 3 module APIHelpers
4 PRIVATE_TOKEN_PARAM = :private_token 4 PRIVATE_TOKEN_PARAM = :private_token
5 - ALLOWED_PARAMETERS = ['parent_id', 'from', 'until', 'content_type'] 5 + ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type]
6 6
7 def current_user 7 def current_user
8 - private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token'] || cookies['_noosfero_api_session']).to_s if params 8 + private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s
9 @current_user ||= User.find_by_private_token(private_token) 9 @current_user ||= User.find_by_private_token(private_token)
10 @current_user = nil if !@current_user.nil? && @current_user.private_token_expired? 10 @current_user = nil if !@current_user.nil? && @current_user.private_token_expired?
11 @current_user 11 @current_user
@@ -53,10 +53,10 @@ module Noosfero @@ -53,10 +53,10 @@ module Noosfero
53 def make_conditions_with_parameter(params = {}) 53 def make_conditions_with_parameter(params = {})
54 parsed_params = parser_params(params) 54 parsed_params = parser_params(params)
55 conditions = {} 55 conditions = {}
56 - from_date = DateTime.parse(parsed_params.delete('from')) if parsed_params['from']  
57 - until_date = DateTime.parse(parsed_params.delete('until')) if parsed_params['until'] 56 + from_date = DateTime.parse(parsed_params.delete(:from)) if parsed_params[:from]
  57 + until_date = DateTime.parse(parsed_params.delete(:until)) if parsed_params[:until]
58 58
59 - conditions[:type] = parse_content_type(parsed_params.delete('content_type')) unless parsed_params['content_type'].nil? 59 + conditions[:type] = parse_content_type(parsed_params.delete(:content_type)) unless parsed_params[:content_type].nil?
60 60
61 conditions[:created_at] = period(from_date, until_date) if from_date || until_date 61 conditions[:created_at] = period(from_date, until_date) if from_date || until_date
62 conditions.merge!(parsed_params) 62 conditions.merge!(parsed_params)
@@ -167,7 +167,11 @@ module Noosfero @@ -167,7 +167,11 @@ module Noosfero
167 private 167 private
168 168
169 def parser_params(params) 169 def parser_params(params)
170 - params.select{|k,v| ALLOWED_PARAMETERS.include?(k)} 170 + parsed_params = {}
  171 + params.map do |k,v|
  172 + parsed_params[k.to_sym] = v if ALLOWED_PARAMETERS.include?(k.to_sym)
  173 + end
  174 + parsed_params
171 end 175 end
172 176
173 def default_limit 177 def default_limit
lib/noosfero/api/v1/articles.rb
@@ -32,10 +32,13 @@ module Noosfero @@ -32,10 +32,13 @@ module Noosfero
32 get ':id/children' do 32 get ':id/children' do
33 article = find_article(environment.articles, params[:id]) 33 article = find_article(environment.articles, params[:id])
34 34
  35 + #TODO make tests for this situation
35 votes_order = params.delete(:order) if params[:order]=='votes_score' 36 votes_order = params.delete(:order) if params[:order]=='votes_score'
36 articles = select_filtered_collection_of(article, 'children', params) 37 articles = select_filtered_collection_of(article, 'children', params)
37 articles = articles.display_filter(current_person, nil) 38 articles = articles.display_filter(current_person, nil)
38 39
  40 +
  41 + #TODO make tests for this situation
39 if votes_order 42 if votes_order
40 articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') 43 articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC')
41 end 44 end
test/unit/api/helpers_test.rb
1 require File.dirname(__FILE__) + '/test_helper' 1 require File.dirname(__FILE__) + '/test_helper'
2 require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") 2 require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers")
3 3
4 -class APITest < ActiveSupport::TestCase 4 +class APIHelpersTest < ActiveSupport::TestCase
5 5
6 include Noosfero::API::APIHelpers 6 include Noosfero::API::APIHelpers
7 7
@@ -126,11 +126,18 @@ class APITest &lt; ActiveSupport::TestCase @@ -126,11 +126,18 @@ class APITest &lt; ActiveSupport::TestCase
126 assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at] 126 assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at]
127 end 127 end
128 128
  129 + should 'make_conditions_with_parameter return created_at parameter if from period is defined as string' do
  130 + assert_not_nil make_conditions_with_parameter('from' => '2010-10-10')[:created_at]
  131 + end
  132 +
129 should 'make_conditions_with_parameter return created_at parameter if until period is defined' do 133 should 'make_conditions_with_parameter return created_at parameter if until period is defined' do
130 assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at] 134 assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at]
131 end 135 end
132 136
133 -# should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do 137 + should 'make_conditions_with_parameter return created_at parameter if until period is defined as string' do
  138 + assert_not_nil make_conditions_with_parameter('until' => '2010-10-10')[:created_at]
  139 + end
  140 +
134 should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do 141 should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do
135 assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min 142 assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min
136 end 143 end
test/unit/api/session_test.rb
1 require File.dirname(__FILE__) + '/test_helper' 1 require File.dirname(__FILE__) + '/test_helper'
2 2
3 -class APITest < ActiveSupport::TestCase 3 +class SessionTest < ActiveSupport::TestCase
4 4
5 def setup 5 def setup
6 login_api 6 login_api