From 4add51790e66248f9ba52e7895165bb2bb70350e Mon Sep 17 00:00:00 2001 From: Michel Felipe de Oliveira Ferreira Date: Fri, 28 Aug 2015 17:58:43 -0300 Subject: [PATCH] New articles endpoint to allow get a article by profile and path --- lib/noosfero/api/v1/articles.rb | 17 ++++++++++++++--- test/unit/api/articles_test.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/api/test_helper.rb | 5 +++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 7bf9f7b..5ef335f 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -26,10 +26,10 @@ module Noosfero end desc "Return the article id" - get ':id' do + get ':id', requirements: {id: /[0-9]+/} do present_article(environment) end - + post ':id/report_abuse' do article = find_article(environment.articles, params[:id]) profile = article.profile @@ -164,7 +164,18 @@ module Noosfero resource :articles do get do profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) - present_articles(profile) + + if params[:path].present? + article = profile.articles.find_by_path(params[:path]) + if !article || !article.display_to?(current_person) + article = forbidden! + end + + present article, :with => Entities::Article, :fields => params[:fields] + else + + present_articles(profile) + end end get ':id' do diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 9535494..ad68824 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -91,6 +91,41 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_includes json['articles'].map {|a| a['id']}, child.id end + should 'follow a article identified by id' do + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") + post "/api/v1/articles/#{article.id}/follow?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_not_equal 401, last_response.status + assert_equal true, json['success'] + end + + should 'return the followers of a article identified by id' do + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") + + article_follower = ArticleFollower.new + article_follower.article = article + article_follower.person = @person + article_follower.save! + + get "/api/v1/articles/#{article.id}/followers?" + json = JSON.parse(last_response.body) + + assert_equal 200, last_response.status + assert_equal 1, json['total_followers'] + end + + should 'perform a vote in a article identified by id' do + article = fast_create(Article, :profile_id => @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_not_equal 401, last_response.status + assert_equal true, json['vote'] + end + ############################# # Profile Articles # ############################# @@ -123,6 +158,29 @@ class ArticlesTest < ActiveSupport::TestCase json = JSON.parse(last_response.body) assert_not_includes json['articles'].map {|a| a['id']}, article.id end + + should "return article by #{kind} and path" do + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) + parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") + article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article) + + params[:path] = parent_article.slug+'/'+article.slug + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal article.id, json["article"]["id"] + end + + should "not return article by #{kind} and path if user has no permission to view it" do + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) + parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") + article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article, :published => false) + + assert !article.published? + + params[:path] = parent_article.slug+'/'+article.slug + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" + assert_equal 403, last_response.status + end end ############################# diff --git a/test/unit/api/test_helper.rb b/test/unit/api/test_helper.rb index 4a384db..c0bc921 100644 --- a/test/unit/api/test_helper.rb +++ b/test/unit/api/test_helper.rb @@ -17,6 +17,11 @@ class ActiveSupport::TestCase post "/api/v1/login?login=testapi&password=testapi" json = JSON.parse(last_response.body) @private_token = json["private_token"] + unless @private_token + @user.generate_private_token! + @private_token = @user.private_token + end + @params = {:private_token => @private_token} end attr_accessor :private_token, :user, :person, :params, :environment -- libgit2 0.21.2