Commit 1ebe69246be928d7943e3320085aa81415852963

Authored by Joenio Costa
2 parents fa90a41d 36b4847b

Merge branch 'archived_articles_api' into 'master'

Archived and Followed articles API improvements

- Adds archived to article API entity;
  - Adds tests to archived and followed articles;
  - Adds archived validation to vote;
  - Limits article children;

  Signed-off-by: Ábner Silva de Oliveira <abner.oliveira@serpro.gov.br>
  Signed-off-by: Michel Felipe de Oliveira Ferreira <michel.ferreira@serpro.gov.br>
  Signed-off-by: Victor Costa <vfcosta@gmail.com>

See merge request !855
lib/noosfero/api/entities.rb
... ... @@ -188,6 +188,7 @@ module Noosfero
188 188 expose :followers_count
189 189 expose :votes_count
190 190 expose :comments_count
  191 + expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
191 192 expose :type
192 193 expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])}
193 194 end
... ... @@ -195,7 +196,9 @@ module Noosfero
195 196 class Article < ArticleBase
196 197 root 'articles', 'article'
197 198 expose :parent, :using => ArticleBase
198   - expose :children, :using => ArticleBase
  199 + expose :children, :using => ArticleBase do |article, options|
  200 + article.children.limit(Noosfero::API::V1::Articles::MAX_PER_PAGE)
  201 + end
199 202 end
200 203  
201 204 class User < Entity
... ...
lib/noosfero/api/helpers.rb
... ... @@ -5,7 +5,7 @@ require_relative &#39;../../find_by_contents&#39;
5 5 module API
6 6 module APIHelpers
7 7 PRIVATE_TOKEN_PARAM = :private_token
8   - DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier]
  8 + DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived]
9 9  
10 10 include SanitizeParams
11 11 include Noosfero::Plugin::HotSpot
... ...
lib/noosfero/vote_ext.rb
... ... @@ -8,4 +8,15 @@ class Vote
8 8 voter.blank?
9 9 end
10 10  
  11 + validate :verify_target_archived
  12 +
  13 + def verify_target_archived
  14 + if voteable.kind_of?(Article) || voteable.kind_of?(Comment)
  15 + if voteable.archived?
  16 + errors.add(:base, _("The target is achived and can't accept votes"))
  17 + false
  18 + end
  19 + end
  20 + end
  21 +
11 22 end
... ...
test/api/articles_test.rb
... ... @@ -195,6 +195,32 @@ class ArticlesTest &lt; ActiveSupport::TestCase
195 195 assert_equal 400, last_response.status
196 196 end
197 197  
  198 + should 'not perform a vote in a archived article' do
  199 + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing", :archived => true)
  200 + @params[:value] = 1
  201 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
  202 + puts JSON.parse(last_response.body)
  203 + assert_equal 400, last_response.status
  204 + end
  205 +
  206 + should 'not update hit attribute of a specific child if a article is archived' do
  207 + folder = fast_create(Folder, :profile_id => user.person.id, :archived => true)
  208 + article = fast_create(Article, :parent_id => folder.id, :profile_id => user.person.id)
  209 + get "/api/v1/articles/#{folder.id}/children/#{article.id}?#{params.to_query}"
  210 + json = JSON.parse(last_response.body)
  211 + assert_equal 0, json['article']['hits']
  212 + end
  213 +
  214 + should 'find archived articles' do
  215 + article1 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  216 + article2 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true)
  217 + params[:archived] = true
  218 + get "/api/v1/articles/?#{params.to_query}"
  219 + json = JSON.parse(last_response.body)
  220 + assert_not_includes json["articles"].map { |a| a["id"] }, article1.id
  221 + assert_includes json["articles"].map { |a| a["id"] }, article2.id
  222 + end
  223 +
198 224 should "update body of article created by me" do
199 225 new_value = "Another body"
200 226 params[:article] = {:body => new_value}
... ... @@ -653,7 +679,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
653 679 assert_equal json['articles'].count, 2
654 680 end
655 681  
656   - ARTICLE_ATTRIBUTES = %w(votes_count comments_count)
  682 + ARTICLE_ATTRIBUTES = %w(followers_count votes_count comments_count)
657 683  
658 684 ARTICLE_ATTRIBUTES.map do |attribute|
659 685  
... ...
test/api/comments_test.rb
... ... @@ -66,6 +66,15 @@ class CommentsTest &lt; ActiveSupport::TestCase
66 66 assert_equal body, json['comment']['body']
67 67 end
68 68  
  69 + should 'not comment an archived article' do
  70 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true)
  71 + body = 'My comment'
  72 + params.merge!({:body => body})
  73 +
  74 + post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
  75 + assert_equal 400, last_response.status
  76 + end
  77 +
69 78 should 'comment creation define the source' do
70 79 amount = Comment.count
71 80 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
... ...
test/api/helpers_test.rb
... ... @@ -163,6 +163,10 @@ class APIHelpersTest &lt; ActiveSupport::TestCase
163 163 assert_nil make_conditions_with_parameter[:type]
164 164 end
165 165  
  166 + should 'make_conditions_with_parameter return archived parameter if archived was defined' do
  167 + assert_not_nil make_conditions_with_parameter('archived' => true)[:archived]
  168 + end
  169 +
166 170 #test_should_make_order_with_parameters_return_order_if attribute_is_found_at_object_association
167 171 should 'make_order_with_parameters return order if attribute is found at object association' do
168 172 environment = Environment.new
... ...
test/api/people_test.rb
... ... @@ -240,7 +240,7 @@ class PeopleTest &lt; ActiveSupport::TestCase
240 240 assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog']
241 241 end
242 242  
243   - PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count)
  243 + PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count)
244 244  
245 245 PERSON_ATTRIBUTES.map do |attribute|
246 246 define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_does_not_contain_the_attribute" do
... ...
test/api/search_test.rb
... ... @@ -147,4 +147,13 @@ class SearchTest &lt; ActiveSupport::TestCase
147 147 assert_includes ids, json['articles'].last["id"]
148 148 end
149 149  
  150 + should 'list only articles that was archived' do
  151 + article1 = fast_create(Article, :profile_id => person.id)
  152 + article2 = fast_create(Article, :profile_id => person.id, archived: true)
  153 +
  154 + get "/api/v1/search/article?archived=true"
  155 + json = JSON.parse(last_response.body)
  156 + assert_equal [article2.id], json['articles'].map {|a| a['id']}
  157 + end
  158 +
150 159 end
... ...