0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-29 20:20:29 +01:00
discourse-custom-wizard/spec/components/custom_wizard/action_spec.rb

189 Zeilen
6,4 KiB
Ruby

require_relative '../../plugin_helper'
2020-10-20 01:15:03 +02:00
2020-04-11 08:22:12 +02:00
describe CustomWizard::Action do
2020-10-20 01:15:03 +02:00
fab!(:user) { Fabricate(:user, name: "Angus", username: 'angus', email: "angus@email.com", trust_level: TrustLevel[2]) }
fab!(:category) { Fabricate(:category, name: 'cat1', slug: 'cat-slug') }
fab!(:group) { Fabricate(:group) }
let(:open_composer) {
JSON.parse(File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/actions/open_composer.json"
).read)
}
2020-10-20 01:15:03 +02:00
before do
Group.refresh_automatic_group!(:trust_level_2)
2020-11-03 01:24:20 +01:00
CustomWizard::Template.save(
2020-10-31 08:05:50 +01:00
JSON.parse(File.open(
2020-11-03 01:24:20 +01:00
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
).read),
skip_jobs: true)
2020-10-31 08:05:50 +01:00
@template = CustomWizard::Template.find('super_mega_fun_wizard')
2020-10-20 01:15:03 +02:00
end
2020-04-11 08:22:12 +02:00
2020-11-03 01:24:20 +01:00
context 'creating a topic' do
it "works" 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
)
expect(topic.exists?).to eq(true)
expect(Post.where(
topic_id: topic.pluck(:id),
raw: "topic body"
).exists?).to eq(true)
end
2020-04-11 08:22:12 +02:00
2020-11-03 01:24:20 +01:00
it "fails silently without basic topic inputs" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
updater = wizard.create_updater(wizard.steps.last.id, {})
updater.update
expect(updater.success?).to eq(true)
expect(UserHistory.where(
acting_user_id: user.id,
context: "super_mega_fun_wizard",
subject: "step_3"
).exists?).to eq(true)
expect(Post.where(
raw: "topic body"
).exists?).to eq(false)
end
2020-04-11 08:22:12 +02:00
end
it 'sends a message' do
2020-10-20 01:15:03 +02:00
User.create(username: 'angus1', email: "angus1@email.com")
2020-04-11 08:22:12 +02:00
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, {}).update
wizard.create_updater(wizard.steps[1].id, {}).update
2020-04-11 08:22:12 +02:00
topic = Topic.where(
archetype: Archetype.private_message,
2020-10-20 01:15:03 +02:00
title: "Message title"
2020-04-11 08:22:12 +02:00
)
2020-10-20 01:15:03 +02:00
post = Post.where(
2020-04-11 08:22:12 +02:00
topic_id: topic.pluck(:id),
2020-10-20 01:15:03 +02:00
raw: "I will interpolate some wizard fields"
)
expect(topic.exists?).to eq(true)
expect(topic.first.topic_allowed_users.first.user.username).to eq('angus1')
expect(post.exists?).to eq(true)
2020-04-11 08:22:12 +02:00
end
it 'updates a profile' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
2020-10-20 01:15:03 +02:00
upload = Upload.create!(
url: '/images/image.png',
original_filename: 'image.png',
filesize: 100,
user_id: -1,
)
2020-10-31 08:05:50 +01:00
steps = wizard.steps
wizard.create_updater(steps[0].id, {}).update
wizard.create_updater(steps[1].id,
step_2_field_7: upload.as_json
2020-10-20 01:15:03 +02:00
).update
expect(user.profile_background_upload.id).to eq(upload.id)
2020-04-11 08:22:12 +02:00
end
context "open composer" do
it 'works' do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
updater = wizard.create_updater(wizard.steps[1].id, {})
updater.update
category = Category.find_by(id: wizard.current_submission['action_8'])
expect(updater.result[:redirect_on_next]).to eq(
2021-02-18 04:44:14 +01:00
"/new-topic?title=Title%20of%20the%20composer%20topic&body=I%20am%20interpolating%20some%20user%20fields%20Angus%20angus%20angus%40email.com&category_id=#{category.id}&tags=tag1"
)
end
2020-04-11 08:22:12 +02:00
it 'encodes special characters in the title and body' do
open_composer['title'][0]['output'] = "Title that's special $"
open_composer['post_template'] = "Body & more body & more body"
wizard = CustomWizard::Wizard.new(@template, user)
action = CustomWizard::Action.new(
wizard: wizard,
action: open_composer,
user: user,
data: {}
)
action.perform
expect(action.result.success?).to eq(true)
decoded_output = CGI.parse(URI.parse(action.result.output).query)
expect(decoded_output['title'][0]).to eq("Title that's special $")
expect(decoded_output['body'][0]).to eq("Body & more body & more body")
end
2020-04-11 08:22:12 +02:00
end
2020-10-20 01:15:03 +02:00
it 'creates a category' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
wizard.create_updater(wizard.steps[1].id, {}).update
2020-11-03 01:24:20 +01:00
expect(Category.where(id: wizard.current_submission['action_8']).exists?).to eq(true)
2020-10-20 01:15:03 +02:00
end
it 'creates a group' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
step_id = wizard.steps[0].id
updater = wizard.create_updater(step_id, step_1_field_1: "Text input").update
2020-11-03 01:24:20 +01:00
expect(Group.where(name: wizard.current_submission['action_9']).exists?).to eq(true)
2020-10-20 01:15:03 +02:00
end
it 'adds a user to a group' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
step_id = wizard.steps[0].id
updater = wizard.create_updater(step_id, step_1_field_1: "Text input").update
2020-11-03 01:24:20 +01:00
group = Group.find_by(name: wizard.current_submission['action_9'])
2020-04-11 08:22:12 +02:00
expect(group.users.first.username).to eq('angus')
end
2020-10-20 01:15:03 +02:00
it 'watches categories' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
wizard.create_updater(wizard.steps[1].id, {}).update
2020-10-20 01:15:03 +02:00
expect(CategoryUser.where(
2020-11-03 01:24:20 +01:00
category_id: wizard.current_submission['action_8'],
2020-10-20 01:15:03 +02:00
user_id: user.id
).first.notification_level).to eq(2)
expect(CategoryUser.where(
category_id: category.id,
user_id: user.id
).first.notification_level).to eq(0)
end
2020-04-11 08:22:12 +02:00
it 're-routes a user' do
2020-10-31 08:05:50 +01:00
wizard = CustomWizard::Builder.new(@template[:id], user).build
updater = wizard.create_updater(wizard.steps.last.id, {})
2020-10-20 01:15:03 +02:00
updater.update
expect(updater.result[:redirect_on_complete]).to eq("https://google.com")
2020-04-11 08:22:12 +02:00
end
end