2016-11-25 16:45:15 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe GroupsController do
|
2016-12-07 17:28:43 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2017-07-21 14:12:24 +08:00
|
|
|
let(:group) { Fabricate(:group, users: [user]) }
|
2018-03-21 16:32:08 +08:00
|
|
|
let(:moderator_group_id) { Group::AUTO_GROUPS[:moderators] }
|
2018-04-10 09:22:01 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2016-11-29 16:25:02 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#index' do
|
2018-03-20 15:50:46 +08:00
|
|
|
let(:staff_group) do
|
2017-07-04 03:26:46 +08:00
|
|
|
Fabricate(:group, name: '0000', visibility_level: Group.visibility_levels[:staff])
|
2016-12-20 14:26:15 +08:00
|
|
|
end
|
2016-12-14 17:26:16 +08:00
|
|
|
|
2016-12-22 14:14:03 +08:00
|
|
|
context 'when group directory is disabled' do
|
2018-04-10 09:22:01 +08:00
|
|
|
before do
|
2017-01-28 10:55:49 +08:00
|
|
|
SiteSetting.enable_group_directory = false
|
2018-04-10 09:22:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should deny access for an anon user' do
|
|
|
|
get "/groups.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should deny access for a normal user' do
|
|
|
|
get "/groups.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not deny access for an admin' do
|
|
|
|
sign_in(admin)
|
|
|
|
get "/groups.json"
|
2017-01-28 10:55:49 +08:00
|
|
|
|
2018-04-10 09:22:01 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not deny access for a moderator' do
|
|
|
|
sign_in(moderator)
|
2016-12-22 14:14:03 +08:00
|
|
|
get "/groups.json"
|
2018-04-10 09:22:01 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2016-12-22 14:14:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-19 17:16:51 +08:00
|
|
|
context 'searchable' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
other_group = Fabricate(:group, name: 'testing')
|
|
|
|
get "/groups.json", params: { filter: 'test' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-20 17:08:50 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body["groups"].first["id"]).to eq(other_group.id)
|
|
|
|
|
|
|
|
expect(response_body["load_more_groups"]).to eq(
|
|
|
|
"/groups?filter=test&page=1"
|
|
|
|
)
|
2018-03-19 17:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-19 16:14:50 +08:00
|
|
|
context 'sortable' do
|
|
|
|
let!(:other_group) { Fabricate(:group, name: "zzzzzz", users: [user]) }
|
|
|
|
|
|
|
|
%w{
|
|
|
|
desc
|
|
|
|
asc
|
|
|
|
}.each do |order|
|
|
|
|
context "#{order} order" do
|
|
|
|
it 'should return the right response' do
|
|
|
|
is_asc = order == 'asc'
|
|
|
|
params = { order: 'name' }
|
|
|
|
params.merge!(asc: true) if is_asc
|
|
|
|
group
|
|
|
|
get "/groups.json", params: params
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2018-03-21 16:32:08 +08:00
|
|
|
group_ids = [moderator_group_id, group.id, other_group.id]
|
2018-03-19 16:14:50 +08:00
|
|
|
group_ids.reverse! if !is_asc
|
|
|
|
|
2018-08-20 17:08:50 +08:00
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body["groups"].map { |group| group["id"] })
|
2018-03-19 16:14:50 +08:00
|
|
|
.to eq(group_ids)
|
2018-08-20 17:08:50 +08:00
|
|
|
|
|
|
|
expect(response_body["load_more_groups"]).to eq(
|
|
|
|
"/groups?#{is_asc ? 'asc=true&' : '' }order=name&page=1"
|
|
|
|
)
|
2018-03-19 16:14:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ascending order' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
group
|
|
|
|
get "/groups.json", params: { order: 'name' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2018-08-20 17:08:50 +08:00
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body["groups"].map { |group| group["id"] })
|
2018-03-21 16:32:08 +08:00
|
|
|
.to eq([other_group.id, group.id, moderator_group_id])
|
2018-08-20 17:08:50 +08:00
|
|
|
|
|
|
|
expect(response_body["load_more_groups"]).to eq(
|
|
|
|
"/groups?order=name&page=1"
|
|
|
|
)
|
2018-03-19 16:14:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-20 14:26:15 +08:00
|
|
|
it 'should return the right response' do
|
2017-07-21 14:12:24 +08:00
|
|
|
group
|
2018-03-20 15:50:46 +08:00
|
|
|
staff_group
|
2016-12-14 17:26:16 +08:00
|
|
|
get "/groups.json"
|
|
|
|
|
2018-03-21 16:32:08 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-14 17:26:16 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
group_ids = response_body["groups"].map { |g| g["id"] }
|
|
|
|
|
2017-07-04 03:26:46 +08:00
|
|
|
expect(group_ids).to include(group.id)
|
|
|
|
expect(group_ids).to_not include(staff_group.id)
|
2016-12-14 17:26:16 +08:00
|
|
|
expect(response_body["load_more_groups"]).to eq("/groups?page=1")
|
2018-03-21 16:32:08 +08:00
|
|
|
expect(response_body["total_rows_groups"]).to eq(2)
|
2018-03-20 15:50:46 +08:00
|
|
|
|
|
|
|
expect(response_body["extras"]["type_filters"].map(&:to_sym)).to eq(
|
|
|
|
described_class::TYPE_FILTERS.keys - [:my, :owner, :automatic]
|
|
|
|
)
|
2016-12-14 17:26:16 +08:00
|
|
|
end
|
2016-12-20 14:26:15 +08:00
|
|
|
|
2018-03-21 16:32:08 +08:00
|
|
|
context 'viewing groups of another user' do
|
|
|
|
describe 'when an invalid username is given' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
group
|
|
|
|
get "/groups.json", params: { username: 'asdasd' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
group
|
|
|
|
sign_in(user2)
|
|
|
|
|
|
|
|
get "/groups.json", params: { username: user.username }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
group_ids = response_body["groups"].map { |g| g["id"] }
|
|
|
|
|
|
|
|
expect(group_ids).to contain_exactly(group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-20 14:26:15 +08:00
|
|
|
context 'viewing as an admin' do
|
2018-03-20 15:50:46 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
2016-12-21 21:21:39 +08:00
|
|
|
sign_in(admin)
|
|
|
|
group.add(admin)
|
2018-03-19 18:28:57 +08:00
|
|
|
group.add_owner(admin)
|
2018-03-20 15:50:46 +08:00
|
|
|
end
|
2016-12-20 14:26:15 +08:00
|
|
|
|
2018-03-20 15:50:46 +08:00
|
|
|
it 'should return the right response' do
|
|
|
|
staff_group
|
2016-12-20 14:26:15 +08:00
|
|
|
get "/groups.json"
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-20 14:26:15 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
group_ids = response_body["groups"].map { |g| g["id"] }
|
2018-03-19 18:28:57 +08:00
|
|
|
group_body = response_body["groups"].find { |g| g["id"] == group.id }
|
2016-12-20 14:26:15 +08:00
|
|
|
|
2018-03-19 18:28:57 +08:00
|
|
|
expect(group_body["is_group_user"]).to eq(true)
|
|
|
|
expect(group_body["is_group_owner"]).to eq(true)
|
2017-07-04 03:26:46 +08:00
|
|
|
expect(group_ids).to include(group.id, staff_group.id)
|
2016-12-20 14:26:15 +08:00
|
|
|
expect(response_body["load_more_groups"]).to eq("/groups?page=1")
|
2016-12-20 14:38:27 +08:00
|
|
|
expect(response_body["total_rows_groups"]).to eq(10)
|
2018-03-20 15:50:46 +08:00
|
|
|
|
|
|
|
expect(response_body["extras"]["type_filters"].map(&:to_sym)).to eq(
|
|
|
|
described_class::TYPE_FILTERS.keys
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filterable by type' do
|
|
|
|
def expect_type_to_return_right_groups(type, expected_group_ids)
|
|
|
|
get "/groups.json", params: { type: type }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
group_ids = response_body["groups"].map { |g| g["id"] }
|
|
|
|
|
2018-03-21 09:25:42 +08:00
|
|
|
expect(response_body["total_rows_groups"]).to eq(expected_group_ids.count)
|
2018-03-20 15:50:46 +08:00
|
|
|
expect(group_ids).to contain_exactly(*expected_group_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'my groups' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
expect_type_to_return_right_groups('my', [group.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'owner groups' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
group2 = Fabricate(:group)
|
2018-06-05 15:29:17 +08:00
|
|
|
_group3 = Fabricate(:group)
|
2018-03-20 15:50:46 +08:00
|
|
|
group2.add_owner(admin)
|
|
|
|
|
|
|
|
expect_type_to_return_right_groups('owner', [group.id, group2.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'automatic groups' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
expect_type_to_return_right_groups(
|
|
|
|
'automatic',
|
|
|
|
Group::AUTO_GROUP_IDS.keys - [0]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'public groups' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
group2 = Fabricate(:group, public_admission: true)
|
|
|
|
|
|
|
|
expect_type_to_return_right_groups('public', [group2.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'close groups' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
group2 = Fabricate(:group, public_admission: false)
|
2018-06-05 15:29:17 +08:00
|
|
|
_group3 = Fabricate(:group, public_admission: true)
|
2018-03-20 15:50:46 +08:00
|
|
|
|
|
|
|
expect_type_to_return_right_groups('close', [group.id, group2.id])
|
|
|
|
end
|
|
|
|
end
|
2016-12-20 14:26:15 +08:00
|
|
|
end
|
|
|
|
end
|
2016-12-14 17:26:16 +08:00
|
|
|
end
|
|
|
|
|
2018-02-01 04:04:09 +08:00
|
|
|
describe '#show' do
|
2018-03-29 14:57:10 +08:00
|
|
|
it "ensures the group can be seen" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
group.update!(visibility_level: Group.visibility_levels[:owners])
|
|
|
|
|
|
|
|
get "/groups/#{group.name}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the right response" do
|
|
|
|
sign_in(user)
|
|
|
|
get "/groups/#{group.name}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body['group']['id']).to eq(group.id)
|
|
|
|
expect(response_body['extras']["visible_group_names"]).to eq([group.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
it "returns the right response" do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
get "/groups/#{group.name}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body['group']['id']).to eq(group.id)
|
|
|
|
|
|
|
|
groups = Group::AUTO_GROUPS.keys
|
|
|
|
groups.delete(:everyone)
|
|
|
|
groups.push(group.name)
|
|
|
|
|
|
|
|
expect(response_body['extras']["visible_group_names"])
|
|
|
|
.to contain_exactly(*groups.map(&:to_s))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-01 04:04:09 +08:00
|
|
|
it 'should respond to HTML' do
|
|
|
|
group.update_attribute(:bio_cooked, 'testing group bio')
|
|
|
|
|
|
|
|
get "/groups/#{group.name}.html"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(response.body).to have_tag(:meta, with: {
|
|
|
|
property: 'og:title', content: group.name
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.body).to have_tag(:meta, with: {
|
|
|
|
property: 'og:description', content: group.bio_cooked
|
|
|
|
})
|
|
|
|
end
|
2018-02-27 13:43:22 +08:00
|
|
|
|
|
|
|
describe 'when viewing activity filters' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
get "/groups/#{group.name}/activity/posts.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2018-03-29 14:57:10 +08:00
|
|
|
response_body = JSON.parse(response.body)['group']
|
2018-02-27 13:43:22 +08:00
|
|
|
|
|
|
|
expect(response_body["id"]).to eq(group.id)
|
|
|
|
end
|
|
|
|
end
|
2018-02-01 04:04:09 +08:00
|
|
|
end
|
|
|
|
|
2018-03-29 14:57:10 +08:00
|
|
|
describe "#posts" do
|
|
|
|
it "ensures the group can be seen" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
group.update!(visibility_level: Group.visibility_levels[:owners])
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/posts.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls `posts_for` and responds with JSON" do
|
|
|
|
sign_in(user)
|
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
get "/groups/#{group.name}/posts.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body).first["id"]).to eq(post.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#members" do
|
2018-06-29 08:14:50 +08:00
|
|
|
|
|
|
|
it "returns correct error code with invalid params" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json?limit=-1"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json?offset=-1"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
2018-03-29 14:57:10 +08:00
|
|
|
it "ensures the group can be seen" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
group.update!(visibility_level: Group.visibility_levels[:owners])
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures that membership can be paginated" do
|
2018-11-01 12:33:28 +08:00
|
|
|
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
first_user = Fabricate(:user)
|
|
|
|
group.add(first_user)
|
|
|
|
|
|
|
|
freeze_time 1.day.from_now
|
|
|
|
|
|
|
|
4.times { group.add(Fabricate(:user)) }
|
2018-03-29 14:57:10 +08:00
|
|
|
usernames = group.users.map { |m| m.username }.sort
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json", params: { limit: 3 }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m['username'] }).to eq(usernames[0..2])
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json", params: { limit: 3, offset: 3 }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.map { |m| m['username'] }).to eq(usernames[3..5])
|
2018-11-01 12:33:28 +08:00
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json", params: { order: 'added_at', desc: true }
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
|
|
|
expect(members.last['added_at']).to eq(first_user.created_at.as_json)
|
2018-03-29 14:57:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#posts_feed' do
|
|
|
|
it 'renders RSS' do
|
|
|
|
get "/groups/#{group.name}/posts.rss"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#mentions_feed' do
|
|
|
|
it 'renders RSS' do
|
|
|
|
get "/groups/#{group.name}/mentions.rss"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when disabled' do
|
|
|
|
SiteSetting.enable_mentions = false
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/mentions.rss"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#mentionable' do
|
2016-11-25 16:45:15 +08:00
|
|
|
it "should return the right response" do
|
2016-12-07 17:28:43 +08:00
|
|
|
sign_in(user)
|
2016-11-25 16:45:15 +08:00
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
get "/groups/#{group.name}/mentionable.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-11-25 16:45:15 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["mentionable"]).to eq(false)
|
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
group.update_attributes!(
|
|
|
|
mentionable_level: Group::ALIAS_LEVELS[:everyone],
|
|
|
|
visibility_level: Group.visibility_levels[:staff]
|
|
|
|
)
|
2016-11-25 16:45:15 +08:00
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
get "/groups/#{group.name}/mentionable.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-11-25 16:45:15 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["mentionable"]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-11-29 16:25:02 +08:00
|
|
|
|
2018-06-05 18:56:51 +08:00
|
|
|
describe '#messageable' do
|
|
|
|
it "should return the right response" do
|
|
|
|
sign_in(user)
|
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
get "/groups/#{group.name}/messageable.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-05 18:56:51 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["messageable"]).to eq(false)
|
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
group.update!(
|
|
|
|
messageable_level: Group::ALIAS_LEVELS[:everyone],
|
|
|
|
visibility_level: Group.visibility_levels[:staff]
|
|
|
|
)
|
2018-06-05 18:56:51 +08:00
|
|
|
|
2018-06-06 09:42:09 +08:00
|
|
|
get "/groups/#{group.name}/messageable.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-05 18:56:51 +08:00
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body["messageable"]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#update' do
|
2017-07-28 10:37:10 +08:00
|
|
|
let(:group) do
|
|
|
|
Fabricate(:group,
|
|
|
|
name: 'test',
|
|
|
|
users: [user],
|
|
|
|
public_admission: false,
|
|
|
|
public_exit: false
|
|
|
|
)
|
|
|
|
end
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2016-11-29 16:25:02 +08:00
|
|
|
context "when user is group owner" do
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
2016-12-07 17:28:43 +08:00
|
|
|
sign_in(user)
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able update the group" do
|
2018-04-05 13:53:00 +08:00
|
|
|
group.update!(
|
|
|
|
allow_membership_requests: false,
|
|
|
|
visibility_level: 2,
|
|
|
|
mentionable_level: 2,
|
|
|
|
messageable_level: 2,
|
|
|
|
default_notification_level: 0,
|
|
|
|
grant_trust_level: 0,
|
2018-04-06 11:44:58 +08:00
|
|
|
automatic_membership_retroactive: false
|
2018-04-05 13:53:00 +08:00
|
|
|
)
|
2016-12-20 15:14:35 +08:00
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
expect do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}.json", params: {
|
|
|
|
group: {
|
2018-04-05 13:53:00 +08:00
|
|
|
mentionable_level: 1,
|
|
|
|
messageable_level: 1,
|
|
|
|
visibility_level: 1,
|
|
|
|
automatic_membership_email_domains: 'test.org',
|
|
|
|
automatic_membership_retroactive: true,
|
|
|
|
title: 'haha',
|
|
|
|
primary_group: true,
|
|
|
|
grant_trust_level: 1,
|
|
|
|
incoming_email: 'test@mail.org',
|
2017-08-31 12:06:56 +08:00
|
|
|
flair_bg_color: 'FFF',
|
|
|
|
flair_color: 'BBB',
|
|
|
|
flair_url: 'fa-adjust',
|
|
|
|
bio_raw: 'testing',
|
|
|
|
full_name: 'awesome team',
|
|
|
|
public_admission: true,
|
|
|
|
public_exit: true,
|
2017-11-02 08:51:43 +08:00
|
|
|
allow_membership_requests: true,
|
|
|
|
membership_request_template: 'testing',
|
2018-04-05 13:53:00 +08:00
|
|
|
default_notification_level: 1,
|
|
|
|
name: 'testing'
|
2017-08-31 12:06:56 +08:00
|
|
|
}
|
2017-07-28 09:20:09 +08:00
|
|
|
}
|
2018-04-06 11:44:58 +08:00
|
|
|
end.to change { GroupHistory.count }.by(13)
|
2016-11-29 16:25:02 +08:00
|
|
|
|
2018-03-27 12:18:03 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-11-29 16:25:02 +08:00
|
|
|
|
|
|
|
group.reload
|
|
|
|
|
|
|
|
expect(group.flair_bg_color).to eq('FFF')
|
|
|
|
expect(group.flair_color).to eq('BBB')
|
|
|
|
expect(group.flair_url).to eq('fa-adjust')
|
2016-12-05 16:18:24 +08:00
|
|
|
expect(group.bio_raw).to eq('testing')
|
2016-12-13 16:16:26 +08:00
|
|
|
expect(group.full_name).to eq('awesome team')
|
2017-07-28 10:37:10 +08:00
|
|
|
expect(group.public_admission).to eq(true)
|
|
|
|
expect(group.public_exit).to eq(true)
|
2016-12-12 22:46:45 +08:00
|
|
|
expect(group.allow_membership_requests).to eq(true)
|
2017-11-02 08:51:43 +08:00
|
|
|
expect(group.membership_request_template).to eq('testing')
|
2018-03-27 12:18:03 +08:00
|
|
|
expect(group.name).to eq('test')
|
2018-04-06 11:44:58 +08:00
|
|
|
expect(group.visibility_level).to eq(2)
|
2018-04-05 13:53:00 +08:00
|
|
|
expect(group.mentionable_level).to eq(1)
|
|
|
|
expect(group.messageable_level).to eq(1)
|
|
|
|
expect(group.default_notification_level).to eq(1)
|
2018-04-06 11:44:58 +08:00
|
|
|
expect(group.automatic_membership_email_domains).to eq(nil)
|
|
|
|
expect(group.automatic_membership_retroactive).to eq(false)
|
2018-04-05 13:53:00 +08:00
|
|
|
expect(group.title).to eq('haha')
|
2018-04-06 11:44:58 +08:00
|
|
|
expect(group.primary_group).to eq(false)
|
|
|
|
expect(group.incoming_email).to eq(nil)
|
|
|
|
expect(group.grant_trust_level).to eq(0)
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
2018-04-05 16:31:09 +08:00
|
|
|
|
|
|
|
it 'should not be allowed to update automatic groups' do
|
|
|
|
group = Group.find(Group::AUTO_GROUPS[:admins])
|
|
|
|
|
|
|
|
put "/groups/#{group.id}.json", params: {
|
|
|
|
group: {
|
|
|
|
messageable_level: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is group admin" do
|
|
|
|
before do
|
|
|
|
user.update_attributes!(admin: true)
|
2016-12-07 17:28:43 +08:00
|
|
|
sign_in(user)
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to update the group' do
|
2018-04-06 11:44:58 +08:00
|
|
|
group.update!(
|
|
|
|
visibility_level: 2,
|
|
|
|
automatic_membership_retroactive: false,
|
|
|
|
grant_trust_level: 0
|
|
|
|
)
|
|
|
|
|
2018-03-27 12:18:03 +08:00
|
|
|
put "/groups/#{group.id}.json", params: {
|
|
|
|
group: {
|
|
|
|
flair_color: 'BBB',
|
2018-04-06 11:44:58 +08:00
|
|
|
name: 'testing',
|
|
|
|
incoming_email: 'test@mail.org',
|
|
|
|
primary_group: true,
|
|
|
|
automatic_membership_email_domains: 'test.org',
|
|
|
|
automatic_membership_retroactive: true,
|
|
|
|
grant_trust_level: 2,
|
|
|
|
visibility_level: 1
|
2018-03-27 12:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 16:25:02 +08:00
|
|
|
|
2018-03-27 12:18:03 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
group.reload
|
|
|
|
expect(group.flair_color).to eq('BBB')
|
|
|
|
expect(group.name).to eq('testing')
|
2018-04-06 11:44:58 +08:00
|
|
|
expect(group.incoming_email).to eq("test@mail.org")
|
|
|
|
expect(group.primary_group).to eq(true)
|
|
|
|
expect(group.visibility_level).to eq(1)
|
|
|
|
expect(group.automatic_membership_email_domains).to eq('test.org')
|
|
|
|
expect(group.automatic_membership_retroactive).to eq(true)
|
|
|
|
expect(group.grant_trust_level).to eq(2)
|
2018-04-06 17:11:00 +08:00
|
|
|
|
|
|
|
expect(Jobs::AutomaticGroupMembership.jobs.first["args"].first["group_id"])
|
|
|
|
.to eq(group.id)
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
2018-03-27 14:23:35 +08:00
|
|
|
|
2018-04-05 13:53:00 +08:00
|
|
|
it "should be able to update an automatic group" do
|
|
|
|
group = Group.find(Group::AUTO_GROUPS[:admins])
|
|
|
|
|
|
|
|
group.update!(
|
|
|
|
visibility_level: 2,
|
|
|
|
mentionable_level: 2,
|
|
|
|
messageable_level: 2,
|
|
|
|
default_notification_level: 2
|
|
|
|
)
|
|
|
|
|
|
|
|
put "/groups/#{group.id}.json", params: {
|
|
|
|
group: {
|
|
|
|
flair_color: 'BBB',
|
|
|
|
name: 'testing',
|
|
|
|
visibility_level: 1,
|
|
|
|
mentionable_level: 1,
|
|
|
|
messageable_level: 1,
|
|
|
|
default_notification_level: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
group.reload
|
|
|
|
expect(group.flair_color).to eq(nil)
|
|
|
|
expect(group.name).to eq('admins')
|
|
|
|
expect(group.visibility_level).to eq(1)
|
|
|
|
expect(group.mentionable_level).to eq(1)
|
|
|
|
expect(group.messageable_level).to eq(1)
|
|
|
|
expect(group.default_notification_level).to eq(1)
|
|
|
|
end
|
|
|
|
|
2018-03-27 14:23:35 +08:00
|
|
|
it 'triggers a extensibility event' do
|
|
|
|
event = DiscourseEvent.track_events {
|
|
|
|
put "/groups/#{group.id}.json", params: { group: { flair_color: 'BBB' } }
|
|
|
|
}.last
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:group_updated)
|
|
|
|
expect(event[:params].first).to eq(group)
|
|
|
|
end
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not a group owner or admin" do
|
|
|
|
it 'should not be able to update the group' do
|
2016-12-07 17:28:43 +08:00
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}.json", params: { group: { name: 'testing' } }
|
2016-11-29 16:25:02 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#members' do
|
2016-12-07 17:28:43 +08:00
|
|
|
let(:user1) do
|
|
|
|
Fabricate(:user,
|
|
|
|
last_seen_at: Time.zone.now,
|
|
|
|
last_posted_at: Time.zone.now - 1.day,
|
|
|
|
email: 'b@test.org'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user2) do
|
|
|
|
Fabricate(:user,
|
|
|
|
last_seen_at: Time.zone.now - 1 .day,
|
|
|
|
last_posted_at: Time.zone.now,
|
|
|
|
email: 'a@test.org'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-12-22 14:55:24 +08:00
|
|
|
let(:user3) do
|
|
|
|
Fabricate(:user,
|
|
|
|
last_seen_at: nil,
|
|
|
|
last_posted_at: nil,
|
|
|
|
email: 'c@test.org'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-10-06 10:35:40 +08:00
|
|
|
let(:bot) { Fabricate(:user, id: -999) }
|
|
|
|
|
|
|
|
let(:group) { Fabricate(:group, users: [user1, user2, user3, bot]) }
|
2016-12-07 17:28:43 +08:00
|
|
|
|
|
|
|
it "should allow members to be sorted by" do
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/members.json", params: {
|
|
|
|
order: 'last_seen_at', desc: true
|
|
|
|
}
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 17:28:43 +08:00
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
2016-12-22 14:55:24 +08:00
|
|
|
expect(members.map { |m| m["id"] }).to eq([user1.id, user2.id, user3.id])
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/members.json", params: { order: 'last_seen_at' }
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 17:28:43 +08:00
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
2016-12-22 14:55:24 +08:00
|
|
|
expect(members.map { |m| m["id"] }).to eq([user2.id, user1.id, user3.id])
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/members.json", params: {
|
|
|
|
order: 'last_posted_at', desc: true
|
|
|
|
}
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 17:28:43 +08:00
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
2016-12-22 14:55:24 +08:00
|
|
|
expect(members.map { |m| m["id"] }).to eq([user2.id, user1.id, user3.id])
|
2016-12-07 17:28:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow members to be sorted by columns that are not allowed" do
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/members.json", params: { order: 'email' }
|
2016-12-07 17:28:43 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 17:28:43 +08:00
|
|
|
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
|
2018-03-19 18:28:57 +08:00
|
|
|
expect(members.map { |m| m["id"] })
|
|
|
|
.to contain_exactly(user1.id, user2.id, user3.id)
|
2016-12-07 17:28:43 +08:00
|
|
|
end
|
2018-03-22 13:42:46 +08:00
|
|
|
|
|
|
|
describe 'filterable' do
|
|
|
|
describe 'as a normal user' do
|
|
|
|
it "should not allow members to be filterable by email" do
|
|
|
|
email = 'uniquetest@discourse.org'
|
|
|
|
user1.update!(email: email)
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/members.json", params: { filter: email }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
expect(members).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'as an admin' do
|
|
|
|
before do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow members to be filterable by username" do
|
|
|
|
email = 'uniquetest@discourse.org'
|
|
|
|
user1.update!(email: email)
|
|
|
|
|
2018-03-26 14:30:37 +08:00
|
|
|
{
|
|
|
|
email.upcase => [user1.id],
|
|
|
|
'QUEtes' => [user1.id],
|
|
|
|
"#{user1.email},#{user2.email}" => [user1.id, user2.id]
|
|
|
|
}.each do |filter, ids|
|
2018-03-22 13:42:46 +08:00
|
|
|
get "/groups/#{group.name}/members.json", params: { filter: filter }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
members = JSON.parse(response.body)["members"]
|
2018-03-26 14:30:37 +08:00
|
|
|
expect(members.map { |m| m["id"] }).to contain_exactly(*ids)
|
2018-03-22 13:42:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow members to be filterable by email" do
|
|
|
|
username = 'uniquetest'
|
|
|
|
user1.update!(username: username)
|
|
|
|
|
|
|
|
[username.upcase, 'QUEtes'].each do |filter|
|
|
|
|
get "/groups/#{group.name}/members.json", params: { filter: filter }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
members = JSON.parse(response.body)["members"]
|
|
|
|
expect(members.map { |m| m["id"] }).to contain_exactly(user1.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 17:28:43 +08:00
|
|
|
end
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-02-01 04:04:09 +08:00
|
|
|
describe "#edit" do
|
2016-12-07 12:06:56 +08:00
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
context 'when user is not signed in' do
|
|
|
|
it 'should be fobidden' do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { username: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'public group' do
|
|
|
|
it 'should be fobidden' do
|
2017-07-28 10:37:10 +08:00
|
|
|
group.update_attributes!(
|
|
|
|
public_admission: true,
|
|
|
|
public_exit: true
|
|
|
|
)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-01-12 11:15:10 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: "bob" }
|
|
|
|
expect(response.status).to eq(403)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-01-12 11:15:10 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { username: "bob" }
|
|
|
|
expect(response.status).to eq(403)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not an owner of the group' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "refuses membership changes to unauthorized users" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { username: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { Fabricate(:admin) }
|
|
|
|
let(:group) { Fabricate(:group, users: [user], automatic: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot add members to automatic groups" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { username: "bob" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "membership edits" do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
2018-03-26 14:30:37 +08:00
|
|
|
context '#add_members' do
|
2016-12-07 12:06:56 +08:00
|
|
|
it "can make incremental adds" do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
expect do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: user2.username }
|
2016-12-11 23:36:15 +08:00
|
|
|
end.to change { group.users.count }.by(1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
group_history = GroupHistory.last
|
|
|
|
|
|
|
|
expect(group_history.action).to eq(GroupHistory.actions[:add_user_to_group])
|
|
|
|
expect(group_history.acting_user).to eq(admin)
|
|
|
|
expect(group_history.target_user).to eq(user2)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot add members to automatic groups" do
|
|
|
|
group.update!(automatic: true)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: "l77t" }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "is able to add several members to a group" do
|
|
|
|
let(:user1) { Fabricate(:user) }
|
2018-10-19 03:17:24 +08:00
|
|
|
let(:user2) { Fabricate(:user, username: "UsEr2") }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
|
|
|
it "adds by username" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
put "/groups/#{group.id}/members.json",
|
2018-10-19 03:32:36 +08:00
|
|
|
params: { usernames: [user1.username, user2.username.upcase].join(",") }
|
2017-08-31 12:06:56 +08:00
|
|
|
end.to change { group.users.count }.by(2)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds by id" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { user_ids: [user1.id, user2.id].join(",") }
|
|
|
|
end.to change { group.users.count }.by(2)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
2017-04-27 02:47:36 +08:00
|
|
|
|
|
|
|
it "adds by email" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { user_emails: [user1.email, user2.email].join(",") }
|
|
|
|
end.to change { group.users.count }.by(2)
|
2017-04-27 02:47:36 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-27 02:47:36 +08:00
|
|
|
end
|
2018-03-26 14:30:37 +08:00
|
|
|
|
|
|
|
it 'fails when multiple member already exists' do
|
2018-03-27 17:14:06 +08:00
|
|
|
user2.update!(username: 'alice')
|
|
|
|
user3 = Fabricate(:user, username: 'bob')
|
2018-03-26 14:30:37 +08:00
|
|
|
[user2, user3].each { |user| group.add(user) }
|
|
|
|
|
|
|
|
expect do
|
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { user_emails: [user1.email, user2.email, user3.email].join(",") }
|
|
|
|
end.to change { group.users.count }.by(0)
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to include(I18n.t(
|
|
|
|
"groups.errors.member_already_exist",
|
2018-03-27 17:14:06 +08:00
|
|
|
username: "alice, bob",
|
2018-03-26 14:30:37 +08:00
|
|
|
count: 2
|
|
|
|
))
|
|
|
|
end
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 422 if member already exists" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json", params: { usernames: user.username }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
2018-03-26 14:30:37 +08:00
|
|
|
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to include(I18n.t(
|
|
|
|
"groups.errors.member_already_exist",
|
|
|
|
username: user.username,
|
|
|
|
count: 1
|
|
|
|
))
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
2018-04-20 10:12:16 +08:00
|
|
|
it "returns 400 if member is not found" do
|
|
|
|
[
|
|
|
|
{ usernames: "some thing" },
|
|
|
|
{ user_ids: "-5,-6" },
|
|
|
|
{ user_emails: "some@test.org" }
|
|
|
|
].each do |params|
|
|
|
|
put "/groups/#{group.id}/members.json", params: params
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-04-20 10:12:16 +08:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(body["error_type"]).to eq("invalid_parameters")
|
|
|
|
end
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'public group' do
|
|
|
|
let(:other_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
2017-07-28 10:37:10 +08:00
|
|
|
group.update!(
|
|
|
|
public_admission: true,
|
|
|
|
public_exit: true
|
|
|
|
)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
2016-12-13 16:39:44 +08:00
|
|
|
context 'admin' do
|
|
|
|
it "can make incremental adds" do
|
|
|
|
expect do
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { usernames: other_user.username }
|
2016-12-13 16:39:44 +08:00
|
|
|
end.to change { group.users.count }.by(1)
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-13 16:39:44 +08:00
|
|
|
|
|
|
|
group_history = GroupHistory.last
|
|
|
|
|
|
|
|
expect(group_history.action).to eq(GroupHistory.actions[:add_user_to_group])
|
|
|
|
expect(group_history.acting_user).to eq(admin)
|
|
|
|
expect(group_history.target_user).to eq(other_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-07 12:06:56 +08:00
|
|
|
it 'should allow a user to join the group' do
|
|
|
|
sign_in(other_user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { usernames: other_user.username }
|
|
|
|
end.to change { group.users.count }.by(1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
2016-12-13 16:39:44 +08:00
|
|
|
it 'should not allow an underprivilege user to add another user to a group' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put "/groups/#{group.id}/members.json",
|
|
|
|
params: { usernames: other_user.username }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
context '#remove_member' do
|
2016-12-07 12:06:56 +08:00
|
|
|
it "cannot remove members from automatic groups" do
|
|
|
|
group.update!(automatic: true)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { user_id: 42 }
|
2016-12-07 12:06:56 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error if user to be removed is not found" do
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { user_id: -10 }
|
2018-10-12 05:27:41 +08:00
|
|
|
expect(response.status).to eq(400)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "is able to remove a member" do
|
|
|
|
it "removes by id" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
delete "/groups/#{group.id}/members.json", params: { user_id: user.id }
|
|
|
|
end.to change { group.users.count }.by(-1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by username" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
delete "/groups/#{group.id}/members.json", params: { username: user.username }
|
|
|
|
end.to change { group.users.count }.by(-1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes user.primary_group_id when user is removed from group" do
|
|
|
|
user.update!(primary_group_id: group.id)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json", params: { user_id: user.id }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
|
|
|
expect(user.reload.primary_group_id).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by user_email" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
delete "/groups/#{group.id}/members.json",
|
|
|
|
params: { user_email: user.email }
|
|
|
|
end.to change { group.users.count }.by(-1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'public group' do
|
|
|
|
let(:other_user) { Fabricate(:user) }
|
2017-07-28 10:37:10 +08:00
|
|
|
let(:group) { Fabricate(:public_group, users: [other_user]) }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2016-12-13 16:39:44 +08:00
|
|
|
context "admin" do
|
|
|
|
it "removes by username" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
delete "/groups/#{group.id}/members.json",
|
|
|
|
params: { username: other_user.username }
|
|
|
|
end.to change { group.users.count }.by(-1)
|
2016-12-13 16:39:44 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-13 16:39:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-07 12:06:56 +08:00
|
|
|
it 'should allow a user to leave a group' do
|
|
|
|
sign_in(other_user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
delete "/groups/#{group.id}/members.json",
|
|
|
|
params: { username: other_user.username }
|
|
|
|
end.to change { group.users.count }.by(-1)
|
2016-12-07 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
2016-12-13 16:39:44 +08:00
|
|
|
it 'should not allow a underprivilege user to leave a group for another user' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/groups/#{group.id}/members.json",
|
|
|
|
params: { username: other_user.username }
|
2016-12-07 12:06:56 +08:00
|
|
|
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-12 05:27:41 +08:00
|
|
|
|
|
|
|
context '#remove_members' do
|
|
|
|
context "is able to remove several members from a group" do
|
|
|
|
let(:user1) { Fabricate(:user) }
|
2018-10-19 03:17:24 +08:00
|
|
|
let(:user2) { Fabricate(:user, username: "UsEr2") }
|
2018-10-12 05:27:41 +08:00
|
|
|
let(:group1) { Fabricate(:group, users: [user1, user2]) }
|
|
|
|
|
|
|
|
it "removes by username" do
|
|
|
|
expect do
|
|
|
|
delete "/groups/#{group1.id}/members.json",
|
2018-10-19 03:32:36 +08:00
|
|
|
params: { usernames: [user1.username, user2.username.upcase].join(",") }
|
2018-10-12 05:27:41 +08:00
|
|
|
end.to change { group1.users.count }.by(-2)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by id" do
|
|
|
|
expect do
|
|
|
|
delete "/groups/#{group1.id}/members.json",
|
|
|
|
params: { user_ids: [user1.id, user2.id].join(",") }
|
|
|
|
end.to change { group1.users.count }.by(-2)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by email" do
|
|
|
|
expect do
|
|
|
|
delete "/groups/#{group1.id}/members.json",
|
|
|
|
params: { user_emails: [user1.email, user2.email].join(",") }
|
|
|
|
end.to change { group1.users.count }.by(-2)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "only removes users in that group" do
|
|
|
|
expect do
|
|
|
|
delete "/groups/#{group1.id}/members.json",
|
|
|
|
params: { usernames: [user.username, user2.username].join(",") }
|
|
|
|
end.to change { group1.users.count }.by(-1)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 12:06:56 +08:00
|
|
|
end
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
describe "#histories" do
|
2016-12-11 23:36:15 +08:00
|
|
|
context 'when user is not signed in' do
|
|
|
|
it 'should raise the right error' do
|
2018-01-12 11:15:10 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
|
|
|
expect(response.status).to eq(403)
|
2016-12-11 23:36:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not a group owner' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be forbidden' do
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
describe 'when user is a group owner' do
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2017-07-28 10:37:10 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
describe 'when viewing a public group' do
|
|
|
|
before do
|
2017-07-28 10:37:10 +08:00
|
|
|
group.update_attributes!(
|
|
|
|
public_admission: true,
|
|
|
|
public_exit: true
|
|
|
|
)
|
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
GroupActionLogger.new(user, group).log_change_group_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow group owner to view history' do
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
result = JSON.parse(response.body)["logs"].last
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
expect(result["action"]).to eq(GroupHistory.actions[1].to_s)
|
2017-07-28 10:37:10 +08:00
|
|
|
expect(result["subject"]).to eq('public_exit')
|
2016-12-11 23:36:15 +08:00
|
|
|
expect(result["prev_value"]).to eq('f')
|
|
|
|
expect(result["new_value"]).to eq('t')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
it 'should not be allowed to view history of an automatic group' do
|
|
|
|
group = Group.find_by(id: Group::AUTO_GROUPS[:admins])
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
it 'should be able to view history' do
|
|
|
|
GroupActionLogger.new(admin, group).log_remove_user_from_group(user)
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
result = JSON.parse(response.body)["logs"].first
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
expect(result["action"]).to eq(GroupHistory.actions[3].to_s)
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
it 'should be able to view history of automatic groups' do
|
|
|
|
group = Group.find_by(id: Group::AUTO_GROUPS[:admins])
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2018-04-05 16:31:09 +08:00
|
|
|
get "/groups/#{group.name}/logs.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to filter through the history' do
|
|
|
|
GroupActionLogger.new(admin, group).log_add_user_to_group(user)
|
|
|
|
GroupActionLogger.new(admin, group).log_remove_user_from_group(user)
|
|
|
|
|
|
|
|
get "/groups/#{group.name}/logs.json", params: {
|
|
|
|
filters: { "action" => "add_user_to_group" }
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-04-05 16:31:09 +08:00
|
|
|
|
|
|
|
logs = JSON.parse(response.body)["logs"]
|
|
|
|
|
|
|
|
expect(logs.count).to eq(1)
|
|
|
|
expect(logs.first["action"]).to eq(GroupHistory.actions[2].to_s)
|
2016-12-11 23:36:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-13 16:10:14 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#request_membership' do
|
2017-06-13 16:10:14 +08:00
|
|
|
let(:new_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it 'requires the user to log in' do
|
2018-01-12 11:15:10 +08:00
|
|
|
post "/groups/#{group.name}/request_membership.json"
|
|
|
|
expect(response.status).to eq(403)
|
2017-06-13 16:10:14 +08:00
|
|
|
end
|
|
|
|
|
2017-08-08 17:53:02 +08:00
|
|
|
it 'requires a reason' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2018-01-12 11:15:10 +08:00
|
|
|
post "/groups/#{group.name}/request_membership.json"
|
|
|
|
expect(response.status).to eq(400)
|
2017-08-08 17:53:02 +08:00
|
|
|
end
|
|
|
|
|
2017-06-13 16:10:14 +08:00
|
|
|
it 'should create the right PM' do
|
2017-06-15 11:36:09 +08:00
|
|
|
owner1 = Fabricate(:user, last_seen_at: Time.zone.now)
|
|
|
|
owner2 = Fabricate(:user, last_seen_at: Time.zone.now - 1 .day)
|
|
|
|
[owner1, owner2].each { |owner| group.add_owner(owner) }
|
|
|
|
|
2017-06-13 16:10:14 +08:00
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post "/groups/#{group.name}/request_membership.json",
|
|
|
|
params: { reason: 'Please add me in' }
|
2017-06-13 16:10:14 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-06-13 16:10:14 +08:00
|
|
|
|
|
|
|
post = Post.last
|
|
|
|
topic = post.topic
|
|
|
|
body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(body['relative_url']).to eq(topic.relative_url)
|
|
|
|
expect(post.user).to eq(user)
|
|
|
|
|
|
|
|
expect(topic.title).to eq(I18n.t('groups.request_membership_pm.title',
|
|
|
|
group_name: group.name
|
|
|
|
))
|
|
|
|
|
2017-08-08 17:53:02 +08:00
|
|
|
expect(post.raw).to eq('Please add me in')
|
2017-06-13 16:10:14 +08:00
|
|
|
expect(topic.archetype).to eq(Archetype.private_message)
|
2017-06-15 11:36:09 +08:00
|
|
|
expect(topic.allowed_users).to contain_exactly(user, owner1, owner2)
|
|
|
|
expect(topic.allowed_groups).to eq([])
|
2017-06-13 16:10:14 +08:00
|
|
|
end
|
|
|
|
end
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe '#search ' do
|
2017-07-21 14:12:24 +08:00
|
|
|
let(:hidden_group) do
|
|
|
|
Fabricate(:group,
|
|
|
|
visibility_level: Group.visibility_levels[:owners],
|
|
|
|
name: 'KingOfTheNorth'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.update!(
|
|
|
|
name: 'GOT',
|
|
|
|
full_name: 'Daenerys Targaryen'
|
|
|
|
)
|
|
|
|
|
|
|
|
hidden_group
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an anon user' do
|
|
|
|
it "returns the right response" do
|
2018-01-12 11:15:10 +08:00
|
|
|
get '/groups/search.json'
|
|
|
|
expect(response.status).to eq(403)
|
2017-07-21 14:12:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as a normal user' do
|
|
|
|
it "returns the right response" do
|
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get '/groups/search.json'
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-21 14:12:24 +08:00
|
|
|
groups = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expected_ids = Group::AUTO_GROUPS.map { |name, id| id }
|
|
|
|
expected_ids.delete(Group::AUTO_GROUPS[:everyone])
|
|
|
|
expected_ids << group.id
|
|
|
|
|
|
|
|
expect(groups.map { |group| group["id"] }).to contain_exactly(*expected_ids)
|
|
|
|
|
|
|
|
['GO', 'nerys'].each do |term|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/search.json?term=#{term}"
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-21 14:12:24 +08:00
|
|
|
groups = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(groups.length).to eq(1)
|
|
|
|
expect(groups.first['id']).to eq(group.id)
|
|
|
|
end
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/search.json?term=KingOfTheNorth"
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-21 14:12:24 +08:00
|
|
|
groups = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(groups).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as a group owner' do
|
|
|
|
before do
|
|
|
|
hidden_group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the right response" do
|
|
|
|
sign_in(user)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/groups/search.json?term=north"
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-21 14:12:24 +08:00
|
|
|
groups = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(groups.length).to eq(1)
|
|
|
|
expect(groups.first['id']).to eq(hidden_group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
it "returns the right response" do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get '/groups/search.json?ignore_automatic=true'
|
2017-07-21 14:12:24 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-21 14:12:24 +08:00
|
|
|
groups = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(groups.length).to eq(2)
|
|
|
|
|
|
|
|
expect(groups.map { |group| group['id'] })
|
|
|
|
.to contain_exactly(group.id, hidden_group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-27 16:45:21 +08:00
|
|
|
|
|
|
|
describe '#new' do
|
|
|
|
describe 'for an anon user' do
|
|
|
|
it 'should return 404' do
|
|
|
|
get '/groups/custom/new'
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for a normal user' do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it 'should return 404' do
|
|
|
|
get '/groups/custom/new'
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for an admin user' do
|
|
|
|
before { sign_in(Fabricate(:admin)) }
|
|
|
|
|
|
|
|
it 'should return 404' do
|
|
|
|
get '/groups/custom/new'
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 11:08:45 +08:00
|
|
|
|
|
|
|
describe '#check_name' do
|
|
|
|
describe 'for an anon user' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
get "/groups/check-name.json", params: { group_name: 'test' }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
SiteSetting.reserved_usernames = 'test|donkey'
|
|
|
|
get "/groups/check-name.json", params: { group_name: 'test' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body)["available"]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-11-25 16:45:15 +08:00
|
|
|
end
|