[Git][noosfero/noosfero][api] api: review comments mountpoint

Rodrigo Souto gitlab at gitlab.com
Thu Jun 25 14:12:27 BRT 2015


Rodrigo Souto pushed to branch api at Noosfero / noosfero


Commits:
c71d67d7 by Rodrigo Souto at 2015-06-25T14:12:11Z
api: review comments mountpoint

- - - - -


3 changed files:

- lib/noosfero/api/helpers.rb
- lib/noosfero/api/v1/comments.rb
- test/unit/api/comments_test.rb


Changes:

=====================================
lib/noosfero/api/helpers.rb
=====================================
--- a/lib/noosfero/api/helpers.rb
+++ b/lib/noosfero/api/helpers.rb
@@ -103,15 +103,21 @@ module Noosfero
         params[:order] || "created_at DESC"
       end
 
+      def by_reference(scope, params)
+        if params[:reference_id]
+          created_at = scope.find(params[:reference_id]).created_at
+          scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at)
+        else
+          scope
+        end
+      end
+
       def select_filtered_collection_of(object, method, params)
         conditions = make_conditions_with_parameter(params)
         order = make_order_with_parameters(params)
 
         objects = object.send(method)
-        if params[:reference_id]
-          created_at = objects.find(params[:reference_id]).created_at
-          objects = objects.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at)
-        end
+        objects = by_reference(objects, params)
         objects = objects.where(conditions).limit(limit).order(order)
 
         objects


=====================================
lib/noosfero/api/v1/comments.rb
=====================================
--- a/lib/noosfero/api/v1/comments.rb
+++ b/lib/noosfero/api/v1/comments.rb
@@ -15,18 +15,10 @@ module Noosfero
           # Example Request:
           #  GET /articles/12/comments?oldest&limit=10&reference_id=23
           get ":id/comments" do
-
-            conditions = make_conditions_with_parameter(params)
             article = find_article(environment.articles, params[:id])
+            comments = select_filtered_collection_of(article, :comments, params)
 
-            if params[:reference_id]
-              created_at = article.comments.find(params[:reference_id]).created_at
-              comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
-            else
-              comments = article.comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
-            end
             present comments, :with => Entities::Comment
-
           end
 
           get ":id/comments/:comment_id" do


=====================================
test/unit/api/comments_test.rb
=====================================
--- a/test/unit/api/comments_test.rb
+++ b/test/unit/api/comments_test.rb
@@ -25,7 +25,7 @@ class CommentsTest < ActiveSupport::TestCase
     assert_equal 403, last_response.status
   end
 
-  should 'not comment a article if user has no permission to view it' do
+  should 'not comment an article 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?
@@ -41,7 +41,28 @@ class CommentsTest < ActiveSupport::TestCase
 
     get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
     json = JSON.parse(last_response.body)
+    assert_equal 200, last_response.status
     assert_equal 2, json["comments"].length
   end
 
+  should 'return comment of an article' do
+    article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
+    comment = article.comments.create!(:body => "another comment", :author => user.person)
+
+    get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 200, last_response.status
+    assert_equal comment.id, json['comment']['id']
+  end
+
+  should 'comment an article' do
+    article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
+    body = 'My comment'
+    params.merge!({:body => body})
+
+    post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 201, last_response.status
+    assert_equal body, json['comment']['body']
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/c71d67d786737154c578562b3ee037ea48c27a67
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150625/f2adfcb2/attachment.html>


More information about the Noosfero-dev mailing list