Commit 5922a11e2f6baf0724bb72a118735364b5ab2f41

Authored by ABNER SILVA DE OLIVEIRA
2 parents 36eec74c 3ca7d77b

Merge branch 'api-search' into staging

lib/noosfero/api/v1/search.rb
... ... @@ -11,18 +11,23 @@ module Noosfero
11 11 # APIHelpers
12 12 asset = :articles
13 13 context = environment
14   - scope = environment.articles.public
  14 +
  15 + profile = environment.profiles.find(params[:profile_id]) if params[:profile_id]
  16 +
  17 + scope = profile.nil? ? environment.articles.public : profile.articles.public
15 18  
16 19 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
17 20  
18 21 category = params[:category] || ""
  22 +
19 23 query = params[:query] || ""
20 24 order = "more_recent"
21 25  
22 26 options = {:filter => order, :template_id => params[:template_id], :category => category}
23 27  
24 28 articles = find_by_contents(asset, context, scope, query, paginate_options, options)
25   - present articles
  29 +
  30 + present articles[:results], :with => Entities::Article
26 31 end
27 32 end
28 33 end
... ...
test/unit/api/search_test.rb
... ... @@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase
12 12 assert !article.published?
13 13 get "/api/v1/search/article"
14 14 json = JSON.parse(last_response.body)
15   - assert_empty json['results']
  15 + assert_empty json['articles']
16 16 end
17 17  
18 18 should 'list articles' do
... ... @@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase
20 20 art = create_article_with_optional_category('an article to be found', person)
21 21 get "/api/v1/search/article"
22 22 json = JSON.parse(last_response.body)
23   - assert_not_empty json['results']
  23 + assert_not_empty json['articles']
24 24 end
25 25  
26 26 should 'invalid search string articles' do
... ... @@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase
28 28 art = create_article_with_optional_category('an article to be found', person)
29 29 get "/api/v1/search/article?query=test"
30 30 json = JSON.parse(last_response.body)
31   - assert_empty json['results']
  31 + assert_empty json['articles']
32 32 end
33 33  
34 34 should 'do not list articles of wrong type' do
... ... @@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase
36 36 art = create_article_with_optional_category('an article to be found', person)
37 37 get "/api/v1/search/article?type=TinyMceArticle"
38 38 json = JSON.parse(last_response.body)
39   - assert_empty json['results']
  39 + assert_empty json['articles']
40 40 end
41 41  
42 42 should 'list articles of one type' do
... ... @@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase
45 45 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true)
46 46 get "/api/v1/search/article?type=TinyMceArticle"
47 47 json = JSON.parse(last_response.body)
48   - assert_equal 1, json['results'].size
  48 + assert_equal 1, json['articles'].count
49 49 end
50 50  
51 51 should 'list articles of one type and query string' do
... ... @@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase
55 55 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true)
56 56 get "/api/v1/search/article?type=TinyMceArticle&query=thing"
57 57 json = JSON.parse(last_response.body)
58   - assert_equal 1, json['results'].size
  58 + assert_equal 1, json['articles'].count
59 59 end
60 60  
61 61 should 'not return more entries than page limit' do
... ... @@ -67,8 +67,7 @@ class SearchTest < ActiveSupport::TestCase
67 67 get "/api/v1/search/article?query=Article&limit=3"
68 68 json = JSON.parse(last_response.body)
69 69  
70   -
71   - assert_equal 3, json['results'].size
  70 + assert_equal 3, json['articles'].count
72 71 end
73 72  
74 73 should 'return entries second page' do
... ... @@ -80,8 +79,20 @@ class SearchTest < ActiveSupport::TestCase
80 79 get "/api/v1/search/article?query=Article&limit=3&page=2"
81 80 json = JSON.parse(last_response.body)
82 81  
83   -
84   - assert_equal 2, json['results'].size
  82 + assert_equal 2, json['articles'].count
85 83 end
86 84  
  85 + should 'search articles in profile' do
  86 + person1 = fast_create(Person)
  87 + person2 = fast_create(Person)
  88 +
  89 + art1 = create_article_with_optional_category("Article 1 for profile #{person1.id}", person1)
  90 + art2 = create_article_with_optional_category("Article for profile #{person2.id}", person2)
  91 + art3 = create_article_with_optional_category("Article 2 for profile #{person1.id}", person1)
  92 +
  93 + get "/api/v1/search/article?query=Article&profile_id=#{person1.id}"
  94 + json = JSON.parse(last_response.body)
  95 + # Only for person1
  96 + assert_equal 2, json['articles'].count
  97 + end
87 98 end
88 99 \ No newline at end of file
... ...