Commit 64154b3e4daadae57ee53d7b0317963ea2468323

Authored by Victor Costa
2 parents 0caacfdd 98f6dc63
Exists in fix_sign_up_form

Merge branch 'community_members' into 'master'

API: avoid to bring all members when get one community



See merge request !1001
app/api/entities.rb
@@ -38,6 +38,13 @@ module Api @@ -38,6 +38,13 @@ module Api
38 PERMISSIONS[current_permission] <= PERMISSIONS[permission] 38 PERMISSIONS[current_permission] <= PERMISSIONS[permission]
39 end 39 end
40 40
  41 + def self.expose_optional_field?(field, options = {})
  42 + return false if options[:params].nil?
  43 + optional_fields = options[:params][:optional_fields] || []
  44 + optional_fields.include?(field.to_s)
  45 + end
  46 +
  47 +
41 class Image < Entity 48 class Image < Entity
42 root 'images', 'image' 49 root 'images', 'image'
43 50
@@ -166,7 +173,8 @@ module Api @@ -166,7 +173,8 @@ module Api
166 community.admins.map{|admin| {"name"=>admin.name, "id"=>admin.id, "username" => admin.identifier}} 173 community.admins.map{|admin| {"name"=>admin.name, "id"=>admin.id, "username" => admin.identifier}}
167 end 174 end
168 expose :categories, :using => Category 175 expose :categories, :using => Category
169 - expose :members, :using => Person , :if => lambda{ |community, options| community.display_info_to? options[:current_person] } 176 + expose :members_count
  177 + expose :members, :if => lambda {|community, options| Entities.expose_optional_field?(:members, options)}
170 end 178 end
171 179
172 class CommentBase < Entity 180 class CommentBase < Entity
@@ -213,7 +221,7 @@ module Api @@ -213,7 +221,7 @@ module Api
213 expose :comments_count 221 expose :comments_count
214 expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"} 222 expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
215 expose :type 223 expose :type
216 - expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])} 224 + expose :comments, using: CommentBase, :if => lambda{|comment,options| Entities.expose_optional_field?(:comments, options)}
217 expose :published 225 expose :published
218 expose :accept_comments?, as: :accept_comments 226 expose :accept_comments?, as: :accept_comments
219 end 227 end
app/api/helpers.rb
@@ -46,9 +46,11 @@ module Api @@ -46,9 +46,11 @@ module Api
46 def present_partial(model, options) 46 def present_partial(model, options)
47 if(params[:fields].present?) 47 if(params[:fields].present?)
48 begin 48 begin
49 - fields = JSON.parse(params[:fields]) 49 + fields = JSON.parse((params.to_hash[:fields] || params.to_hash['fields']).to_json)
50 if fields.present? 50 if fields.present?
51 - options.merge!(fields.symbolize_keys.slice(:only, :except)) 51 + fields = fields.symbolize_keys
  52 + options.merge!(:only => fields[:only]) if fields[:only].present?
  53 + options.merge!(:except => fields[:except]) if fields[:except].present?
52 end 54 end
53 rescue 55 rescue
54 fields = params[:fields] 56 fields = params[:fields]
app/api/v1/communities.rb
@@ -18,7 +18,7 @@ module Api @@ -18,7 +18,7 @@ module Api
18 communities = select_filtered_collection_of(environment, 'communities', params) 18 communities = select_filtered_collection_of(environment, 'communities', params)
19 communities = profiles_for_person(communities, current_person) 19 communities = profiles_for_person(communities, current_person)
20 communities = communities.by_location(params) # Must be the last. May return Exception obj 20 communities = communities.by_location(params) # Must be the last. May return Exception obj
21 - present communities, :with => Entities::Community, :current_person => current_person 21 + present communities, :with => Entities::Community, :current_person => current_person, :params => params
22 end 22 end
23 23
24 24
@@ -49,7 +49,7 @@ module Api @@ -49,7 +49,7 @@ module Api
49 49
50 get ':id' do 50 get ':id' do
51 community = profiles_for_person(environment.communities, current_person).find_by_id(params[:id]) 51 community = profiles_for_person(environment.communities, current_person).find_by_id(params[:id])
52 - present community, :with => Entities::Community, :current_person => current_person 52 + present community, :with => Entities::Community, :current_person => current_person, :params => params
53 end 53 end
54 54
55 end 55 end
test/api/articles_test.rb
@@ -773,12 +773,12 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -773,12 +773,12 @@ class ArticlesTest &lt; ActiveSupport::TestCase
773 end 773 end
774 end 774 end
775 775
776 - should 'only show article comments when show_comments is present' do 776 + should 'only show article comments when optional_fields comments is present' do
777 person = fast_create(Person) 777 person = fast_create(Person)
778 article = fast_create(Article, :profile_id => person.id, :name => "Some thing") 778 article = fast_create(Article, :profile_id => person.id, :name => "Some thing")
779 article.comments.create!(:body => "another comment", :author => person) 779 article.comments.create!(:body => "another comment", :author => person)
780 780
781 - get "/api/v1/articles/#{article.id}/?#{params.merge(:show_comments => '1').to_query}" 781 + get "/api/v1/articles/#{article.id}/?#{params.merge(:optional_fields => [:comments]).to_query}"
782 json = JSON.parse(last_response.body) 782 json = JSON.parse(last_response.body)
783 assert_includes json["article"].keys, "comments" 783 assert_includes json["article"].keys, "comments"
784 assert_equal json["article"]["comments"].first["body"], "another comment" 784 assert_equal json["article"]["comments"].first["body"], "another comment"
@@ -805,4 +805,24 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -805,4 +805,24 @@ class ArticlesTest &lt; ActiveSupport::TestCase
805 json = JSON.parse(last_response.body) 805 json = JSON.parse(last_response.body)
806 assert_includes json["article"]["permissions"], 'allow_post_content' 806 assert_includes json["article"]["permissions"], 'allow_post_content'
807 end 807 end
  808 +
  809 + should 'return only article fields defined in parameter' do
  810 + Article.destroy_all
  811 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  812 + params[:fields] = {:only => ['id', 'title']}
  813 + get "/api/v1/articles/?#{params.to_query}"
  814 + json = JSON.parse(last_response.body)
  815 + assert_equivalent ['id', 'title'], json["articles"].first.keys
  816 + end
  817 +
  818 + should 'return all article fields except the ones defined in parameter' do
  819 + Article.destroy_all
  820 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  821 + params[:fields] = {:except => ['id', 'title']}
  822 + get "/api/v1/articles/?#{params.to_query}"
  823 + json = JSON.parse(last_response.body)
  824 + assert_not_includes json["articles"].first.keys, 'id'
  825 + assert_not_includes json["articles"].first.keys, 'title'
  826 + end
  827 +
808 end 828 end
test/api/communities_test.rb
@@ -100,7 +100,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase @@ -100,7 +100,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
100 get "/api/v1/communities/#{community.id}?#{params.to_query}" 100 get "/api/v1/communities/#{community.id}?#{params.to_query}"
101 json = JSON.parse(last_response.body) 101 json = JSON.parse(last_response.body)
102 assert_equal community.id, json['community']['id'] 102 assert_equal community.id, json['community']['id']
103 - assert_nil json['community']['members'] 103 + assert_nil json['community']['admins']
104 end 104 end
105 105
106 should 'get private community to logged member' do 106 should 'get private community to logged member' do
@@ -108,6 +108,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase @@ -108,6 +108,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
108 community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true) 108 community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true)
109 community.add_member(person) 109 community.add_member(person)
110 110
  111 + params[:optional_fields] = ['members']
111 get "/api/v1/communities/#{community.id}?#{params.to_query}" 112 get "/api/v1/communities/#{community.id}?#{params.to_query}"
112 json = JSON.parse(last_response.body) 113 json = JSON.parse(last_response.body)
113 assert_equal community.id, json['community']['id'] 114 assert_equal community.id, json['community']['id']
@@ -333,4 +334,51 @@ class CommunitiesTest &lt; ActiveSupport::TestCase @@ -333,4 +334,51 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
333 refute json['community']['additional_data'].has_key?('Rating') 334 refute json['community']['additional_data'].has_key?('Rating')
334 end 335 end
335 336
  337 + should 'not display members value by default' do
  338 + login_api
  339 + community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true)
  340 + community.add_member(person)
  341 +
  342 + get "/api/v1/communities/#{community.id}?#{params.to_query}"
  343 + json = JSON.parse(last_response.body)
  344 + assert_equal community.id, json['community']['id']
  345 + assert_nil json['community']['members']
  346 + end
  347 +
  348 + should 'display members values if optional field parameter is passed' do
  349 + community = fast_create(Community, :environment_id => environment.id)
  350 +
  351 + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => [:members]}).to_query}"
  352 + json = JSON.parse(last_response.body)
  353 + assert_equal community.id, json['community']['id']
  354 + assert_not_nil json['community']['members']
  355 + end
  356 +
  357 + should 'display members values if optional_fields has members value as string in array' do
  358 + community = fast_create(Community, :environment_id => environment.id)
  359 +
  360 + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => ['members']}).to_query}"
  361 + json = JSON.parse(last_response.body)
  362 + assert_equal community.id, json['community']['id']
  363 + assert_not_nil json['community']['members']
  364 + end
  365 +
  366 + should 'display members values if optional_fields has members value in array' do
  367 + community = fast_create(Community, :environment_id => environment.id)
  368 +
  369 + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => ['members', 'another']}).to_query}"
  370 + json = JSON.parse(last_response.body)
  371 + assert_equal community.id, json['community']['id']
  372 + assert_not_nil json['community']['members']
  373 + end
  374 +
  375 + should 'display members values if optional_fields has members value as string' do
  376 + community = fast_create(Community, :environment_id => environment.id)
  377 +
  378 + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => 'members'}).to_query}"
  379 + json = JSON.parse(last_response.body)
  380 + assert_equal community.id, json['community']['id']
  381 + assert_not_nil json['community']['members']
  382 + end
  383 +
336 end 384 end
test/api/helpers_test.rb
@@ -233,7 +233,7 @@ class Api::HelpersTest &lt; ActiveSupport::TestCase @@ -233,7 +233,7 @@ class Api::HelpersTest &lt; ActiveSupport::TestCase
233 233
234 should 'accept json as fields parameter when calling present partial' do 234 should 'accept json as fields parameter when calling present partial' do
235 model = mock 235 model = mock
236 - params[:fields] = {only: [:name, {user: [:login]}]}.to_json 236 + params[:fields] = {only: [:name, {user: [:login]}]}
237 expects(:present).with(model, {:only => ['name', {'user' => ['login']}]}) 237 expects(:present).with(model, {:only => ['name', {'user' => ['login']}]})
238 present_partial(model, {}) 238 present_partial(model, {})
239 end 239 end
test/api/people_test.rb
@@ -119,8 +119,8 @@ class PeopleTest &lt; ActiveSupport::TestCase @@ -119,8 +119,8 @@ class PeopleTest &lt; ActiveSupport::TestCase
119 119
120 should 'people endpoint filter by fields parameter with hierarchy for logged user' do 120 should 'people endpoint filter by fields parameter with hierarchy for logged user' do
121 login_api 121 login_api
122 - fields = URI.encode({only: [:name, {user: [:login]}]}.to_json.to_str)  
123 - get "/api/v1/people?#{params.to_query}&fields=#{fields}" 122 + params[:fields] = {only: [:name, {user: [:login]}]}
  123 + get "/api/v1/people?#{params.to_query}"
124 json = JSON.parse(last_response.body) 124 json = JSON.parse(last_response.body)
125 expected = {'people' => [{'name' => person.name, 'user' => {'login' => 'testapi'}}]} 125 expected = {'people' => [{'name' => person.name, 'user' => {'login' => 'testapi'}}]}
126 assert_equal expected, json 126 assert_equal expected, json