discourse/spec/controllers/admin/groups_controller_spec.rb

97 lines
2.3 KiB
Ruby
Raw Normal View History

2013-05-08 13:20:38 +08:00
require 'spec_helper'
describe Admin::GroupsController do
2013-07-16 14:17:44 +08:00
before do
@admin = log_in(:admin)
end
2013-05-08 13:20:38 +08:00
it "is a subclass of AdminController" do
(Admin::GroupsController < Admin::AdminController).should be_true
end
it "produces valid json for groups" do
group = Fabricate.build(:group, name: "test")
group.add(@admin)
2013-05-08 13:20:38 +08:00
group.save
xhr :get, :index
response.status.should == 200
2013-07-16 14:17:44 +08:00
::JSON.parse(response.body).keep_if{|r| r["id"] == group.id}.should == [{
2013-05-08 13:20:38 +08:00
"id"=>group.id,
"name"=>group.name,
"user_count"=>1,
"automatic"=>false,
"alias_level"=>0,
"visible"=>true
2013-05-08 13:20:38 +08:00
}]
end
it "is able to refresh automatic groups" do
Group.expects(:refresh_automatic_groups!).returns(true)
xhr :post, :refresh_automatic_groups
response.status.should == 200
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
response.status.should == 422
Group.where(id: group.id).count.should == 1
end
it "is able to destroy a non-automatic group" do
group = Fabricate(:group)
xhr :delete, :destroy, id: group.id
response.status.should == 200
Group.where(id: group.id).count.should == 0
end
end
context '.create' do
let(:usernames) { @admin.username }
it "is able to create a group" do
xhr :post, :create, group: {
usernames: usernames,
name: "bob"
}
response.status.should == 200
groups = Group.where(name: "bob").to_a
groups.count.should == 1
groups[0].usernames.should == usernames
groups[0].name.should == "bob"
end
it "strips spaces from group name" do
lambda {
xhr :post, :create, group: {
usernames: usernames,
name: " bob "
}
2013-08-26 05:18:11 +08:00
}.should_not raise_error()
Group.where(name: "bob").count.should == 1
end
end
it "is able to update group members" do
user1 = Fabricate(:user)
user2 = Fabricate(:user)
group = Fabricate(:group)
xhr :put, :update, id: group.id, name: 'fred', group: {
name: 'fred',
usernames: "#{user1.username},#{user2.username}"
}
group.reload
group.users.count.should == 2
group.name.should == 'fred'
end
2013-05-08 13:20:38 +08:00
end