From ea0ab128b54a72e7f56265cb7a00fee4f0b7a096 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 15 Oct 2013 18:26:24 -0300 Subject: [PATCH] Profile#may_display_field_to: rewrite tests --- app/helpers/profile_helper.rb | 2 +- test/unit/profile_helper_test.rb | 86 +++++++++++++++++++++++++------------------------------------------------------------- test/unit/profile_test.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 62 deletions(-) diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb index 5dd5c80..a508c75 100644 --- a/app/helpers/profile_helper.rb +++ b/app/helpers/profile_helper.rb @@ -1,7 +1,7 @@ module ProfileHelper def display_field(title, profile, field, force = false) - if not force or not profile.may_display_field_to? field, user + unless force || profile.may_display_field_to?(field, user) return '' end value = profile.send(field) diff --git a/test/unit/profile_helper_test.rb b/test/unit/profile_helper_test.rb index c280c46..6678e39 100644 --- a/test/unit/profile_helper_test.rb +++ b/test/unit/profile_helper_test.rb @@ -13,84 +13,48 @@ class ProfileHelperTest < ActiveSupport::TestCase end attr_reader :profile, :helper - should 'not display field if field is not active and not forced' do - profile.expects(:active_fields).returns([]) - assert_equal '', display_field('Title', profile, 'field') - end - - should 'display field if field is not active but is forced' do - profile.expects(:active_fields).returns([]) + should 'display field if may display it' do + self.stubs(:user).returns(nil) + profile.expects(:may_display_field_to?).returns(true) profile.expects(:field).returns('value') - assert_match /Title.*value/, display_field('Title', profile, 'field', true) + assert_match /Title.*value/, display_field('Title', profile, 'field') end - should 'not display field if field is active but not public and not logged in' do - profile.stubs(:active_fields).returns(['field']) - profile.expects(:public_fields).returns([]) - @controller = mock - @controller.stubs(:user).returns(nil) + should 'not display field if may not display it and not forced' do + self.stubs(:user).returns(nil) + profile.expects(:may_display_field_to?).returns(false) assert_equal '', display_field('Title', profile, 'field') end - should 'not display field if field is active but not public and user is not friend' do - profile.stubs(:active_fields).returns(['field']) - profile.expects(:public_fields).returns([]) - user = mock - user.expects(:is_a_friend?).with(profile).returns(false) - @controller = mock - @controller.stubs(:user).returns(user) - assert_equal '', display_field('Title', profile, 'field') - end - - should 'display field if field is active and not public but user is profile owner' do - profile.stubs(:active_fields).returns(['field']) - profile.expects(:public_fields).returns([]) + should 'display field if may not display it but is forced' do + self.stubs(:user).returns(nil) + profile.stubs(:may_display_field_to?).returns(false) profile.expects(:field).returns('value') - @controller = mock - @controller.stubs(:user).returns(profile) assert_match /Title.*value/, display_field('Title', profile, 'field', true) end - should 'display field if field is active and not public but user is a friend' do - profile.stubs(:active_fields).returns(['field']) - profile.expects(:public_fields).returns([]) - profile.expects(:field).returns('value') - user = mock - user.expects(:is_a_friend?).with(profile).returns(true) - @controller = mock - @controller.stubs(:user).returns(user) - assert_match /Title.*value/, display_field('Title', profile, 'field', true) + should 'display work info if at least one of the fields should be displayed' do + self.stubs(:user).returns(nil) + profile.stubs(:may_display_field_to?).with(:organization, nil).returns(true) + profile.stubs(:may_display_field_to?).with(:organization_website, nil).returns(false) + profile.expects(:organization).returns('Organization Name') + profile.expects(:organization_website).never + assert_match /Work.*Organization Name/, display_work_info(profile) end - should 'not display work info if field is active but not public and user is not friend' do - profile.stubs(:active_fields).returns(['organization', 'organization_website']) - profile.expects(:public_fields).returns([]).times(2) - user = mock - user.expects(:is_a_friend?).with(profile).returns(false).times(2) - @controller = mock - @controller.stubs(:user).returns(user) + should 'not display work info if none of the fields should be displayed' do + self.stubs(:user).returns(nil) + profile.stubs(:may_display_field_to?).returns(false) + profile.expects(:organization).never + profile.expects(:organization_website).never assert_equal '', display_work_info(profile) end - should 'display work info if field is active and not public but user is profile owner' do - profile.stubs(:active_fields).returns(['organization', 'organization_website']) - profile.expects(:public_fields).returns([]).times(2) - profile.expects(:organization).returns('Organization Name') - profile.expects(:organization_website).returns('') - @controller = mock - @controller.stubs(:user).returns(profile) - assert_match /Work.*Organization Name/, display_work_info(profile) - end - - should 'display work info if field is active and not public but user is a friend' do - profile.stubs(:active_fields).returns(['organization', 'organization_website']) - profile.expects(:public_fields).returns([]).times(2) + should 'display work info if both fields should be displayed' do + self.stubs(:user).returns(nil) + profile.stubs(:may_display_field_to?).returns(true) profile.expects(:organization).returns('Organization Name') profile.expects(:organization_website).returns('') - user = mock - user.expects(:is_a_friend?).with(profile).returns(true).times(2) - @controller = mock - @controller.stubs(:user).returns(user) assert_match /Work.*Organization Name/, display_work_info(profile) end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 72215f8..5da1312 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1833,4 +1833,59 @@ class ProfileTest < ActiveSupport::TestCase assert_equal f, p.fields_privacy end + should 'not display field if field is active but not public and user not logged in' do + profile = fast_create(Profile) + profile.stubs(:active_fields).returns(['field']) + profile.stubs(:public_fields).returns([]) + assert !profile.may_display_field_to?('field', nil) + end + + should 'not display field if field is active but not public and user is not friend' do + profile = fast_create(Profile) + profile.stubs(:active_fields).returns(['field']) + profile.expects(:public_fields).returns([]) + user = mock + user.expects(:is_a_friend?).with(profile).returns(false) + assert !profile.may_display_field_to?('field', user) + end + + should 'display field if field is active and not public but user is profile owner' do + user = profile = fast_create(Profile) + profile.stubs(:active_fields).returns(['field']) + profile.expects(:public_fields).returns([]) + assert profile.may_display_field_to?('field', user) + end + + should 'display field if field is active and not public but user is a friend' do + profile = fast_create(Profile) + profile.stubs(:active_fields).returns(['field']) + profile.expects(:public_fields).returns([]) + user = mock + user.expects(:is_a_friend?).with(profile).returns(true) + assert profile.may_display_field_to?('field', user) + end + + should 'call may_display on field name if the field is not active' do + user = fast_create(Person) + profile = fast_create(Profile) + profile.stubs(:active_fields).returns(['humble']) + profile.expects(:may_display_humble_to?).never + profile.expects(:may_display_bundle_to?).once + + profile.may_display_field_to?('humble', user) + profile.may_display_field_to?('bundle', user) + end + + # TODO Eventually we would like to specify it in a deeper granularity... + should 'not display location if any field is private' do + user = fast_create(Person) + profile = fast_create(Profile) + profile.stubs(:active_fields).returns(Profile::LOCATION_FIELDS) + Profile::LOCATION_FIELDS.each { |field| profile.stubs(:may_display_field_to?).with(field, user).returns(true)} + assert profile.may_display_location_to?(user) + + profile.stubs(:may_display_field_to?).with(Profile::LOCATION_FIELDS[0], user).returns(false) + assert !profile.may_display_location_to?(user) + end + end -- libgit2 0.21.2