mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Move spec/components to spec/lib (#15987)
Lib specs were inexplicably split into two directories (`lib` and `components`) This moves them all into `lib`.
This commit is contained in:
410
spec/lib/wizard/step_updater_spec.rb
Normal file
410
spec/lib/wizard/step_updater_spec.rb
Normal file
@@ -0,0 +1,410 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe Wizard::StepUpdater do
|
||||
before do
|
||||
SiteSetting.wizard_enabled = true
|
||||
end
|
||||
|
||||
fab!(:user) { Fabricate(:admin) }
|
||||
let(:wizard) { Wizard::Builder.new(user).build }
|
||||
|
||||
context "locale" do
|
||||
it "does not require refresh when the language stays the same" do
|
||||
locale = SiteSettings::DefaultsProvider::DEFAULT_LOCALE
|
||||
updater = wizard.create_updater('locale', default_locale: locale)
|
||||
updater.update
|
||||
expect(updater.refresh_required?).to eq(false)
|
||||
expect(wizard.completed_steps?('locale')).to eq(true)
|
||||
end
|
||||
|
||||
it "updates the locale and requires refresh when it does change" do
|
||||
updater = wizard.create_updater('locale', default_locale: 'ru')
|
||||
updater.update
|
||||
expect(SiteSetting.default_locale).to eq('ru')
|
||||
expect(updater.refresh_required?).to eq(true)
|
||||
expect(wizard.completed_steps?('locale')).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
it "updates the forum title step" do
|
||||
updater = wizard.create_updater('forum_title', title: 'new forum title', site_description: 'neat place', short_site_description: 'best community')
|
||||
updater.update
|
||||
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(SiteSetting.title).to eq("new forum title")
|
||||
expect(SiteSetting.site_description).to eq("neat place")
|
||||
expect(SiteSetting.short_site_description).to eq("best community")
|
||||
expect(wizard.completed_steps?('forum-title')).to eq(true)
|
||||
end
|
||||
|
||||
it "updates the introduction step" do
|
||||
topic = Fabricate(:topic, title: "Welcome to Discourse")
|
||||
welcome_post = Fabricate(:post, topic: topic, raw: "this will be the welcome topic post\n\ncool!")
|
||||
|
||||
updater = wizard.create_updater('introduction', welcome: "Welcome to my new awesome forum!")
|
||||
updater.update
|
||||
|
||||
expect(updater.success?).to eq(true)
|
||||
welcome_post.reload
|
||||
expect(welcome_post.raw).to eq("Welcome to my new awesome forum!\n\ncool!")
|
||||
|
||||
expect(wizard.completed_steps?('introduction')).to eq(true)
|
||||
|
||||
end
|
||||
|
||||
it "won't allow updates to the default value, when required" do
|
||||
updater = wizard.create_updater('forum_title', title: SiteSetting.title, site_description: 'neat place')
|
||||
updater.update
|
||||
|
||||
expect(updater.success?).to eq(false)
|
||||
end
|
||||
|
||||
context "privacy settings" do
|
||||
it "updates to open correctly" do
|
||||
updater = wizard.create_updater('privacy', privacy: 'open', privacy_options: 'open')
|
||||
updater.update
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(SiteSetting.login_required?).to eq(false)
|
||||
expect(SiteSetting.invite_only?).to eq(false)
|
||||
expect(wizard.completed_steps?('privacy')).to eq(true)
|
||||
end
|
||||
|
||||
it "updates to private correctly" do
|
||||
updater = wizard.create_updater('privacy', privacy: 'restricted', privacy_options: 'invite_only')
|
||||
updater.update
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(SiteSetting.login_required?).to eq(true)
|
||||
expect(SiteSetting.invite_only?).to eq(true)
|
||||
expect(wizard.completed_steps?('privacy')).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context "contact step" do
|
||||
it "updates the fields correctly" do
|
||||
p = Fabricate(:post, raw: '<contact_email> template')
|
||||
SiteSetting.tos_topic_id = p.topic_id
|
||||
|
||||
updater = wizard.create_updater('contact',
|
||||
contact_email: 'eviltrout@example.com',
|
||||
contact_url: 'http://example.com/custom-contact-url',
|
||||
site_contact: user.username)
|
||||
|
||||
updater.update
|
||||
expect(updater).to be_success
|
||||
expect(SiteSetting.contact_email).to eq("eviltrout@example.com")
|
||||
expect(SiteSetting.contact_url).to eq("http://example.com/custom-contact-url")
|
||||
expect(SiteSetting.site_contact_username).to eq(user.username)
|
||||
|
||||
# Should update the TOS topic
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("<eviltrout@example.com> template")
|
||||
|
||||
# Can update the TOS topic again
|
||||
updater = wizard.create_updater('contact', contact_email: 'alice@example.com')
|
||||
updater.update
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("<alice@example.com> template")
|
||||
|
||||
# Can update the TOS to nothing
|
||||
updater = wizard.create_updater('contact', {})
|
||||
updater.update
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("<contact_email> template")
|
||||
|
||||
expect(wizard.completed_steps?('contact')).to eq(true)
|
||||
end
|
||||
|
||||
it "doesn't update when there are errors" do
|
||||
updater = wizard.create_updater('contact',
|
||||
contact_email: 'not-an-email',
|
||||
site_contact_username: 'not-a-username')
|
||||
updater.update
|
||||
expect(updater).to_not be_success
|
||||
expect(updater.errors).to be_present
|
||||
expect(wizard.completed_steps?('contact')).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "corporate step" do
|
||||
|
||||
it "updates the fields properly" do
|
||||
|
||||
p = Fabricate(:post, raw: 'company_name - governing_law - city_for_disputes template')
|
||||
SiteSetting.tos_topic_id = p.topic_id
|
||||
|
||||
updater = wizard.create_updater('corporate',
|
||||
company_name: 'ACME, Inc.',
|
||||
governing_law: 'New Jersey law',
|
||||
city_for_disputes: 'Fairfield, New Jersey')
|
||||
updater.update
|
||||
expect(updater).to be_success
|
||||
expect(SiteSetting.company_name).to eq("ACME, Inc.")
|
||||
expect(SiteSetting.governing_law).to eq("New Jersey law")
|
||||
expect(SiteSetting.city_for_disputes).to eq("Fairfield, New Jersey")
|
||||
|
||||
# Should update the TOS topic
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("ACME, Inc. - New Jersey law - Fairfield, New Jersey template")
|
||||
|
||||
# Can update the TOS topic again
|
||||
updater = wizard.create_updater('corporate',
|
||||
company_name: 'Pied Piper Inc',
|
||||
governing_law: 'California law',
|
||||
city_for_disputes: 'San Francisco, California')
|
||||
updater.update
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("Pied Piper Inc - California law - San Francisco, California template")
|
||||
|
||||
# Can update the TOS to nothing
|
||||
updater = wizard.create_updater('corporate', {})
|
||||
updater.update
|
||||
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck_first(:raw)
|
||||
expect(raw).to eq("company_name - governing_law - city_for_disputes template")
|
||||
|
||||
expect(wizard.completed_steps?('corporate')).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context "styling step" do
|
||||
it "updates fonts" do
|
||||
updater = wizard.create_updater('styling',
|
||||
body_font: 'open_sans',
|
||||
heading_font: 'oswald',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
updater.update
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(wizard.completed_steps?('styling')).to eq(true)
|
||||
expect(SiteSetting.base_font).to eq('open_sans')
|
||||
expect(SiteSetting.heading_font).to eq('oswald')
|
||||
end
|
||||
|
||||
context "colors" do
|
||||
context "with an existing color scheme" do
|
||||
fab!(:color_scheme) { Fabricate(:color_scheme, name: 'existing', via_wizard: true) }
|
||||
|
||||
it "updates the scheme" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Dark',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
updater.update
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(wizard.completed_steps?('styling')).to eq(true)
|
||||
theme = Theme.find_by(id: SiteSetting.default_theme_id)
|
||||
expect(theme.color_scheme.base_scheme_id).to eq('Dark')
|
||||
end
|
||||
end
|
||||
|
||||
context "with an existing default theme" do
|
||||
fab!(:theme) { Fabricate(:theme) }
|
||||
|
||||
before do
|
||||
theme.set_default!
|
||||
end
|
||||
|
||||
it "should update the color scheme of the default theme" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Neutral',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
expect { updater.update }.not_to change { Theme.count }
|
||||
theme.reload
|
||||
expect(theme.color_scheme.base_scheme_id).to eq('Neutral')
|
||||
end
|
||||
end
|
||||
|
||||
context "without an existing theme" do
|
||||
before do
|
||||
Theme.delete_all
|
||||
end
|
||||
|
||||
context 'dark theme' do
|
||||
it "creates the theme" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Dark',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
|
||||
expect { updater.update }.to change { Theme.count }.by(1)
|
||||
|
||||
theme = Theme.last
|
||||
|
||||
expect(theme.user_id).to eq(wizard.user.id)
|
||||
expect(theme.color_scheme.base_scheme_id).to eq('Dark')
|
||||
end
|
||||
end
|
||||
|
||||
context 'light theme' do
|
||||
it "creates the theme" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: ColorScheme::LIGHT_THEME_ID,
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
|
||||
expect { updater.update }.to change { Theme.count }.by(1)
|
||||
|
||||
theme = Theme.last
|
||||
|
||||
expect(theme.user_id).to eq(wizard.user.id)
|
||||
|
||||
expect(theme.color_scheme).to eq(ColorScheme.find_by(name:
|
||||
ColorScheme::LIGHT_THEME_ID
|
||||
))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "without an existing scheme" do
|
||||
it "creates the scheme" do
|
||||
ColorScheme.destroy_all
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Dark',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
updater.update
|
||||
expect(updater.success?).to eq(true)
|
||||
expect(wizard.completed_steps?('styling')).to eq(true)
|
||||
|
||||
color_scheme = ColorScheme.where(via_wizard: true).first
|
||||
expect(color_scheme).to be_present
|
||||
expect(color_scheme.colors).to be_present
|
||||
|
||||
theme = Theme.find_by(id: SiteSetting.default_theme_id)
|
||||
expect(theme.color_scheme_id).to eq(color_scheme.id)
|
||||
end
|
||||
end
|
||||
|
||||
context "auto dark mode" do
|
||||
before do
|
||||
dark_scheme = ColorScheme.where(name: "Dark").first
|
||||
SiteSetting.default_dark_mode_color_scheme_id = dark_scheme.id
|
||||
end
|
||||
|
||||
it "does nothing when selected scheme is light" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Neutral',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
|
||||
expect { updater.update }.not_to change { SiteSetting.default_dark_mode_color_scheme_id }
|
||||
end
|
||||
|
||||
it "unsets auto dark mode site setting when default selected scheme is also dark" do
|
||||
updater = wizard.create_updater('styling',
|
||||
color_scheme: 'Latte',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: 'latest'
|
||||
)
|
||||
|
||||
expect { updater.update }.to change { SiteSetting.default_dark_mode_color_scheme_id }.to(-1)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "homepage style" do
|
||||
it "updates the fields correctly" do
|
||||
updater = wizard.create_updater('styling',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: "categories_and_top_topics"
|
||||
)
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('styling')).to eq(true)
|
||||
expect(SiteSetting.top_menu).to eq('categories|latest|new|unread|top')
|
||||
expect(SiteSetting.desktop_category_page_style).to eq('categories_and_top_topics')
|
||||
|
||||
updater = wizard.create_updater('styling',
|
||||
body_font: 'arial',
|
||||
heading_font: 'arial',
|
||||
homepage_style: "latest"
|
||||
)
|
||||
updater.update
|
||||
expect(updater).to be_success
|
||||
expect(SiteSetting.top_menu).to eq('latest|new|unread|top|categories')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "logos step" do
|
||||
it "updates the fields correctly" do
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
updater = wizard.create_updater(
|
||||
'logos',
|
||||
logo: upload.url,
|
||||
logo_small: upload2.url
|
||||
)
|
||||
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('logos')).to eq(true)
|
||||
expect(SiteSetting.logo).to eq(upload)
|
||||
expect(SiteSetting.logo_small).to eq(upload2)
|
||||
end
|
||||
end
|
||||
|
||||
context "icons step" do
|
||||
it "updates the fields correctly" do
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
updater = wizard.create_updater('icons',
|
||||
favicon: upload.url,
|
||||
large_icon: upload2.url
|
||||
)
|
||||
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('icons')).to eq(true)
|
||||
expect(SiteSetting.favicon).to eq(upload)
|
||||
expect(SiteSetting.large_icon).to eq(upload2)
|
||||
end
|
||||
end
|
||||
|
||||
context "invites step" do
|
||||
let(:invites) {
|
||||
return [{ email: 'regular@example.com', role: 'regular' },
|
||||
{ email: 'moderator@example.com', role: 'moderator' }]
|
||||
}
|
||||
|
||||
it "updates the fields correctly" do
|
||||
updater = wizard.create_updater('invites', invite_list: invites.to_json)
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('invites')).to eq(true)
|
||||
|
||||
reg_invite = Invite.where(email: 'regular@example.com').first
|
||||
expect(reg_invite).to be_present
|
||||
expect(reg_invite.moderator?).to eq(false)
|
||||
|
||||
mod_invite = Invite.where(email: 'moderator@example.com').first
|
||||
expect(mod_invite).to be_present
|
||||
expect(mod_invite.moderator?).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
210
spec/lib/wizard/wizard_builder_spec.rb
Normal file
210
spec/lib/wizard/wizard_builder_spec.rb
Normal file
@@ -0,0 +1,210 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'wizard'
|
||||
require 'wizard/builder'
|
||||
require 'global_path'
|
||||
|
||||
class GlobalPathInstance
|
||||
extend GlobalPath
|
||||
end
|
||||
|
||||
describe Wizard::Builder do
|
||||
let(:moderator) { Fabricate.build(:moderator) }
|
||||
let(:wizard) { Wizard::Builder.new(moderator).build }
|
||||
|
||||
it "returns a wizard with steps when enabled" do
|
||||
SiteSetting.wizard_enabled = true
|
||||
|
||||
expect(wizard).to be_present
|
||||
expect(wizard.steps).to be_present
|
||||
end
|
||||
|
||||
it "returns a wizard without steps when enabled, but not staff" do
|
||||
wizard = Wizard::Builder.new(Fabricate.build(:user)).build
|
||||
expect(wizard).to be_present
|
||||
expect(wizard.steps).to be_blank
|
||||
end
|
||||
|
||||
it "returns a wizard without steps when disabled" do
|
||||
SiteSetting.wizard_enabled = false
|
||||
|
||||
expect(wizard).to be_present
|
||||
expect(wizard.steps).to be_blank
|
||||
end
|
||||
|
||||
it "returns wizard with disabled invites step when local_logins are off" do
|
||||
SiteSetting.enable_local_logins = false
|
||||
|
||||
invites_step = wizard.steps.find { |s| s.id == "invites" }
|
||||
expect(invites_step.fields).to be_blank
|
||||
expect(invites_step.disabled).to be_truthy
|
||||
end
|
||||
|
||||
context 'styling step' do
|
||||
let(:styling_step) { wizard.steps.find { |s| s.id == 'styling' } }
|
||||
let(:font_field) { styling_step.fields[1] }
|
||||
fab!(:theme) { Fabricate(:theme) }
|
||||
let(:colors_field) { styling_step.fields.first }
|
||||
|
||||
it 'has the full list of available fonts' do
|
||||
expect(font_field.choices.size).to eq(DiscourseFonts.fonts.size)
|
||||
end
|
||||
|
||||
context "colors" do
|
||||
describe "when the default theme has not been override" do
|
||||
before do
|
||||
SiteSetting.find_by(name: "default_theme_id").destroy!
|
||||
end
|
||||
|
||||
it 'should set the right default values' do
|
||||
expect(colors_field.required).to eq(true)
|
||||
expect(colors_field.value).to eq(ColorScheme::LIGHT_THEME_ID)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the default theme has been override and the color scheme doesn't have a base scheme" do
|
||||
let(:color_scheme) { Fabricate(:color_scheme, base_scheme_id: nil) }
|
||||
|
||||
before do
|
||||
SiteSetting.default_theme_id = theme.id
|
||||
theme.update(color_scheme: color_scheme)
|
||||
end
|
||||
|
||||
it 'fallbacks to the color scheme name' do
|
||||
expect(colors_field.required).to eq(false)
|
||||
expect(colors_field.value).to eq(color_scheme.name)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the default theme has been overridden by a theme without a color scheme" do
|
||||
before do
|
||||
theme.set_default!
|
||||
end
|
||||
|
||||
it 'should set the right default values' do
|
||||
expect(colors_field.required).to eq(false)
|
||||
expect(colors_field.value).to eq("Light")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the default theme has been overridden by a theme with a color scheme" do
|
||||
before do
|
||||
theme.update(color_scheme_id: ColorScheme.find_by_name("Dark").id)
|
||||
theme.set_default!
|
||||
end
|
||||
|
||||
it 'should set the right default values' do
|
||||
expect(colors_field.required).to eq(false)
|
||||
expect(colors_field.value).to eq("Dark")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'logos step' do
|
||||
let(:logos_step) { wizard.steps.find { |s| s.id == 'logos' } }
|
||||
|
||||
it 'should set the right default value for the fields' do
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
SiteSetting.logo = upload
|
||||
SiteSetting.logo_small = upload2
|
||||
|
||||
fields = logos_step.fields
|
||||
logo_field = fields.first
|
||||
logo_small_field = fields.last
|
||||
|
||||
expect(logo_field.id).to eq('logo')
|
||||
expect(logo_field.value).to eq(GlobalPathInstance.full_cdn_url(upload.url))
|
||||
expect(logo_small_field.id).to eq('logo_small')
|
||||
expect(logo_small_field.value).to eq(GlobalPathInstance.full_cdn_url(upload2.url))
|
||||
end
|
||||
end
|
||||
|
||||
context 'icons step' do
|
||||
let(:icons_step) { wizard.steps.find { |s| s.id == 'icons' } }
|
||||
|
||||
it 'should set the right default value for the fields' do
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
SiteSetting.favicon = upload
|
||||
SiteSetting.large_icon = upload2
|
||||
|
||||
fields = icons_step.fields
|
||||
favicon_field = fields.first
|
||||
large_icon_field = fields.last
|
||||
|
||||
expect(favicon_field.id).to eq('favicon')
|
||||
expect(favicon_field.value).to eq(GlobalPathInstance.full_cdn_url(upload.url))
|
||||
expect(large_icon_field.id).to eq('large_icon')
|
||||
expect(large_icon_field.value).to eq(GlobalPathInstance.full_cdn_url(upload2.url))
|
||||
end
|
||||
end
|
||||
|
||||
context 'introduction step' do
|
||||
let(:wizard) { Wizard::Builder.new(moderator).build }
|
||||
let(:introduction_step) { wizard.steps.find { |s| s.id == 'introduction' } }
|
||||
|
||||
context 'step has not been completed' do
|
||||
it 'enables the step' do
|
||||
expect(introduction_step.disabled).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'step has been completed' do
|
||||
before do
|
||||
wizard = Wizard::Builder.new(moderator).build
|
||||
introduction_step = wizard.steps.find { |s| s.id == 'introduction' }
|
||||
|
||||
# manually sets the step as completed
|
||||
logger = StaffActionLogger.new(moderator)
|
||||
logger.log_wizard_step(introduction_step)
|
||||
end
|
||||
|
||||
it 'disables step if no welcome topic' do
|
||||
expect(introduction_step.disabled).to eq(true)
|
||||
end
|
||||
|
||||
it 'enables step if welcome topic is present' do
|
||||
topic = Fabricate(:topic, title: 'Welcome to Discourse')
|
||||
welcome_post = Fabricate(:post, topic: topic, raw: "this will be the welcome topic post\n\ncool!")
|
||||
|
||||
expect(introduction_step.disabled).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'privacy step' do
|
||||
let(:privacy_step) { wizard.steps.find { |s| s.id == 'privacy' } }
|
||||
|
||||
it 'should set the right default value for the fields' do
|
||||
SiteSetting.login_required = true
|
||||
SiteSetting.invite_only = true
|
||||
|
||||
fields = privacy_step.fields
|
||||
login_required_field = fields.first
|
||||
privacy_options_field = fields.last
|
||||
|
||||
expect(fields.length).to eq(2)
|
||||
expect(login_required_field.id).to eq('privacy')
|
||||
expect(login_required_field.value).to eq("restricted")
|
||||
expect(privacy_options_field.id).to eq('privacy_options')
|
||||
expect(privacy_options_field.value).to eq("invite_only")
|
||||
end
|
||||
|
||||
it 'should not show privacy_options field on special case' do
|
||||
SiteSetting.invite_only = true
|
||||
SiteSetting.must_approve_users = true
|
||||
|
||||
fields = privacy_step.fields
|
||||
login_required_field = fields.first
|
||||
|
||||
expect(fields.length).to eq(1)
|
||||
expect(login_required_field.id).to eq('privacy')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
233
spec/lib/wizard/wizard_spec.rb
Normal file
233
spec/lib/wizard/wizard_spec.rb
Normal file
@@ -0,0 +1,233 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'wizard'
|
||||
|
||||
describe Wizard do
|
||||
fab!(:admin) { Fabricate(:admin) }
|
||||
|
||||
before do
|
||||
SiteSetting.wizard_enabled = true
|
||||
end
|
||||
|
||||
context "defaults" do
|
||||
it "has default values" do
|
||||
wizard = Wizard.new(Fabricate.build(:moderator))
|
||||
expect(wizard.steps).to be_empty
|
||||
expect(wizard.user).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
describe "append_step" do
|
||||
let(:user) { Fabricate.build(:moderator) }
|
||||
let(:wizard) { Wizard.new(user) }
|
||||
let(:step1) { wizard.create_step('first-step') }
|
||||
let(:step2) { wizard.create_step('second-step') }
|
||||
|
||||
it "works with a block format" do
|
||||
wizard.append_step('wat') do |step|
|
||||
expect(step).to be_present
|
||||
end
|
||||
|
||||
expect(wizard.steps.size).to eq(1)
|
||||
end
|
||||
|
||||
it "adds the step correctly" do
|
||||
expect(step1.index).to be_blank
|
||||
|
||||
wizard.append_step(step1)
|
||||
expect(wizard.steps.size).to eq(1)
|
||||
expect(wizard.start).to eq(step1)
|
||||
expect(step1.next).to be_blank
|
||||
expect(step1.previous).to be_blank
|
||||
expect(step1.index).to eq(0)
|
||||
|
||||
expect(step1.fields).to be_empty
|
||||
field = step1.add_field(id: 'test', type: 'text')
|
||||
expect(step1.fields).to eq([field])
|
||||
end
|
||||
|
||||
it "sequences multiple steps" do
|
||||
wizard.append_step(step1)
|
||||
wizard.append_step(step2)
|
||||
|
||||
expect(wizard.steps.size).to eq(2)
|
||||
expect(wizard.start).to eq(step1)
|
||||
expect(step1.next).to eq(step2)
|
||||
expect(step1.previous).to be_blank
|
||||
expect(step2.previous).to eq(step1)
|
||||
expect(step1.index).to eq(0)
|
||||
expect(step2.index).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".exclude_step" do
|
||||
let(:user) { Fabricate.build(:moderator) }
|
||||
let(:wizard) { Wizard.new(user) }
|
||||
|
||||
it 'excludes steps even if they are added via append_step' do
|
||||
wizard.append_step('first') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
|
||||
Wizard.exclude_step("random-step123")
|
||||
|
||||
wizard.append_step('random-step123') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('finished')
|
||||
|
||||
expect(wizard.steps.map(&:id)).to eq(['first', 'finished'])
|
||||
end
|
||||
end
|
||||
|
||||
describe "#append_step with after specified" do
|
||||
let(:user) { Fabricate.build(:moderator) }
|
||||
let(:wizard) { Wizard.new(user) }
|
||||
|
||||
it 'inserts steps after the proper step' do
|
||||
wizard.append_step('first') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('second') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('actually-second', after: 'first') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
|
||||
expect(wizard.steps.sort_by(&:index).map(&:id)).to eq(["first", "actually-second", "second"])
|
||||
expect(wizard.steps.map(&:index).sort).to eq([0, 1, 2])
|
||||
end
|
||||
|
||||
it 'inserts steps at the end if the after value does not match an existing step' do
|
||||
wizard.append_step('first') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('second') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('should_be_last', after: 'abcdefghi') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
|
||||
expect(wizard.steps.sort_by(&:index).map(&:id)).to eq(["first", "second", "should_be_last"])
|
||||
expect(wizard.steps.map(&:index).sort).to eq([0, 1, 2])
|
||||
end
|
||||
|
||||
it 'inserts steps at the end' do
|
||||
wizard.append_step('first') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('second') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
wizard.append_step('last', after: 'second') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
|
||||
expect(wizard.steps.sort_by(&:index).map(&:id)).to eq(["first", "second", "last"])
|
||||
expect(wizard.steps.map(&:index).sort).to eq([0, 1, 2])
|
||||
end
|
||||
end
|
||||
|
||||
describe "completed?" do
|
||||
let(:user) { Fabricate.build(:moderator) }
|
||||
let(:wizard) { Wizard.new(user) }
|
||||
|
||||
it "is complete when all steps with fields have logs" do
|
||||
wizard.append_step('first') do |step|
|
||||
step.add_field(id: 'element', type: 'text')
|
||||
end
|
||||
|
||||
wizard.append_step('second') do |step|
|
||||
step.add_field(id: 'another_element', type: 'text')
|
||||
end
|
||||
|
||||
wizard.append_step('finished')
|
||||
|
||||
expect(wizard.start.id).to eq('first')
|
||||
expect(wizard.completed_steps?('first')).to eq(false)
|
||||
expect(wizard.completed_steps?('second')).to eq(false)
|
||||
expect(wizard.completed?).to eq(false)
|
||||
|
||||
updater = wizard.create_updater('first', element: 'test')
|
||||
updater.update
|
||||
expect(wizard.start.id).to eq('second')
|
||||
expect(wizard.completed_steps?('first')).to eq(true)
|
||||
expect(wizard.completed?).to eq(false)
|
||||
|
||||
updater = wizard.create_updater('second', element: 'test')
|
||||
updater.update
|
||||
|
||||
expect(wizard.completed_steps?('first')).to eq(true)
|
||||
expect(wizard.completed_steps?('second')).to eq(true)
|
||||
expect(wizard.completed_steps?('finished')).to eq(false)
|
||||
expect(wizard.completed?).to eq(true)
|
||||
|
||||
# Once you've completed the wizard start at the beginning
|
||||
expect(wizard.start.id).to eq('first')
|
||||
end
|
||||
end
|
||||
|
||||
describe "#requires_completion?" do
|
||||
|
||||
def build_simple(user)
|
||||
wizard = Wizard.new(user)
|
||||
wizard.append_step('simple') do |step|
|
||||
step.add_field(id: 'name', type: 'text')
|
||||
end
|
||||
wizard
|
||||
end
|
||||
|
||||
it "is false for anonymous" do
|
||||
expect(build_simple(nil).requires_completion?).to eq(false)
|
||||
end
|
||||
|
||||
it "is false for regular users" do
|
||||
expect(build_simple(Fabricate.build(:user)).requires_completion?).to eq(false)
|
||||
end
|
||||
|
||||
it "it's false when the wizard is disabled" do
|
||||
SiteSetting.wizard_enabled = false
|
||||
expect(build_simple(admin).requires_completion?).to eq(false)
|
||||
end
|
||||
|
||||
it "its false when the wizard is bypassed" do
|
||||
SiteSetting.bypass_wizard_check = true
|
||||
expect(build_simple(admin).requires_completion?).to eq(false)
|
||||
end
|
||||
|
||||
it "its automatically bypasses after you reach topic limit" do
|
||||
Fabricate(:topic)
|
||||
wizard = build_simple(admin)
|
||||
|
||||
wizard.max_topics_to_require_completion = Topic.count - 1
|
||||
|
||||
expect(wizard.requires_completion?).to eq(false)
|
||||
expect(SiteSetting.bypass_wizard_check).to eq(true)
|
||||
end
|
||||
|
||||
it "it's true for the first admin who logs in" do
|
||||
second_admin = Fabricate(:admin)
|
||||
UserAuthToken.generate!(user_id: second_admin.id)
|
||||
|
||||
expect(build_simple(admin).requires_completion?).to eq(false)
|
||||
expect(build_simple(second_admin).requires_completion?).to eq(true)
|
||||
end
|
||||
|
||||
it "is false for staff when complete" do
|
||||
wizard = build_simple(admin)
|
||||
updater = wizard.create_updater('simple', name: 'Evil Trout')
|
||||
updater.update
|
||||
|
||||
expect(wizard.requires_completion?).to eq(false)
|
||||
|
||||
# It's also false for another user
|
||||
wizard = build_simple(admin)
|
||||
expect(wizard.requires_completion?).to eq(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
25
spec/lib/wizard/wizard_step_spec.rb
Normal file
25
spec/lib/wizard/wizard_step_spec.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'wizard'
|
||||
|
||||
describe Wizard::Step do
|
||||
|
||||
let(:wizard) { Wizard.new(Fabricate.build(:user)) }
|
||||
let(:step) { wizard.create_step('test-step') }
|
||||
|
||||
it "supports fields and options" do
|
||||
expect(step.fields).to be_empty
|
||||
text = step.add_field(id: 'test', type: 'text')
|
||||
expect(step.fields).to eq([text])
|
||||
|
||||
dropdown = step.add_field(id: 'snacks', type: 'dropdown')
|
||||
dropdown.add_choice('candy')
|
||||
dropdown.add_choice('nachos', data: { color: 'yellow' })
|
||||
dropdown.add_choice('pizza', label: 'Pizza!')
|
||||
|
||||
expect(step.fields).to eq([text, dropdown])
|
||||
expect(dropdown.choices.size).to eq(3)
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user