1
0
Fork 0
discourse-custom-wizard-unl.../spec/components/custom_wizard/action_spec.rb

134 Zeilen
5,1 KiB
Ruby

2020-10-20 01:15:03 +02:00
require 'rails_helper'
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) }
before do
Group.refresh_automatic_group!(:trust_level_2)
template = JSON.parse(File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
).read)
CustomWizard::Wizard.add_wizard(template)
@wizard = CustomWizard::Wizard.create('super_mega_fun_wizard', user)
end
2020-04-11 08:22:12 +02:00
it 'creates a topic' do
2020-10-20 01:15:03 +02:00
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
updater = built_wizard.create_updater(built_wizard.steps[0].id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
updater2 = built_wizard.create_updater(built_wizard.steps[1].id, {}).update
2020-04-11 08:22:12 +02:00
topic = Topic.where(title: "Topic Title")
expect(topic.exists?).to eq(true)
expect(Post.where(
topic_id: topic.pluck(:id),
raw: "topic body"
).exists?).to eq(true)
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-20 01:15:03 +02:00
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
built_wizard.create_updater(built_wizard.steps[0].id, {}).update
built_wizard.create_updater(built_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-20 01:15:03 +02:00
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
upload = Upload.create!(
url: '/images/image.png',
original_filename: 'image.png',
filesize: 100,
user_id: -1,
)
steps = built_wizard.steps
built_wizard.create_updater(steps[0].id, {}).update
built_wizard.create_updater(steps[1].id,
step_2_field_7: upload.as_json,
).update
expect(user.profile_background_upload.id).to eq(upload.id)
2020-04-11 08:22:12 +02:00
end
it 'opens a composer' do
2020-10-20 01:15:03 +02:00
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
built_wizard.create_updater(built_wizard.steps[0].id, step_1_field_1: "Text input").update
2020-04-11 08:22:12 +02:00
2020-10-20 01:15:03 +02:00
updater = built_wizard.create_updater(built_wizard.steps[1].id, {})
updater.update
2020-04-11 08:22:12 +02:00
2020-10-20 01:15:03 +02:00
submissions = PluginStore.get("super_mega_fun_wizard_submissions", user.id)
category = Category.find_by(id: submissions.first['action_8'])
2020-04-11 08:22:12 +02:00
2020-10-20 01:15:03 +02:00
expect(updater.result[:redirect_on_next]).to eq(
"/new-topic?title=Title%20of%20the%20composer%20topic&body=I%20am%20interpolating%20some%20user%20fields%20Angus%20angus%20angus@email.com&category=#{category.slug}/#{category.id}&tags=tag1"
2020-04-11 08:22:12 +02:00
)
end
2020-10-20 01:15:03 +02:00
it 'creates a category' do
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
built_wizard.create_updater(built_wizard.steps[0].id, step_1_field_1: "Text input").update
built_wizard.create_updater(built_wizard.steps[1].id, {}).update
submissions = PluginStore.get("super_mega_fun_wizard_submissions", user.id)
expect(Category.where(id: submissions.first['action_8']).exists?).to eq(true)
end
it 'creates a group' do
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
step_id = built_wizard.steps[0].id
updater = built_wizard.create_updater(step_id, step_1_field_1: "Text input").update
submissions = PluginStore.get("super_mega_fun_wizard_submissions", user.id)
expect(Group.where(name: submissions.first['action_9']).exists?).to eq(true)
end
it 'adds a user to a group' do
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
step_id = built_wizard.steps[0].id
updater = built_wizard.create_updater(step_id, step_1_field_1: "Text input").update
submissions = PluginStore.get("super_mega_fun_wizard_submissions", user.id)
group = Group.find_by(name: submissions.first['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
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
built_wizard.create_updater(built_wizard.steps[0].id, step_1_field_1: "Text input").update
built_wizard.create_updater(built_wizard.steps[1].id, {}).update
submissions = PluginStore.get("super_mega_fun_wizard_submissions", user.id)
expect(CategoryUser.where(
category_id: submissions.first['action_8'],
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-20 01:15:03 +02:00
built_wizard = CustomWizard::Builder.new(@wizard.id, user).build
updater = built_wizard.create_updater(built_wizard.steps.last.id, {})
updater.update
expect(updater.result[:redirect_on_complete]).to eq("https://google.com")
2020-04-11 08:22:12 +02:00
end
end