FIX: Group can't be deleted if certain users are demoted.

This commit is contained in:
Guo Xiang Tan 2018-04-10 14:19:08 +08:00
parent 9df4f7bf8e
commit 874003b7b1
3 changed files with 76 additions and 50 deletions

View File

@ -82,15 +82,8 @@ class GroupUser < ActiveRecord::Base
.includes(:group) .includes(:group)
.maximum("groups.grant_trust_level") .maximum("groups.grant_trust_level")
if highest_level.nil? user.update!(group_locked_trust_level: highest_level)
# If the user no longer has a group with a trust level, Promotion.recalculate(user)
# unlock them, start at 0 and consider promotions.
user.update!(group_locked_trust_level: nil)
Promotion.recalculate(user)
else
user.update!(group_locked_trust_level: highest_level)
user.change_trust_level!(highest_level)
end
end end
end end

View File

@ -111,16 +111,16 @@ class Promotion
def self.recalculate(user, performed_by = nil) def self.recalculate(user, performed_by = nil)
# First, use the manual locked level # First, use the manual locked level
unless user.manual_locked_trust_level.nil? unless user.manual_locked_trust_level.nil?
user.trust_level = user.manual_locked_trust_level return user.update!(
user.save trust_level: user.manual_locked_trust_level
return )
end end
# Then consider the group locked level # Then consider the group locked level
if user.group_locked_trust_level if user.group_locked_trust_level
user.trust_level = user.group_locked_trust_level return user.update!(
user.save trust_level: user.group_locked_trust_level
return )
end end
user.update_column(:trust_level, TrustLevel[0]) user.update_column(:trust_level, TrustLevel[0])

View File

@ -541,50 +541,83 @@ describe Group do
end end
end end
it "correctly grants a trust level to members" do describe 'trust level management' do
group = Fabricate(:group, grant_trust_level: 2) it "correctly grants a trust level to members" do
u0 = Fabricate(:user, trust_level: 0) group = Fabricate(:group, grant_trust_level: 2)
u3 = Fabricate(:user, trust_level: 3) u0 = Fabricate(:user, trust_level: 0)
u3 = Fabricate(:user, trust_level: 3)
group.add(u0) group.add(u0)
expect(u0.reload.trust_level).to eq(2) expect(u0.reload.trust_level).to eq(2)
group.add(u3) group.add(u3)
expect(u3.reload.trust_level).to eq(3) expect(u3.reload.trust_level).to eq(3)
end end
it "adjusts the user trust level" do describe 'when a user has qualified for trust level 1' do
g0 = Fabricate(:group, grant_trust_level: 2) let(:user) do
g1 = Fabricate(:group, grant_trust_level: 3) Fabricate(:user,
g2 = Fabricate(:group) trust_level: 1,
created_at: Time.zone.now - 10.years
)
end
user = Fabricate(:user, trust_level: 0) let(:group) { Fabricate(:group, grant_trust_level: 1) }
let(:group2) { Fabricate(:group, grant_trust_level: 0) }
# Add a group without one to consider `NULL` check before do
g2.add(user) user.user_stat.update!(
expect(user.group_locked_trust_level).to be_nil topics_entered: 999,
expect(user.manual_locked_trust_level).to be_nil posts_read_count: 999,
time_read: 999
)
end
g0.add(user) it "should not demote the user" do
expect(user.reload.trust_level).to eq(2) group.add(user)
expect(user.group_locked_trust_level).to eq(2) group2.add(user)
expect(user.manual_locked_trust_level).to be_nil
g1.add(user) expect(user.reload.trust_level).to eq(1)
expect(user.reload.trust_level).to eq(3)
expect(user.group_locked_trust_level).to eq(3)
expect(user.manual_locked_trust_level).to be_nil
g1.remove(user) group.remove(user)
expect(user.reload.trust_level).to eq(2)
expect(user.group_locked_trust_level).to eq(2)
expect(user.manual_locked_trust_level).to be_nil
g0.remove(user) expect(user.reload.trust_level).to eq(0)
user.reload end
expect(user.manual_locked_trust_level).to be_nil end
expect(user.group_locked_trust_level).to be_nil
expect(user.trust_level).to eq(0) it "adjusts the user trust level" do
g0 = Fabricate(:group, grant_trust_level: 2)
g1 = Fabricate(:group, grant_trust_level: 3)
g2 = Fabricate(:group)
user = Fabricate(:user, trust_level: 0)
# Add a group without one to consider `NULL` check
g2.add(user)
expect(user.group_locked_trust_level).to be_nil
expect(user.manual_locked_trust_level).to be_nil
g0.add(user)
expect(user.reload.trust_level).to eq(2)
expect(user.group_locked_trust_level).to eq(2)
expect(user.manual_locked_trust_level).to be_nil
g1.add(user)
expect(user.reload.trust_level).to eq(3)
expect(user.group_locked_trust_level).to eq(3)
expect(user.manual_locked_trust_level).to be_nil
g1.remove(user)
expect(user.reload.trust_level).to eq(2)
expect(user.group_locked_trust_level).to eq(2)
expect(user.manual_locked_trust_level).to be_nil
g0.remove(user)
user.reload
expect(user.manual_locked_trust_level).to be_nil
expect(user.group_locked_trust_level).to be_nil
expect(user.trust_level).to eq(0)
end
end end
it 'should cook the bio' do it 'should cook the bio' do