2013-04-29 02:33:24 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Group do
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
# UGLY but perf is horrible with this callback
|
|
|
|
before do
|
|
|
|
User.set_callback(:create, :after, :ensure_in_trust_level_group)
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
User.skip_callback(:create, :after, :ensure_in_trust_level_group)
|
|
|
|
end
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
describe "validation" do
|
|
|
|
let(:group) { build(:group) }
|
|
|
|
|
|
|
|
it "is invalid for blank" do
|
|
|
|
group.name = ""
|
2014-09-25 11:44:48 -04:00
|
|
|
group.valid?.should == false
|
2013-05-09 03:37:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid for a longer name" do
|
|
|
|
group.name = "this_is_a_name"
|
2014-09-25 11:44:48 -04:00
|
|
|
group.valid?.should == true
|
2013-05-09 03:37:34 -04:00
|
|
|
end
|
2013-06-16 23:43:06 -04:00
|
|
|
|
|
|
|
it "is invalid for non names" do
|
|
|
|
group.name = "this is_a_name"
|
2014-09-25 11:44:48 -04:00
|
|
|
group.valid?.should == false
|
2013-06-16 23:43:06 -04:00
|
|
|
end
|
2013-05-09 03:37:34 -04:00
|
|
|
end
|
|
|
|
|
2013-09-06 00:07:23 -04:00
|
|
|
def real_admins
|
|
|
|
Group[:admins].user_ids - [-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def real_moderators
|
|
|
|
Group[:moderators].user_ids - [-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def real_staff
|
|
|
|
Group[:staff].user_ids - [-1]
|
|
|
|
end
|
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
it "Can update moderator/staff/admin groups correctly" do
|
2013-05-09 03:37:34 -04:00
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
admin = Fabricate(:admin)
|
|
|
|
moderator = Fabricate(:moderator)
|
|
|
|
|
|
|
|
Group.refresh_automatic_groups!(:admins, :staff, :moderators)
|
|
|
|
|
2013-09-06 00:07:23 -04:00
|
|
|
real_admins.should == [admin.id]
|
|
|
|
real_moderators.should == [moderator.id]
|
|
|
|
real_staff.sort.should == [moderator.id,admin.id].sort
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
admin.admin = false
|
|
|
|
admin.save
|
|
|
|
|
|
|
|
Group.refresh_automatic_group!(:admins)
|
2013-09-06 00:07:23 -04:00
|
|
|
real_admins.should == []
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
moderator.revoke_moderation!
|
|
|
|
|
|
|
|
admin.grant_admin!
|
2013-09-06 00:07:23 -04:00
|
|
|
real_admins.should == [admin.id]
|
|
|
|
real_staff.should == [admin.id]
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
admin.revoke_admin!
|
2013-09-06 00:07:23 -04:00
|
|
|
real_admins.should == []
|
|
|
|
real_staff.should == []
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
admin.grant_moderation!
|
2013-09-06 00:07:23 -04:00
|
|
|
real_moderators.should == [admin.id]
|
|
|
|
real_staff.should == [admin.id]
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
admin.revoke_moderation!
|
2013-09-06 00:07:23 -04:00
|
|
|
real_admins.should == []
|
|
|
|
real_staff.should == []
|
2013-05-06 00:49:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "Correctly updates automatic trust level groups" do
|
|
|
|
user = Fabricate(:user)
|
2014-06-17 04:14:07 -04:00
|
|
|
Group[:trust_level_0].user_ids.should include user.id
|
2014-06-16 20:46:30 -04:00
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
user.change_trust_level!(TrustLevel[1])
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
Group[:trust_level_1].user_ids.should include user.id
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
user.change_trust_level!(TrustLevel[2])
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
Group[:trust_level_1].user_ids.should include user.id
|
|
|
|
Group[:trust_level_2].user_ids.should include user.id
|
2013-05-07 20:32:29 -04:00
|
|
|
|
|
|
|
user2 = Fabricate(:coding_horror)
|
2014-09-05 01:20:39 -04:00
|
|
|
user2.change_trust_level!(TrustLevel[3])
|
2013-05-07 20:32:29 -04:00
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
Group[:trust_level_2].user_ids.sort.should == [-1, user.id, user2.id].sort
|
2013-05-06 00:49:56 -04:00
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
it "Correctly updates all automatic groups upon request" do
|
2013-06-25 10:15:41 -04:00
|
|
|
Fabricate(:admin)
|
2013-05-08 01:20:38 -04:00
|
|
|
user = Fabricate(:user)
|
2014-09-05 01:20:39 -04:00
|
|
|
user.change_trust_level!(TrustLevel[2])
|
2013-05-08 01:20:38 -04:00
|
|
|
|
|
|
|
Group.exec_sql("update groups set user_count=0 where id = #{Group::AUTO_GROUPS[:trust_level_2]}")
|
|
|
|
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
|
|
|
groups = Group.includes(:users).to_a
|
|
|
|
groups.count.should == Group::AUTO_GROUPS.count
|
|
|
|
|
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:admins]}
|
2013-09-06 00:07:23 -04:00
|
|
|
g.users.count.should == 2
|
|
|
|
g.user_count.should == 2
|
2013-05-08 01:20:38 -04:00
|
|
|
|
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:staff]}
|
2013-09-06 00:07:23 -04:00
|
|
|
g.users.count.should == 2
|
|
|
|
g.user_count.should == 2
|
2013-05-08 01:20:38 -04:00
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
|
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:trust_level_1]}
|
|
|
|
# admin, system and user
|
|
|
|
g.users.count.should == 3
|
|
|
|
g.user_count.should == 3
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:trust_level_2]}
|
2014-06-16 20:46:30 -04:00
|
|
|
# system and user
|
|
|
|
g.users.count.should == 2
|
|
|
|
g.user_count.should == 2
|
2013-05-08 01:20:38 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
it "can set members via usernames helper" do
|
|
|
|
g = Fabricate(:group)
|
|
|
|
u1 = Fabricate(:user)
|
|
|
|
u2 = Fabricate(:user)
|
|
|
|
u3 = Fabricate(:user)
|
|
|
|
|
|
|
|
g.add(u1)
|
|
|
|
g.save!
|
|
|
|
|
|
|
|
usernames = "#{u2.username},#{u3.username}"
|
|
|
|
|
|
|
|
# no side effects please
|
|
|
|
g.usernames = usernames
|
|
|
|
g.reload
|
|
|
|
g.users.count.should == 1
|
|
|
|
|
|
|
|
g.usernames = usernames
|
|
|
|
g.save!
|
|
|
|
|
|
|
|
g.usernames.split(",").sort.should == usernames.split(",").sort
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly destroys groups" do
|
2013-11-18 10:52:01 -05:00
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
g = Fabricate(:group)
|
|
|
|
u1 = Fabricate(:user)
|
|
|
|
g.add(u1)
|
|
|
|
g.save!
|
|
|
|
|
|
|
|
g.destroy
|
|
|
|
|
|
|
|
User.where(id: u1.id).count.should == 1
|
2014-06-16 20:46:30 -04:00
|
|
|
GroupUser.where(group_id: g.id).count.should == 0
|
2013-05-08 21:33:56 -04:00
|
|
|
end
|
|
|
|
|
2014-04-25 09:14:05 -04:00
|
|
|
|
|
|
|
it "has custom fields" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.custom_fields["a"].should == nil
|
|
|
|
|
|
|
|
group.custom_fields["hugh"] = "jackman"
|
|
|
|
group.custom_fields["jack"] = "black"
|
|
|
|
group.save
|
|
|
|
|
|
|
|
group = Group.find(group.id)
|
|
|
|
group.custom_fields.should == {"hugh" => "jackman", "jack" => "black"}
|
|
|
|
end
|
|
|
|
|
2013-07-22 20:10:36 -04:00
|
|
|
it "allows you to lookup a new group by name" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.id.should == Group[group.name].id
|
|
|
|
group.id.should == Group[group.name.to_sym].id
|
|
|
|
end
|
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
it "can find desired groups correctly" do
|
|
|
|
Group.desired_trust_level_groups(2).sort.should == [10,11,12]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it "correctly handles trust level changes" do
|
|
|
|
user = Fabricate(:user, trust_level: 2)
|
|
|
|
Group.user_trust_level_change!(user.id, 2)
|
|
|
|
|
2014-06-17 04:14:07 -04:00
|
|
|
user.groups.map(&:name).sort.should == ["trust_level_0","trust_level_1", "trust_level_2"]
|
2014-06-16 20:46:30 -04:00
|
|
|
|
|
|
|
Group.user_trust_level_change!(user.id, 0)
|
|
|
|
user.reload
|
2014-06-17 04:14:07 -04:00
|
|
|
user.groups.map(&:name).sort.should == ["trust_level_0"]
|
2014-06-16 20:46:30 -04:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|