discourse/spec/controllers/admin/groups_controller_spec.rb

135 lines
3.1 KiB
Ruby
Raw Normal View History

2013-05-08 01:20:38 -04:00
require 'spec_helper'
describe Admin::GroupsController do
2013-07-16 02:17:44 -04:00
before do
@admin = log_in(:admin)
end
2013-05-08 01:20:38 -04:00
it "is a subclass of AdminController" do
2015-01-09 12:04:02 -05:00
expect(Admin::GroupsController < Admin::AdminController).to eq(true)
2013-05-08 01:20:38 -04:00
end
context ".index" do
it "produces valid json for groups" do
group = Fabricate.build(:group, name: "test")
group.add(@admin)
group.save
xhr :get, :index
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
expect(::JSON.parse(response.body).keep_if {|r| r["id"] == group.id }).to eq([{
"id"=>group.id,
"name"=>group.name,
"user_count"=>1,
"automatic"=>false,
"alias_level"=>0,
"visible"=>true
2015-01-09 12:04:02 -05:00
}])
end
end
context ".create" do
it "strip spaces on the group name" do
xhr :post, :create, name: " bob "
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
groups = Group.where(name: "bob").to_a
2015-01-09 12:04:02 -05:00
expect(groups.count).to eq(1)
expect(groups[0].name).to eq("bob")
end
2013-05-08 01:20:38 -04:00
end
context ".update" do
it "ignore name change on automatic group" do
xhr :put, :update, id: 1, name: "WAT", visible: "true"
2015-01-09 12:04:02 -05:00
expect(response).to be_success
group = Group.find(1)
2015-01-09 12:04:02 -05:00
expect(group.name).not_to eq("WAT")
expect(group.visible).to eq(true)
end
2013-05-08 01:20:38 -04:00
end
context ".destroy" do
it "returns a 422 if the group is automatic" do
group = Fabricate(:group, automatic: true)
xhr :delete, :destroy, id: group.id
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
expect(Group.where(id: group.id).count).to eq(1)
end
it "is able to destroy a non-automatic group" do
group = Fabricate(:group)
xhr :delete, :destroy, id: group.id
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
expect(Group.where(id: group.id).count).to eq(0)
end
end
context ".refresh_automatic_groups" do
it "is able to refresh automatic groups" do
Group.expects(:refresh_automatic_groups!).returns(true)
xhr :post, :refresh_automatic_groups
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
end
end
context ".add_members" do
it "cannot add members to automatic groups" do
xhr :put, :add_members, group_id: 1, usernames: "l77t"
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
end
it "is able to add several members to a group" do
user1 = Fabricate(:user)
user2 = Fabricate(:user)
group = Fabricate(:group)
xhr :put, :add_members, group_id: group.id, usernames: [user1.username, user2.username].join(",")
2015-01-09 12:04:02 -05:00
expect(response).to be_success
group.reload
2015-01-09 12:04:02 -05:00
expect(group.users.count).to eq(2)
end
end
context ".remove_member" do
it "cannot remove members from automatic groups" do
xhr :put, :remove_member, group_id: 1, user_id: 42
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
end
it "is able to remove a member" do
group = Fabricate(:group)
user = Fabricate(:user)
group.add(user)
group.save
xhr :delete, :remove_member, group_id: group.id, user_id: user.id
2015-01-09 12:04:02 -05:00
expect(response).to be_success
group.reload
2015-01-09 12:04:02 -05:00
expect(group.users.count).to eq(0)
end
end
2013-05-08 01:20:38 -04:00
end