2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
RSpec.describe Admin::GroupsController do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-11-03 11:42:44 +08:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2017-07-27 14:39:47 +08:00
|
|
|
|
2017-08-24 14:28:18 +08:00
|
|
|
describe '#create' do
|
2019-06-06 10:05:33 +08:00
|
|
|
let(:group_params) do
|
|
|
|
{
|
2017-08-31 12:06:56 +08:00
|
|
|
group: {
|
|
|
|
name: 'testing',
|
|
|
|
usernames: [admin.username, user.username].join(","),
|
|
|
|
owner_usernames: [user.username].join(","),
|
|
|
|
allow_membership_requests: true,
|
|
|
|
membership_request_template: 'Testing',
|
2019-08-14 21:30:04 +08:00
|
|
|
members_visibility_level: Group.visibility_levels[:staff]
|
2017-08-31 12:06:56 +08:00
|
|
|
}
|
2017-07-27 14:39:47 +08:00
|
|
|
}
|
2019-06-06 10:05:33 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2017-07-27 14:39:47 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'should work' do
|
|
|
|
post "/admin/groups.json", params: group_params
|
2017-07-27 14:39:47 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-27 14:39:47 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
group = Group.last
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(group.name).to eq('testing')
|
|
|
|
expect(group.users).to contain_exactly(admin, user)
|
|
|
|
expect(group.allow_membership_requests).to eq(true)
|
|
|
|
expect(group.membership_request_template).to eq('Testing')
|
|
|
|
expect(group.members_visibility_level).to eq(Group.visibility_levels[:staff])
|
2019-06-06 10:05:33 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with custom_fields" do
|
|
|
|
before do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.register_editable_group_custom_field :test
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "only updates allowed user fields" do
|
|
|
|
params = group_params
|
|
|
|
params[:group].merge!(custom_fields: { test: :hello1, test2: :hello2 })
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/groups.json", params: params
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
group = Group.last
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(group.custom_fields['test']).to eq('hello1')
|
|
|
|
expect(group.custom_fields['test2']).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is secure when there are no registered editable fields" do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
params = group_params
|
|
|
|
params[:group].merge!(custom_fields: { test: :hello1, test2: :hello2 })
|
|
|
|
|
|
|
|
post "/admin/groups.json", params: params
|
|
|
|
|
|
|
|
group = Group.last
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(group.custom_fields['test']).to be_blank
|
|
|
|
expect(group.custom_fields['test2']).to be_blank
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context 'with Group.plugin_permitted_params' do
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'filter unpermitted params' do
|
|
|
|
params = group_params
|
|
|
|
params[:group].merge!(allow_unknown_sender_topic_replies: true)
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/groups.json", params: params
|
|
|
|
expect(Group.last.allow_unknown_sender_topic_replies).to eq(false)
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'allows plugin to allow custom params' do
|
|
|
|
params = group_params
|
|
|
|
params[:group].merge!(allow_unknown_sender_topic_replies: true)
|
|
|
|
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.register_group_param :allow_unknown_sender_topic_replies
|
|
|
|
|
|
|
|
post "/admin/groups.json", params: params
|
|
|
|
expect(Group.last.allow_unknown_sender_topic_replies).to eq(true)
|
|
|
|
end
|
2019-06-06 10:05:33 +08:00
|
|
|
end
|
|
|
|
end
|
2021-09-06 08:18:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates group" do
|
|
|
|
expect do
|
|
|
|
post "/admin/groups.json", params: group_params
|
|
|
|
end.to change { Group.count }.by(1)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
group = Group.last
|
|
|
|
|
|
|
|
expect(group.name).to eq('testing')
|
|
|
|
expect(group.users).to contain_exactly(admin, user)
|
|
|
|
expect(group.allow_membership_requests).to eq(true)
|
|
|
|
expect(group.membership_request_template).to eq('Testing')
|
|
|
|
expect(group.members_visibility_level).to eq(Group.visibility_levels[:staff])
|
|
|
|
end
|
2021-09-06 08:18:51 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
2021-09-06 08:18:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents creation with a 403 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/groups.json", params: group_params
|
|
|
|
end.to_not change { Group.count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))
|
|
|
|
end
|
2021-09-06 08:18:51 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2021-09-06 08:18:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2021-09-06 08:18:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents creation with a 404 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/groups.json", params: group_params
|
|
|
|
end.to_not change { Group.count }
|
2021-09-06 08:18:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2021-09-06 08:18:51 +08:00
|
|
|
end
|
|
|
|
end
|
2017-07-27 14:39:47 +08:00
|
|
|
end
|
2017-08-02 19:58:35 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#add_owners' do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it 'should work' do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: [user.username, admin.username].join(",")
|
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
}
|
2017-08-02 19:58:35 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-03-26 17:33:03 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
response_body = response.parsed_body
|
2018-03-26 17:33:03 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response_body["usernames"]).to contain_exactly(user.username, admin.username)
|
2017-08-02 19:58:35 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(group.group_users.where(owner: true).map(&:user))
|
|
|
|
.to contain_exactly(user, admin)
|
|
|
|
end
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'returns not-found error when there is no group' do
|
|
|
|
group.destroy!
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: user.username
|
|
|
|
}
|
2020-04-21 09:50:20 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow adding owners to an automatic group' do
|
|
|
|
group.update!(automatic: true)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: user.username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end.to_not change { group.group_users.count }
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(response.parsed_body["errors"]).to eq(["You cannot modify an automatic group"])
|
|
|
|
end
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'does not notify users when the param is not present' do
|
2020-04-21 09:50:20 +08:00
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: user.username
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
topic = Topic.find_by(
|
|
|
|
title: I18n.t("system_messages.user_added_to_group_as_owner.subject_template", group_name: group.name),
|
|
|
|
archetype: "private_message"
|
|
|
|
)
|
|
|
|
expect(topic.nil?).to eq(true)
|
|
|
|
end
|
2020-08-04 23:02:01 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'notifies users when the param is present' do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: user.username,
|
|
|
|
notify_users: true
|
|
|
|
}
|
2020-08-04 23:02:01 +08:00
|
|
|
}
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-08-04 23:02:01 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
topic = Topic.find_by(
|
|
|
|
title: I18n.t("system_messages.user_added_to_group_as_owner.subject_template", group_name: group.name),
|
|
|
|
archetype: "private_message"
|
|
|
|
)
|
|
|
|
expect(topic.nil?).to eq(false)
|
|
|
|
expect(topic.topic_users.map(&:user_id)).to include(-1, user.id)
|
|
|
|
end
|
2020-08-04 23:02:01 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds owners" do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: [user.username, admin.username, moderator.username].join(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response_body = response.parsed_body
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response_body["usernames"]).to contain_exactly(
|
|
|
|
user.username,
|
|
|
|
admin.username,
|
|
|
|
moderator.username
|
|
|
|
)
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user))
|
|
|
|
.to contain_exactly(user, admin, moderator)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents adding of owners with a 403 response" do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: [user.username, admin.username, moderator.username].join(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user))
|
|
|
|
.to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "prevents adding of owners with a 404 response" do
|
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: [user.username, admin.username].join(",")
|
|
|
|
}
|
2020-08-04 23:02:01 +08:00
|
|
|
}
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user))
|
|
|
|
.to be_empty
|
|
|
|
end
|
2020-08-04 23:02:01 +08:00
|
|
|
end
|
2017-08-02 19:58:35 +08:00
|
|
|
end
|
2018-04-06 17:11:00 +08:00
|
|
|
|
2018-05-07 13:56:27 +08:00
|
|
|
describe '#remove_owner' do
|
2021-02-03 22:11:08 +08:00
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:user3) { Fabricate(:user) }
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2018-05-07 13:56:27 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'should work' do
|
|
|
|
group.add_owner(user)
|
2018-05-07 13:56:27 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
|
|
|
}
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(group.group_users.where(owner: true)).to eq([])
|
|
|
|
end
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'should work with multiple users' do
|
|
|
|
group.add_owner(user)
|
|
|
|
group.add_owner(user3)
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: "#{user.username},#{user2.username},#{user3.username}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(group.group_users.where(owner: true)).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns not-found error when there is no group' do
|
|
|
|
group.destroy!
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow removing owners from an automatic group' do
|
|
|
|
group.update!(automatic: true)
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
2021-02-03 22:11:08 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(response.parsed_body["errors"]).to eq(["You cannot modify an automatic group"])
|
|
|
|
end
|
2021-02-03 22:11:08 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes owner" do
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(group.group_users.where(owner: true)).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents owner removal with a 403 response" do
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user)).to contain_exactly(user)
|
|
|
|
end
|
|
|
|
end
|
2020-04-21 09:50:20 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents owner removal with a 404 response" do
|
|
|
|
group.add_owner(user)
|
2020-04-21 09:50:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
delete "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user)).to contain_exactly(user)
|
|
|
|
end
|
2020-04-21 09:50:20 +08:00
|
|
|
end
|
2018-05-07 13:56:27 +08:00
|
|
|
end
|
|
|
|
|
2021-02-03 22:11:08 +08:00
|
|
|
describe "#set_primary" do
|
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:user3) { Fabricate(:user) }
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'sets with multiple users' do
|
|
|
|
user2.update!(primary_group_id: group.id)
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/#{group.id}/primary.json", params: {
|
|
|
|
group: { usernames: "#{user.username},#{user2.username},#{user3.username}" },
|
|
|
|
primary: "true"
|
|
|
|
}
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(User.where(primary_group_id: group.id).size).to eq(3)
|
|
|
|
end
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'unsets with multiple users' do
|
|
|
|
user.update!(primary_group_id: group.id)
|
|
|
|
user3.update!(primary_group_id: group.id)
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/#{group.id}/primary.json", params: {
|
|
|
|
group: { usernames: "#{user.username},#{user2.username},#{user3.username}" },
|
|
|
|
primary: "false"
|
|
|
|
}
|
2021-02-03 22:11:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(User.where(primary_group_id: group.id).size).to eq(0)
|
|
|
|
end
|
2018-04-06 17:11:00 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "sets multiple primary users" do
|
|
|
|
user2.update!(primary_group_id: group.id)
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/#{group.id}/primary.json", params: {
|
|
|
|
group: { usernames: "#{user.username},#{user2.username},#{user3.username}" },
|
|
|
|
primary: "true"
|
|
|
|
}
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(User.where(primary_group_id: group.id).size).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2022-11-11 19:06:05 +08:00
|
|
|
it "prevents setting of primary group with a 403 response" do
|
2022-11-03 11:42:44 +08:00
|
|
|
user2.update!(primary_group_id: group.id)
|
2018-04-06 17:11:00 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/#{group.id}/primary.json", params: {
|
|
|
|
group: { usernames: "#{user.username},#{user2.username},#{user3.username}" },
|
|
|
|
primary: "true"
|
|
|
|
}
|
2018-04-06 17:11:00 +08:00
|
|
|
|
2022-11-11 19:06:05 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))
|
|
|
|
expect(User.where(primary_group_id: group.id).size).to eq(1)
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2018-04-06 17:11:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2018-04-06 17:11:00 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents setting of primary user with a 404 response" do
|
|
|
|
user2.update!(primary_group_id: group.id)
|
|
|
|
|
|
|
|
put "/admin/groups/#{group.id}/primary.json", params: {
|
|
|
|
group: { usernames: "#{user.username},#{user2.username},#{user3.username}" },
|
|
|
|
primary: "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(User.where(primary_group_id: group.id).size).to eq(1)
|
2018-04-06 17:11:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-26 13:40:09 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#destroy" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2020-05-26 13:40:09 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'should return the right response for an invalid group_id' do
|
|
|
|
max_id = Group.maximum(:id).to_i
|
|
|
|
delete "/admin/groups/#{max_id + 1}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2020-05-26 13:40:09 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'logs when a group is destroyed' do
|
|
|
|
delete "/admin/groups/#{group.id}.json"
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
history = UserHistory.where(acting_user: admin).last
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(history).to be_present
|
|
|
|
expect(history.details).to include("name: #{group.name}")
|
|
|
|
expect(history.details).to include("id: #{group.id}")
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it 'logs the grant_trust_level attribute' do
|
|
|
|
trust_level = TrustLevel[4]
|
|
|
|
group.update!(grant_trust_level: trust_level)
|
|
|
|
delete "/admin/groups/#{group.id}.json"
|
|
|
|
|
|
|
|
history = UserHistory.where(acting_user: admin).last
|
|
|
|
|
|
|
|
expect(history).to be_present
|
|
|
|
expect(history.details).to include("grant_trust_level: #{trust_level}")
|
|
|
|
expect(history.details).to include("name: #{group.name}")
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when group is automatic" do
|
|
|
|
it "returns the right response" do
|
|
|
|
group.update!(automatic: true)
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
delete "/admin/groups/#{group.id}.json"
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Group.find(group.id)).to eq(group)
|
|
|
|
end
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when group is not automatic" do
|
|
|
|
it "returns the right response" do
|
|
|
|
delete "/admin/groups/#{group.id}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Group.find_by(id: group.id)).to eq(nil)
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "group deletion not allowed" do
|
|
|
|
it "prevents deletion with a 404 response" do
|
|
|
|
expect do
|
|
|
|
delete "/admin/groups/#{group.id}.json"
|
|
|
|
end.not_to change { Group.count }
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "group deletion not allowed"
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "group deletion not allowed"
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "group deletion not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#automatic_membership_count' do
|
|
|
|
context "when logged in as admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it 'returns count of users whose emails match the domain' do
|
|
|
|
Fabricate(:user, email: 'user1@somedomain.org')
|
|
|
|
Fabricate(:user, email: 'user1@somedomain.com')
|
|
|
|
Fabricate(:user, email: 'user1@notsomedomain.com')
|
|
|
|
group = Fabricate(:group)
|
|
|
|
|
|
|
|
put "/admin/groups/automatic_membership_count.json", params: {
|
|
|
|
automatic_membership_email_domains: 'somedomain.org|somedomain.com',
|
|
|
|
id: group.id
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["user_count"]).to eq(2)
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "doesn't responde with 500 if domain is invalid" do
|
|
|
|
group = Fabricate(:group)
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/groups/automatic_membership_count.json", params: {
|
|
|
|
automatic_membership_email_domains: '@somedomain.org|@somedomain.com',
|
|
|
|
id: group.id
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["user_count"]).to eq(0)
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "automatic membership count inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
put "/admin/groups/automatic_membership_count.json", params: {
|
|
|
|
automatic_membership_email_domains: 'somedomain.org|somedomain.com',
|
|
|
|
id: group.id
|
2020-10-20 04:30:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "with moderators_manage_categories_and_groups enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "automatic membership count inaccessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with moderators_manage_categories_and_groups disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = false
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "automatic membership count inaccessible"
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "automatic membership count inaccessible"
|
|
|
|
end
|
2020-10-20 04:30:21 +08:00
|
|
|
end
|
2017-07-27 14:39:47 +08:00
|
|
|
end
|