Commit 516f9d889f1a7cda21e7741cfb15bd478520fb3e
Exists in
staging
and in
31 other branches
Merge branch 'create-article-api' into 'master'
API: changes in article creation 1. Accept article type attribute 1. Refactor children endpoint See merge request !878
Showing
3 changed files
with
32 additions
and
20 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -108,7 +108,7 @@ require_relative '../../find_by_contents' |
108 | 108 | def post_article(asset, params) |
109 | 109 | return forbidden! unless current_person.can_post_content?(asset) |
110 | 110 | |
111 | - klass_type= params[:content_type].nil? ? TinyMceArticle.name : params[:content_type] | |
111 | + klass_type = params[:content_type] || params[:article].delete(:type) || TinyMceArticle.name | |
112 | 112 | return forbidden! unless ARTICLE_TYPES.include?(klass_type) |
113 | 113 | |
114 | 114 | article = klass_type.constantize.new(params[:article]) | ... | ... |
lib/noosfero/api/v1/articles.rb
... | ... | @@ -226,27 +226,10 @@ module Noosfero |
226 | 226 | named 'ArticleAddChild' |
227 | 227 | end |
228 | 228 | post ':id/children' do |
229 | - authenticate! | |
230 | 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].nil? ? 'TinyMceArticle' : params[:content_type] | |
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 | 232 | end |
249 | - | |
250 | 233 | end |
251 | 234 | |
252 | 235 | resource :profiles do | ... | ... |
test/api/articles_test.rb
... | ... | @@ -408,6 +408,18 @@ class ArticlesTest < ActiveSupport::TestCase |
408 | 408 | assert_kind_of TextArticle, Article.last |
409 | 409 | end |
410 | 410 | |
411 | + should "#{kind} create article with type passed as parameter" do | |
412 | + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) | |
413 | + Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) | |
414 | + | |
415 | + Article.delete_all | |
416 | + params[:article] = {:name => "Title", :type => 'TextArticle'} | |
417 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
418 | + json = JSON.parse(last_response.body) | |
419 | + | |
420 | + assert_kind_of TextArticle, Article.last | |
421 | + end | |
422 | + | |
411 | 423 | should "#{kind}: create article of TinyMceArticle type if no content type is passed as parameter" do |
412 | 424 | profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) |
413 | 425 | Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) |
... | ... | @@ -552,6 +564,23 @@ class ArticlesTest < ActiveSupport::TestCase |
552 | 564 | assert_equal ['title'], json['articles'].first.keys |
553 | 565 | end |
554 | 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 | + | |
555 | 584 | should 'suggest article children' do |
556 | 585 | article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") |
557 | 586 | params[:target_id] = user.person.id | ... | ... |