Commit 5922a11e2f6baf0724bb72a118735364b5ab2f41
Exists in
staging
and in
4 other branches
Merge branch 'api-search' into staging
Showing
2 changed files
with
28 additions
and
12 deletions
Show diff stats
lib/noosfero/api/v1/search.rb
@@ -11,18 +11,23 @@ module Noosfero | @@ -11,18 +11,23 @@ module Noosfero | ||
11 | # APIHelpers | 11 | # APIHelpers |
12 | asset = :articles | 12 | asset = :articles |
13 | context = environment | 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 | scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') | 19 | scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') |
17 | 20 | ||
18 | category = params[:category] || "" | 21 | category = params[:category] || "" |
22 | + | ||
19 | query = params[:query] || "" | 23 | query = params[:query] || "" |
20 | order = "more_recent" | 24 | order = "more_recent" |
21 | 25 | ||
22 | options = {:filter => order, :template_id => params[:template_id], :category => category} | 26 | options = {:filter => order, :template_id => params[:template_id], :category => category} |
23 | 27 | ||
24 | articles = find_by_contents(asset, context, scope, query, paginate_options, options) | 28 | articles = find_by_contents(asset, context, scope, query, paginate_options, options) |
25 | - present articles | 29 | + |
30 | + present articles[:results], :with => Entities::Article | ||
26 | end | 31 | end |
27 | end | 32 | end |
28 | end | 33 | end |
test/unit/api/search_test.rb
@@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase | ||
12 | assert !article.published? | 12 | assert !article.published? |
13 | get "/api/v1/search/article" | 13 | get "/api/v1/search/article" |
14 | json = JSON.parse(last_response.body) | 14 | json = JSON.parse(last_response.body) |
15 | - assert_empty json['results'] | 15 | + assert_empty json['articles'] |
16 | end | 16 | end |
17 | 17 | ||
18 | should 'list articles' do | 18 | should 'list articles' do |
@@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase | ||
20 | art = create_article_with_optional_category('an article to be found', person) | 20 | art = create_article_with_optional_category('an article to be found', person) |
21 | get "/api/v1/search/article" | 21 | get "/api/v1/search/article" |
22 | json = JSON.parse(last_response.body) | 22 | json = JSON.parse(last_response.body) |
23 | - assert_not_empty json['results'] | 23 | + assert_not_empty json['articles'] |
24 | end | 24 | end |
25 | 25 | ||
26 | should 'invalid search string articles' do | 26 | should 'invalid search string articles' do |
@@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase | ||
28 | art = create_article_with_optional_category('an article to be found', person) | 28 | art = create_article_with_optional_category('an article to be found', person) |
29 | get "/api/v1/search/article?query=test" | 29 | get "/api/v1/search/article?query=test" |
30 | json = JSON.parse(last_response.body) | 30 | json = JSON.parse(last_response.body) |
31 | - assert_empty json['results'] | 31 | + assert_empty json['articles'] |
32 | end | 32 | end |
33 | 33 | ||
34 | should 'do not list articles of wrong type' do | 34 | should 'do not list articles of wrong type' do |
@@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase | ||
36 | art = create_article_with_optional_category('an article to be found', person) | 36 | art = create_article_with_optional_category('an article to be found', person) |
37 | get "/api/v1/search/article?type=TinyMceArticle" | 37 | get "/api/v1/search/article?type=TinyMceArticle" |
38 | json = JSON.parse(last_response.body) | 38 | json = JSON.parse(last_response.body) |
39 | - assert_empty json['results'] | 39 | + assert_empty json['articles'] |
40 | end | 40 | end |
41 | 41 | ||
42 | should 'list articles of one type' do | 42 | should 'list articles of one type' do |
@@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase | ||
45 | article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | 45 | article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) |
46 | get "/api/v1/search/article?type=TinyMceArticle" | 46 | get "/api/v1/search/article?type=TinyMceArticle" |
47 | json = JSON.parse(last_response.body) | 47 | json = JSON.parse(last_response.body) |
48 | - assert_equal 1, json['results'].size | 48 | + assert_equal 1, json['articles'].count |
49 | end | 49 | end |
50 | 50 | ||
51 | should 'list articles of one type and query string' do | 51 | should 'list articles of one type and query string' do |
@@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase | ||
55 | article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | 55 | article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) |
56 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" | 56 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" |
57 | json = JSON.parse(last_response.body) | 57 | json = JSON.parse(last_response.body) |
58 | - assert_equal 1, json['results'].size | 58 | + assert_equal 1, json['articles'].count |
59 | end | 59 | end |
60 | 60 | ||
61 | should 'not return more entries than page limit' do | 61 | should 'not return more entries than page limit' do |
@@ -67,8 +67,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -67,8 +67,7 @@ class SearchTest < ActiveSupport::TestCase | ||
67 | get "/api/v1/search/article?query=Article&limit=3" | 67 | get "/api/v1/search/article?query=Article&limit=3" |
68 | json = JSON.parse(last_response.body) | 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 | end | 71 | end |
73 | 72 | ||
74 | should 'return entries second page' do | 73 | should 'return entries second page' do |
@@ -80,8 +79,20 @@ class SearchTest < ActiveSupport::TestCase | @@ -80,8 +79,20 @@ class SearchTest < ActiveSupport::TestCase | ||
80 | get "/api/v1/search/article?query=Article&limit=3&page=2" | 79 | get "/api/v1/search/article?query=Article&limit=3&page=2" |
81 | json = JSON.parse(last_response.body) | 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 | end | 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 | end | 98 | end |
88 | \ No newline at end of file | 99 | \ No newline at end of file |