Commit 5da7a87ab15ef42c2fe7f62d96c3e47216382f22
1 parent
4ca9a146
Exists in
api_tasks
and in
4 other branches
api: refactoring articles api
Showing
3 changed files
with
166 additions
and
377 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| ... | ... | @@ -45,11 +45,41 @@ module Noosfero |
| 45 | 45 | end |
| 46 | 46 | end |
| 47 | 47 | |
| 48 | + ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} | |
| 49 | + | |
| 48 | 50 | def find_article(articles, id) |
| 49 | 51 | article = articles.find(id) |
| 50 | 52 | article.display_to?(current_user.person) ? article : forbidden! |
| 51 | 53 | end |
| 52 | 54 | |
| 55 | + def post_article(asset, params) | |
| 56 | + return forbidden! unless current_person.can_post_content?(asset) | |
| 57 | + | |
| 58 | + klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | |
| 59 | + return forbidden! unless ARTICLE_TYPES.include?(klass_type) | |
| 60 | + | |
| 61 | + article = klass_type.constantize.new(params[:article]) | |
| 62 | + article.last_changed_by = current_person | |
| 63 | + article.created_by= current_person | |
| 64 | + article.profile = asset | |
| 65 | + | |
| 66 | + if !article.save | |
| 67 | + render_api_errors!(article.errors.full_messages) | |
| 68 | + end | |
| 69 | + present article, :with => Entities::Article, :fields => params[:fields] | |
| 70 | + end | |
| 71 | + | |
| 72 | + def present_article(asset) | |
| 73 | + article = find_article(asset.articles, params[:id]) | |
| 74 | + present article, :with => Entities::Article, :fields => params[:fields] | |
| 75 | + end | |
| 76 | + | |
| 77 | + def present_articles(asset) | |
| 78 | + articles = select_filtered_collection_of(asset, 'articles', params) | |
| 79 | + articles = articles.display_filter(current_person, nil) | |
| 80 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
| 81 | + end | |
| 82 | + | |
| 53 | 83 | def find_task(tasks, id) |
| 54 | 84 | task = tasks.find(id) |
| 55 | 85 | task.display_to?(current_user.person) ? task : forbidden! | ... | ... |
lib/noosfero/api/v1/articles.rb
| ... | ... | @@ -18,15 +18,12 @@ module Noosfero |
| 18 | 18 | # Example Request: |
| 19 | 19 | # GET host/api/v1/articles?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317 |
| 20 | 20 | get do |
| 21 | - articles = select_filtered_collection_of(environment, 'articles', params) | |
| 22 | - articles = articles.display_filter(current_person, nil) | |
| 23 | - present articles, :with => Entities::Article, :fields => params[:fields] | |
| 21 | + present_articles(environment) | |
| 24 | 22 | end |
| 25 | 23 | |
| 26 | 24 | desc "Return the article id" |
| 27 | 25 | get ':id' do |
| 28 | - article = find_article(environment.articles, params[:id]) | |
| 29 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 26 | + present_article(environment) | |
| 30 | 27 | end |
| 31 | 28 | |
| 32 | 29 | get ':id/children' do |
| ... | ... | @@ -93,125 +90,31 @@ module Noosfero |
| 93 | 90 | |
| 94 | 91 | end |
| 95 | 92 | |
| 96 | - resource :communities do | |
| 97 | - segment '/:community_id' do | |
| 98 | - resource :articles do | |
| 99 | - get do | |
| 100 | - community = environment.communities.find(params[:community_id]) | |
| 101 | - articles = select_filtered_collection_of(community, 'articles', params) | |
| 102 | - articles = articles.display_filter(current_person, community) | |
| 103 | - present articles, :with => Entities::Article, :fields => params[:fields] | |
| 104 | - end | |
| 105 | - | |
| 106 | - get ':id' do | |
| 107 | - community = environment.communities.find(params[:community_id]) | |
| 108 | - article = find_article(community.articles, params[:id]) | |
| 109 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 110 | - end | |
| 111 | - | |
| 112 | - # Example Request: | |
| 113 | - # POST api/v1/communites/:community_id/articles?private_token=234298743290432&article[name]=title&article[body]=body | |
| 114 | - post do | |
| 115 | - community = environment.communities.find(params[:community_id]) | |
| 116 | - return forbidden! unless current_person.can_post_content?(community) | |
| 117 | - | |
| 118 | - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | |
| 119 | - return forbidden! unless ARTICLE_TYPES.include?(klass_type) | |
| 120 | - | |
| 121 | - article = klass_type.constantize.new(params[:article]) | |
| 122 | - article.last_changed_by = current_person | |
| 123 | - article.created_by= current_person | |
| 124 | - article.profile = community | |
| 125 | - | |
| 126 | - if !article.save | |
| 127 | - render_api_errors!(article.errors.full_messages) | |
| 93 | + kinds = %w[community person enterprise] | |
| 94 | + kinds.each do |kind| | |
| 95 | + resource kind.pluralize.to_sym do | |
| 96 | + segment "/:#{kind}_id" do | |
| 97 | + resource :articles do | |
| 98 | + get do | |
| 99 | + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) | |
| 100 | + present_articles(profile) | |
| 128 | 101 | end |
| 129 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 130 | - end | |
| 131 | - | |
| 132 | - end | |
| 133 | - end | |
| 134 | - | |
| 135 | - end | |
| 136 | - | |
| 137 | - resource :people do | |
| 138 | - segment '/:person_id' do | |
| 139 | - resource :articles do | |
| 140 | - get do | |
| 141 | - person = environment.people.find(params[:person_id]) | |
| 142 | - articles = select_filtered_collection_of(person, 'articles', params) | |
| 143 | - articles = articles.display_filter(current_person, person) | |
| 144 | - present articles, :with => Entities::Article, :fields => params[:fields] | |
| 145 | - end | |
| 146 | - | |
| 147 | - get ':id' do | |
| 148 | - person = environment.people.find(params[:person_id]) | |
| 149 | - article = find_article(person.articles, params[:id]) | |
| 150 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 151 | - end | |
| 152 | - | |
| 153 | - post do | |
| 154 | - person = environment.people.find(params[:person_id]) | |
| 155 | - return forbidden! unless current_person.can_post_content?(person) | |
| 156 | - | |
| 157 | - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | |
| 158 | - return forbidden! unless ARTICLE_TYPES.include?(klass_type) | |
| 159 | 102 | |
| 160 | - article = klass_type.constantize.new(params[:article]) | |
| 161 | - article.last_changed_by = current_person | |
| 162 | - article.created_by= current_person | |
| 163 | - article.profile = person | |
| 164 | - | |
| 165 | - if !article.save | |
| 166 | - render_api_errors!(article.errors.full_messages) | |
| 103 | + get ':id' do | |
| 104 | + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) | |
| 105 | + present_article(profile) | |
| 167 | 106 | end |
| 168 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 169 | - end | |
| 170 | - | |
| 171 | - end | |
| 172 | - end | |
| 173 | - | |
| 174 | - end | |
| 175 | 107 | |
| 176 | - resource :enterprises do | |
| 177 | - segment '/:enterprise_id' do | |
| 178 | - resource :articles do | |
| 179 | - get do | |
| 180 | - enterprise = environment.enterprises.find(params[:enterprise_id]) | |
| 181 | - articles = select_filtered_collection_of(enterprise, 'articles', params) | |
| 182 | - articles = articles.display_filter(current_person, enterprise) | |
| 183 | - present articles, :with => Entities::Article, :fields => params[:fields] | |
| 184 | - end | |
| 185 | - | |
| 186 | - get ':id' do | |
| 187 | - enterprise = environment.enterprises.find(params[:enterprise_id]) | |
| 188 | - article = find_article(enterprise.articles, params[:id]) | |
| 189 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 190 | - end | |
| 191 | - | |
| 192 | - post do | |
| 193 | - enterprise = environment.enterprises.find(params[:enterprise_id]) | |
| 194 | - return forbidden! unless current_person.can_post_content?(enterprise) | |
| 195 | - | |
| 196 | - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | |
| 197 | - return forbidden! unless ARTICLE_TYPES.include?(klass_type) | |
| 198 | - | |
| 199 | - article = klass_type.constantize.new(params[:article]) | |
| 200 | - article.last_changed_by = current_person | |
| 201 | - article.created_by= current_person | |
| 202 | - article.profile = enterprise | |
| 203 | - | |
| 204 | - if !article.save | |
| 205 | - render_api_errors!(article.errors.full_messages) | |
| 108 | + # Example Request: | |
| 109 | + # POST api/v1/{people,communities,enterprises}/:asset_id/articles?private_token=234298743290432&article[name]=title&article[body]=body | |
| 110 | + post do | |
| 111 | + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) | |
| 112 | + post_article(profile, params) | |
| 206 | 113 | end |
| 207 | - present article, :with => Entities::Article, :fields => params[:fields] | |
| 208 | 114 | end |
| 209 | - | |
| 210 | 115 | end |
| 211 | 116 | end |
| 212 | - | |
| 213 | 117 | end |
| 214 | - | |
| 215 | 118 | end |
| 216 | 119 | end |
| 217 | 120 | end | ... | ... |
test/unit/api/articles_test.rb
| ... | ... | @@ -83,163 +83,147 @@ class ArticlesTest < ActiveSupport::TestCase |
| 83 | 83 | end |
| 84 | 84 | |
| 85 | 85 | ############################# |
| 86 | - # Community Articles # | |
| 86 | + # Profile Articles # | |
| 87 | 87 | ############################# |
| 88 | 88 | |
| 89 | - should 'return article by community' do | |
| 90 | - community = fast_create(Community) | |
| 91 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing") | |
| 92 | - get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
| 93 | - json = JSON.parse(last_response.body) | |
| 94 | - assert_equal article.id, json["article"]["id"] | |
| 95 | - end | |
| 89 | + profile_kinds = %w(community person enterprise) | |
| 90 | + profile_kinds.each do |kind| | |
| 91 | + should "return article by #{kind}" do | |
| 92 | + profile = fast_create(kind.camelcase.constantize) | |
| 93 | + article = fast_create(Article, :profile_id => profile.id, :name => "Some thing") | |
| 94 | + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles/#{article.id}?#{params.to_query}" | |
| 95 | + json = JSON.parse(last_response.body) | |
| 96 | + assert_equal article.id, json["article"]["id"] | |
| 97 | + end | |
| 96 | 98 | |
| 97 | - should 'not return article by community if user has no permission to view it' do | |
| 98 | - community = fast_create(Community) | |
| 99 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
| 100 | - assert !article.published? | |
| 99 | + should "not return article by #{kind} if user has no permission to view it" do | |
| 100 | + profile = fast_create(kind.camelcase.constantize) | |
| 101 | + article = fast_create(Article, :profile_id => profile.id, :name => "Some thing", :published => false) | |
| 102 | + assert !article.published? | |
| 101 | 103 | |
| 102 | - get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
| 103 | - assert_equal 403, last_response.status | |
| 104 | - end | |
| 104 | + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles/#{article.id}?#{params.to_query}" | |
| 105 | + assert_equal 403, last_response.status | |
| 106 | + end | |
| 105 | 107 | |
| 106 | - should 'not list forbidden article when listing articles by community' do | |
| 107 | - community = fast_create(Community) | |
| 108 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
| 109 | - assert !article.published? | |
| 108 | + should "not list forbidden article when listing articles by #{kind}" do | |
| 109 | + profile = fast_create(kind.camelcase.constantize) | |
| 110 | + article = fast_create(Article, :profile_id => profile.id, :name => "Some thing", :published => false) | |
| 111 | + assert !article.published? | |
| 110 | 112 | |
| 111 | - get "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 112 | - json = JSON.parse(last_response.body) | |
| 113 | - assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
| 113 | + get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 114 | + json = JSON.parse(last_response.body) | |
| 115 | + assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
| 116 | + end | |
| 114 | 117 | end |
| 115 | 118 | |
| 116 | - should 'create article in a community' do | |
| 117 | - community = fast_create(Community) | |
| 118 | - give_permission(user.person, 'post_content', community) | |
| 119 | - params[:article] = {:name => "Title"} | |
| 120 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 121 | - json = JSON.parse(last_response.body) | |
| 122 | - assert_equal "Title", json["article"]["title"] | |
| 123 | - end | |
| 119 | + ############################# | |
| 120 | + # Group Profile Articles # | |
| 121 | + ############################# | |
| 124 | 122 | |
| 125 | - should 'do not create article if user has no permission to post content' do | |
| 126 | - community = fast_create(Community) | |
| 127 | - give_permission(user.person, 'invite_members', community) | |
| 128 | - params[:article] = {:name => "Title"} | |
| 129 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 130 | - assert_equal 403, last_response.status | |
| 131 | - end | |
| 123 | + group_kinds = %w(community enterprise) | |
| 124 | + group_kinds.each do |kind| | |
| 125 | + should "#{kind}: create article" do | |
| 126 | + profile = fast_create(kind.camelcase.constantize) | |
| 127 | + give_permission(user.person, 'post_content', profile) | |
| 128 | + params[:article] = {:name => "Title"} | |
| 129 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 130 | + json = JSON.parse(last_response.body) | |
| 131 | + assert_equal "Title", json["article"]["title"] | |
| 132 | + end | |
| 132 | 133 | |
| 133 | - should 'create article with parent' do | |
| 134 | - community = fast_create(Community) | |
| 135 | - community.add_member(user.person) | |
| 136 | - article = fast_create(Article) | |
| 134 | + should "#{kind}: do not create article if user has no permission to post content" do | |
| 135 | + profile = fast_create(kind.camelcase.constantize) | |
| 136 | + give_permission(user.person, 'invite_members', profile) | |
| 137 | + params[:article] = {:name => "Title"} | |
| 138 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 139 | + assert_equal 403, last_response.status | |
| 140 | + end | |
| 137 | 141 | |
| 138 | - params[:article] = {:name => "Title", :parent_id => article.id} | |
| 139 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 140 | - json = JSON.parse(last_response.body) | |
| 141 | - assert_equal article.id, json["article"]["parent"]["id"] | |
| 142 | - end | |
| 142 | + should "#{kind}: create article with parent" do | |
| 143 | + profile = fast_create(kind.camelcase.constantize) | |
| 144 | + profile.add_member(user.person) | |
| 145 | + article = fast_create(Article) | |
| 143 | 146 | |
| 144 | - should 'create article with content type passed as parameter' do | |
| 145 | - community = fast_create(Community) | |
| 146 | - community.add_member(user.person) | |
| 147 | + params[:article] = {:name => "Title", :parent_id => article.id} | |
| 148 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 149 | + json = JSON.parse(last_response.body) | |
| 150 | + assert_equal article.id, json["article"]["parent"]["id"] | |
| 151 | + end | |
| 147 | 152 | |
| 148 | - Article.delete_all | |
| 149 | - params[:article] = {:name => "Title"} | |
| 150 | - params[:content_type] = 'TextArticle' | |
| 151 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 152 | - json = JSON.parse(last_response.body) | |
| 153 | - | |
| 154 | - assert_kind_of TextArticle, Article.last | |
| 155 | - end | |
| 156 | - | |
| 157 | - should 'create article of TinyMceArticle type if no content type is passed as parameter' do | |
| 158 | - community = fast_create(Community) | |
| 159 | - community.add_member(user.person) | |
| 153 | + should "#{kind}: create article with content type passed as parameter" do | |
| 154 | + profile = fast_create(kind.camelcase.constantize) | |
| 155 | + profile.add_member(user.person) | |
| 160 | 156 | |
| 161 | - params[:article] = {:name => "Title"} | |
| 162 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 163 | - json = JSON.parse(last_response.body) | |
| 164 | - | |
| 165 | - assert_kind_of TinyMceArticle, Article.last | |
| 166 | - end | |
| 157 | + Article.delete_all | |
| 158 | + params[:article] = {:name => "Title"} | |
| 159 | + params[:content_type] = 'TextArticle' | |
| 160 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 161 | + json = JSON.parse(last_response.body) | |
| 167 | 162 | |
| 168 | - should 'not create article with invalid article content type' do | |
| 169 | - community = fast_create(Community) | |
| 170 | - community.add_member(user.person) | |
| 163 | + assert_kind_of TextArticle, Article.last | |
| 164 | + end | |
| 171 | 165 | |
| 172 | - params[:article] = {:name => "Title"} | |
| 173 | - params[:content_type] = 'Person' | |
| 174 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 175 | - json = JSON.parse(last_response.body) | |
| 176 | - | |
| 177 | - assert_equal 403, last_response.status | |
| 178 | - end | |
| 166 | + should "#{kind}: create article of TinyMceArticle type if no content type is passed as parameter" do | |
| 167 | + profile = fast_create(kind.camelcase.constantize) | |
| 168 | + profile.add_member(user.person) | |
| 179 | 169 | |
| 180 | - should 'create article defining the correct profile' do | |
| 181 | - community = fast_create(Community) | |
| 182 | - community.add_member(user.person) | |
| 170 | + params[:article] = {:name => "Title"} | |
| 171 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 172 | + json = JSON.parse(last_response.body) | |
| 183 | 173 | |
| 184 | - params[:article] = {:name => "Title"} | |
| 185 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 186 | - json = JSON.parse(last_response.body) | |
| 187 | - | |
| 188 | - assert_equal community, Article.last.profile | |
| 189 | - end | |
| 174 | + assert_kind_of TinyMceArticle, Article.last | |
| 175 | + end | |
| 190 | 176 | |
| 191 | - should 'create article defining the created_by' do | |
| 192 | - community = fast_create(Community) | |
| 193 | - community.add_member(user.person) | |
| 177 | + should "#{kind}: not create article with invalid article content type" do | |
| 178 | + profile = fast_create(kind.camelcase.constantize) | |
| 179 | + profile.add_member(user.person) | |
| 194 | 180 | |
| 195 | - params[:article] = {:name => "Title"} | |
| 196 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 197 | - json = JSON.parse(last_response.body) | |
| 198 | - | |
| 199 | - assert_equal user.person, Article.last.created_by | |
| 200 | - end | |
| 181 | + params[:article] = {:name => "Title"} | |
| 182 | + params[:content_type] = 'Person' | |
| 183 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 184 | + json = JSON.parse(last_response.body) | |
| 201 | 185 | |
| 202 | - should 'create article defining the last_changed_by' do | |
| 203 | - community = fast_create(Community) | |
| 204 | - community.add_member(user.person) | |
| 186 | + assert_equal 403, last_response.status | |
| 187 | + end | |
| 205 | 188 | |
| 206 | - params[:article] = {:name => "Title"} | |
| 207 | - post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
| 208 | - json = JSON.parse(last_response.body) | |
| 209 | - | |
| 210 | - assert_equal user.person, Article.last.last_changed_by | |
| 211 | - end | |
| 189 | + should "#{kind}: create article defining the correct profile" do | |
| 190 | + profile = fast_create(kind.camelcase.constantize) | |
| 191 | + profile.add_member(user.person) | |
| 212 | 192 | |
| 213 | - ############################# | |
| 214 | - # Person Articles # | |
| 215 | - ############################# | |
| 193 | + params[:article] = {:name => "Title"} | |
| 194 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 195 | + json = JSON.parse(last_response.body) | |
| 216 | 196 | |
| 217 | - should 'return article by person' do | |
| 218 | - person = fast_create(Person) | |
| 219 | - article = fast_create(Article, :profile_id => person.id, :name => "Some thing") | |
| 220 | - get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}" | |
| 221 | - json = JSON.parse(last_response.body) | |
| 222 | - assert_equal article.id, json["article"]["id"] | |
| 223 | - end | |
| 197 | + assert_equal profile, Article.last.profile | |
| 198 | + end | |
| 224 | 199 | |
| 225 | - should 'not return article by person if user has no permission to view it' do | |
| 226 | - person = fast_create(Person) | |
| 227 | - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | |
| 228 | - assert !article.published? | |
| 200 | + should "#{kind}: create article defining the created_by" do | |
| 201 | + profile = fast_create(kind.camelcase.constantize) | |
| 202 | + profile.add_member(user.person) | |
| 229 | 203 | |
| 230 | - get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}" | |
| 231 | - assert_equal 403, last_response.status | |
| 232 | - end | |
| 204 | + params[:article] = {:name => "Title"} | |
| 205 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 206 | + json = JSON.parse(last_response.body) | |
| 233 | 207 | |
| 234 | - should 'not list forbidden article when listing articles by person' do | |
| 235 | - person = fast_create(Person) | |
| 236 | - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | |
| 237 | - assert !article.published? | |
| 238 | - get "/api/v1/people/#{person.id}/articles?#{params.to_query}" | |
| 239 | - json = JSON.parse(last_response.body) | |
| 240 | - assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
| 208 | + assert_equal user.person, Article.last.created_by | |
| 209 | + end | |
| 210 | + | |
| 211 | + should "#{kind}: create article defining the last_changed_by" do | |
| 212 | + profile = fast_create(kind.camelcase.constantize) | |
| 213 | + profile.add_member(user.person) | |
| 214 | + | |
| 215 | + params[:article] = {:name => "Title"} | |
| 216 | + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" | |
| 217 | + json = JSON.parse(last_response.body) | |
| 218 | + | |
| 219 | + assert_equal user.person, Article.last.last_changed_by | |
| 220 | + end | |
| 241 | 221 | end |
| 242 | 222 | |
| 223 | + ############################# | |
| 224 | + # Person Articles # | |
| 225 | + ############################# | |
| 226 | + | |
| 243 | 227 | should 'create article in a person' do |
| 244 | 228 | params[:article] = {:name => "Title"} |
| 245 | 229 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| ... | ... | @@ -269,15 +253,15 @@ class ArticlesTest < ActiveSupport::TestCase |
| 269 | 253 | params[:content_type] = 'TextArticle' |
| 270 | 254 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 271 | 255 | json = JSON.parse(last_response.body) |
| 272 | - | |
| 256 | + | |
| 273 | 257 | assert_kind_of TextArticle, Article.last |
| 274 | 258 | end |
| 275 | - | |
| 259 | + | |
| 276 | 260 | should 'person create article of TinyMceArticle type if no content type is passed as parameter' do |
| 277 | 261 | params[:article] = {:name => "Title"} |
| 278 | 262 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 279 | 263 | json = JSON.parse(last_response.body) |
| 280 | - | |
| 264 | + | |
| 281 | 265 | assert_kind_of TinyMceArticle, Article.last |
| 282 | 266 | end |
| 283 | 267 | |
| ... | ... | @@ -286,7 +270,7 @@ class ArticlesTest < ActiveSupport::TestCase |
| 286 | 270 | params[:content_type] = 'Person' |
| 287 | 271 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 288 | 272 | json = JSON.parse(last_response.body) |
| 289 | - | |
| 273 | + | |
| 290 | 274 | assert_equal 403, last_response.status |
| 291 | 275 | end |
| 292 | 276 | |
| ... | ... | @@ -294,7 +278,7 @@ class ArticlesTest < ActiveSupport::TestCase |
| 294 | 278 | params[:article] = {:name => "Title"} |
| 295 | 279 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 296 | 280 | json = JSON.parse(last_response.body) |
| 297 | - | |
| 281 | + | |
| 298 | 282 | assert_equal user.person, Article.last.profile |
| 299 | 283 | end |
| 300 | 284 | |
| ... | ... | @@ -302,7 +286,7 @@ class ArticlesTest < ActiveSupport::TestCase |
| 302 | 286 | params[:article] = {:name => "Title"} |
| 303 | 287 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 304 | 288 | json = JSON.parse(last_response.body) |
| 305 | - | |
| 289 | + | |
| 306 | 290 | assert_equal user.person, Article.last.created_by |
| 307 | 291 | end |
| 308 | 292 | |
| ... | ... | @@ -310,135 +294,7 @@ class ArticlesTest < ActiveSupport::TestCase |
| 310 | 294 | params[:article] = {:name => "Title"} |
| 311 | 295 | post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" |
| 312 | 296 | json = JSON.parse(last_response.body) |
| 313 | - | |
| 314 | - assert_equal user.person, Article.last.last_changed_by | |
| 315 | - end | |
| 316 | - | |
| 317 | - ############################# | |
| 318 | - # Enterprise Articles # | |
| 319 | - ############################# | |
| 320 | 297 | |
| 321 | - should 'return article by enterprise' do | |
| 322 | - enterprise = fast_create(Enterprise) | |
| 323 | - article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing") | |
| 324 | - get "/api/v1/enterprises/#{enterprise.id}/articles/#{article.id}?#{params.to_query}" | |
| 325 | - json = JSON.parse(last_response.body) | |
| 326 | - assert_equal article.id, json["article"]["id"] | |
| 327 | - end | |
| 328 | - | |
| 329 | - should 'not return article by enterprise if user has no permission to view it' do | |
| 330 | - enterprise = fast_create(Enterprise) | |
| 331 | - article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing", :published => false) | |
| 332 | - assert !article.published? | |
| 333 | - | |
| 334 | - get "/api/v1/enterprises/#{enterprise.id}/articles/#{article.id}?#{params.to_query}" | |
| 335 | - assert_equal 403, last_response.status | |
| 336 | - end | |
| 337 | - | |
| 338 | - should 'not list forbidden article when listing articles by enterprise' do | |
| 339 | - enterprise = fast_create(Enterprise) | |
| 340 | - article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing", :published => false) | |
| 341 | - assert !article.published? | |
| 342 | - | |
| 343 | - get "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 344 | - json = JSON.parse(last_response.body) | |
| 345 | - assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
| 346 | - end | |
| 347 | - | |
| 348 | - should 'create article in a enterprise' do | |
| 349 | - enterprise = fast_create(Enterprise) | |
| 350 | - give_permission(user.person, 'post_content', enterprise) | |
| 351 | - params[:article] = {:name => "Title"} | |
| 352 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 353 | - json = JSON.parse(last_response.body) | |
| 354 | - assert_equal "Title", json["article"]["title"] | |
| 355 | - end | |
| 356 | - | |
| 357 | - should 'enterprise: do not create article if user has no permission to post content' do | |
| 358 | - enterprise = fast_create(Enterprise) | |
| 359 | - give_permission(user.person, 'invite_members', enterprise) | |
| 360 | - params[:article] = {:name => "Title"} | |
| 361 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 362 | - assert_equal 403, last_response.status | |
| 363 | - end | |
| 364 | - | |
| 365 | - should 'enterprise: create article with parent' do | |
| 366 | - enterprise = fast_create(Enterprise) | |
| 367 | - enterprise.add_member(user.person) | |
| 368 | - article = fast_create(Article) | |
| 369 | - | |
| 370 | - params[:article] = {:name => "Title", :parent_id => article.id} | |
| 371 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 372 | - json = JSON.parse(last_response.body) | |
| 373 | - assert_equal article.id, json["article"]["parent"]["id"] | |
| 374 | - end | |
| 375 | - | |
| 376 | - should 'enterprise: create article with content type passed as parameter' do | |
| 377 | - enterprise = fast_create(Enterprise) | |
| 378 | - enterprise.add_member(user.person) | |
| 379 | - | |
| 380 | - Article.delete_all | |
| 381 | - params[:article] = {:name => "Title"} | |
| 382 | - params[:content_type] = 'TextArticle' | |
| 383 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 384 | - json = JSON.parse(last_response.body) | |
| 385 | - | |
| 386 | - assert_kind_of TextArticle, Article.last | |
| 387 | - end | |
| 388 | - | |
| 389 | - should 'enterprise: create article of TinyMceArticle type if no content type is passed as parameter' do | |
| 390 | - enterprise = fast_create(Enterprise) | |
| 391 | - enterprise.add_member(user.person) | |
| 392 | - | |
| 393 | - params[:article] = {:name => "Title"} | |
| 394 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 395 | - json = JSON.parse(last_response.body) | |
| 396 | - | |
| 397 | - assert_kind_of TinyMceArticle, Article.last | |
| 398 | - end | |
| 399 | - | |
| 400 | - should 'enterprise: not create article with invalid article content type' do | |
| 401 | - enterprise = fast_create(Enterprise) | |
| 402 | - enterprise.add_member(user.person) | |
| 403 | - | |
| 404 | - params[:article] = {:name => "Title"} | |
| 405 | - params[:content_type] = 'Person' | |
| 406 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 407 | - json = JSON.parse(last_response.body) | |
| 408 | - | |
| 409 | - assert_equal 403, last_response.status | |
| 410 | - end | |
| 411 | - | |
| 412 | - should 'enterprise: create article defining the correct profile' do | |
| 413 | - enterprise = fast_create(Enterprise) | |
| 414 | - enterprise.add_member(user.person) | |
| 415 | - | |
| 416 | - params[:article] = {:name => "Title"} | |
| 417 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 418 | - json = JSON.parse(last_response.body) | |
| 419 | - | |
| 420 | - assert_equal enterprise, Article.last.profile | |
| 421 | - end | |
| 422 | - | |
| 423 | - should 'enterprise: create article defining the created_by' do | |
| 424 | - enterprise = fast_create(Enterprise) | |
| 425 | - enterprise.add_member(user.person) | |
| 426 | - | |
| 427 | - params[:article] = {:name => "Title"} | |
| 428 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 429 | - json = JSON.parse(last_response.body) | |
| 430 | - | |
| 431 | - assert_equal user.person, Article.last.created_by | |
| 432 | - end | |
| 433 | - | |
| 434 | - should 'enterprise: create article defining the last_changed_by' do | |
| 435 | - enterprise = fast_create(Enterprise) | |
| 436 | - enterprise.add_member(user.person) | |
| 437 | - | |
| 438 | - params[:article] = {:name => "Title"} | |
| 439 | - post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}" | |
| 440 | - json = JSON.parse(last_response.body) | |
| 441 | - | |
| 442 | 298 | assert_equal user.person, Article.last.last_changed_by |
| 443 | 299 | end |
| 444 | 300 | ... | ... |