Commit 4add51790e66248f9ba52e7895165bb2bb70350e

Authored by Michel Felipe
1 parent a3678363

New articles endpoint to allow get a article by profile and path

lib/noosfero/api/v1/articles.rb
... ... @@ -26,10 +26,10 @@ module Noosfero
26 26 end
27 27  
28 28 desc "Return the article id"
29   - get ':id' do
  29 + get ':id', requirements: {id: /[0-9]+/} do
30 30 present_article(environment)
31 31 end
32   -
  32 +
33 33 post ':id/report_abuse' do
34 34 article = find_article(environment.articles, params[:id])
35 35 profile = article.profile
... ... @@ -164,7 +164,18 @@ module Noosfero
164 164 resource :articles do
165 165 get do
166 166 profile = environment.send(kind.pluralize).find(params["#{kind}_id"])
167   - present_articles(profile)
  167 +
  168 + if params[:path].present?
  169 + article = profile.articles.find_by_path(params[:path])
  170 + if !article || !article.display_to?(current_person)
  171 + article = forbidden!
  172 + end
  173 +
  174 + present article, :with => Entities::Article, :fields => params[:fields]
  175 + else
  176 +
  177 + present_articles(profile)
  178 + end
168 179 end
169 180  
170 181 get ':id' do
... ...
test/unit/api/articles_test.rb
... ... @@ -91,6 +91,41 @@ class ArticlesTest < ActiveSupport::TestCase
91 91 assert_not_includes json['articles'].map {|a| a['id']}, child.id
92 92 end
93 93  
  94 + should 'follow a article identified by id' do
  95 + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing")
  96 + post "/api/v1/articles/#{article.id}/follow?#{params.to_query}"
  97 + json = JSON.parse(last_response.body)
  98 +
  99 + assert_not_equal 401, last_response.status
  100 + assert_equal true, json['success']
  101 + end
  102 +
  103 + should 'return the followers of a article identified by id' do
  104 + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing")
  105 +
  106 + article_follower = ArticleFollower.new
  107 + article_follower.article = article
  108 + article_follower.person = @person
  109 + article_follower.save!
  110 +
  111 + get "/api/v1/articles/#{article.id}/followers?"
  112 + json = JSON.parse(last_response.body)
  113 +
  114 + assert_equal 200, last_response.status
  115 + assert_equal 1, json['total_followers']
  116 + end
  117 +
  118 + should 'perform a vote in a article identified by id' do
  119 + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing")
  120 + @params[:value] = 1
  121 +
  122 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
  123 + json = JSON.parse(last_response.body)
  124 +
  125 + assert_not_equal 401, last_response.status
  126 + assert_equal true, json['vote']
  127 + end
  128 +
94 129 #############################
95 130 # Profile Articles #
96 131 #############################
... ... @@ -123,6 +158,29 @@ class ArticlesTest < ActiveSupport::TestCase
123 158 json = JSON.parse(last_response.body)
124 159 assert_not_includes json['articles'].map {|a| a['id']}, article.id
125 160 end
  161 +
  162 + should "return article by #{kind} and path" do
  163 + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
  164 + parent_article = Folder.create!(:profile => profile, :name => "Parent Folder")
  165 + article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article)
  166 +
  167 + params[:path] = parent_article.slug+'/'+article.slug
  168 + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}"
  169 + json = JSON.parse(last_response.body)
  170 + assert_equal article.id, json["article"]["id"]
  171 + end
  172 +
  173 + should "not return article by #{kind} and path if user has no permission to view it" do
  174 + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
  175 + parent_article = Folder.create!(:profile => profile, :name => "Parent Folder")
  176 + article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article, :published => false)
  177 +
  178 + assert !article.published?
  179 +
  180 + params[:path] = parent_article.slug+'/'+article.slug
  181 + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}"
  182 + assert_equal 403, last_response.status
  183 + end
126 184 end
127 185  
128 186 #############################
... ...
test/unit/api/test_helper.rb
... ... @@ -17,6 +17,11 @@ class ActiveSupport::TestCase
17 17 post "/api/v1/login?login=testapi&password=testapi"
18 18 json = JSON.parse(last_response.body)
19 19 @private_token = json["private_token"]
  20 + unless @private_token
  21 + @user.generate_private_token!
  22 + @private_token = @user.private_token
  23 + end
  24 +
20 25 @params = {:private_token => @private_token}
21 26 end
22 27 attr_accessor :private_token, :user, :person, :params, :environment
... ...