diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index a71d805..9652d22 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -174,13 +174,16 @@ module Noosfero expose :followers_count expose :votes_count expose :comments_count + expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"} expose :type end class Article < ArticleBase root 'articles', 'article' expose :parent, :using => ArticleBase - expose :children, :using => ArticleBase + expose :children, :using => ArticleBase do |article, options| + article.children.limit(Noosfero::API::V1::Articles::MAX_PER_PAGE) + end end class CommentBase < Entity diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index dcdae61..5c14292 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -5,7 +5,7 @@ require_relative '../../find_by_contents' module API module APIHelpers PRIVATE_TOKEN_PARAM = :private_token - DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier] + DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived] include SanitizeParams include Noosfero::Plugin::HotSpot diff --git a/lib/noosfero/vote_ext.rb b/lib/noosfero/vote_ext.rb index 4d1e601..b39bd12 100644 --- a/lib/noosfero/vote_ext.rb +++ b/lib/noosfero/vote_ext.rb @@ -8,4 +8,15 @@ class Vote voter.blank? end + validate :verify_target_archived + + def verify_target_archived + if voteable.kind_of?(Article) || voteable.kind_of?(Comment) + if voteable.archived? + errors.add(:base, _("The target is achived and can't accept votes")) + false + end + end + end + end diff --git a/test/api/articles_test.rb b/test/api/articles_test.rb index dbe6c3d..b8a3124 100644 --- a/test/api/articles_test.rb +++ b/test/api/articles_test.rb @@ -195,6 +195,32 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal 400, last_response.status end + should 'not perform a vote in a archived article' do + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing", :archived => true) + @params[:value] = 1 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + puts JSON.parse(last_response.body) + assert_equal 400, last_response.status + end + + should 'not update hit attribute of a specific child if a article is archived' do + folder = fast_create(Folder, :profile_id => user.person.id, :archived => true) + article = fast_create(Article, :parent_id => folder.id, :profile_id => user.person.id) + get "/api/v1/articles/#{folder.id}/children/#{article.id}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 0, json['article']['hits'] + end + + should 'find archived articles' do + article1 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + article2 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true) + params[:archived] = true + get "/api/v1/articles/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_includes json["articles"].map { |a| a["id"] }, article1.id + assert_includes json["articles"].map { |a| a["id"] }, article2.id + end + should "update body of article created by me" do new_value = "Another body" params[:article] = {:body => new_value} @@ -653,7 +679,7 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal json['articles'].count, 2 end - ARTICLE_ATTRIBUTES = %w(votes_count comments_count) + ARTICLE_ATTRIBUTES = %w(followers_count votes_count comments_count) ARTICLE_ATTRIBUTES.map do |attribute| diff --git a/test/api/comments_test.rb b/test/api/comments_test.rb index b2aa960..5e0299f 100644 --- a/test/api/comments_test.rb +++ b/test/api/comments_test.rb @@ -66,6 +66,15 @@ class CommentsTest < ActiveSupport::TestCase assert_equal body, json['comment']['body'] end + should 'not comment an archived article' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true) + body = 'My comment' + params.merge!({:body => body}) + + post "/api/v1/articles/#{article.id}/comments?#{params.to_query}" + assert_equal 400, last_response.status + end + should 'comment creation define the source' do amount = Comment.count article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") diff --git a/test/api/helpers_test.rb b/test/api/helpers_test.rb index 9a877db..cbc820d 100644 --- a/test/api/helpers_test.rb +++ b/test/api/helpers_test.rb @@ -163,6 +163,10 @@ class APIHelpersTest < ActiveSupport::TestCase assert_nil make_conditions_with_parameter[:type] end + should 'make_conditions_with_parameter return archived parameter if archived was defined' do + assert_not_nil make_conditions_with_parameter('archived' => true)[:archived] + end + #test_should_make_order_with_parameters_return_order_if attribute_is_found_at_object_association should 'make_order_with_parameters return order if attribute is found at object association' do environment = Environment.new diff --git a/test/api/people_test.rb b/test/api/people_test.rb index a9f3951..f7eec5a 100644 --- a/test/api/people_test.rb +++ b/test/api/people_test.rb @@ -240,7 +240,7 @@ class PeopleTest < ActiveSupport::TestCase assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog'] end - PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count) + PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count) PERSON_ATTRIBUTES.map do |attribute| define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_does_not_contain_the_attribute" do diff --git a/test/api/search_test.rb b/test/api/search_test.rb index 1f90a47..7102674 100644 --- a/test/api/search_test.rb +++ b/test/api/search_test.rb @@ -147,4 +147,13 @@ class SearchTest < ActiveSupport::TestCase assert_includes ids, json['articles'].last["id"] end + should 'list only articles that was archived' do + article1 = fast_create(Article, :profile_id => person.id) + article2 = fast_create(Article, :profile_id => person.id, archived: true) + + get "/api/v1/search/article?archived=true" + json = JSON.parse(last_response.body) + assert_equal [article2.id], json['articles'].map {|a| a['id']} + end + end -- libgit2 0.21.2