Commit ea0ab128b54a72e7f56265cb7a00fee4f0b7a096

Authored by Rodrigo Souto
1 parent ab0be61d

Profile#may_display_field_to: rewrite tests

   * Adding tests for the new location method
   * Moving tests from profile_helper_test to profile_test
   * Rewriting tests of profile_helper_test
app/helpers/profile_helper.rb
1 module ProfileHelper 1 module ProfileHelper
2 2
3 def display_field(title, profile, field, force = false) 3 def display_field(title, profile, field, force = false)
4 - if not force or not profile.may_display_field_to? field, user 4 + unless force || profile.may_display_field_to?(field, user)
5 return '' 5 return ''
6 end 6 end
7 value = profile.send(field) 7 value = profile.send(field)
test/unit/profile_helper_test.rb
@@ -13,84 +13,48 @@ class ProfileHelperTest < ActiveSupport::TestCase @@ -13,84 +13,48 @@ class ProfileHelperTest < ActiveSupport::TestCase
13 end 13 end
14 attr_reader :profile, :helper 14 attr_reader :profile, :helper
15 15
16 - should 'not display field if field is not active and not forced' do  
17 - profile.expects(:active_fields).returns([])  
18 - assert_equal '', display_field('Title', profile, 'field')  
19 - end  
20 -  
21 - should 'display field if field is not active but is forced' do  
22 - profile.expects(:active_fields).returns([]) 16 + should 'display field if may display it' do
  17 + self.stubs(:user).returns(nil)
  18 + profile.expects(:may_display_field_to?).returns(true)
23 profile.expects(:field).returns('value') 19 profile.expects(:field).returns('value')
24 - assert_match /Title.*value/, display_field('Title', profile, 'field', true) 20 + assert_match /Title.*value/, display_field('Title', profile, 'field')
25 end 21 end
26 22
27 - should 'not display field if field is active but not public and not logged in' do  
28 - profile.stubs(:active_fields).returns(['field'])  
29 - profile.expects(:public_fields).returns([])  
30 - @controller = mock  
31 - @controller.stubs(:user).returns(nil) 23 + should 'not display field if may not display it and not forced' do
  24 + self.stubs(:user).returns(nil)
  25 + profile.expects(:may_display_field_to?).returns(false)
32 assert_equal '', display_field('Title', profile, 'field') 26 assert_equal '', display_field('Title', profile, 'field')
33 end 27 end
34 28
35 - should 'not display field if field is active but not public and user is not friend' do  
36 - profile.stubs(:active_fields).returns(['field'])  
37 - profile.expects(:public_fields).returns([])  
38 - user = mock  
39 - user.expects(:is_a_friend?).with(profile).returns(false)  
40 - @controller = mock  
41 - @controller.stubs(:user).returns(user)  
42 - assert_equal '', display_field('Title', profile, 'field')  
43 - end  
44 -  
45 - should 'display field if field is active and not public but user is profile owner' do  
46 - profile.stubs(:active_fields).returns(['field'])  
47 - profile.expects(:public_fields).returns([]) 29 + should 'display field if may not display it but is forced' do
  30 + self.stubs(:user).returns(nil)
  31 + profile.stubs(:may_display_field_to?).returns(false)
48 profile.expects(:field).returns('value') 32 profile.expects(:field).returns('value')
49 - @controller = mock  
50 - @controller.stubs(:user).returns(profile)  
51 assert_match /Title.*value/, display_field('Title', profile, 'field', true) 33 assert_match /Title.*value/, display_field('Title', profile, 'field', true)
52 end 34 end
53 35
54 - should 'display field if field is active and not public but user is a friend' do  
55 - profile.stubs(:active_fields).returns(['field'])  
56 - profile.expects(:public_fields).returns([])  
57 - profile.expects(:field).returns('value')  
58 - user = mock  
59 - user.expects(:is_a_friend?).with(profile).returns(true)  
60 - @controller = mock  
61 - @controller.stubs(:user).returns(user)  
62 - assert_match /Title.*value/, display_field('Title', profile, 'field', true) 36 + should 'display work info if at least one of the fields should be displayed' do
  37 + self.stubs(:user).returns(nil)
  38 + profile.stubs(:may_display_field_to?).with(:organization, nil).returns(true)
  39 + profile.stubs(:may_display_field_to?).with(:organization_website, nil).returns(false)
  40 + profile.expects(:organization).returns('Organization Name')
  41 + profile.expects(:organization_website).never
  42 + assert_match /Work.*Organization Name/, display_work_info(profile)
63 end 43 end
64 44
65 - should 'not display work info if field is active but not public and user is not friend' do  
66 - profile.stubs(:active_fields).returns(['organization', 'organization_website'])  
67 - profile.expects(:public_fields).returns([]).times(2)  
68 - user = mock  
69 - user.expects(:is_a_friend?).with(profile).returns(false).times(2)  
70 - @controller = mock  
71 - @controller.stubs(:user).returns(user) 45 + should 'not display work info if none of the fields should be displayed' do
  46 + self.stubs(:user).returns(nil)
  47 + profile.stubs(:may_display_field_to?).returns(false)
  48 + profile.expects(:organization).never
  49 + profile.expects(:organization_website).never
72 assert_equal '', display_work_info(profile) 50 assert_equal '', display_work_info(profile)
73 end 51 end
74 52
75 - should 'display work info if field is active and not public but user is profile owner' do  
76 - profile.stubs(:active_fields).returns(['organization', 'organization_website'])  
77 - profile.expects(:public_fields).returns([]).times(2)  
78 - profile.expects(:organization).returns('Organization Name')  
79 - profile.expects(:organization_website).returns('')  
80 - @controller = mock  
81 - @controller.stubs(:user).returns(profile)  
82 - assert_match /Work.*Organization Name/, display_work_info(profile)  
83 - end  
84 -  
85 - should 'display work info if field is active and not public but user is a friend' do  
86 - profile.stubs(:active_fields).returns(['organization', 'organization_website'])  
87 - profile.expects(:public_fields).returns([]).times(2) 53 + should 'display work info if both fields should be displayed' do
  54 + self.stubs(:user).returns(nil)
  55 + profile.stubs(:may_display_field_to?).returns(true)
88 profile.expects(:organization).returns('Organization Name') 56 profile.expects(:organization).returns('Organization Name')
89 profile.expects(:organization_website).returns('') 57 profile.expects(:organization_website).returns('')
90 - user = mock  
91 - user.expects(:is_a_friend?).with(profile).returns(true).times(2)  
92 - @controller = mock  
93 - @controller.stubs(:user).returns(user)  
94 assert_match /Work.*Organization Name/, display_work_info(profile) 58 assert_match /Work.*Organization Name/, display_work_info(profile)
95 end 59 end
96 60
test/unit/profile_test.rb
@@ -1833,4 +1833,59 @@ class ProfileTest < ActiveSupport::TestCase @@ -1833,4 +1833,59 @@ class ProfileTest < ActiveSupport::TestCase
1833 assert_equal f, p.fields_privacy 1833 assert_equal f, p.fields_privacy
1834 end 1834 end
1835 1835
  1836 + should 'not display field if field is active but not public and user not logged in' do
  1837 + profile = fast_create(Profile)
  1838 + profile.stubs(:active_fields).returns(['field'])
  1839 + profile.stubs(:public_fields).returns([])
  1840 + assert !profile.may_display_field_to?('field', nil)
  1841 + end
  1842 +
  1843 + should 'not display field if field is active but not public and user is not friend' do
  1844 + profile = fast_create(Profile)
  1845 + profile.stubs(:active_fields).returns(['field'])
  1846 + profile.expects(:public_fields).returns([])
  1847 + user = mock
  1848 + user.expects(:is_a_friend?).with(profile).returns(false)
  1849 + assert !profile.may_display_field_to?('field', user)
  1850 + end
  1851 +
  1852 + should 'display field if field is active and not public but user is profile owner' do
  1853 + user = profile = fast_create(Profile)
  1854 + profile.stubs(:active_fields).returns(['field'])
  1855 + profile.expects(:public_fields).returns([])
  1856 + assert profile.may_display_field_to?('field', user)
  1857 + end
  1858 +
  1859 + should 'display field if field is active and not public but user is a friend' do
  1860 + profile = fast_create(Profile)
  1861 + profile.stubs(:active_fields).returns(['field'])
  1862 + profile.expects(:public_fields).returns([])
  1863 + user = mock
  1864 + user.expects(:is_a_friend?).with(profile).returns(true)
  1865 + assert profile.may_display_field_to?('field', user)
  1866 + end
  1867 +
  1868 + should 'call may_display on field name if the field is not active' do
  1869 + user = fast_create(Person)
  1870 + profile = fast_create(Profile)
  1871 + profile.stubs(:active_fields).returns(['humble'])
  1872 + profile.expects(:may_display_humble_to?).never
  1873 + profile.expects(:may_display_bundle_to?).once
  1874 +
  1875 + profile.may_display_field_to?('humble', user)
  1876 + profile.may_display_field_to?('bundle', user)
  1877 + end
  1878 +
  1879 + # TODO Eventually we would like to specify it in a deeper granularity...
  1880 + should 'not display location if any field is private' do
  1881 + user = fast_create(Person)
  1882 + profile = fast_create(Profile)
  1883 + profile.stubs(:active_fields).returns(Profile::LOCATION_FIELDS)
  1884 + Profile::LOCATION_FIELDS.each { |field| profile.stubs(:may_display_field_to?).with(field, user).returns(true)}
  1885 + assert profile.may_display_location_to?(user)
  1886 +
  1887 + profile.stubs(:may_display_field_to?).with(Profile::LOCATION_FIELDS[0], user).returns(false)
  1888 + assert !profile.may_display_location_to?(user)
  1889 + end
  1890 +
1836 end 1891 end