2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-05-08 01:20:38 -04:00
|
|
|
|
|
|
|
describe Admin::GroupsController do
|
2017-06-02 10:15:57 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2013-07-16 02:17:44 -04:00
|
|
|
|
2013-07-21 22:37:01 -04:00
|
|
|
before do
|
|
|
|
@admin = log_in(:admin)
|
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
it "is a subclass of AdminController" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(Admin::GroupsController < Admin::AdminController).to eq(true)
|
2013-05-08 01:20:38 -04:00
|
|
|
end
|
|
|
|
|
2015-01-05 12:51:45 -05: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-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2016-10-24 04:53:31 -04:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json.select { |r| r["id"] == Group::AUTO_GROUPS[:everyone] }).to be_empty
|
|
|
|
expect(json.select { |r| r["id"] == group.id }).to eq([{
|
2015-01-05 12:51:45 -05:00
|
|
|
"id"=>group.id,
|
|
|
|
"name"=>group.name,
|
|
|
|
"user_count"=>1,
|
|
|
|
"automatic"=>false,
|
|
|
|
"alias_level"=>0,
|
2015-01-23 12:25:43 -05:00
|
|
|
"visible"=>true,
|
2015-01-23 14:31:48 -05:00
|
|
|
"automatic_membership_email_domains"=>nil,
|
2015-04-09 22:17:28 -04:00
|
|
|
"automatic_membership_retroactive"=>false,
|
|
|
|
"title"=>nil,
|
2015-09-01 16:52:05 -04:00
|
|
|
"primary_group"=>false,
|
2015-12-07 06:39:28 -05:00
|
|
|
"grant_trust_level"=>nil,
|
2015-12-14 17:17:09 -05:00
|
|
|
"incoming_email"=>nil,
|
2016-03-02 13:18:17 -05:00
|
|
|
"has_messages"=>false,
|
2016-08-16 12:34:04 -04:00
|
|
|
"flair_url"=>nil,
|
2016-08-26 17:15:37 -04:00
|
|
|
"flair_bg_color"=>nil,
|
2016-12-05 04:13:47 -05:00
|
|
|
"flair_color"=>nil,
|
|
|
|
"bio_raw"=>nil,
|
2016-12-06 23:06:56 -05:00
|
|
|
"bio_cooked"=>nil,
|
2016-12-12 09:46:45 -05:00
|
|
|
"public"=>false,
|
2016-12-13 03:16:26 -05:00
|
|
|
"allow_membership_requests"=>false,
|
2017-04-20 17:24:21 -04:00
|
|
|
"full_name"=>group.full_name,
|
|
|
|
"default_notification_level"=>3
|
2015-01-09 12:04:02 -05:00
|
|
|
}])
|
2016-12-06 23:06:56 -05:00
|
|
|
|
2015-01-05 12:51:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-10-26 15:56:59 -04:00
|
|
|
context ".bulk" do
|
|
|
|
it "can assign users to a group by email or username" do
|
2017-03-02 13:16:01 -05:00
|
|
|
group = Fabricate(:group, name: "test", primary_group: true, title: 'WAT', grant_trust_level: 3)
|
|
|
|
user = Fabricate(:user, trust_level: 2)
|
|
|
|
user2 = Fabricate(:user, trust_level: 4)
|
2015-10-26 15:56:59 -04:00
|
|
|
|
|
|
|
xhr :put, :bulk_perform, group_id: group.id, users: [user.username.upcase, user2.email, 'doesnt_exist']
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.primary_group).to eq(group)
|
|
|
|
expect(user.title).to eq("WAT")
|
2017-03-02 13:16:01 -05:00
|
|
|
expect(user.trust_level).to eq(3)
|
2015-10-26 15:56:59 -04:00
|
|
|
|
|
|
|
user2.reload
|
|
|
|
expect(user2.primary_group).to eq(group)
|
2017-03-02 13:16:01 -05:00
|
|
|
expect(user2.title).to eq("WAT")
|
|
|
|
expect(user2.trust_level).to eq(4)
|
2017-04-26 15:00:05 -04:00
|
|
|
|
|
|
|
# verify JSON response
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['message']).to eq("2 users have been added to the group.")
|
|
|
|
expect(json['users_not_added'][0]).to eq("doesnt_exist")
|
2015-10-26 15:56:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 10:15:57 -04:00
|
|
|
context "#create" do
|
2015-01-05 12:51:45 -05:00
|
|
|
|
|
|
|
it "strip spaces on the group name" do
|
2016-11-29 03:25:02 -05:00
|
|
|
xhr :post, :create, { group: { name: " bob " } }
|
2015-01-05 12:51:45 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-05 12:51:45 -05:00
|
|
|
|
|
|
|
groups = Group.where(name: "bob").to_a
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(groups.count).to eq(1)
|
|
|
|
expect(groups[0].name).to eq("bob")
|
2015-01-05 12:51:45 -05:00
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
end
|
|
|
|
|
2017-06-02 10:15:57 -04:00
|
|
|
context "#update" do
|
|
|
|
it 'should update a group' do
|
|
|
|
group.add_owner(user)
|
2015-01-05 12:51:45 -05:00
|
|
|
|
2016-12-11 10:36:15 -05:00
|
|
|
expect do
|
2017-06-02 10:15:57 -04:00
|
|
|
xhr :put, :update, { id: group.id, group: {
|
|
|
|
visible: "false",
|
2016-12-20 02:14:35 -05:00
|
|
|
allow_membership_requests: "true"
|
|
|
|
} }
|
2017-06-02 10:15:57 -04:00
|
|
|
end.to change { GroupHistory.count }.by(2)
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
group.reload
|
|
|
|
expect(group.visible).to eq(false)
|
|
|
|
expect(group.allow_membership_requests).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignore name change on automatic group" do
|
|
|
|
expect do
|
|
|
|
xhr :put, :update, { id: 1, group: { name: "WAT" } }
|
2016-12-20 02:14:35 -05:00
|
|
|
end.to change { GroupHistory.count }.by(1)
|
2016-12-11 10:36:15 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2015-01-05 12:51:45 -05:00
|
|
|
|
|
|
|
group = Group.find(1)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(group.name).not_to eq("WAT")
|
2015-01-05 12:51:45 -05:00
|
|
|
end
|
2013-05-08 01:20:38 -04:00
|
|
|
|
2015-01-23 12:25:43 -05:00
|
|
|
it "doesn't launch the 'automatic group membership' job when it's not retroactive" do
|
|
|
|
Jobs.expects(:enqueue).never
|
2015-04-09 22:17:28 -04:00
|
|
|
group = Fabricate(:group)
|
2016-11-29 03:25:02 -05:00
|
|
|
xhr :put, :update, { id: group.id, group: { automatic_membership_retroactive: "false" } }
|
2015-01-23 12:25:43 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "launches the 'automatic group membership' job when it's retroactive" do
|
2015-04-09 22:17:28 -04:00
|
|
|
group = Fabricate(:group)
|
|
|
|
Jobs.expects(:enqueue).with(:automatic_group_membership, group_id: group.id)
|
2016-11-29 03:25:02 -05:00
|
|
|
xhr :put, :update, { id: group.id, group: { automatic_membership_retroactive: "true" } }
|
2015-01-23 12:25:43 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
end
|
2013-05-08 21:33:56 -04:00
|
|
|
|
2015-01-05 12:51:45 -05:00
|
|
|
context ".destroy" do
|
|
|
|
|
2013-07-21 22:37:01 -04:00
|
|
|
it "returns a 422 if the group is automatic" do
|
|
|
|
group = Fabricate(:group, automatic: true)
|
|
|
|
xhr :delete, :destroy, id: group.id
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Group.where(id: group.id).count).to eq(1)
|
2013-07-21 22:37:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is able to destroy a non-automatic group" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
xhr :delete, :destroy, id: group.id
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Group.where(id: group.id).count).to eq(0)
|
2013-07-21 22:37:01 -04:00
|
|
|
end
|
2015-01-05 12:51:45 -05:00
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
end
|
|
|
|
|
2015-01-05 12:51:45 -05:00
|
|
|
context ".refresh_automatic_groups" do
|
2013-05-08 21:33:56 -04:00
|
|
|
|
2015-01-05 12:51:45 -05:00
|
|
|
it "is able to refresh automatic groups" do
|
|
|
|
Group.expects(:refresh_automatic_groups!).returns(true)
|
2013-07-24 00:00:17 -04:00
|
|
|
|
2015-01-05 12:51:45 -05:00
|
|
|
xhr :post, :refresh_automatic_groups
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-07-24 00:00:17 -04:00
|
|
|
end
|
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
end
|