discourse/spec/controllers/categories_controller_spec.rb

245 lines
8.3 KiB
Ruby
Raw Normal View History

require "spec_helper"
2013-02-05 14:16:51 -05:00
describe CategoriesController do
describe "create" do
2013-02-05 14:16:51 -05:00
it "requires the user to be logged in" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :create }.to raise_error(Discourse::NotLoggedIn)
2013-02-05 14:16:51 -05:00
end
describe "logged in" do
2013-02-05 14:16:51 -05:00
before do
@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)
xhr :post, :create, name: 'hello', color: 'ff0', text_color: 'fff'
2015-01-09 12:04:02 -05:00
expect(response).to be_forbidden
2013-02-05 14:16:51 -05:00
end
it "raises an exception when the name is missing" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :create, color: "ff0", text_color: "fff" }.to raise_error(ActionController::ParameterMissing)
2013-02-05 14:16:51 -05:00
end
it "raises an exception when the color is missing" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :create, name: "hello", text_color: "fff" }.to raise_error(ActionController::ParameterMissing)
end
it "raises an exception when the text color is missing" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :create, name: "hello", color: "ff0" }.to raise_error(ActionController::ParameterMissing)
2013-02-05 14:16:51 -05:00
end
describe "failure" do
2013-02-05 14:16:51 -05:00
before do
@category = Fabricate(:category, user: @user)
xhr :post, :create, name: @category.name, color: "ff0", text_color: "fff"
2013-02-05 14:16:51 -05:00
end
2015-01-09 12:04:02 -05:00
it { is_expected.not_to respond_with(:success) }
2013-02-05 14:16:51 -05:00
it "returns errors on a duplicate category name" do
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
2013-02-05 14:16:51 -05:00
end
end
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", slug: "hello-cat",
auto_close_hours: 72,
permissions: {
"everyone" => readonly,
"staff" => create_post
}
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
category = Category.find_by(name: "hello")
2015-01-09 12:04:02 -05:00
expect(category.category_groups.map{|g| [g.group_id, g.permission_type]}.sort).to eq([
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
2015-01-09 12:04:02 -05:00
])
expect(category.name).to eq("hello")
expect(category.slug).to eq("hello-cat")
expect(category.color).to eq("ff0")
expect(category.auto_close_hours).to eq(72)
2013-02-05 14:16:51 -05:00
end
end
end
end
describe "destroy" do
2013-02-05 14:16:51 -05:00
it "requires the user to be logged in" do
2015-01-09 12:04:02 -05:00
expect { xhr :delete, :destroy, id: "category"}.to raise_error(Discourse::NotLoggedIn)
2013-02-05 14:16:51 -05:00
end
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
2015-01-09 12:04:02 -05:00
expect(response).to 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)
2015-01-09 12:04:02 -05:00
expect { xhr :delete, :destroy, id: @category.slug}.to change(Category, :count).by(-1)
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
end
describe "upload" do
it "requires the user to be logged in" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :upload, image_type: 'logo'}.to raise_error(Discourse::NotLoggedIn)
end
describe "logged in" do
let!(:user) { log_in(:admin) }
let(:logo) { file_from_fixtures("logo.png") }
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
2015-01-09 12:04:02 -05:00
expect(response).to be_forbidden
end
it "requires the `image_type` param" do
2015-01-09 12:04:02 -05:00
expect { xhr :post, :upload }.to 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
2015-01-09 12:04:02 -05:00
expect(response).to be_success
end
end
end
describe "update" do
2013-02-05 14:16:51 -05:00
it "requires the user to be logged in" do
2015-01-09 12:04:02 -05:00
expect { xhr :put, :update, id: 'category'}.to raise_error(Discourse::NotLoggedIn)
2013-02-05 14:16:51 -05:00
end
describe "logged in" do
let(:valid_attrs) { {id: @category.id, name: "hello", color: "ff0", text_color: "fff"} }
2013-02-25 11:42:20 -05:00
before do
@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)
xhr :put, :update, id: @category.slug, name: 'hello', color: 'ff0', text_color: 'fff'
2015-01-09 12:04:02 -05:00
expect(response).to be_forbidden
2013-02-05 14:16:51 -05:00
end
2013-02-25 11:42:20 -05:00
it "requires a name" do
2015-01-09 12:04:02 -05:00
expect { xhr :put, :update, id: @category.slug, color: 'fff', text_color: '0ff' }.to 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
2015-01-09 12:04:02 -05:00
expect { xhr :put, :update, id: @category.slug, name: 'asdf', text_color: '0ff' }.to raise_error(ActionController::ParameterMissing)
end
it "requires a text color" do
2015-01-09 12:04:02 -05:00
expect { xhr :put, :update, id: @category.slug, name: 'asdf', color: 'fff' }.to raise_error(ActionController::ParameterMissing)
2013-02-05 14:16:51 -05:00
end
describe "failure" do
2013-02-05 14:16:51 -05:00
before do
@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
it "returns errors on a duplicate category name" do
2015-01-09 12:04:02 -05:00
expect(response).not_to be_success
2013-02-05 14:16:51 -05:00
end
it "returns errors on a duplicate category name" do
2015-01-09 12:04:02 -05:00
expect(response.code.to_i).to eq(422)
2013-02-05 14:16:51 -05:00
end
end
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", slug: "hello-category",
auto_close_hours: 72,
permissions: {
"everyone" => readonly,
"staff" => create_post
}
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(200)
2013-02-05 14:16:51 -05:00
@category.reload
2015-01-09 12:04:02 -05:00
expect(@category.category_groups.map{|g| [g.group_id, g.permission_type]}.sort).to eq([
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
2015-01-09 12:04:02 -05:00
])
expect(@category.name).to eq("hello")
expect(@category.slug).to eq("hello-category")
expect(@category.color).to eq("ff0")
expect(@category.auto_close_hours).to eq(72)
end
2013-02-05 14:16:51 -05:00
end
end
end
2014-12-20 09:07:29 -05:00
describe 'update_slug' do
it 'requires the user to be logged in' do
2015-01-09 12:04:02 -05:00
expect { xhr :put, :update_slug, category_id: 'category'}.to raise_error(Discourse::NotLoggedIn)
2014-12-20 09:07:29 -05:00
end
describe 'logged in' do
let(:valid_attrs) { {id: @category.id, slug: 'fff'} }
before do
@user = log_in(:admin)
@category = Fabricate(:happy_category, user: @user)
end
it 'rejects blank' do
xhr :put, :update_slug, category_id: @category.id, slug: nil
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
2014-12-20 09:07:29 -05:00
end
it 'accepts valid custom slug' do
xhr :put, :update_slug, category_id: @category.id, slug: 'valid-slug'
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-12-20 09:07:29 -05:00
category = Category.find(@category.id)
2015-01-09 12:04:02 -05:00
expect(category.slug).to eq('valid-slug')
2014-12-20 09:07:29 -05:00
end
it 'accepts not well formed custom slug' do
xhr :put, :update_slug, category_id: @category.id, slug: ' valid slug'
2015-01-09 12:04:02 -05:00
expect(response).to be_success
2014-12-20 09:07:29 -05:00
category = Category.find(@category.id)
2015-01-09 12:04:02 -05:00
expect(category.slug).to eq('valid-slug')
2014-12-20 09:07:29 -05:00
end
it 'rejects invalid custom slug' do
xhr :put, :update_slug, category_id: @category.id, slug: ' '
2015-01-09 12:04:02 -05:00
expect(response.status).to eq(422)
2014-12-20 09:07:29 -05:00
end
end
end
2013-02-05 14:16:51 -05:00
end