2013-07-16 01:44:07 -04:00
|
|
|
require "spec_helper"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe CategoriesController do
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "create" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "requires the user to be logged in" do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda { xhr :post, :create }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "logged in" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-02-06 22:11:52 -05:00
|
|
|
@user = log_in(:admin)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when they don't have permission to create it" do
|
|
|
|
Guardian.any_instance.expects(:can_create?).with(Category, nil).returns(false)
|
2013-03-14 09:16:57 -04:00
|
|
|
xhr :post, :create, name: 'hello', color: 'ff0', text_color: 'fff'
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "raises an exception when the name is missing" do
|
|
|
|
lambda { xhr :post, :create, color: "ff0", text_color: "fff" }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "raises an exception when the color is missing" do
|
|
|
|
lambda { xhr :post, :create, name: "hello", text_color: "fff" }.should raise_error(ActionController::ParameterMissing)
|
2013-03-14 09:16:57 -04:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "raises an exception when the text color is missing" do
|
|
|
|
lambda { xhr :post, :create, name: "hello", color: "ff0" }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "failure" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
|
|
|
@category = Fabricate(:category, user: @user)
|
2013-07-16 01:44:07 -04:00
|
|
|
xhr :post, :create, name: @category.name, color: "ff0", text_color: "fff"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { should_not respond_with(:success) }
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "returns errors on a duplicate category name" do
|
|
|
|
response.status.should == 422
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "success" do
|
|
|
|
it "works" do
|
|
|
|
readonly = CategoryGroup.permission_types[:readonly]
|
|
|
|
create_post = CategoryGroup.permission_types[:create_post]
|
|
|
|
|
|
|
|
xhr :post, :create, name: "hello", color: "ff0", text_color: "fff",
|
2013-12-06 16:39:35 -05:00
|
|
|
auto_close_hours: 72,
|
2013-07-16 01:44:07 -04:00
|
|
|
permissions: {
|
|
|
|
"everyone" => readonly,
|
|
|
|
"staff" => create_post
|
|
|
|
}
|
|
|
|
|
|
|
|
response.status.should == 200
|
2014-05-06 09:41:59 -04:00
|
|
|
category = Category.find_by(name: "hello")
|
2013-07-16 01:44:07 -04:00
|
|
|
category.category_groups.map{|g| [g.group_id, g.permission_type]}.sort.should == [
|
|
|
|
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
|
|
|
|
]
|
|
|
|
category.name.should == "hello"
|
|
|
|
category.color.should == "ff0"
|
2013-12-06 16:39:35 -05:00
|
|
|
category.auto_close_hours.should == 72
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "destroy" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "requires the user to be logged in" do
|
|
|
|
lambda { xhr :delete, :destroy, id: "category"}.should raise_error(Discourse::NotLoggedIn)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "logged in" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
|
|
|
@user = log_in
|
2013-02-25 11:42:20 -05:00
|
|
|
@category = Fabricate(:category, user: @user)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception if they don't have permission to delete it" do
|
|
|
|
Guardian.any_instance.expects(:can_delete_category?).returns(false)
|
|
|
|
xhr :delete, :destroy, id: @category.slug
|
|
|
|
response.should be_forbidden
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "deletes the record" do
|
|
|
|
Guardian.any_instance.expects(:can_delete_category?).returns(true)
|
|
|
|
lambda { xhr :delete, :destroy, id: @category.slug}.should change(Category, :count).by(-1)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-27 15:35:25 -04:00
|
|
|
describe "upload" do
|
|
|
|
it "requires the user to be logged in" do
|
|
|
|
lambda { xhr :post, :upload, image_type: 'logo'}.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "logged in" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:logo) { file_from_fixtures("logo.png") }
|
2014-06-27 15:35:25 -04:00
|
|
|
let(:upload) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({ filename: 'logo.png', tempfile: logo })
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when you don't have permission to upload" do
|
|
|
|
Guardian.any_instance.expects(:can_create?).with(Category).returns(false)
|
|
|
|
xhr :post, :upload, image_type: 'logo', file: upload
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "requires the `image_type` param" do
|
|
|
|
-> { xhr :post, :upload }.should raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls Upload.create_for" do
|
|
|
|
Upload.expects(:create_for).returns(Upload.new)
|
|
|
|
xhr :post, :upload, image_type: 'logo', file: upload
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "update" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "requires the user to be logged in" do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda { xhr :put, :update, id: 'category'}.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "logged in" do
|
2013-12-16 15:13:43 -05:00
|
|
|
let(:valid_attrs) { {id: @category.id, name: "hello", color: "ff0", text_color: "fff"} }
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2014-02-06 22:11:52 -05:00
|
|
|
@user = log_in(:admin)
|
2013-02-05 14:16:51 -05:00
|
|
|
@category = Fabricate(:category, user: @user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception if they don't have permission to edit it" do
|
|
|
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
2013-03-14 09:16:57 -04:00
|
|
|
xhr :put, :update, id: @category.slug, name: 'hello', color: 'ff0', text_color: 'fff'
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "requires a name" do
|
2013-06-05 02:45:25 -04:00
|
|
|
lambda { xhr :put, :update, id: @category.slug, color: 'fff', text_color: '0ff' }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "requires a color" do
|
2013-06-05 02:45:25 -04:00
|
|
|
lambda { xhr :put, :update, id: @category.slug, name: 'asdf', text_color: '0ff' }.should raise_error(ActionController::ParameterMissing)
|
2013-03-14 09:16:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a text color" do
|
2013-06-05 02:45:25 -04:00
|
|
|
lambda { xhr :put, :update, id: @category.slug, name: 'asdf', color: 'fff' }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "failure" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2013-07-16 01:44:07 -04:00
|
|
|
@other_category = Fabricate(:category, name: "Other", user: @user )
|
|
|
|
xhr :put, :update, id: @category.id, name: @other_category.name, color: "ff0", text_color: "fff"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "returns errors on a duplicate category name" do
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "returns errors on a duplicate category name" do
|
2013-02-05 14:16:51 -05:00
|
|
|
response.code.to_i.should == 422
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
describe "success" do
|
|
|
|
|
|
|
|
it "updates the group correctly" do
|
|
|
|
readonly = CategoryGroup.permission_types[:readonly]
|
|
|
|
create_post = CategoryGroup.permission_types[:create_post]
|
|
|
|
|
|
|
|
xhr :put, :update, id: @category.id, name: "hello", color: "ff0", text_color: "fff",
|
2013-12-06 16:39:35 -05:00
|
|
|
auto_close_hours: 72,
|
2013-07-16 01:44:07 -04:00
|
|
|
permissions: {
|
|
|
|
"everyone" => readonly,
|
|
|
|
"staff" => create_post
|
|
|
|
}
|
|
|
|
|
|
|
|
response.status.should == 200
|
2013-02-05 14:16:51 -05:00
|
|
|
@category.reload
|
2013-07-16 01:44:07 -04:00
|
|
|
@category.category_groups.map{|g| [g.group_id, g.permission_type]}.sort.should == [
|
|
|
|
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
|
|
|
|
]
|
|
|
|
@category.name.should == "hello"
|
|
|
|
@category.color.should == "ff0"
|
2013-12-06 16:39:35 -05:00
|
|
|
@category.auto_close_hours.should == 72
|
2013-12-16 15:13:43 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|