1
0
Fork 0
discourse-custom-wizard-unl.../spec/extensions/guardian_extension_spec.rb

96 Zeilen
2,9 KiB
Ruby

2021-10-19 05:35:55 +02:00
# frozen_string_literal: true
require_relative '../plugin_helper'
describe ::Guardian do
2021-10-30 12:33:30 +02:00
fab!(:user) {
2021-10-19 05:35:55 +02:00
Fabricate(:user, name: "Angus", username: 'angus', email: "angus@email.com")
}
fab!(:category) { Fabricate(:category, name: 'cat1', slug: 'cat-slug') }
let(:wizard_template) {
JSON.parse(
File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
).read
)
}
before do
CustomWizard::Template.save(wizard_template, skip_jobs: true)
@template = CustomWizard::Template.find('super_mega_fun_wizard')
end
context "the user has access to creating wizard" do
it "allows editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_truthy
end
end
context "the user doesn't have access to creating wizard" do
it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
CustomWizard::Wizard.any_instance.stubs(:permitted?).returns(false)
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
end
end
2021-10-30 12:33:30 +02:00
context "the wizard can't create a topic in the category" do
it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
new_category = Fabricate(:category)
topic.category_id = new_category.id
topic.save
topic.reload
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
end
end
2021-10-19 05:35:55 +02:00
end