From 518e6a90fa20bccb50e33739fc25c18c8d114de6 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 16 Mar 2015 23:58:00 -0300 Subject: [PATCH] adding tests for article api --- lib/api/v1/articles.rb | 14 +++++++++----- test/unit/api/articles_test.rb | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 105 insertions(+), 32 deletions(-) diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index 74c1ec6..273ec57 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -3,6 +3,8 @@ module API class Articles < Grape::API before { authenticate! } + ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} + resource :articles do # Collect articles @@ -16,7 +18,7 @@ module API # GET host/api/v1/articles?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317 get do articles = select_filtered_collection_of(environment, 'articles', params) - articles = articles.display_filter(current_user.person, nil) + articles = articles.display_filter(current_person, nil) present articles, :with => Entities::Article end @@ -29,7 +31,7 @@ module API get ':id/children' do article = find_article(environment.articles, params[:id]) articles = select_filtered_collection_of(article, 'children', params) - articles = articles.display_filter(current_user.person, nil) + articles = articles.display_filter(current_person, nil) present articles, :with => Entities::Article end @@ -46,11 +48,11 @@ module API get do community = environment.communities.find(params[:community_id]) articles = select_filtered_collection_of(community, 'articles', params) - articles = articles.display_filter(current_user.person, community) + articles = articles.display_filter(current_person, community) present articles, :with => Entities::Article end - get '/:id' do + get ':id' do community = environment.communities.find(params[:community_id]) article = find_article(community.articles, params[:id]) present article, :with => Entities::Article @@ -60,9 +62,11 @@ module API # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body post do community = environment.communities.find(params[:community_id]) - return forbidden! unless current_user.person.can_post_content?(community) + return forbidden! unless current_person.can_post_content?(community) 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 diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 1b355f2..8b93440 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -39,33 +39,6 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal 403, last_response.status end - should 'return article by community' do - community = fast_create(Community) - article = fast_create(Article, :profile_id => community.id, :name => "Some thing") - get "/api/v1/communities/#{community.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 community if user has no permission to view it' do - community = fast_create(Community) - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) - assert !article.published? - - get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" - assert_equal 403, last_response.status - end - - should 'not list forbidden article when listing articles by community' do - community = fast_create(Community) - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) - assert !article.published? - - get "/api/v1/communities/#{community.id}/articles?#{params.to_query}" - json = JSON.parse(last_response.body) - assert_not_includes json['articles'].map {|a| a['id']}, article.id - end - should 'list article children' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") @@ -109,6 +82,33 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_includes json['articles'].map {|a| a['id']}, child.id end + should 'return article by community' do + community = fast_create(Community) + article = fast_create(Article, :profile_id => community.id, :name => "Some thing") + get "/api/v1/communities/#{community.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 community if user has no permission to view it' do + community = fast_create(Community) + article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) + assert !article.published? + + get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" + assert_equal 403, last_response.status + end + + should 'not list forbidden article when listing articles by community' do + community = fast_create(Community) + article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) + assert !article.published? + + get "/api/v1/communities/#{community.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 community' do community = fast_create(Community) give_permission(user.person, 'post_content', community) @@ -137,4 +137,73 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal article.id, json["article"]["parent"]["id"] end + should 'create article with content type passed as parameter' do + community = fast_create(Community) + community.add_member(user.person) + + Article.delete_all + params[:article] = {:name => "Title"} + params[:content_type] = 'TextArticle' + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_kind_of TextArticle, Article.last + end + + should 'create article of TinyMceArticle type if no content type is passed as parameter' do + community = fast_create(Community) + community.add_member(user.person) + + params[:article] = {:name => "Title"} + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_kind_of TinyMceArticle, Article.last + end + + should 'not create article with invalid article content type' do + community = fast_create(Community) + community.add_member(user.person) + + params[:article] = {:name => "Title"} + params[:content_type] = 'Person' + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal 403, last_response.status + end + + should 'create article defining the correct profile' do + community = fast_create(Community) + community.add_member(user.person) + + params[:article] = {:name => "Title"} + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal community, Article.last.profile + end + + should 'create article defining the created_by' do + community = fast_create(Community) + community.add_member(user.person) + + params[:article] = {:name => "Title"} + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + + assert_equal user.person, Article.last.created_by + end + + should 'create article defining the last_changed_by' do + community = fast_create(Community) + community.add_member(user.person) + + params[:article] = {:name => "Title"} + post "/api/v1/communities/#{community.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