2014-02-08 00:07:23 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GroupsController do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
describe 'show' do
|
|
|
|
it "ensures the group can be seen" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(false)
|
|
|
|
xhr :get, :show, id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "responds with JSON" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
|
|
|
xhr :get, :show, id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(::JSON.parse(response.body)['basic_group']['id']).to eq(group.id)
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
2014-02-19 05:43:02 +08:00
|
|
|
|
|
|
|
it "works even with an upper case group name" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
|
|
|
xhr :get, :show, id: group.name.upcase
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(::JSON.parse(response.body)['basic_group']['id']).to eq(group.id)
|
2014-02-19 05:43:02 +08:00
|
|
|
end
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
|
|
|
|
2014-02-19 05:17:04 +08:00
|
|
|
describe "counts" do
|
2014-02-13 03:00:45 +08:00
|
|
|
it "ensures the group can be seen" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(false)
|
2014-02-19 05:17:04 +08:00
|
|
|
xhr :get, :counts, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-02-13 03:00:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "performs the query and responds with JSON" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
|
|
|
Group.any_instance.expects(:posts_for).returns(Group.none)
|
2014-02-19 05:17:04 +08:00
|
|
|
xhr :get, :counts, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-02-13 03:00:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-08 00:07:23 +08:00
|
|
|
describe "posts" do
|
|
|
|
it "ensures the group can be seen" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(false)
|
|
|
|
xhr :get, :posts, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls `posts_for` and responds with JSON" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
2014-02-13 03:00:45 +08:00
|
|
|
Group.any_instance.expects(:posts_for).returns(Group.none)
|
2014-02-08 00:07:23 +08:00
|
|
|
xhr :get, :posts, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "members" do
|
|
|
|
it "ensures the group can be seen" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(false)
|
|
|
|
xhr :get, :members, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls `posts_for` and responds with JSON" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
|
|
|
xhr :get, :posts, group_id: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
2014-11-25 04:12:48 +08:00
|
|
|
|
2015-01-01 01:58:50 +08:00
|
|
|
# Pending until we fix group truncation
|
2015-01-09 20:55:58 +08:00
|
|
|
skip "ensures that membership can be paginated" do
|
2014-11-25 04:12:48 +08:00
|
|
|
5.times { group.add(Fabricate(:user)) }
|
2014-11-26 01:12:24 +08:00
|
|
|
usernames = group.users.map{ |m| m['username'] }.sort
|
|
|
|
|
2014-11-25 04:12:48 +08:00
|
|
|
xhr :get, :members, group_id: group.name, limit: 3
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-11-25 04:12:48 +08:00
|
|
|
members = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(members.map{ |m| m['username'] }).to eq(usernames[0..2])
|
2014-11-25 04:12:48 +08:00
|
|
|
|
|
|
|
xhr :get, :members, group_id: group.name, limit: 3, offset: 3
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-11-25 04:12:48 +08:00
|
|
|
members = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(members.map{ |m| m['username'] }).to eq(usernames[3..4])
|
2014-11-25 04:12:48 +08:00
|
|
|
end
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|
2015-01-09 07:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
describe "membership edit permission" do
|
|
|
|
it "refuses membership changes to unauthorized users" do
|
|
|
|
Guardian.any_instance.stubs(:can_edit?).with(group).returns(false)
|
|
|
|
|
|
|
|
xhr :put, :add_members, group_id: group.name, usernames: "bob"
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_forbidden
|
2015-01-09 07:35:52 +08:00
|
|
|
|
|
|
|
xhr :delete, :remove_member, group_id: group.name, username: "bob"
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_forbidden
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot add members to automatic groups" do
|
|
|
|
Guardian.any_instance.stubs(:is_admin?).returns(true)
|
|
|
|
auto_group = Fabricate(:group, name: "auto_group", automatic: true)
|
|
|
|
|
|
|
|
xhr :put, :add_members, group_id: group.name, usernames: "bob"
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_forbidden
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "membership edits" do
|
|
|
|
before do
|
|
|
|
@user1 = Fabricate(:user)
|
|
|
|
group.add(@user1)
|
|
|
|
group.reload
|
|
|
|
|
|
|
|
Guardian.any_instance.stubs(:can_edit?).with(group).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can make incremental adds" do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
xhr :put, :add_members, group_id: group.name, usernames: user2.username
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
2015-01-09 07:35:52 +08:00
|
|
|
group.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(group.users.count).to eq(2)
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds silently when adding non-existent users" do
|
|
|
|
xhr :put, :add_members, group_id: group.name, usernames: "nosuchperson"
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
2015-01-09 07:35:52 +08:00
|
|
|
group.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(group.users.count).to eq(1)
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds silently when adding duplicate users" do
|
|
|
|
xhr :put, :add_members, group_id: group.name, usernames: @user1.username
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
2015-01-09 07:35:52 +08:00
|
|
|
group.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(group.users).to eq([@user1])
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can make incremental deletes" do
|
|
|
|
xhr :delete, :remove_member, group_id: group.name, username: @user1.username
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
2015-01-09 07:35:52 +08:00
|
|
|
group.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(group.users.count).to eq(0)
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds silently when removing non-members" do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
xhr :delete, :remove_member, group_id: group.name, username: user2.username
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
2015-01-09 07:35:52 +08:00
|
|
|
group.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(group.users.count).to eq(1)
|
2015-01-09 07:35:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-08 00:07:23 +08:00
|
|
|
end
|