2015-10-11 05:41:23 -04:00
|
|
|
require "rails_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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect { post :create, format: :json }.to 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
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
name: 'hello', color: 'ff0', text_color: 'fff'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
it "raises an exception when the name is missing" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, params: { color: "ff0", text_color: "fff" }, format: :json
|
|
|
|
end.to 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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, params: { name: "hello", text_color: "fff" }, format: :json
|
|
|
|
end.to 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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, params: { name: "hello", color: "ff0" }, format: :json
|
|
|
|
end.to 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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
name: @category.name, color: "ff0", text_color: "fff"
|
|
|
|
}, format: :json
|
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
|
|
|
|
2013-07-16 01:44:07 -04: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
|
|
|
|
|
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]
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
name: "hello",
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "fff",
|
|
|
|
slug: "hello-cat",
|
|
|
|
auto_close_hours: 72,
|
|
|
|
permissions: {
|
|
|
|
"everyone" => readonly,
|
|
|
|
"staff" => create_post
|
|
|
|
}
|
|
|
|
}, format: :json
|
2013-07-16 01:44:07 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-05-06 09:41:59 -04:00
|
|
|
category = Category.find_by(name: "hello")
|
2017-07-27 21:20:09 -04: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)
|
2016-04-26 13:08:19 -04:00
|
|
|
expect(UserHistory.count).to eq(4) # 1 + 3 (bootstrap mode)
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect { delete :destroy, params: { id: "category" }, format: :json }
|
|
|
|
.to 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)
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: @category.slug }, format: :json
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
delete :destroy, params: { id: @category.slug }, format: :json
|
|
|
|
end.to change(Category, :count).by(-1)
|
|
|
|
|
2015-09-17 03:51:32 -04:00
|
|
|
expect(UserHistory.count).to eq(1)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-08-27 13:14:59 -04:00
|
|
|
describe "reorder" do
|
|
|
|
it "reorders the categories" do
|
|
|
|
admin = log_in(:admin)
|
|
|
|
|
|
|
|
c1 = Fabricate(:category)
|
|
|
|
c2 = Fabricate(:category)
|
|
|
|
c3 = Fabricate(:category)
|
|
|
|
c4 = Fabricate(:category)
|
|
|
|
if c3.id < c2.id
|
|
|
|
tmp = c3; c2 = c3; c3 = tmp;
|
|
|
|
end
|
|
|
|
c1.position = 8
|
|
|
|
c2.position = 6
|
|
|
|
c3.position = 7
|
|
|
|
c4.position = 5
|
|
|
|
|
|
|
|
payload = {}
|
|
|
|
payload[c1.id] = 4
|
|
|
|
payload[c2.id] = 6
|
|
|
|
payload[c3.id] = 6
|
|
|
|
payload[c4.id] = 5
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :reorder, params: { mapping: MultiJson.dump(payload) }, format: :json
|
2015-08-27 13:14:59 -04:00
|
|
|
|
|
|
|
SiteSetting.fixed_category_positions = true
|
|
|
|
list = CategoryList.new(Guardian.new(admin))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2015-08-27 13:14:59 -04:00
|
|
|
expect(list.categories).to eq([
|
2017-08-31 00:06:56 -04:00
|
|
|
Category.find(SiteSetting.uncategorized_category_id),
|
|
|
|
c1,
|
|
|
|
c4,
|
|
|
|
c2,
|
|
|
|
c3
|
|
|
|
])
|
2015-08-27 13:14:59 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect { put :update, params: { id: 'category' }, format: :json }.to 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
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:valid_attrs) { { id: @category.id, name: "hello", color: "ff0", text_color: "fff" } }
|
2013-12-16 15:13:43 -05:00
|
|
|
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: @category.slug,
|
|
|
|
name: 'hello',
|
|
|
|
color: 'ff0',
|
|
|
|
text_color: 'fff'
|
|
|
|
}, format: :json
|
|
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
put :update, params: {
|
|
|
|
id: @category.slug,
|
|
|
|
color: 'fff',
|
|
|
|
text_color: '0ff',
|
|
|
|
}, format: :json
|
|
|
|
end.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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
put :update, params: {
|
|
|
|
id: @category.slug,
|
|
|
|
name: 'asdf',
|
|
|
|
text_color: '0ff',
|
|
|
|
}, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2013-03-14 09:16:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a text color" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
put :update,
|
|
|
|
params: { id: @category.slug, name: 'asdf', color: 'fff' },
|
|
|
|
format: :json
|
|
|
|
end.to 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
|
2017-07-27 21:20:09 -04:00
|
|
|
@other_category = Fabricate(:category, name: "Other", user: @user)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update, params: {
|
|
|
|
id: @category.id,
|
|
|
|
name: @other_category.name,
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "fff",
|
|
|
|
}, format: :json
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
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
|
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
|
|
|
|
|
2015-04-16 06:39:48 -04:00
|
|
|
it "returns 422 if email_in address is already in use for other category" do
|
2017-07-27 21:20:09 -04:00
|
|
|
@other_category = Fabricate(:category, name: "Other", email_in: "mail@examle.com")
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update, params: {
|
|
|
|
id: @category.id,
|
|
|
|
name: "Email",
|
|
|
|
email_in: "mail@examle.com",
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "fff",
|
|
|
|
}, format: :json
|
2015-04-16 06:39:48 -04:00
|
|
|
|
|
|
|
expect(response).not_to be_success
|
|
|
|
expect(response.code.to_i).to eq(422)
|
|
|
|
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]
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: @category.id,
|
|
|
|
name: "hello",
|
|
|
|
color: "ff0",
|
|
|
|
text_color: "fff",
|
|
|
|
slug: "hello-category",
|
|
|
|
auto_close_hours: 72,
|
|
|
|
permissions: {
|
|
|
|
"everyone" => readonly,
|
|
|
|
"staff" => create_post
|
|
|
|
},
|
|
|
|
custom_fields: {
|
|
|
|
"dancing" => "frogs"
|
|
|
|
},
|
|
|
|
}, format: :json
|
2013-07-16 01:44:07 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-02-05 14:16:51 -05:00
|
|
|
@category.reload
|
2017-07-27 21:20:09 -04: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)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(@category.custom_fields).to eq("dancing" => "frogs")
|
2013-12-16 15:13:43 -05:00
|
|
|
end
|
2015-09-17 03:51:32 -04:00
|
|
|
|
|
|
|
it 'logs the changes correctly' do
|
2015-12-06 11:09:02 -05:00
|
|
|
@category.update!(permissions: { "admins" => CategoryGroup.permission_types[:create_post] })
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: @category.id,
|
|
|
|
name: 'new name',
|
|
|
|
color: @category.color,
|
|
|
|
text_color: @category.text_color,
|
|
|
|
slug: @category.slug,
|
|
|
|
permissions: {
|
2015-09-17 03:51:32 -04:00
|
|
|
"everyone" => CategoryGroup.permission_types[:create_post]
|
2017-08-31 00:06:56 -04:00
|
|
|
},
|
|
|
|
}, format: :json
|
2015-09-17 03:51:32 -04:00
|
|
|
|
2016-04-26 13:08:19 -04:00
|
|
|
expect(UserHistory.count).to eq(5) # 2 + 3 (bootstrap mode)
|
2015-09-17 03:51:32 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
put :update_slug, params: { category_id: 'category' }, format: :json
|
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
2014-12-20 09:07:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'logged in' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:valid_attrs) { { id: @category.id, slug: 'fff' } }
|
2014-12-20 09:07:29 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
@user = log_in(:admin)
|
|
|
|
@category = Fabricate(:happy_category, user: @user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects blank' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_slug, params: { category_id: @category.id, slug: nil }, format: :json
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_slug,
|
|
|
|
params: { category_id: @category.id, slug: 'valid-slug' },
|
|
|
|
format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2015-05-13 04:52:48 -04:00
|
|
|
expect(@category.reload.slug).to eq('valid-slug')
|
2014-12-20 09:07:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts not well formed custom slug' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_slug,
|
|
|
|
params: { category_id: @category.id, slug: ' valid slug' },
|
|
|
|
format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2015-05-13 04:52:48 -04:00
|
|
|
expect(@category.reload.slug).to eq('valid-slug')
|
|
|
|
end
|
|
|
|
|
2017-10-03 06:52:24 -04:00
|
|
|
it 'accepts and sanitize custom slug when the slug generation method is not ascii' do
|
2015-05-13 04:52:48 -04:00
|
|
|
SiteSetting.slug_generation_method = 'none'
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_slug,
|
|
|
|
params: { category_id: @category.id, slug: ' another !_ slug @' },
|
|
|
|
format: :json
|
|
|
|
|
2015-05-13 04:52:48 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(@category.reload.slug).to eq('another-slug')
|
|
|
|
SiteSetting.slug_generation_method = 'ascii'
|
2014-12-20 09:07:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects invalid custom slug' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_slug,
|
|
|
|
params: { category_id: @category.id, slug: ' ' },
|
|
|
|
format: :json
|
|
|
|
|
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
|