Commit f56d5e71f8d24d0278ae8bcf6f2d04682f5c5550

Authored by Michel Felipe
1 parent 37e45606

Changes in endpoints to verify archived article validation

lib/noosfero/api/entities.rb
@@ -120,6 +120,7 @@ module Noosfero @@ -120,6 +120,7 @@ module Noosfero
120 expose :followers_count 120 expose :followers_count
121 expose :votes_count 121 expose :votes_count
122 expose :comments_count 122 expose :comments_count
  123 + expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
123 end 124 end
124 125
125 class Article < ArticleBase 126 class Article < ArticleBase
lib/noosfero/api/v1/articles.rb
@@ -131,9 +131,8 @@ module Noosfero @@ -131,9 +131,8 @@ module Noosfero
131 failure [[403, 'Forbidden']] 131 failure [[403, 'Forbidden']]
132 named 'ArticleFollowers' 132 named 'ArticleFollowers'
133 end 133 end
134 -  
135 - #FIXME refactor this method  
136 get 'voted_by_me' do 134 get 'voted_by_me' do
  135 + #FIXME refactor this method
137 present_articles_paginated(current_person.votes.where(:voteable_type => 'Article').collect(&:voteable)) 136 present_articles_paginated(current_person.votes.where(:voteable_type => 'Article').collect(&:voteable))
138 end 137 end
139 138
@@ -150,8 +149,14 @@ module Noosfero @@ -150,8 +149,14 @@ module Noosfero
150 # FIXME verify allowed values 149 # FIXME verify allowed values
151 render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value) 150 render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value)
152 article = find_article(environment.articles, params[:id]) 151 article = find_article(environment.articles, params[:id])
153 - vote = Vote.new(:voteable => article, :voter => current_person, :vote => value)  
154 - {:vote => vote.save} 152 +
  153 + begin
  154 + vote = Vote.new(:voteable => article, :voter => current_person, :vote => value)
  155 + saved = vote.save!
  156 + {:vote => saved}
  157 + rescue ActiveRecord::RecordInvalid => e
  158 + render_api_error!(e.message, 400)
  159 + end
155 end 160 end
156 161
157 desc 'Return the children of a article identified by id' do 162 desc 'Return the children of a article identified by id' do
lib/noosfero/api/v1/comments.rb
@@ -31,7 +31,13 @@ module Noosfero @@ -31,7 +31,13 @@ module Noosfero
31 post ":id/comments" do 31 post ":id/comments" do
32 article = find_article(environment.articles, params[:id]) 32 article = find_article(environment.articles, params[:id])
33 options = params.select { |key,v| !['id','private_token'].include?(key) }.merge(:author => current_person, :source => article) 33 options = params.select { |key,v| !['id','private_token'].include?(key) }.merge(:author => current_person, :source => article)
34 - present Comment.create(options), :with => Entities::Comment 34 + begin
  35 + comment = Comment.create(options)
  36 + comment.save!
  37 + rescue ActiveRecord::RecordInvalid => e
  38 + render_api_error!(e.message, 400)
  39 + end
  40 + present comment, :with => Entities::Comment
35 end 41 end
36 end 42 end
37 43
test/unit/api/articles_test.rb
@@ -138,6 +138,16 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -138,6 +138,16 @@ class ArticlesTest &lt; ActiveSupport::TestCase
138 assert_equal true, json['vote'] 138 assert_equal true, json['vote']
139 end 139 end
140 140
  141 + should 'not perform a vote in a archived article' do
  142 + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing", :archived => true)
  143 + @params[:value] = 1
  144 +
  145 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
  146 + json = JSON.parse(last_response.body)
  147 +
  148 + assert_equal 400, last_response.status
  149 + end
  150 +
141 expose_attributes = %w(id body abstract created_at title author profile categories image votes_for votes_against setting position hits start_date end_date tag_list parent children children_count) 151 expose_attributes = %w(id body abstract created_at title author profile categories image votes_for votes_against setting position hits start_date end_date tag_list parent children children_count)
142 152
143 expose_attributes.each do |attr| 153 expose_attributes.each do |attr|
@@ -151,7 +161,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -151,7 +161,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
151 161
152 should "update body of article created by me" do 162 should "update body of article created by me" do
153 new_value = "Another body" 163 new_value = "Another body"
154 - params[:article] = {:body => new_value} 164 + params[:article] = {:body => new_value}
155 article = fast_create(Article, :profile_id => person.id) 165 article = fast_create(Article, :profile_id => person.id)
156 post "/api/v1/articles/#{article.id}?#{params.to_query}" 166 post "/api/v1/articles/#{article.id}?#{params.to_query}"
157 json = JSON.parse(last_response.body) 167 json = JSON.parse(last_response.body)
@@ -160,7 +170,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -160,7 +170,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
160 170
161 should "update title of article created by me" do 171 should "update title of article created by me" do
162 new_value = "Another name" 172 new_value = "Another name"
163 - params[:article] = {:name => new_value} 173 + params[:article] = {:name => new_value}
164 article = fast_create(Article, :profile_id => person.id) 174 article = fast_create(Article, :profile_id => person.id)
165 post "/api/v1/articles/#{article.id}?#{params.to_query}" 175 post "/api/v1/articles/#{article.id}?#{params.to_query}"
166 json = JSON.parse(last_response.body) 176 json = JSON.parse(last_response.body)
@@ -170,7 +180,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -170,7 +180,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
170 should 'not update article of another user' do 180 should 'not update article of another user' do
171 another_person = fast_create(Person, :environment_id => environment.id) 181 another_person = fast_create(Person, :environment_id => environment.id)
172 article = fast_create(Article, :profile_id => another_person.id) 182 article = fast_create(Article, :profile_id => another_person.id)
173 - params[:article] = {:title => 'Some title'} 183 + params[:article] = {:title => 'Some title'}
174 post "/api/v1/articles/#{article.id}?#{params.to_query}" 184 post "/api/v1/articles/#{article.id}?#{params.to_query}"
175 assert_equal 403, last_response.status 185 assert_equal 403, last_response.status
176 end 186 end
@@ -178,7 +188,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -178,7 +188,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
178 should 'not update article without permission in community' do 188 should 'not update article without permission in community' do
179 community = fast_create(Community, :environment_id => environment.id) 189 community = fast_create(Community, :environment_id => environment.id)
180 article = fast_create(Article, :profile_id => community.id) 190 article = fast_create(Article, :profile_id => community.id)
181 - params[:article] = {:name => 'New title'} 191 + params[:article] = {:name => 'New title'}
182 post "/api/v1/articles/#{article.id}?#{params.to_query}" 192 post "/api/v1/articles/#{article.id}?#{params.to_query}"
183 assert_equal 403, last_response.status 193 assert_equal 403, last_response.status
184 end 194 end
@@ -189,11 +199,11 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -189,11 +199,11 @@ class ArticlesTest &lt; ActiveSupport::TestCase
189 give_permission(person, 'post_content', community) 199 give_permission(person, 'post_content', community)
190 article = fast_create(Article, :profile_id => community.id) 200 article = fast_create(Article, :profile_id => community.id)
191 new_value = "Another body" 201 new_value = "Another body"
192 - params[:article] = {:body => new_value} 202 + params[:article] = {:body => new_value}
193 post "/api/v1/articles/#{article.id}?#{params.to_query}" 203 post "/api/v1/articles/#{article.id}?#{params.to_query}"
194 json = JSON.parse(last_response.body) 204 json = JSON.parse(last_response.body)
195 assert_equal new_value, json["article"]["body"] 205 assert_equal new_value, json["article"]["body"]
196 - end 206 + end
197 207
198 should 'list articles with pagination' do 208 should 'list articles with pagination' do
199 Article.destroy_all 209 Article.destroy_all
@@ -520,6 +530,14 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -520,6 +530,14 @@ class ArticlesTest &lt; ActiveSupport::TestCase
520 assert_equal 1, json['article']['hits'] 530 assert_equal 1, json['article']['hits']
521 end 531 end
522 532
  533 + should 'not update hit attribute of a specific child if a article is archived' do
  534 + folder = fast_create(Folder, :profile_id => user.person.id, :archived => true)
  535 + article = fast_create(Article, :parent_id => folder.id, :profile_id => user.person.id)
  536 + get "/api/v1/articles/#{folder.id}/children/#{article.id}?#{params.to_query}"
  537 + json = JSON.parse(last_response.body)
  538 + assert_equal 0, json['article']['hits']
  539 + end
  540 +
523 should 'list all events of a community in a given category' do 541 should 'list all events of a community in a given category' do
524 co = Community.create(identifier: 'my-community', name: 'name-my-community') 542 co = Community.create(identifier: 'my-community', name: 'name-my-community')
525 c1 = Category.create(environment: Environment.default, name: 'my-category') 543 c1 = Category.create(environment: Environment.default, name: 'my-category')
test/unit/api/comments_test.rb
@@ -66,6 +66,15 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -66,6 +66,15 @@ class CommentsTest &lt; ActiveSupport::TestCase
66 assert_equal body, json['comment']['body'] 66 assert_equal body, json['comment']['body']
67 end 67 end
68 68
  69 + should 'not comment an archived article' do
  70 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true)
  71 + body = 'My comment'
  72 + params.merge!({:body => body})
  73 +
  74 + post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
  75 + assert_equal 400, last_response.status
  76 + end
  77 +
69 should 'comment creation define the source' do 78 should 'comment creation define the source' do
70 amount = Comment.count 79 amount = Comment.count
71 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 80 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")