2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-02-07 11:07:23 -05:00
|
|
|
|
|
|
|
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-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2014-02-07 11:07:23 -05: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-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(::JSON.parse(response.body)['basic_group']['id']).to eq(group.id)
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|
2014-02-18 16:43:02 -05: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-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(::JSON.parse(response.body)['basic_group']['id']).to eq(group.id)
|
2014-02-18 16:43:02 -05:00
|
|
|
end
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls `posts_for` and responds with JSON" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
|
2014-02-12 14:00:45 -05:00
|
|
|
Group.any_instance.expects(:posts_for).returns(Group.none)
|
2014-02-07 11:07:23 -05:00
|
|
|
xhr :get, :posts, group_id: group.name
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-07 11:07:23 -05: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-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2014-02-07 11:07:23 -05: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-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|
2014-11-24 15:12:48 -05:00
|
|
|
|
2014-12-31 12:58:50 -05:00
|
|
|
# Pending until we fix group truncation
|
2015-01-09 07:55:58 -05:00
|
|
|
skip "ensures that membership can be paginated" do
|
2014-11-24 15:12:48 -05:00
|
|
|
5.times { group.add(Fabricate(:user)) }
|
2014-11-25 12:12:24 -05:00
|
|
|
usernames = group.users.map{ |m| m['username'] }.sort
|
|
|
|
|
2014-11-24 15:12:48 -05:00
|
|
|
xhr :get, :members, group_id: group.name, limit: 3
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-11-24 15:12:48 -05:00
|
|
|
members = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(members.map{ |m| m['username'] }).to eq(usernames[0..2])
|
2014-11-24 15:12:48 -05:00
|
|
|
|
|
|
|
xhr :get, :members, group_id: group.name, limit: 3, offset: 3
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-11-24 15:12:48 -05:00
|
|
|
members = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(members.map{ |m| m['username'] }).to eq(usernames[3..4])
|
2014-11-24 15:12:48 -05:00
|
|
|
end
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|
2015-01-08 18:35:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
describe "membership edit permission" do
|
|
|
|
it "refuses membership changes to unauthorized users" do
|
|
|
|
Guardian.any_instance.stubs(:can_edit?).with(group).returns(false)
|
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
xhr :put, :add_members, id: group.id, usernames: "bob"
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_forbidden
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
xhr :delete, :remove_member, id: group.id, username: "bob"
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_forbidden
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot add members to automatic groups" do
|
|
|
|
Guardian.any_instance.stubs(:is_admin?).returns(true)
|
2015-11-09 08:52:04 -05:00
|
|
|
group = Fabricate(:group, name: "auto_group", automatic: true)
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
xhr :put, :add_members, id: group.id, usernames: "bob"
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_forbidden
|
2015-01-08 18:35:52 -05: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)
|
2015-11-09 08:52:04 -05:00
|
|
|
xhr :put, :add_members, id: group.id, usernames: user2.username
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_success
|
2015-01-08 18:35:52 -05:00
|
|
|
group.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(group.users.count).to eq(2)
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
it "can make incremental deletes" do
|
|
|
|
xhr :delete, :remove_member, id: group.id, username: @user1.username
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_success
|
2015-01-08 18:35:52 -05:00
|
|
|
group.reload
|
2015-11-09 08:52:04 -05:00
|
|
|
expect(group.users.count).to eq(0)
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context ".add_members" do
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
before do
|
|
|
|
@admin = log_in(:admin)
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
it "cannot add members to automatic groups" do
|
|
|
|
xhr :put, :add_members, id: 1, usernames: "l77t"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
context "is able to add several members to a group" do
|
|
|
|
|
|
|
|
let(:user1) { Fabricate(:user) }
|
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
it "adds by username" do
|
|
|
|
xhr :put, :add_members, id: group.id, usernames: [user1.username, user2.username].join(",")
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds by id" do
|
|
|
|
xhr :put, :add_members, id: group.id, user_ids: [user1.id, user2.id].join(",")
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
expect(group.users.count).to eq(2)
|
|
|
|
end
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
it "returns 422 if member already exists" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
existing_member = Fabricate(:user)
|
|
|
|
group.add(existing_member)
|
|
|
|
group.save
|
2015-01-08 18:35:52 -05:00
|
|
|
|
2015-11-09 08:52:04 -05:00
|
|
|
xhr :put, :add_members, id: group.id, usernames: existing_member.username
|
|
|
|
expect(response.status).to eq(422)
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
2015-11-09 08:52:04 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".remove_member" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@admin = log_in(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot remove members from automatic groups" do
|
|
|
|
xhr :put, :remove_member, id: 1, user_id: 42
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "is able to remove a member" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by id" do
|
2016-10-23 22:32:21 -04:00
|
|
|
expect do
|
|
|
|
xhr :delete, :remove_member, id: group.id, user_id: user.id
|
2015-11-09 08:52:04 -05:00
|
|
|
|
2016-10-23 22:32:21 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
end.to change{group.users.count}.from(1).to(0)
|
2015-11-09 08:52:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes by username" do
|
2016-10-23 22:32:21 -04:00
|
|
|
expect do
|
|
|
|
xhr :delete, :remove_member, id: group.id, username: user.username
|
2015-11-09 08:52:04 -05:00
|
|
|
|
2016-10-23 22:32:21 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
|
|
|
|
end.to change{group.users.count}.from(1).to(0)
|
2015-11-09 08:52:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes user.primary_group_id when user is removed from group" do
|
|
|
|
user.primary_group_id = group.id
|
|
|
|
user.save
|
|
|
|
|
|
|
|
xhr :delete, :remove_member, id: group.id, username: user.username
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.primary_group_id).to eq(nil)
|
|
|
|
end
|
2016-10-18 05:10:47 -04:00
|
|
|
|
|
|
|
it "removes by user_email" do
|
2016-10-23 22:32:21 -04:00
|
|
|
expect do
|
|
|
|
xhr :delete, :remove_member, id: group.id, user_email: user.email
|
|
|
|
expect(response).to be_success
|
|
|
|
group.reload
|
|
|
|
end.to change{group.users.count}.from(1).to(0)
|
2016-10-18 05:10:47 -04:00
|
|
|
end
|
2015-11-09 08:52:04 -05:00
|
|
|
end
|
|
|
|
|
2015-01-08 18:35:52 -05:00
|
|
|
end
|
|
|
|
|
2016-03-18 12:19:45 -04:00
|
|
|
describe '.posts_feed' do
|
|
|
|
it 'renders RSS' do
|
|
|
|
get :posts_feed, group_id: group.name, format: :rss
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.mentions_feed' do
|
|
|
|
it 'renders RSS' do
|
|
|
|
get :mentions_feed, group_id: group.name, format: :rss
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-07 11:07:23 -05:00
|
|
|
end
|