From f56d5e71f8d24d0278ae8bcf6f2d04682f5c5550 Mon Sep 17 00:00:00 2001 From: Michel Felipe de Oliveira Ferreira Date: Wed, 18 Nov 2015 14:51:40 -0300 Subject: [PATCH] Changes in endpoints to verify archived article validation --- lib/noosfero/api/entities.rb | 1 + lib/noosfero/api/v1/articles.rb | 13 +++++++++---- lib/noosfero/api/v1/comments.rb | 8 +++++++- test/unit/api/articles_test.rb | 30 ++++++++++++++++++++++++------ test/unit/api/comments_test.rb | 9 +++++++++ 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index d825e66..17a255c 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -120,6 +120,7 @@ module Noosfero expose :followers_count expose :votes_count expose :comments_count + expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"} end class Article < ArticleBase diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index c70575f..5639389 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -131,9 +131,8 @@ module Noosfero failure [[403, 'Forbidden']] named 'ArticleFollowers' end - - #FIXME refactor this method get 'voted_by_me' do + #FIXME refactor this method present_articles_paginated(current_person.votes.where(:voteable_type => 'Article').collect(&:voteable)) end @@ -150,8 +149,14 @@ 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]) - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value) - {:vote => vote.save} + + 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 end desc 'Return the children of a article identified by id' do diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index edbcba8..d2a2ae6 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -31,7 +31,13 @@ module Noosfero post ":id/comments" do article = find_article(environment.articles, params[:id]) options = params.select { |key,v| !['id','private_token'].include?(key) }.merge(:author => current_person, :source => article) - present Comment.create(options), :with => Entities::Comment + begin + comment = Comment.create(options) + comment.save! + rescue ActiveRecord::RecordInvalid => e + render_api_error!(e.message, 400) + end + present comment, :with => Entities::Comment end end diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 3235f38..0b18af8 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -138,6 +138,16 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal true, json['vote'] 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}" + json = JSON.parse(last_response.body) + + assert_equal 400, last_response.status + end + expose_attributes = %w(id body abstract created_at title author profile categories image votes_for votes_against setting position hits start_date end_date tag_list parent children children_count) expose_attributes.each do |attr| @@ -151,7 +161,7 @@ class ArticlesTest < ActiveSupport::TestCase should "update body of article created by me" do new_value = "Another body" - params[:article] = {:body => new_value} + params[:article] = {:body => new_value} article = fast_create(Article, :profile_id => person.id) post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) @@ -160,7 +170,7 @@ class ArticlesTest < ActiveSupport::TestCase should "update title of article created by me" do new_value = "Another name" - params[:article] = {:name => new_value} + params[:article] = {:name => new_value} article = fast_create(Article, :profile_id => person.id) post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) @@ -170,7 +180,7 @@ class ArticlesTest < ActiveSupport::TestCase should 'not update article of another user' do another_person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => another_person.id) - params[:article] = {:title => 'Some title'} + params[:article] = {:title => 'Some title'} post "/api/v1/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end @@ -178,7 +188,7 @@ class ArticlesTest < ActiveSupport::TestCase should 'not update article without permission in community' do community = fast_create(Community, :environment_id => environment.id) article = fast_create(Article, :profile_id => community.id) - params[:article] = {:name => 'New title'} + params[:article] = {:name => 'New title'} post "/api/v1/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end @@ -189,11 +199,11 @@ class ArticlesTest < ActiveSupport::TestCase give_permission(person, 'post_content', community) article = fast_create(Article, :profile_id => community.id) new_value = "Another body" - params[:article] = {:body => new_value} + params[:article] = {:body => new_value} post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal new_value, json["article"]["body"] - end + end should 'list articles with pagination' do Article.destroy_all @@ -520,6 +530,14 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal 1, json['article']['hits'] 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 'list all events of a community in a given category' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') diff --git a/test/unit/api/comments_test.rb b/test/unit/api/comments_test.rb index 3f1bfbe..bf7304c 100644 --- a/test/unit/api/comments_test.rb +++ b/test/unit/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") -- libgit2 0.21.2