2013-04-29 14:33:24 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Group do
|
2013-05-06 12:49:56 +08:00
|
|
|
|
2013-05-09 15:37:34 +08:00
|
|
|
describe "validation" do
|
|
|
|
let(:group) { build(:group) }
|
|
|
|
|
|
|
|
it "is invalid for blank" do
|
|
|
|
group.name = ""
|
|
|
|
group.valid?.should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid for a longer name" do
|
|
|
|
group.name = "this_is_a_name"
|
|
|
|
group.valid?.should be_true
|
|
|
|
end
|
2013-06-17 11:43:06 +08:00
|
|
|
|
|
|
|
it "is invalid for non names" do
|
|
|
|
group.name = "this is_a_name"
|
|
|
|
group.valid?.should be_false
|
|
|
|
end
|
2013-05-09 15:37:34 +08:00
|
|
|
end
|
|
|
|
|
2013-09-06 12:07:23 +08: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 12:49:56 +08:00
|
|
|
it "Can update moderator/staff/admin groups correctly" do
|
2013-05-09 15:37:34 +08:00
|
|
|
|
2013-05-06 12:49:56 +08:00
|
|
|
admin = Fabricate(:admin)
|
|
|
|
moderator = Fabricate(:moderator)
|
|
|
|
|
|
|
|
Group.refresh_automatic_groups!(:admins, :staff, :moderators)
|
|
|
|
|
2013-09-06 12:07:23 +08:00
|
|
|
real_admins.should == [admin.id]
|
|
|
|
real_moderators.should == [moderator.id]
|
|
|
|
real_staff.sort.should == [moderator.id,admin.id].sort
|
2013-05-06 12:49:56 +08:00
|
|
|
|
|
|
|
admin.admin = false
|
|
|
|
admin.save
|
|
|
|
|
|
|
|
Group.refresh_automatic_group!(:admins)
|
2013-09-06 12:07:23 +08:00
|
|
|
real_admins.should == []
|
2013-05-06 12:49:56 +08:00
|
|
|
|
|
|
|
moderator.revoke_moderation!
|
|
|
|
|
|
|
|
admin.grant_admin!
|
2013-09-06 12:07:23 +08:00
|
|
|
real_admins.should == [admin.id]
|
|
|
|
real_staff.should == [admin.id]
|
2013-05-06 12:49:56 +08:00
|
|
|
|
|
|
|
admin.revoke_admin!
|
2013-09-06 12:07:23 +08:00
|
|
|
real_admins.should == []
|
|
|
|
real_staff.should == []
|
2013-05-06 12:49:56 +08:00
|
|
|
|
|
|
|
admin.grant_moderation!
|
2013-09-06 12:07:23 +08:00
|
|
|
real_moderators.should == [admin.id]
|
|
|
|
real_staff.should == [admin.id]
|
2013-05-06 12:49:56 +08:00
|
|
|
|
|
|
|
admin.revoke_moderation!
|
2013-09-06 12:07:23 +08:00
|
|
|
real_admins.should == []
|
|
|
|
real_staff.should == []
|
2013-05-06 12:49:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "Correctly updates automatic trust level groups" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.change_trust_level!(:basic)
|
|
|
|
|
|
|
|
Group[:trust_level_1].user_ids.should == [user.id]
|
|
|
|
|
|
|
|
user.change_trust_level!(:regular)
|
|
|
|
|
|
|
|
Group[:trust_level_1].user_ids.should == []
|
|
|
|
Group[:trust_level_2].user_ids.should == [user.id]
|
2013-05-08 08:32:29 +08:00
|
|
|
|
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
user2.change_trust_level!(:regular)
|
|
|
|
|
|
|
|
Group[:trust_level_2].user_ids.sort.should == [user.id, user2.id].sort
|
2013-05-06 12:49:56 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
it "Correctly updates all automatic groups upon request" do
|
2013-06-25 22:15:41 +08:00
|
|
|
Fabricate(:admin)
|
2013-05-08 13:20:38 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
user.change_trust_level!(:regular)
|
|
|
|
|
|
|
|
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 12:07:23 +08:00
|
|
|
g.users.count.should == 2
|
|
|
|
g.user_count.should == 2
|
2013-05-08 13:20:38 +08:00
|
|
|
|
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:staff]}
|
2013-09-06 12:07:23 +08:00
|
|
|
g.users.count.should == 2
|
|
|
|
g.user_count.should == 2
|
2013-05-08 13:20:38 +08:00
|
|
|
|
|
|
|
g = groups.find{|g| g.id == Group::AUTO_GROUPS[:trust_level_2]}
|
|
|
|
g.users.count.should == 1
|
|
|
|
g.user_count.should == 1
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-09 09:33:56 +08: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 23:52:01 +08:00
|
|
|
|
|
|
|
original_count = GroupUser.count
|
2013-05-09 09:33:56 +08:00
|
|
|
g = Fabricate(:group)
|
|
|
|
u1 = Fabricate(:user)
|
|
|
|
g.add(u1)
|
|
|
|
g.save!
|
|
|
|
|
|
|
|
g.destroy
|
|
|
|
|
|
|
|
User.where(id: u1.id).count.should == 1
|
2013-11-18 23:52:01 +08:00
|
|
|
GroupUser.count.should == original_count
|
2013-05-09 09:33:56 +08:00
|
|
|
end
|
|
|
|
|
2014-04-25 21:14:05 +08: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-23 08:10:36 +08: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
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|