2013-05-08 13:20:38 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Admin::GroupsController do
|
2013-07-16 14:17:44 +08:00
|
|
|
|
2013-07-22 10:37:01 +08:00
|
|
|
before do
|
|
|
|
@admin = log_in(:admin)
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
it "is a subclass of AdminController" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(Admin::GroupsController < Admin::AdminController).to eq(true)
|
2013-05-08 13:20:38 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
context ".index" do
|
|
|
|
|
|
|
|
it "produces valid json for groups" do
|
|
|
|
group = Fabricate.build(:group, name: "test")
|
|
|
|
group.add(@admin)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
xhr :get, :index
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(::JSON.parse(response.body).keep_if {|r| r["id"] == group.id }).to eq([{
|
2015-01-06 01:51:45 +08:00
|
|
|
"id"=>group.id,
|
|
|
|
"name"=>group.name,
|
|
|
|
"user_count"=>1,
|
|
|
|
"automatic"=>false,
|
|
|
|
"alias_level"=>0,
|
2015-01-24 01:25:43 +08:00
|
|
|
"visible"=>true,
|
2015-01-24 03:31:48 +08:00
|
|
|
"automatic_membership_email_domains"=>nil,
|
2015-04-10 10:17:28 +08:00
|
|
|
"automatic_membership_retroactive"=>false,
|
|
|
|
"title"=>nil,
|
2015-09-02 04:52:05 +08:00
|
|
|
"primary_group"=>false,
|
|
|
|
"grant_trust_level"=>nil
|
2015-01-10 01:04:02 +08:00
|
|
|
}])
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".create" do
|
|
|
|
|
|
|
|
it "strip spaces on the group name" do
|
|
|
|
xhr :post, :create, name: " bob "
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
groups = Group.where(name: "bob").to_a
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(groups.count).to eq(1)
|
|
|
|
expect(groups[0].name).to eq("bob")
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
context ".update" do
|
|
|
|
|
|
|
|
it "ignore name change on automatic group" do
|
|
|
|
xhr :put, :update, id: 1, name: "WAT", visible: "true"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2015-01-06 01:51:45 +08:00
|
|
|
|
|
|
|
group = Group.find(1)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(group.name).not_to eq("WAT")
|
|
|
|
expect(group.visible).to eq(true)
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
2013-05-08 13:20:38 +08:00
|
|
|
|
2015-01-24 01:25:43 +08:00
|
|
|
it "doesn't launch the 'automatic group membership' job when it's not retroactive" do
|
|
|
|
Jobs.expects(:enqueue).never
|
2015-04-10 10:17:28 +08:00
|
|
|
group = Fabricate(:group)
|
|
|
|
xhr :put, :update, id: group.id, automatic_membership_retroactive: "false"
|
2015-01-24 01:25:43 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "launches the 'automatic group membership' job when it's retroactive" do
|
2015-04-10 10:17:28 +08:00
|
|
|
group = Fabricate(:group)
|
|
|
|
Jobs.expects(:enqueue).with(:automatic_group_membership, group_id: group.id)
|
|
|
|
xhr :put, :update, id: group.id, automatic_membership_retroactive: "true"
|
2015-01-24 01:25:43 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
end
|
2013-05-09 09:33:56 +08:00
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
context ".destroy" do
|
|
|
|
|
2013-07-22 10:37:01 +08:00
|
|
|
it "returns a 422 if the group is automatic" do
|
|
|
|
group = Fabricate(:group, automatic: true)
|
|
|
|
xhr :delete, :destroy, id: group.id
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Group.where(id: group.id).count).to eq(1)
|
2013-07-22 10:37:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is able to destroy a non-automatic group" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
xhr :delete, :destroy, id: group.id
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Group.where(id: group.id).count).to eq(0)
|
2013-07-22 10:37:01 +08:00
|
|
|
end
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2013-05-09 09:33:56 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
context ".refresh_automatic_groups" do
|
2013-05-09 09:33:56 +08:00
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
it "is able to refresh automatic groups" do
|
|
|
|
Group.expects(:refresh_automatic_groups!).returns(true)
|
2013-07-24 12:00:17 +08:00
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
xhr :post, :refresh_automatic_groups
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2013-07-24 12:00:17 +08:00
|
|
|
end
|
|
|
|
|
2013-05-09 09:33:56 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
context ".add_members" do
|
2014-11-21 01:29:56 +08:00
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
it "cannot add members to automatic groups" do
|
2015-01-22 03:52:48 +08:00
|
|
|
xhr :put, :add_members, id: 1, usernames: "l77t"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(422)
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
context "is able to add several members to a group" do
|
2014-11-21 01:29:56 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
let(:user1) { Fabricate(:user) }
|
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2014-11-21 01:29:56 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
it "adds by username" do
|
|
|
|
xhr :put, :add_members, id: group.id, usernames: [user1.username, user2.username].join(",")
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds by id" do
|
|
|
|
xhr :put, :add_members, id: group.id, user_ids: [user1.id, user2.id].join(",")
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(2)
|
|
|
|
end
|
2014-11-21 01:29:56 +08:00
|
|
|
end
|
|
|
|
|
2015-04-15 16:06:54 +08:00
|
|
|
it "returns 422 if member already exists" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
existing_member = Fabricate(:user)
|
|
|
|
group.add(existing_member)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
xhr :put, :add_members, id: group.id, usernames: existing_member.username
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context ".remove_member" do
|
|
|
|
|
|
|
|
it "cannot remove members from automatic groups" do
|
2015-01-22 03:52:48 +08:00
|
|
|
xhr :put, :remove_member, id: 1, user_id: 42
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(422)
|
2015-01-06 01:51:45 +08:00
|
|
|
end
|
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
context "is able to remove a member" do
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2015-02-09 13:03:09 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
before do
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
end
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
it "removes by id" do
|
|
|
|
xhr :delete, :remove_member, id: group.id, user_id: user.id
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by username" do
|
|
|
|
xhr :delete, :remove_member, id: group.id, username: user.username
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes user.primary_group_id when user is removed from group" do
|
|
|
|
user.primary_group_id = group.id
|
|
|
|
user.save
|
|
|
|
|
|
|
|
xhr :delete, :remove_member, id: group.id, username: user.username
|
2015-02-09 13:03:09 +08:00
|
|
|
|
2015-02-25 22:47:45 +08:00
|
|
|
user.reload
|
|
|
|
expect(user.primary_group_id).to eq(nil)
|
|
|
|
end
|
2014-11-21 01:29:56 +08:00
|
|
|
end
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2013-05-09 09:33:56 +08:00
|
|
|
end
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2013-05-08 13:20:38 +08:00
|
|
|
end
|