Commit 1c395f6b4936d3a9291a9781d066ecaeb02a5631
Exists in
staging
and in
4 other branches
Merge branch 'staging' of gitlab.com:participa/noosfero into staging
Showing
6 changed files
with
121 additions
and
50 deletions
Show diff stats
lib/noosfero/api/api.rb
lib/noosfero/api/helpers.rb
@@ -143,9 +143,6 @@ require 'grape' | @@ -143,9 +143,6 @@ require 'grape' | ||
143 | else | 143 | else |
144 | articles = articles.published | 144 | articles = articles.published |
145 | end | 145 | end |
146 | - if params[:categories_ids] | ||
147 | - articles = articles.joins(:categories).where('category_id in (?)', params[:categories_ids]) | ||
148 | - end | ||
149 | articles | 146 | articles |
150 | end | 147 | end |
151 | 148 | ||
@@ -248,6 +245,15 @@ require 'grape' | @@ -248,6 +245,15 @@ require 'grape' | ||
248 | end | 245 | end |
249 | end | 246 | end |
250 | 247 | ||
248 | + def by_categories(scope, params) | ||
249 | + category_ids = params[:category_ids] | ||
250 | + if category_ids.nil? | ||
251 | + scope | ||
252 | + else | ||
253 | + scope.joins(:categories).where(:categories => {:id => category_ids}) | ||
254 | + end | ||
255 | + end | ||
256 | + | ||
251 | def select_filtered_collection_of(object, method, params) | 257 | def select_filtered_collection_of(object, method, params) |
252 | conditions = make_conditions_with_parameter(params) | 258 | conditions = make_conditions_with_parameter(params) |
253 | order = make_order_with_parameters(object,method,params) | 259 | order = make_order_with_parameters(object,method,params) |
@@ -257,6 +263,7 @@ require 'grape' | @@ -257,6 +263,7 @@ require 'grape' | ||
257 | 263 | ||
258 | objects = object.send(method) | 264 | objects = object.send(method) |
259 | objects = by_reference(objects, params) | 265 | objects = by_reference(objects, params) |
266 | + objects = by_categories(objects, params) | ||
260 | 267 | ||
261 | objects = objects.where(conditions).where(timestamp).page(page_number).per_page(per_page).reorder(order) | 268 | objects = objects.where(conditions).where(timestamp).page(page_number).per_page(per_page).reorder(order) |
262 | 269 |
@@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
1 | +module Noosfero | ||
2 | + module API | ||
3 | + module V1 | ||
4 | + class Contacts < Grape::API | ||
5 | + | ||
6 | + resource :communities do | ||
7 | + | ||
8 | + resource ':id/contact' do | ||
9 | + #contaxt => {:name => 'some name', :email => 'test@mail.com', :subject => 'some title', :message => 'some message'} | ||
10 | + desc "Send a contact message" | ||
11 | + post do | ||
12 | + profile = environment.communities.find(params[:id]) | ||
13 | + forbidden! unless profile.present? | ||
14 | + contact = Contact.new params[:contact].merge(dest: profile) | ||
15 | + if contact.deliver | ||
16 | + {:success => true} | ||
17 | + else | ||
18 | + {:success => false} | ||
19 | + end | ||
20 | + end | ||
21 | + | ||
22 | + end | ||
23 | + end | ||
24 | + | ||
25 | + end | ||
26 | + end | ||
27 | + end | ||
28 | +end |
lib/noosfero/api/v1/search.rb
@@ -20,13 +20,13 @@ module Noosfero | @@ -20,13 +20,13 @@ module Noosfero | ||
20 | scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') | 20 | scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') |
21 | 21 | ||
22 | scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? | 22 | scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? |
23 | - | ||
24 | - category = params[:category] || "" | ||
25 | 23 | ||
24 | + scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present? | ||
25 | + | ||
26 | query = params[:query] || "" | 26 | query = params[:query] || "" |
27 | order = "more_recent" | 27 | order = "more_recent" |
28 | 28 | ||
29 | - options = {:filter => order, :template_id => params[:template_id], :category => category} | 29 | + options = {:filter => order, :template_id => params[:template_id]} |
30 | 30 | ||
31 | search_result = find_by_contents(asset, context, scope, query, paginate_options, options) | 31 | search_result = find_by_contents(asset, context, scope, query, paginate_options, options) |
32 | 32 |
test/unit/api/articles_test.rb
@@ -546,7 +546,7 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -546,7 +546,7 @@ class ArticlesTest < ActiveSupport::TestCase | ||
546 | e2.categories << c2 | 546 | e2.categories << c2 |
547 | e1.save! | 547 | e1.save! |
548 | e2.save! | 548 | e2.save! |
549 | - params['categories_ids[]']=c1.id | 549 | + params['category_ids[]']=c1.id |
550 | params['content_type']='Event' | 550 | params['content_type']='Event' |
551 | get "api/v1/communities/#{co.id}/articles?#{params.to_query}" | 551 | get "api/v1/communities/#{co.id}/articles?#{params.to_query}" |
552 | json = JSON.parse(last_response.body) | 552 | json = JSON.parse(last_response.body) |
@@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase | ||
555 | assert_equal e1.id, json['articles'][0]['id'] | 555 | assert_equal e1.id, json['articles'][0]['id'] |
556 | end | 556 | end |
557 | 557 | ||
558 | + should 'not list uncategorized event of a community if a category is given' do | ||
559 | + co = Community.create(identifier: 'my-community', name: 'name-my-community') | ||
560 | + c1 = Category.create(environment: Environment.default, name: 'my-category') | ||
561 | + c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') | ||
562 | + e1 = fast_create(Event, :profile_id => co.id) | ||
563 | + e2 = fast_create(Event, :profile_id => co.id) | ||
564 | + e3 = fast_create(Event, :profile_id => co.id) | ||
565 | + e1.categories << c1 | ||
566 | + e2.categories << c2 | ||
567 | + params['category_ids[]']=c1.id | ||
568 | + params['content_type']='Event' | ||
569 | + get "api/v1/communities/#{co.id}/articles?#{params.to_query}" | ||
570 | + json = JSON.parse(last_response.body) | ||
571 | + assert_equal 1, json['articles'].count | ||
572 | + assert_equal e1.id, json['articles'][0]['id'] | ||
573 | + end | ||
574 | + | ||
558 | should 'list events of a community in a given 2 categories' do | 575 | should 'list events of a community in a given 2 categories' do |
559 | co = Community.create(identifier: 'my-community', name: 'name-my-community') | 576 | co = Community.create(identifier: 'my-community', name: 'name-my-community') |
560 | c1 = Category.create(environment: Environment.default, name: 'my-category') | 577 | c1 = Category.create(environment: Environment.default, name: 'my-category') |
test/unit/api/search_test.rb
@@ -2,13 +2,14 @@ require File.dirname(__FILE__) + '/test_helper' | @@ -2,13 +2,14 @@ require File.dirname(__FILE__) + '/test_helper' | ||
2 | 2 | ||
3 | class SearchTest < ActiveSupport::TestCase | 3 | class SearchTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | - def create_article_with_optional_category(name, profile, category = nil, parent = nil) | ||
6 | - fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name, :parent => parent) | 5 | + def setup |
6 | + @person = create_user('testing').person | ||
7 | end | 7 | end |
8 | + attr_reader :person | ||
8 | 9 | ||
9 | should 'not list unpublished articles' do | 10 | should 'not list unpublished articles' do |
10 | - person = fast_create(Person) | ||
11 | - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | 11 | + Article.delete_all |
12 | + article = fast_create(Article, :profile_id => person.id, :published => false) | ||
12 | assert !article.published? | 13 | assert !article.published? |
13 | get "/api/v1/search/article" | 14 | get "/api/v1/search/article" |
14 | json = JSON.parse(last_response.body) | 15 | json = JSON.parse(last_response.body) |
@@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase | @@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase | ||
16 | end | 17 | end |
17 | 18 | ||
18 | should 'list articles' do | 19 | should 'list articles' do |
19 | - person = fast_create(Person) | ||
20 | - art = create_article_with_optional_category('an article to be found', person) | 20 | + fast_create(Article, :profile_id => person.id) |
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['articles'] | 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 |
27 | - person = fast_create(Person) | ||
28 | - art = create_article_with_optional_category('an article to be found', person) | 27 | + fast_create(Article, :profile_id => person.id, :name => 'some article') |
29 | get "/api/v1/search/article?query=test" | 28 | get "/api/v1/search/article?query=test" |
30 | json = JSON.parse(last_response.body) | 29 | json = JSON.parse(last_response.body) |
31 | assert_empty json['articles'] | 30 | assert_empty json['articles'] |
32 | end | 31 | end |
33 | 32 | ||
34 | should 'do not list articles of wrong type' do | 33 | should 'do not list articles of wrong type' do |
35 | - person = fast_create(Person) | ||
36 | - art = create_article_with_optional_category('an article to be found', person) | 34 | + fast_create(Article, :profile_id => person.id) |
37 | get "/api/v1/search/article?type=TinyMceArticle" | 35 | get "/api/v1/search/article?type=TinyMceArticle" |
38 | json = JSON.parse(last_response.body) | 36 | json = JSON.parse(last_response.body) |
39 | assert_empty json['articles'] | 37 | assert_empty json['articles'] |
40 | end | 38 | end |
41 | 39 | ||
42 | should 'list articles of one type' do | 40 | should 'list articles of one type' do |
43 | - person = fast_create(Person) | ||
44 | - art = create_article_with_optional_category('an article to be found', person) | ||
45 | - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | 41 | + fast_create(Article, :profile_id => person.id) |
42 | + article = fast_create(TinyMceArticle, :profile_id => person.id) | ||
43 | + | ||
46 | get "/api/v1/search/article?type=TinyMceArticle" | 44 | get "/api/v1/search/article?type=TinyMceArticle" |
47 | json = JSON.parse(last_response.body) | 45 | json = JSON.parse(last_response.body) |
48 | - assert_equal 1, json['articles'].count | 46 | + assert_equal article.id, json['articles'].first['id'] |
49 | end | 47 | end |
50 | 48 | ||
51 | should 'list articles of one type and query string' do | 49 | should 'list articles of one type and query string' do |
52 | - person = fast_create(Person) | ||
53 | - art = create_article_with_optional_category('an article to be found', person) | ||
54 | - art1 = create_article_with_optional_category('article for query', person) | ||
55 | - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | 50 | + fast_create(Article, :profile_id => person.id, :name => 'some article') |
51 | + fast_create(Article, :profile_id => person.id, :name => 'Some thing') | ||
52 | + article = fast_create(TinyMceArticle, :profile_id => person.id, :name => 'Some thing') | ||
56 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" | 53 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" |
57 | json = JSON.parse(last_response.body) | 54 | json = JSON.parse(last_response.body) |
58 | assert_equal 1, json['articles'].count | 55 | assert_equal 1, json['articles'].count |
56 | + assert_equal article.id, json['articles'].first['id'] | ||
59 | end | 57 | end |
60 | 58 | ||
61 | should 'not return more entries than page limit' do | 59 | should 'not return more entries than page limit' do |
62 | - person = fast_create(Person) | ||
63 | - ('1'..'5').each do |n| | ||
64 | - art = create_article_with_optional_category("Article #{n}", person) | 60 | + 1.upto(5).each do |n| |
61 | + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") | ||
65 | end | 62 | end |
66 | 63 | ||
67 | get "/api/v1/search/article?query=Article&per_page=3" | 64 | get "/api/v1/search/article?query=Article&per_page=3" |
@@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase | @@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase | ||
71 | end | 68 | end |
72 | 69 | ||
73 | should 'return entries second page' do | 70 | should 'return entries second page' do |
74 | - person = fast_create(Person) | ||
75 | - ('1'..'5').each do |n| | ||
76 | - art = create_article_with_optional_category("Article #{n}", person) | 71 | + 1.upto(5).each do |n| |
72 | + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") | ||
77 | end | 73 | end |
78 | 74 | ||
79 | get "/api/v1/search/article?query=Article&per_page=3&page=2" | 75 | get "/api/v1/search/article?query=Article&per_page=3&page=2" |
@@ -83,38 +79,60 @@ class SearchTest < ActiveSupport::TestCase | @@ -83,38 +79,60 @@ class SearchTest < ActiveSupport::TestCase | ||
83 | end | 79 | end |
84 | 80 | ||
85 | should 'search articles in profile' do | 81 | 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) | 82 | + person2 = fast_create(Person) |
83 | + fast_create(Article, :profile_id => person.id) | ||
84 | + fast_create(Article, :profile_id => person.id) | ||
85 | + article = fast_create(Article, :profile_id => person2.id) | ||
92 | 86 | ||
93 | - get "/api/v1/search/article?query=Article&profile_id=#{person1.id}" | 87 | + get "/api/v1/search/article?query=Article&profile_id=#{person2.id}" |
94 | json = JSON.parse(last_response.body) | 88 | json = JSON.parse(last_response.body) |
95 | - # Only for person1 | ||
96 | - assert_equal 2, json['articles'].count | 89 | + assert_equal article.id, json['articles'].first['id'] |
97 | end | 90 | end |
98 | 91 | ||
99 | - should 'search with fields' do | ||
100 | - person = fast_create(Person) | ||
101 | - art = create_article_with_optional_category('an article to be found', person) | 92 | + should 'search and return values specified in fields parameter' do |
93 | + fast_create(Article, :profile_id => person.id) | ||
102 | get "/api/v1/search/article?fields=title" | 94 | get "/api/v1/search/article?fields=title" |
103 | json = JSON.parse(last_response.body) | 95 | json = JSON.parse(last_response.body) |
104 | assert_not_empty json['articles'] | 96 | assert_not_empty json['articles'] |
105 | - assert_equal ['title'], json['articles'].first.keys | 97 | + assert_equal ['title'], json['articles'].first.keys |
106 | end | 98 | end |
107 | 99 | ||
108 | should 'search with parent' do | 100 | should 'search with parent' do |
109 | - person = fast_create(Person) | ||
110 | - parent = fast_create(Folder, :profile_id => person.id, :name => "parent", :published => true) | ||
111 | - art = fast_create(Article, :profile_id => person.id, :name => "child", :parent_id => parent.id) | ||
112 | - art2 = fast_create(Article, :profile_id => person.id, :name => "child2") | 101 | + parent = fast_create(Folder, :profile_id => person.id) |
102 | + fast_create(Article, :profile_id => person.id) | ||
103 | + article = fast_create(Article, :profile_id => person.id, :parent_id => parent.id) | ||
113 | get "/api/v1/search/article?parent_id=#{parent.id}" | 104 | get "/api/v1/search/article?parent_id=#{parent.id}" |
114 | json = JSON.parse(last_response.body) | 105 | json = JSON.parse(last_response.body) |
115 | - assert_not_empty json['articles'] | ||
116 | - assert_equal art.id, json['articles'].first["id"] | ||
117 | assert_equal 1, json['articles'].count | 106 | assert_equal 1, json['articles'].count |
107 | + assert_equal article.id, json['articles'].first["id"] | ||
108 | + end | ||
109 | + | ||
110 | + should 'search filter by category' do | ||
111 | + Article.delete_all | ||
112 | + fast_create(Article, :profile_id => person.id) | ||
113 | + article = fast_create(Article, :profile_id => person.id) | ||
114 | + category = fast_create(Category) | ||
115 | + article.categories<< category | ||
116 | + get "/api/v1/search/article?category_ids=#{category.id}" | ||
117 | + json = JSON.parse(last_response.body) | ||
118 | + assert_equal 1, json['articles'].count | ||
119 | + assert_equal article.id, json['articles'].first["id"] | ||
120 | + end | ||
121 | + | ||
122 | + should 'search filter by more than one category' do | ||
123 | + Article.delete_all | ||
124 | + fast_create(Article, :profile_id => person.id) | ||
125 | + article1 = fast_create(Article, :profile_id => person.id) | ||
126 | + article2 = fast_create(Article, :profile_id => person.id) | ||
127 | + category1 = fast_create(Category) | ||
128 | + category2 = fast_create(Category) | ||
129 | + article1.categories<< category1 | ||
130 | + article2.categories<< category2 | ||
131 | + get "/api/v1/search/article?category_ids[]=#{category1.id}&category_ids[]=#{category2.id}" | ||
132 | + json = JSON.parse(last_response.body) | ||
133 | + assert_equal 2, json['articles'].count | ||
134 | + assert_equal article1.id, json['articles'].first["id"] | ||
135 | + assert_equal article2.id, json['articles'].last["id"] | ||
118 | end | 136 | end |
119 | 137 | ||
120 | end | 138 | end |