Commit 98f6dc63719eb4271f0e4156ee3958c033c568c5

Authored by Leandro Santos
1 parent 0caacfdd
Exists in fix_sign_up_form

API: avoid to bring all members of a community object

app/api/entities.rb
... ... @@ -38,6 +38,13 @@ module Api
38 38 PERMISSIONS[current_permission] <= PERMISSIONS[permission]
39 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 48 class Image < Entity
42 49 root 'images', 'image'
43 50  
... ... @@ -166,7 +173,8 @@ module Api
166 173 community.admins.map{|admin| {"name"=>admin.name, "id"=>admin.id, "username" => admin.identifier}}
167 174 end
168 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 178 end
171 179  
172 180 class CommentBase < Entity
... ... @@ -213,7 +221,7 @@ module Api
213 221 expose :comments_count
214 222 expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
215 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 225 expose :published
218 226 expose :accept_comments?, as: :accept_comments
219 227 end
... ...
app/api/helpers.rb
... ... @@ -46,9 +46,11 @@ module Api
46 46 def present_partial(model, options)
47 47 if(params[:fields].present?)
48 48 begin
49   - fields = JSON.parse(params[:fields])
  49 + fields = JSON.parse((params.to_hash[:fields] || params.to_hash['fields']).to_json)
50 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 54 end
53 55 rescue
54 56 fields = params[:fields]
... ...
app/api/v1/communities.rb
... ... @@ -18,7 +18,7 @@ module Api
18 18 communities = select_filtered_collection_of(environment, 'communities', params)
19 19 communities = profiles_for_person(communities, current_person)
20 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 22 end
23 23  
24 24  
... ... @@ -49,7 +49,7 @@ module Api
49 49  
50 50 get ':id' do
51 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 53 end
54 54  
55 55 end
... ...
test/api/articles_test.rb
... ... @@ -773,12 +773,12 @@ class ArticlesTest &lt; ActiveSupport::TestCase
773 773 end
774 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 777 person = fast_create(Person)
778 778 article = fast_create(Article, :profile_id => person.id, :name => "Some thing")
779 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 782 json = JSON.parse(last_response.body)
783 783 assert_includes json["article"].keys, "comments"
784 784 assert_equal json["article"]["comments"].first["body"], "another comment"
... ... @@ -805,4 +805,24 @@ class ArticlesTest &lt; ActiveSupport::TestCase
805 805 json = JSON.parse(last_response.body)
806 806 assert_includes json["article"]["permissions"], 'allow_post_content'
807 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 828 end
... ...
test/api/communities_test.rb
... ... @@ -100,7 +100,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
100 100 get "/api/v1/communities/#{community.id}?#{params.to_query}"
101 101 json = JSON.parse(last_response.body)
102 102 assert_equal community.id, json['community']['id']
103   - assert_nil json['community']['members']
  103 + assert_nil json['community']['admins']
104 104 end
105 105  
106 106 should 'get private community to logged member' do
... ... @@ -108,6 +108,7 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
108 108 community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true)
109 109 community.add_member(person)
110 110  
  111 + params[:optional_fields] = ['members']
111 112 get "/api/v1/communities/#{community.id}?#{params.to_query}"
112 113 json = JSON.parse(last_response.body)
113 114 assert_equal community.id, json['community']['id']
... ... @@ -333,4 +334,51 @@ class CommunitiesTest &lt; ActiveSupport::TestCase
333 334 refute json['community']['additional_data'].has_key?('Rating')
334 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 384 end
... ...
test/api/helpers_test.rb
... ... @@ -233,7 +233,7 @@ class Api::HelpersTest &lt; ActiveSupport::TestCase
233 233  
234 234 should 'accept json as fields parameter when calling present partial' do
235 235 model = mock
236   - params[:fields] = {only: [:name, {user: [:login]}]}.to_json
  236 + params[:fields] = {only: [:name, {user: [:login]}]}
237 237 expects(:present).with(model, {:only => ['name', {'user' => ['login']}]})
238 238 present_partial(model, {})
239 239 end
... ...
test/api/people_test.rb
... ... @@ -119,8 +119,8 @@ class PeopleTest &lt; ActiveSupport::TestCase
119 119  
120 120 should 'people endpoint filter by fields parameter with hierarchy for logged user' do
121 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 124 json = JSON.parse(last_response.body)
125 125 expected = {'people' => [{'name' => person.name, 'user' => {'login' => 'testapi'}}]}
126 126 assert_equal expected, json
... ...