Commit 02a2d022a80d428f791d50c033ed1e1c039a6c9d
1 parent
e31e4159
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
api: vote on articles
Showing
2 changed files
with
43 additions
and
1 deletions
Show diff stats
lib/noosfero/api/v1/articles.rb
| ... | ... | @@ -28,7 +28,16 @@ module Noosfero |
| 28 | 28 | article = find_article(environment.articles, params[:id]) |
| 29 | 29 | present article, :with => Entities::Article |
| 30 | 30 | end |
| 31 | - | |
| 31 | + | |
| 32 | + post ':id/vote' do | |
| 33 | + value = (params[:value] || 1).to_i | |
| 34 | + # FIXME verify allowed values | |
| 35 | + render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) | |
| 36 | + article = find_article(environment.articles, params[:id]) | |
| 37 | + Vote.create!(:voteable => article, :voter => current_person, :vote => value) | |
| 38 | + {:vote => true} | |
| 39 | + end | |
| 40 | + | |
| 32 | 41 | get ':id/children' do |
| 33 | 42 | article = find_article(environment.articles, params[:id]) |
| 34 | 43 | articles = select_filtered_collection_of(article, 'children', params) | ... | ... |
test/unit/api/articles_test.rb
| ... | ... | @@ -442,5 +442,38 @@ class ArticlesTest < ActiveSupport::TestCase |
| 442 | 442 | assert_equal user.person, Article.last.last_changed_by |
| 443 | 443 | end |
| 444 | 444 | |
| 445 | + should 'vote for an article' do | |
| 446 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
| 447 | + params[:value] = 1 | |
| 448 | + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" | |
| 449 | + json = JSON.parse(last_response.body) | |
| 450 | + assert json["vote"] | |
| 451 | + assert_equal 1, Vote.where(:voteable_id => article.id).sum(:vote) | |
| 452 | + end | |
| 453 | + | |
| 454 | + should 'vote for an article with default value' do | |
| 455 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
| 456 | + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" | |
| 457 | + json = JSON.parse(last_response.body) | |
| 458 | + assert json["vote"] | |
| 459 | + assert_equal 1, Vote.where(:voteable_id => article.id).sum(:vote) | |
| 460 | + end | |
| 461 | + | |
| 462 | + should 'vote against an article' do | |
| 463 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
| 464 | + params[:value] = -1 | |
| 465 | + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" | |
| 466 | + json = JSON.parse(last_response.body) | |
| 467 | + assert json["vote"] | |
| 468 | + assert_equal -1, Vote.where(:voteable_id => article.id).sum(:vote) | |
| 469 | + end | |
| 470 | + | |
| 471 | + should 'do not allow forbidden vote value' do | |
| 472 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
| 473 | + params[:value] = 100 | |
| 474 | + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" | |
| 475 | + json = JSON.parse(last_response.body) | |
| 476 | + assert_equal 400, last_response.status | |
| 477 | + end | |
| 445 | 478 | |
| 446 | 479 | end | ... | ... |