Commit 4c3176e0a7be978c9c826bec2bf83832f31d0736
1 parent
56cc28dc
Exists in
profile_api_improvements
and in
1 other branch
Adds public fields to Profile API
Signed-off-by: Paulo Tada <paulohtfs@gmail.com> Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
Showing
2 changed files
with
22 additions
and
0 deletions
Show diff stats
app/api/entities.rb
| @@ -111,6 +111,10 @@ module Api | @@ -111,6 +111,10 @@ module Api | ||
| 111 | hash[value.custom_field.name]=value.value | 111 | hash[value.custom_field.name]=value.value |
| 112 | end | 112 | end |
| 113 | 113 | ||
| 114 | + profile.public_fields.each do |field| | ||
| 115 | + hash[field] = profile.send(field.to_sym) | ||
| 116 | + end | ||
| 117 | + | ||
| 114 | private_values = profile.custom_field_values - profile.public_values | 118 | private_values = profile.custom_field_values - profile.public_values |
| 115 | private_values.each do |value| | 119 | private_values.each do |value| |
| 116 | if Entities.can_display_profile_field?(profile,options) | 120 | if Entities.can_display_profile_field?(profile,options) |
test/api/profiles_test.rb
| @@ -123,6 +123,24 @@ class ProfilesTest < ActiveSupport::TestCase | @@ -123,6 +123,24 @@ class ProfilesTest < ActiveSupport::TestCase | ||
| 123 | assert_equal community.id, json['id'] | 123 | assert_equal community.id, json['id'] |
| 124 | end | 124 | end |
| 125 | 125 | ||
| 126 | + should 'display profile public fields to anonymous' do | ||
| 127 | + some_person = create_user('test', { :email => "lappis@unb.br" }).person | ||
| 128 | + Person.any_instance.stubs(:public_fields).returns(["email"]) | ||
| 129 | + | ||
| 130 | + get "/api/v1/profiles/#{some_person.id}?#{params.to_query}" | ||
| 131 | + json = JSON.parse(last_response.body) | ||
| 132 | + assert json['additional_data'].has_key?('email') | ||
| 133 | + assert_equal "lappis@unb.br", json['additional_data']['email'] | ||
| 134 | + end | ||
| 135 | + | ||
| 136 | + should 'not display private fields to anonymous' do | ||
| 137 | + some_person = create_user('test', { :email => "lappis@unb.br" }).person | ||
| 138 | + | ||
| 139 | + get "/api/v1/profiles/#{some_person.id}/?#{params.to_query}" | ||
| 140 | + json = JSON.parse(last_response.body) | ||
| 141 | + assert !json['additional_data'].has_key?('email') | ||
| 142 | + end | ||
| 143 | + | ||
| 126 | should 'display public custom fields to anonymous' do | 144 | should 'display public custom fields to anonymous' do |
| 127 | CustomField.create!(:name => "Rating", :format => "string", :customized_type => "Profile", :active => true, :environment => Environment.default) | 145 | CustomField.create!(:name => "Rating", :format => "string", :customized_type => "Profile", :active => true, :environment => Environment.default) |
| 128 | some_profile = fast_create(Profile) | 146 | some_profile = fast_create(Profile) |