Commit df9c49abb4ff0967b03867acc140f804257d72c1

Authored by Victor Costa
Committed by Marcos Pereira
1 parent d90595ac

api: use post_article helper when create article in children endpoint

lib/noosfero/api/v1/articles.rb
@@ -226,27 +226,10 @@ module Noosfero @@ -226,27 +226,10 @@ module Noosfero
226 named 'ArticleAddChild' 226 named 'ArticleAddChild'
227 end 227 end
228 post ':id/children' do 228 post ':id/children' do
229 - authenticate!  
230 parent_article = environment.articles.find(params[:id]) 229 parent_article = environment.articles.find(params[:id])
231 - return forbidden! unless parent_article.allow_create?(current_person)  
232 -  
233 - klass_type = params[:content_type] || params[:article].delete(:type) || 'TinyMceArticle'  
234 - #FIXME see how to check the article types  
235 - #return forbidden! unless ARTICLE_TYPES.include?(klass_type)  
236 -  
237 - article = klass_type.constantize.new(params[:article])  
238 - article.parent = parent_article  
239 - article.last_changed_by = current_person  
240 - article.created_by= current_person  
241 - article.author= current_person  
242 - article.profile = parent_article.profile  
243 -  
244 - if !article.save  
245 - render_api_errors!(article.errors.full_messages)  
246 - end  
247 - present_partial article, :with => Entities::Article 230 + params[:article][:parent_id] = parent_article.id
  231 + post_article(parent_article.profile, params)
248 end 232 end
249 -  
250 end 233 end
251 234
252 resource :profiles do 235 resource :profiles do
test/api/articles_test.rb
@@ -564,6 +564,23 @@ class ArticlesTest < ActiveSupport::TestCase @@ -564,6 +564,23 @@ class ArticlesTest < ActiveSupport::TestCase
564 assert_equal ['title'], json['articles'].first.keys 564 assert_equal ['title'], json['articles'].first.keys
565 end 565 end
566 566
  567 + should "create article child" do
  568 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  569 + params[:article] = {:name => "Title"}
  570 + post "/api/v1/articles/#{article.id}/children?#{params.to_query}"
  571 + json = JSON.parse(last_response.body)
  572 + assert_equal article.id, json["article"]["parent"]["id"]
  573 + end
  574 +
  575 + should "do not create article child if user has no permission to post content" do
  576 + profile = fast_create(Profile, :environment_id => environment.id)
  577 + article = fast_create(Article, :profile_id => profile.id, :name => "Some thing")
  578 + give_permission(user.person, 'invite_members', profile)
  579 + params[:article] = {:name => "Title"}
  580 + post "/api/v1/articles/#{article.id}/children?#{params.to_query}"
  581 + assert_equal 403, last_response.status
  582 + end
  583 +
567 should 'suggest article children' do 584 should 'suggest article children' do
568 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 585 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
569 params[:target_id] = user.person.id 586 params[:target_id] = user.person.id