diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 842f39b..9c294e1 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -28,7 +28,16 @@ module Noosfero article = find_article(environment.articles, params[:id]) present article, :with => Entities::Article end - + + post ':id/vote' do + value = (params[:value] || 1).to_i + # FIXME verify allowed values + render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) + article = find_article(environment.articles, params[:id]) + Vote.create!(:voteable => article, :voter => current_person, :vote => value) + {:vote => true} + end + get ':id/children' do article = find_article(environment.articles, params[:id]) articles = select_filtered_collection_of(article, 'children', params) diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 6d57c79..bdf0d0a 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -442,5 +442,38 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal user.person, Article.last.last_changed_by end + should 'vote for an article' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + params[:value] = 1 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + json = JSON.parse(last_response.body) + assert json["vote"] + assert_equal 1, Vote.where(:voteable_id => article.id).sum(:vote) + end + + should 'vote for an article with default value' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + json = JSON.parse(last_response.body) + assert json["vote"] + assert_equal 1, Vote.where(:voteable_id => article.id).sum(:vote) + end + + should 'vote against an article' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + params[:value] = -1 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + json = JSON.parse(last_response.body) + assert json["vote"] + assert_equal -1, Vote.where(:voteable_id => article.id).sum(:vote) + end + + should 'do not allow forbidden vote value' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + params[:value] = 100 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 400, last_response.status + end end -- libgit2 0.21.2