2017-07-27 14:39:47 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
RSpec.describe Admin::GroupsController do
|
2017-07-27 14:39:47 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
2017-08-02 19:58:35 +08:00
|
|
|
let(:group) { Fabricate(:group) }
|
2017-07-27 14:39:47 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
2017-08-24 14:28:18 +08:00
|
|
|
describe '#create' do
|
2017-07-27 14:39:47 +08:00
|
|
|
it 'should work' do
|
2017-08-31 12:06:56 +08:00
|
|
|
post "/admin/groups.json", params: {
|
|
|
|
group: {
|
|
|
|
name: 'testing',
|
|
|
|
usernames: [admin.username, user.username].join(","),
|
|
|
|
owner_usernames: [user.username].join(","),
|
|
|
|
allow_membership_requests: true,
|
|
|
|
membership_request_template: 'Testing',
|
|
|
|
}
|
2017-07-27 14:39:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-27 14:39:47 +08:00
|
|
|
|
|
|
|
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)
|
2017-08-08 17:53:02 +08:00
|
|
|
expect(group.membership_request_template).to eq('Testing')
|
2017-07-27 14:39:47 +08:00
|
|
|
end
|
|
|
|
end
|
2017-08-02 19:58:35 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#add_owners' do
|
2017-08-02 19:58:35 +08:00
|
|
|
it 'should work' do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/admin/groups/#{group.id}/owners.json", params: {
|
|
|
|
group: {
|
|
|
|
usernames: [user.username, admin.username].join(",")
|
|
|
|
}
|
2017-08-02 19:58:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:33:03 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body["usernames"]).to contain_exactly(user.username, admin.username)
|
2017-08-02 19:58:35 +08:00
|
|
|
|
|
|
|
expect(group.group_users.where(owner: true).map(&:user))
|
|
|
|
.to contain_exactly(user, admin)
|
|
|
|
end
|
|
|
|
end
|
2018-04-06 17:11:00 +08:00
|
|
|
|
2018-05-07 13:56:27 +08:00
|
|
|
describe '#remove_owner' do
|
|
|
|
it 'should work' 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
|
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
describe "#bulk_perform" do
|
|
|
|
let(:group) do
|
|
|
|
Fabricate(:group,
|
|
|
|
name: "test",
|
|
|
|
primary_group: true,
|
|
|
|
title: 'WAT',
|
|
|
|
grant_trust_level: 3
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user, trust_level: 2) }
|
|
|
|
let(:user2) { Fabricate(:user, trust_level: 4) }
|
|
|
|
|
|
|
|
it "can assign users to a group by email or username" do
|
2018-05-31 15:53:49 +08:00
|
|
|
SiteSetting.queue_jobs = false
|
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
put "/admin/groups/bulk.json", params: {
|
|
|
|
group_id: group.id, users: [user.username.upcase, user2.email, 'doesnt_exist']
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.primary_group).to eq(group)
|
|
|
|
expect(user.title).to eq("WAT")
|
|
|
|
expect(user.trust_level).to eq(3)
|
|
|
|
|
|
|
|
user2.reload
|
|
|
|
expect(user2.primary_group).to eq(group)
|
|
|
|
expect(user2.title).to eq("WAT")
|
|
|
|
expect(user2.trust_level).to eq(4)
|
|
|
|
|
|
|
|
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")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#destroy" do
|
|
|
|
it 'should return the right response for an invalid group_id' do
|
|
|
|
delete "/admin/groups/123.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when group is automatic' do
|
|
|
|
it "returns the right response" do
|
|
|
|
group.update!(automatic: true)
|
|
|
|
|
|
|
|
delete "/admin/groups/#{group.id}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Group.find(group.id)).to eq(group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for a non automatic group' 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
|
|
|
|
end
|
|
|
|
end
|
2017-07-27 14:39:47 +08:00
|
|
|
end
|