Commit fb7c807f7cc4d231bac5acfaf4f2de8f130e7386
1 parent
2937a8f3
Tests Follower concern
Signed-off-by: Dylan Guedes <djmgguedes@gmail.com> Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Showing
6 changed files
with
202 additions
and
139 deletions
Show diff stats
app/models/concerns/follower.rb
@@ -4,6 +4,7 @@ module Follower | @@ -4,6 +4,7 @@ module Follower | ||
4 | def follow(profile, circles) | 4 | def follow(profile, circles) |
5 | circles = [circles] unless circles.is_a?(Array) | 5 | circles = [circles] unless circles.is_a?(Array) |
6 | circles.each do |new_circle| | 6 | circles.each do |new_circle| |
7 | + next if new_circle.person != self || new_circle.profile_type != profile.class.name | ||
7 | ProfileFollower.create(profile: profile, circle: new_circle) | 8 | ProfileFollower.create(profile: profile, circle: new_circle) |
8 | end | 9 | end |
9 | end | 10 | end |
@@ -24,16 +25,15 @@ module Follower | @@ -24,16 +25,15 @@ module Follower | ||
24 | def update_profile_circles(profile, new_circles) | 25 | def update_profile_circles(profile, new_circles) |
25 | profile_circles = ProfileFollower.with_profile(profile).with_follower(self).map(&:circle) | 26 | profile_circles = ProfileFollower.with_profile(profile).with_follower(self).map(&:circle) |
26 | circles_to_add = new_circles - profile_circles | 27 | circles_to_add = new_circles - profile_circles |
27 | - circles_to_remove = profile_circles - new_circles | ||
28 | - circles_to_add.each do |new_circle| | ||
29 | - ProfileFollower.create(profile: profile, circle: new_circle) | ||
30 | - end | 28 | + self.follow(profile, circles_to_add) |
31 | 29 | ||
30 | + circles_to_remove = profile_circles - new_circles | ||
32 | ProfileFollower.where('circle_id IN (?) AND profile_id = ?', | 31 | ProfileFollower.where('circle_id IN (?) AND profile_id = ?', |
33 | circles_to_remove.map(&:id), profile.id).destroy_all | 32 | circles_to_remove.map(&:id), profile.id).destroy_all |
34 | end | 33 | end |
35 | 34 | ||
36 | def remove_profile_from_circle(profile, circle) | 35 | def remove_profile_from_circle(profile, circle) |
36 | + return if circle.person != self | ||
37 | ProfileFollower.with_profile(profile).with_circle(circle).destroy_all | 37 | ProfileFollower.with_profile(profile).with_circle(circle).destroy_all |
38 | end | 38 | end |
39 | 39 |
app/models/profile.rb
@@ -249,11 +249,6 @@ class Profile < ApplicationRecord | @@ -249,11 +249,6 @@ class Profile < ApplicationRecord | ||
249 | has_many :email_templates, :foreign_key => :owner_id | 249 | has_many :email_templates, :foreign_key => :owner_id |
250 | 250 | ||
251 | has_many :profile_followers | 251 | has_many :profile_followers |
252 | - scope :memberships_of, -> person { | ||
253 | - distinct.select('profiles.*'). | ||
254 | - joins(:role_assignments). | ||
255 | - where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id) | ||
256 | - } | ||
257 | 252 | ||
258 | def in_circles | 253 | def in_circles |
259 | Circle.joins(:profile_followers). | 254 | Circle.joins(:profile_followers). |
@@ -261,10 +256,10 @@ class Profile < ApplicationRecord | @@ -261,10 +256,10 @@ class Profile < ApplicationRecord | ||
261 | end | 256 | end |
262 | 257 | ||
263 | def followers | 258 | def followers |
264 | - person_followers = Person.joins(:circles).merge(in_circles) | ||
265 | - external_person_followers = ExternalPerson.joins(:circles).merge(in_circles) | 259 | + person_followers = Person.joins(:circles).merge(in_circles).uniq |
260 | + external_person_followers = ExternalPerson.joins(:circles).merge(in_circles).uniq | ||
266 | 261 | ||
267 | - person_followers+external_person_followers | 262 | + person_followers + external_person_followers |
268 | end | 263 | end |
269 | 264 | ||
270 | # has_many :followers, -> { uniq }, :through => :profile_followers, :source => :person | 265 | # has_many :followers, -> { uniq }, :through => :profile_followers, :source => :person |
@@ -0,0 +1,152 @@ | @@ -0,0 +1,152 @@ | ||
1 | +require_relative "../test_helper" | ||
2 | + | ||
3 | +class FollowerTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + def setup | ||
6 | + @person1 = create_user('perso-test-1').person | ||
7 | + @person2 = create_user('person-test-2').person | ||
8 | + | ||
9 | + @circle1 = Circle.create!(person: @person1, name: "Zombies", profile_type: 'Person') | ||
10 | + @circle2 = Circle.create!(person: @person1, name: "Humans", profile_type: 'Person') | ||
11 | + | ||
12 | + @external_person = ExternalPerson.create!(identifier: 'johnlocke', | ||
13 | + name: 'John Locke', | ||
14 | + source: 'anerenvironment.org', | ||
15 | + email: 'locke@island.org', | ||
16 | + created_at: Date.yesterday) | ||
17 | + end | ||
18 | + | ||
19 | + should 'follows? return false when no profile is passed as parameter' do | ||
20 | + assert_equal false, @person1.follows?(nil) | ||
21 | + end | ||
22 | + | ||
23 | + should 'follow person with regular user' do | ||
24 | + assert_difference '@person1.followed_profiles.count' do | ||
25 | + @person1.follow(@person2, @circle1) | ||
26 | + end | ||
27 | + assert @person1.follows?(@person2) | ||
28 | + end | ||
29 | + | ||
30 | + should 'follow a community' do | ||
31 | + community = fast_create(Community) | ||
32 | + circle = Circle.create!(person: @person1, name: "Terminus", profile_type: 'Community') | ||
33 | + | ||
34 | + assert_difference '@person1.followed_profiles.count' do | ||
35 | + @person1.follow(community, circle) | ||
36 | + end | ||
37 | + assert @person1.follows?(community) | ||
38 | + end | ||
39 | + | ||
40 | + should 'not follow person if the user is not the ownner of the circle' do | ||
41 | + person3 = create_user('perso-test-3').person | ||
42 | + | ||
43 | + assert_no_difference '@circle1.person.followed_profiles.count' do | ||
44 | + person3.follow(@person2, @circle1) | ||
45 | + end | ||
46 | + assert_not @circle1.person.follows?(@person2) | ||
47 | + end | ||
48 | + | ||
49 | + should 'not follow a community if circle profile type does not match' do | ||
50 | + community = fast_create(Community) | ||
51 | + | ||
52 | + assert_no_difference '@person1.followed_profiles.count' do | ||
53 | + @person1.follow(community, @circle1) | ||
54 | + end | ||
55 | + assert_not @person1.follows?(community) | ||
56 | + end | ||
57 | + | ||
58 | + should 'follow person with external user' do | ||
59 | + @circle1.update_attributes(person: @external_person) | ||
60 | + | ||
61 | + assert_difference '@external_person.followed_profiles.count' do | ||
62 | + @external_person.follow(@person2, @circle1) | ||
63 | + end | ||
64 | + assert @external_person.follows?(@person2) | ||
65 | + end | ||
66 | + | ||
67 | + should 'unfollow person with regular user' do | ||
68 | + @person1.follow(@person2, @circle1) | ||
69 | + | ||
70 | + assert_difference '@person1.followed_profiles.count', -1 do | ||
71 | + @person1.unfollow(@person2) | ||
72 | + end | ||
73 | + assert_not @person1.follows?(@person2) | ||
74 | + end | ||
75 | + | ||
76 | + should 'unfollow person with external user' do | ||
77 | + @circle1.update_attributes(person: @external_person) | ||
78 | + @external_person.follow(@person2, @circle1) | ||
79 | + | ||
80 | + assert_difference '@external_person.followed_profiles.count', -1 do | ||
81 | + @external_person.unfollow(@person2) | ||
82 | + end | ||
83 | + assert_not @external_person.follows?(@person2) | ||
84 | + end | ||
85 | + | ||
86 | + should 'get the followed profiles for a regular user' do | ||
87 | + person3 = create_user('person-test-3').person | ||
88 | + | ||
89 | + @person1.follow(@person2, @circle1) | ||
90 | + @person1.follow(person3, @circle1) | ||
91 | + assert_equivalent [@person2, person3], @person1.followed_profiles | ||
92 | + end | ||
93 | + | ||
94 | + should 'get the followed profiles for an external user' do | ||
95 | + person3 = create_user('person-test-3').person | ||
96 | + @circle1.update_attributes(person: @external_person) | ||
97 | + | ||
98 | + @external_person.follow(@person2, @circle1) | ||
99 | + @external_person.follow(person3, @circle1) | ||
100 | + assert_equivalent [@person2, person3], @external_person.followed_profiles | ||
101 | + end | ||
102 | + | ||
103 | + should 'not follow same person twice even with different circles' do | ||
104 | + circle3 = Circle.create!(person: @person1, name: "Free Folk", profile_type: 'Person') | ||
105 | + | ||
106 | + @person1.follow(@person2, @circle1) | ||
107 | + @person1.follow(@person2, @circle2) | ||
108 | + @person1.follow(@person2, circle3) | ||
109 | + assert_equivalent [@person2], @person1.followed_profiles | ||
110 | + end | ||
111 | + | ||
112 | + should 'display an error if a person is already being followed' do | ||
113 | + @person1.follow(@person2, @circle1) | ||
114 | + profile_follower = ProfileFollower.new(circle: @circle1, profile: @person2) | ||
115 | + | ||
116 | + profile_follower.valid? | ||
117 | + assert profile_follower.errors.messages[:profile_id].present? | ||
118 | + end | ||
119 | + | ||
120 | + should 'update profile circles for a person' do | ||
121 | + circle3 = Circle.create!(person: @person1, name: "Brains", profile_type: 'Person') | ||
122 | + @person1.follow(@person2, [@circle1, @circle2]) | ||
123 | + | ||
124 | + @person1.update_profile_circles(@person2, [@circle2, circle3]) | ||
125 | + assert_equivalent [@circle2, circle3], @person2.in_circles | ||
126 | + end | ||
127 | + | ||
128 | + should 'keep other follower circles after update' do | ||
129 | + person3 = create_user('person-test-3').person | ||
130 | + circle3 = Circle.create!(person: person3, name: "Humans", profile_type: 'Person') | ||
131 | + @person1.follow(@person2, @circle1) | ||
132 | + person3.follow(@person2, circle3) | ||
133 | + | ||
134 | + @person1.update_profile_circles(@person2, [@circle1, @circle2]) | ||
135 | + assert_equivalent [@circle1, @circle2, circle3], @person2.in_circles | ||
136 | + end | ||
137 | + | ||
138 | + should 'remove a person from a circle' do | ||
139 | + @person1.follow(@person2, [@circle1, @circle2]) | ||
140 | + | ||
141 | + @person1.remove_profile_from_circle(@person2, @circle2) | ||
142 | + assert_equivalent [@circle1], @person2.in_circles | ||
143 | + end | ||
144 | + | ||
145 | + should 'not remove a person from a circle if the user is not the owner' do | ||
146 | + person3 = create_user('person-test-3').person | ||
147 | + @person1.follow(@person2, [@circle1, @circle2]) | ||
148 | + | ||
149 | + person3.remove_profile_from_circle(@person2, @circle2) | ||
150 | + assert_equivalent [@circle1, @circle2], @person2.in_circles | ||
151 | + end | ||
152 | +end |
test/unit/person_test.rb
@@ -1640,11 +1640,6 @@ class PersonTest < ActiveSupport::TestCase | @@ -1640,11 +1640,6 @@ class PersonTest < ActiveSupport::TestCase | ||
1640 | assert person.can_change_homepage? | 1640 | assert person.can_change_homepage? |
1641 | end | 1641 | end |
1642 | 1642 | ||
1643 | - should 'follow? return false when no profile is passed as parameter' do | ||
1644 | - person = Person.new | ||
1645 | - assert_equal false, person.follows?(nil) | ||
1646 | - end | ||
1647 | - | ||
1648 | should 'allow posting content when has post_content permission' do | 1643 | should 'allow posting content when has post_content permission' do |
1649 | person = create_user('person').person | 1644 | person = create_user('person').person |
1650 | profile = mock | 1645 | profile = mock |
@@ -1957,53 +1952,4 @@ class PersonTest < ActiveSupport::TestCase | @@ -1957,53 +1952,4 @@ class PersonTest < ActiveSupport::TestCase | ||
1957 | person.user.expects(:save!).never | 1952 | person.user.expects(:save!).never |
1958 | person.save! | 1953 | person.save! |
1959 | end | 1954 | end |
1960 | - | ||
1961 | - should 'update profile circles for a person' do | ||
1962 | - person = create_user('testuser').person | ||
1963 | - community = fast_create(Community) | ||
1964 | - circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Community') | ||
1965 | - circle2 = Circle.create!(:person=> person, :name => "Dota", :profile_type => 'Community') | ||
1966 | - circle3 = Circle.create!(:person=> person, :name => "Quadrado", :profile_type => 'Community') | ||
1967 | - person.follow(community, [circle, circle2]) | ||
1968 | - person.update_profile_circles(community, [circle2, circle3]) | ||
1969 | - assert_equivalent [circle2, circle3], ProfileFollower.with_profile(community).with_follower(person).map(&:circle) | ||
1970 | - end | ||
1971 | - | ||
1972 | - should 'a person follow a profile' do | ||
1973 | - person = create_user('testuser').person | ||
1974 | - community = fast_create(Community) | ||
1975 | - circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Community') | ||
1976 | - person.follow(community, circle) | ||
1977 | - assert_includes person.followed_profiles, community | ||
1978 | - end | ||
1979 | - | ||
1980 | - should 'a person follow a profile with more than one circle' do | ||
1981 | - person = create_user('testuser').person | ||
1982 | - community = fast_create(Community) | ||
1983 | - circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Community') | ||
1984 | - circle2 = Circle.create!(:person=> person, :name => "Dota", :profile_type => 'Community') | ||
1985 | - person.follow(community, [circle, circle2]) | ||
1986 | - assert_includes person.followed_profiles, community | ||
1987 | - assert_equivalent [circle, circle2], ProfileFollower.with_profile(community).with_follower(person).map(&:circle) | ||
1988 | - end | ||
1989 | - | ||
1990 | - should 'a person unfollow a profile' do | ||
1991 | - person = create_user('testuser').person | ||
1992 | - community = fast_create(Community) | ||
1993 | - circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Community') | ||
1994 | - person.follow(community, circle) | ||
1995 | - person.unfollow(community) | ||
1996 | - assert_not_includes person.followed_profiles, community | ||
1997 | - end | ||
1998 | - | ||
1999 | - should 'a person remove a profile from a circle' do | ||
2000 | - person = create_user('testuser').person | ||
2001 | - community = fast_create(Community) | ||
2002 | - circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Community') | ||
2003 | - circle2 = Circle.create!(:person=> person, :name => "Dota", :profile_type => 'Community') | ||
2004 | - person.follow(community, [circle, circle2]) | ||
2005 | - person.remove_profile_from_circle(community, circle) | ||
2006 | - assert_equivalent [circle2], ProfileFollower.with_profile(community).with_follower(person).map(&:circle) | ||
2007 | - end | ||
2008 | - | ||
2009 | end | 1955 | end |
test/unit/profile_followers_test.rb
@@ -1,73 +0,0 @@ | @@ -1,73 +0,0 @@ | ||
1 | -require_relative "../test_helper" | ||
2 | - | ||
3 | -class ProfileFollowersTest < ActiveSupport::TestCase | ||
4 | - | ||
5 | - should 'a person follow another' do | ||
6 | - p1 = create_user('person_test').person | ||
7 | - p2 = create_user('person_test_2').person | ||
8 | - circle = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person') | ||
9 | - | ||
10 | - assert_difference 'ProfileFollower.count' do | ||
11 | - p1.follow(p2, circle) | ||
12 | - end | ||
13 | - | ||
14 | - assert_includes p2.followers, p1 | ||
15 | - assert_not_includes p1.followers, p2 | ||
16 | - end | ||
17 | - | ||
18 | - should 'a person unfollow another person' do | ||
19 | - p1 = create_user('person_test').person | ||
20 | - p2 = create_user('person_test_2').person | ||
21 | - circle = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person') | ||
22 | - | ||
23 | - p1.follow(p2,circle) | ||
24 | - | ||
25 | - assert_difference 'ProfileFollower.count', -1 do | ||
26 | - p1.unfollow(p2) | ||
27 | - end | ||
28 | - | ||
29 | - assert_not_includes p2.followers, p1 | ||
30 | - end | ||
31 | - | ||
32 | - should 'get the followed persons for a profile' do | ||
33 | - p1 = create_user('person_test').person | ||
34 | - p2 = create_user('person_test_2').person | ||
35 | - p3 = create_user('person_test_3').person | ||
36 | - circle = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person') | ||
37 | - | ||
38 | - p1.follow(p2, circle) | ||
39 | - p1.follow(p3, circle) | ||
40 | - | ||
41 | - assert_equivalent p1.followed_profiles, [p2,p3] | ||
42 | - assert_equivalent Profile.followed_by(p1), [p2,p3] | ||
43 | - end | ||
44 | - | ||
45 | - should 'not follow same person twice' do | ||
46 | - p1 = create_user('person_test').person | ||
47 | - p2 = create_user('person_test_2').person | ||
48 | - circle = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person') | ||
49 | - | ||
50 | - assert_difference 'ProfileFollower.count' do | ||
51 | - p1.follow(p2, circle) | ||
52 | - p1.follow(p2, circle) | ||
53 | - end | ||
54 | - | ||
55 | - assert_equivalent p1.followed_profiles, [p2] | ||
56 | - assert_equivalent p2.followers, [p1] | ||
57 | - end | ||
58 | - | ||
59 | - should 'show the correct message when a profile is followed by the same person' do | ||
60 | - p1 = create_user('person_test').person | ||
61 | - p2 = create_user('person_test_2').person | ||
62 | - circle = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person') | ||
63 | - | ||
64 | - p1.follow(p2, circle) | ||
65 | - profile_follower = ProfileFollower.new | ||
66 | - profile_follower.circle = circle | ||
67 | - profile_follower.profile = p2 | ||
68 | - profile_follower.valid? | ||
69 | - | ||
70 | - assert_includes profile_follower.errors.messages[:profile_id], | ||
71 | - "can't put a profile in the same circle twice" | ||
72 | - end | ||
73 | -end |
test/unit/profile_test.rb
@@ -2232,4 +2232,47 @@ class ProfileTest < ActiveSupport::TestCase | @@ -2232,4 +2232,47 @@ class ProfileTest < ActiveSupport::TestCase | ||
2232 | c.add_member(p) | 2232 | c.add_member(p) |
2233 | end | 2233 | end |
2234 | end | 2234 | end |
2235 | + | ||
2236 | + should 'return all circles and followers, and do not repeat' do | ||
2237 | + person1 = create_user('testperson-1').person | ||
2238 | + person2 = create_user('testperson-2').person | ||
2239 | + person3 = create_user('testperson-3').person | ||
2240 | + circle1 = Circle.create!(:person => person1, :name => "Night's Watch", :profile_type => 'Person') | ||
2241 | + circle2 = Circle.create!(:person => person1, :name => "Free Folk", :profile_type => 'Person') | ||
2242 | + circle3 = Circle.create!(:person => person2, :name => "The Unsullied", :profile_type => 'Person') | ||
2243 | + | ||
2244 | + person1.follow(person3, circle1) | ||
2245 | + person1.follow(person3, circle2) | ||
2246 | + person2.follow(person3, circle3) | ||
2247 | + | ||
2248 | + assert_equivalent person3.in_circles, [circle1, circle2, circle3] | ||
2249 | + assert_equivalent person3.followers, [person1, person2] | ||
2250 | + end | ||
2251 | + | ||
2252 | + should 'return all profiles followed by a regular person' do | ||
2253 | + person1 = create_user('testperson-1').person | ||
2254 | + person2 = create_user('testperson-2').person | ||
2255 | + community = fast_create(Community) | ||
2256 | + circle1 = Circle.create!(:person => person1, :name => "Night's Watch", :profile_type => 'Person') | ||
2257 | + circle2 = Circle.create!(:person => person1, :name => "Free Folk", :profile_type => 'Community') | ||
2258 | + | ||
2259 | + person1.follow(person2, circle1) | ||
2260 | + person1.follow(community, circle2) | ||
2261 | + assert_equivalent [person2, community], Profile.followed_by(person1) | ||
2262 | + end | ||
2263 | + | ||
2264 | + should 'return all profiles followed by an external person' do | ||
2265 | + external_person = ExternalPerson.create!(identifier: 'johnlocke', name: 'John Locke', | ||
2266 | + source: 'anerenvironment.org', email: 'locke@island.org', | ||
2267 | + created_at: Date.yesterday) | ||
2268 | + person = create_user('testperson-2').person | ||
2269 | + community = fast_create(Community) | ||
2270 | + circle1 = Circle.create!(:person => external_person, :name => "Night's Watch", :profile_type => 'Person') | ||
2271 | + circle2 = Circle.create!(:person => external_person, :name => "Free Folk", :profile_type => 'Community') | ||
2272 | + | ||
2273 | + external_person.follow(person, circle1) | ||
2274 | + external_person.follow(community, circle2) | ||
2275 | + assert_equivalent [person, community], Profile.followed_by(external_person) | ||
2276 | + end | ||
2277 | + | ||
2235 | end | 2278 | end |