From 39e535a036c465c6480ceea25d2a05dcda92a75c Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Tue, 17 Mar 2015 00:23:49 -0300 Subject: [PATCH] adding people articles endpoint --- lib/api/v1/articles.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ test/unit/api/articles_test.rb | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 0 deletions(-) diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index 273ec57..5e0948b 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -83,6 +83,48 @@ module API end + resource :people do + segment '/:person_id' do + resource :articles do + get do + person = environment.people.find(params[:person_id]) + articles = select_filtered_collection_of(person, 'articles', params) + articles = articles.display_filter(current_person, person) + present articles, :with => Entities::Article + end + + get ':id' do + person = environment.people.find(params[:person_id]) + article = find_article(person.articles, params[:id]) + present article, :with => Entities::Article + end + + # Example Request: + # POST api/v1/communites/:person_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body + post do + person = environment.people.find(params[:person_id]) + return forbidden! unless current_person.can_post_content?(person) + + klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] + return forbidden! unless ARTICLE_TYPES.include?(klass_type) + + article = klass_type.constantize.new(params[:article]) + article.last_changed_by = current_person + article.created_by= current_person + article.profile = person + + if !article.save + render_api_errors!(article.errors.full_messages) + end + present article, :with => Entities::Article + end + + end + end + + end + + end end end diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 8b93440..a406a41 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -82,6 +82,10 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_includes json['articles'].map {|a| a['id']}, child.id end + ############################# + # Community Articles # + ############################# + should 'return article by community' do community = fast_create(Community) article = fast_create(Article, :profile_id => community.id, :name => "Some thing") @@ -206,4 +210,109 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal user.person, Article.last.last_changed_by end + ############################# + # Person Articles # + ############################# + + should 'return article by person' do + person = fast_create(Person) + article = fast_create(Article, :profile_id => person.id, :name => "Some thing") + get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal article.id, json["article"]["id"] + end + + should 'not return article by person if user has no permission to view it' do + person = fast_create(Person) + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + assert !article.published? + + get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}" + assert_equal 403, last_response.status + end + + should 'not list forbidden article when listing articles by person' do + person = fast_create(Person) + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + assert !article.published? + get "/api/v1/people/#{person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_includes json['articles'].map {|a| a['id']}, article.id + end + + should 'create article in a person' do + params[:article] = {:name => "Title"} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal "Title", json["article"]["title"] + end + + should 'person do not create article if user has no permission to post content' do + person = fast_create(Person) + params[:article] = {:name => "Title"} + post "/api/v1/people/#{person.id}/articles?#{params.to_query}" + assert_equal 403, last_response.status + end + + should 'person create article with parent' do + article = fast_create(Article) + + params[:article] = {:name => "Title", :parent_id => article.id} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal article.id, json["article"]["parent"]["id"] + end + + should 'person create article with content type passed as parameter' do + Article.delete_all + params[:article] = {:name => "Title"} + params[:content_type] = 'TextArticle' + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_kind_of TextArticle, Article.last + end + + should 'person create article of TinyMceArticle type if no content type is passed as parameter' do + params[:article] = {:name => "Title"} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_kind_of TinyMceArticle, Article.last + end + + should 'person not create article with invalid article content type' do + params[:article] = {:name => "Title"} + params[:content_type] = 'Person' + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal 403, last_response.status + end + + should 'person create article defining the correct profile' do + params[:article] = {:name => "Title"} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal user.person, Article.last.profile + end + + should 'person create article defining the created_by' do + params[:article] = {:name => "Title"} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal user.person, Article.last.created_by + end + + should 'person create article defining the last_changed_by' do + params[:article] = {:name => "Title"} + post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal user.person, Article.last.last_changed_by + end + + end -- libgit2 0.21.2