diff --git a/app/api/entities.rb b/app/api/entities.rb index eb12e3f..f945629 100644 --- a/app/api/entities.rb +++ b/app/api/entities.rb @@ -38,6 +38,13 @@ module Api PERMISSIONS[current_permission] <= PERMISSIONS[permission] end + def self.expose_optional_field?(field, options = {}) + return false if options[:params].nil? + optional_fields = options[:params][:optional_fields] || [] + optional_fields.include?(field.to_s) + end + + class Image < Entity root 'images', 'image' @@ -166,7 +173,8 @@ module Api community.admins.map{|admin| {"name"=>admin.name, "id"=>admin.id, "username" => admin.identifier}} end expose :categories, :using => Category - expose :members, :using => Person , :if => lambda{ |community, options| community.display_info_to? options[:current_person] } + expose :members_count + expose :members, :if => lambda {|community, options| Entities.expose_optional_field?(:members, options)} end class CommentBase < Entity @@ -213,7 +221,7 @@ module Api expose :comments_count expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"} expose :type - expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])} + expose :comments, using: CommentBase, :if => lambda{|comment,options| Entities.expose_optional_field?(:comments, options)} expose :published expose :accept_comments?, as: :accept_comments end diff --git a/app/api/helpers.rb b/app/api/helpers.rb index 946c05d..bfc91c5 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -46,9 +46,11 @@ module Api def present_partial(model, options) if(params[:fields].present?) begin - fields = JSON.parse(params[:fields]) + fields = JSON.parse((params.to_hash[:fields] || params.to_hash['fields']).to_json) if fields.present? - options.merge!(fields.symbolize_keys.slice(:only, :except)) + fields = fields.symbolize_keys + options.merge!(:only => fields[:only]) if fields[:only].present? + options.merge!(:except => fields[:except]) if fields[:except].present? end rescue fields = params[:fields] diff --git a/app/api/v1/communities.rb b/app/api/v1/communities.rb index 0dcb9a0..3f78967 100644 --- a/app/api/v1/communities.rb +++ b/app/api/v1/communities.rb @@ -18,7 +18,7 @@ module Api communities = select_filtered_collection_of(environment, 'communities', params) communities = profiles_for_person(communities, current_person) communities = communities.by_location(params) # Must be the last. May return Exception obj - present communities, :with => Entities::Community, :current_person => current_person + present communities, :with => Entities::Community, :current_person => current_person, :params => params end @@ -49,7 +49,7 @@ module Api get ':id' do community = profiles_for_person(environment.communities, current_person).find_by_id(params[:id]) - present community, :with => Entities::Community, :current_person => current_person + present community, :with => Entities::Community, :current_person => current_person, :params => params end end diff --git a/test/api/articles_test.rb b/test/api/articles_test.rb index 348b929..7f2dd0f 100644 --- a/test/api/articles_test.rb +++ b/test/api/articles_test.rb @@ -773,12 +773,12 @@ class ArticlesTest < ActiveSupport::TestCase end end - should 'only show article comments when show_comments is present' do + should 'only show article comments when optional_fields comments is present' do person = fast_create(Person) article = fast_create(Article, :profile_id => person.id, :name => "Some thing") article.comments.create!(:body => "another comment", :author => person) - get "/api/v1/articles/#{article.id}/?#{params.merge(:show_comments => '1').to_query}" + get "/api/v1/articles/#{article.id}/?#{params.merge(:optional_fields => [:comments]).to_query}" json = JSON.parse(last_response.body) assert_includes json["article"].keys, "comments" assert_equal json["article"]["comments"].first["body"], "another comment" @@ -805,4 +805,24 @@ class ArticlesTest < ActiveSupport::TestCase json = JSON.parse(last_response.body) assert_includes json["article"]["permissions"], 'allow_post_content' end + + should 'return only article fields defined in parameter' do + Article.destroy_all + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + params[:fields] = {:only => ['id', 'title']} + get "/api/v1/articles/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equivalent ['id', 'title'], json["articles"].first.keys + end + + should 'return all article fields except the ones defined in parameter' do + Article.destroy_all + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + params[:fields] = {:except => ['id', 'title']} + get "/api/v1/articles/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_includes json["articles"].first.keys, 'id' + assert_not_includes json["articles"].first.keys, 'title' + end + end diff --git a/test/api/communities_test.rb b/test/api/communities_test.rb index c502dc1..2cc7003 100644 --- a/test/api/communities_test.rb +++ b/test/api/communities_test.rb @@ -100,7 +100,7 @@ class CommunitiesTest < ActiveSupport::TestCase get "/api/v1/communities/#{community.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal community.id, json['community']['id'] - assert_nil json['community']['members'] + assert_nil json['community']['admins'] end should 'get private community to logged member' do @@ -108,6 +108,7 @@ class CommunitiesTest < ActiveSupport::TestCase community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true) community.add_member(person) + params[:optional_fields] = ['members'] get "/api/v1/communities/#{community.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal community.id, json['community']['id'] @@ -333,4 +334,51 @@ class CommunitiesTest < ActiveSupport::TestCase refute json['community']['additional_data'].has_key?('Rating') end + should 'not display members value by default' do + login_api + community = fast_create(Community, :environment_id => environment.id, :public_profile => false, :visible => true) + community.add_member(person) + + get "/api/v1/communities/#{community.id}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal community.id, json['community']['id'] + assert_nil json['community']['members'] + end + + should 'display members values if optional field parameter is passed' do + community = fast_create(Community, :environment_id => environment.id) + + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => [:members]}).to_query}" + json = JSON.parse(last_response.body) + assert_equal community.id, json['community']['id'] + assert_not_nil json['community']['members'] + end + + should 'display members values if optional_fields has members value as string in array' do + community = fast_create(Community, :environment_id => environment.id) + + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => ['members']}).to_query}" + json = JSON.parse(last_response.body) + assert_equal community.id, json['community']['id'] + assert_not_nil json['community']['members'] + end + + should 'display members values if optional_fields has members value in array' do + community = fast_create(Community, :environment_id => environment.id) + + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => ['members', 'another']}).to_query}" + json = JSON.parse(last_response.body) + assert_equal community.id, json['community']['id'] + assert_not_nil json['community']['members'] + end + + should 'display members values if optional_fields has members value as string' do + community = fast_create(Community, :environment_id => environment.id) + + get "/api/v1/communities/#{community.id}?#{params.merge({:optional_fields => 'members'}).to_query}" + json = JSON.parse(last_response.body) + assert_equal community.id, json['community']['id'] + assert_not_nil json['community']['members'] + end + end diff --git a/test/api/helpers_test.rb b/test/api/helpers_test.rb index 191b95f..2408805 100644 --- a/test/api/helpers_test.rb +++ b/test/api/helpers_test.rb @@ -233,7 +233,7 @@ class Api::HelpersTest < ActiveSupport::TestCase should 'accept json as fields parameter when calling present partial' do model = mock - params[:fields] = {only: [:name, {user: [:login]}]}.to_json + params[:fields] = {only: [:name, {user: [:login]}]} expects(:present).with(model, {:only => ['name', {'user' => ['login']}]}) present_partial(model, {}) end diff --git a/test/api/people_test.rb b/test/api/people_test.rb index a2cd273..7ac1cb2 100644 --- a/test/api/people_test.rb +++ b/test/api/people_test.rb @@ -119,8 +119,8 @@ class PeopleTest < ActiveSupport::TestCase should 'people endpoint filter by fields parameter with hierarchy for logged user' do login_api - fields = URI.encode({only: [:name, {user: [:login]}]}.to_json.to_str) - get "/api/v1/people?#{params.to_query}&fields=#{fields}" + params[:fields] = {only: [:name, {user: [:login]}]} + get "/api/v1/people?#{params.to_query}" json = JSON.parse(last_response.body) expected = {'people' => [{'name' => person.name, 'user' => {'login' => 'testapi'}}]} assert_equal expected, json -- libgit2 0.21.2