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 120 expose :followers_count
121 121 expose :votes_count
122 122 expose :comments_count
  123 + expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
123 124 end
124 125  
125 126 class Article < ArticleBase
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -131,9 +131,8 @@ module Noosfero
131 131 failure [[403, 'Forbidden']]
132 132 named 'ArticleFollowers'
133 133 end
134   -
135   - #FIXME refactor this method
136 134 get 'voted_by_me' do
  135 + #FIXME refactor this method
137 136 present_articles_paginated(current_person.votes.where(:voteable_type => 'Article').collect(&:voteable))
138 137 end
139 138  
... ... @@ -150,8 +149,14 @@ module Noosfero
150 149 # FIXME verify allowed values
151 150 render_api_error!('Vote value not allowed', 400) unless [-1, 1].include?(value)
152 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 160 end
156 161  
157 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 31 post ":id/comments" do
32 32 article = find_article(environment.articles, params[:id])
33 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 41 end
36 42 end
37 43  
... ...
test/unit/api/articles_test.rb
... ... @@ -138,6 +138,16 @@ class ArticlesTest &lt; ActiveSupport::TestCase
138 138 assert_equal true, json['vote']
139 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 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 153 expose_attributes.each do |attr|
... ... @@ -151,7 +161,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
151 161  
152 162 should "update body of article created by me" do
153 163 new_value = "Another body"
154   - params[:article] = {:body => new_value}
  164 + params[:article] = {:body => new_value}
155 165 article = fast_create(Article, :profile_id => person.id)
156 166 post "/api/v1/articles/#{article.id}?#{params.to_query}"
157 167 json = JSON.parse(last_response.body)
... ... @@ -160,7 +170,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
160 170  
161 171 should "update title of article created by me" do
162 172 new_value = "Another name"
163   - params[:article] = {:name => new_value}
  173 + params[:article] = {:name => new_value}
164 174 article = fast_create(Article, :profile_id => person.id)
165 175 post "/api/v1/articles/#{article.id}?#{params.to_query}"
166 176 json = JSON.parse(last_response.body)
... ... @@ -170,7 +180,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
170 180 should 'not update article of another user' do
171 181 another_person = fast_create(Person, :environment_id => environment.id)
172 182 article = fast_create(Article, :profile_id => another_person.id)
173   - params[:article] = {:title => 'Some title'}
  183 + params[:article] = {:title => 'Some title'}
174 184 post "/api/v1/articles/#{article.id}?#{params.to_query}"
175 185 assert_equal 403, last_response.status
176 186 end
... ... @@ -178,7 +188,7 @@ class ArticlesTest &lt; ActiveSupport::TestCase
178 188 should 'not update article without permission in community' do
179 189 community = fast_create(Community, :environment_id => environment.id)
180 190 article = fast_create(Article, :profile_id => community.id)
181   - params[:article] = {:name => 'New title'}
  191 + params[:article] = {:name => 'New title'}
182 192 post "/api/v1/articles/#{article.id}?#{params.to_query}"
183 193 assert_equal 403, last_response.status
184 194 end
... ... @@ -189,11 +199,11 @@ class ArticlesTest &lt; ActiveSupport::TestCase
189 199 give_permission(person, 'post_content', community)
190 200 article = fast_create(Article, :profile_id => community.id)
191 201 new_value = "Another body"
192   - params[:article] = {:body => new_value}
  202 + params[:article] = {:body => new_value}
193 203 post "/api/v1/articles/#{article.id}?#{params.to_query}"
194 204 json = JSON.parse(last_response.body)
195 205 assert_equal new_value, json["article"]["body"]
196   - end
  206 + end
197 207  
198 208 should 'list articles with pagination' do
199 209 Article.destroy_all
... ... @@ -520,6 +530,14 @@ class ArticlesTest &lt; ActiveSupport::TestCase
520 530 assert_equal 1, json['article']['hits']
521 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 541 should 'list all events of a community in a given category' do
524 542 co = Community.create(identifier: 'my-community', name: 'name-my-community')
525 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 66 assert_equal body, json['comment']['body']
67 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 78 should 'comment creation define the source' do
70 79 amount = Comment.count
71 80 article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
... ...