From a0944909011e1bdb9bac474f06a4871ead049939 Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Thu, 10 Dec 2015 15:55:46 -0300 Subject: [PATCH] Merged login-captcha with staging --- lib/noosfero/api/helpers.rb | 13 +++++++++++++ lib/noosfero/api/v1/articles.rb | 18 +++--------------- test/unit/api/helpers_test.rb | 3 ++- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 9a5a96c..b53c7b7 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -51,6 +51,19 @@ require 'grape' end #################################################################### + #### VOTE + #################################################################### + def do_vote(article, current_person, value) + begin + vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) + return vote.save! + rescue ActiveRecord::RecordInvalid => e + render_api_error!(e.message, 400) + return false + end + end + + #################################################################### #### SEARCH #################################################################### def find_by_contents(asset, context, scope, query, paginate_options={:page => 1}, options={}) diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 2e161f2..d978e38 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -149,31 +149,19 @@ module Noosfero # FIXME verify allowed values render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) article = find_article(environment.articles, params[:id]) - ## If login with captcha if @current_tmp_user + # Vote allowed only if data does not include this article vote = (@current_tmp_user.data.include? article.id) ? false : true if vote @current_tmp_user.data << article.id @current_tmp_user.store - begin - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) - saved = vote.save! - {:vote => saved} - rescue ActiveRecord::RecordInvalid => e - render_api_error!(e.message, 400) - end + {:vote => do_vote(article, current_person, value)} else {:vote => false} end else - begin - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) - saved = vote.save! - {:vote => saved} - rescue ActiveRecord::RecordInvalid => e - render_api_error!(e.message, 400) - end + {:vote => do_vote(article, current_person, value)} end end diff --git a/test/unit/api/helpers_test.rb b/test/unit/api/helpers_test.rb index b27e87a..f34c3c6 100644 --- a/test/unit/api/helpers_test.rb +++ b/test/unit/api/helpers_test.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/test_helper'; + require File.dirname(__FILE__) + '/test_helper'; require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") @@ -31,6 +31,7 @@ class APIHelpersTest < ActiveSupport::TestCase user.generate_private_token! user.private_token_generated_at = DateTime.now.prev_year user.save + binding.pry self.params = {:private_token => user.private_token} assert_equal user, current_user end -- libgit2 0.21.2