Track steps the user has completed, nag them to finish it.

This commit is contained in:
Robin Ward
2016-09-14 16:36:08 -04:00
parent ef84981e38
commit 29cf47cfb2
21 changed files with 290 additions and 19 deletions

View File

@@ -4,15 +4,19 @@ require_dependency 'wizard/builder'
require_dependency 'wizard/step_updater'
describe Wizard::StepUpdater do
before do
SiteSetting.wizard_enabled = true
end
let(: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
updater = wizard.create_updater('locale', default_locale: 'en')
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
@@ -20,6 +24,7 @@ describe Wizard::StepUpdater do
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
@@ -30,6 +35,7 @@ describe Wizard::StepUpdater do
expect(updater.success?).to eq(true)
expect(SiteSetting.title).to eq("new forum title")
expect(SiteSetting.site_description).to eq("neat place")
expect(wizard.completed_steps?('forum-title')).to eq(true)
end
context "privacy settings" do
@@ -39,6 +45,7 @@ describe Wizard::StepUpdater do
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
@@ -47,6 +54,7 @@ describe Wizard::StepUpdater do
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
@@ -62,6 +70,7 @@ describe Wizard::StepUpdater do
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)
expect(wizard.completed_steps?('contact')).to eq(true)
end
it "doesn't update when there are errors" do
@@ -71,6 +80,7 @@ describe Wizard::StepUpdater do
updater.update
expect(updater).to_not be_success
expect(updater.errors).to be_present
expect(wizard.completed_steps?('contact')).to eq(false)
end
end
@@ -109,6 +119,8 @@ describe Wizard::StepUpdater do
updater.update
raw = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).pluck(:raw).first
expect(raw).to eq("company_domain - company_full_name - company_short_name template")
expect(wizard.completed_steps?('corporate')).to eq(true)
end
end
@@ -120,6 +132,7 @@ describe Wizard::StepUpdater do
updater = wizard.create_updater('colors', theme_id: 'dark')
updater.update
expect(updater.success?).to eq(true)
expect(wizard.completed_steps?('colors')).to eq(true)
color_scheme.reload
expect(color_scheme).to be_enabled
@@ -131,6 +144,7 @@ describe Wizard::StepUpdater do
updater = wizard.create_updater('colors', theme_id: 'dark')
updater.update
expect(updater.success?).to eq(true)
expect(wizard.completed_steps?('colors')).to eq(true)
color_scheme = ColorScheme.where(via_wizard: true).first
expect(color_scheme).to be_present
@@ -150,6 +164,7 @@ describe Wizard::StepUpdater do
updater.update
expect(updater).to be_success
expect(wizard.completed_steps?('logos')).to eq(true)
expect(SiteSetting.logo_url).to eq('/uploads/logo.png')
expect(SiteSetting.logo_small_url).to eq('/uploads/logo-small.png')
expect(SiteSetting.favicon_url).to eq('/uploads/favicon.png')
@@ -158,7 +173,6 @@ describe Wizard::StepUpdater do
end
context "invites step" do
let(:invites) {
return [{ email: 'regular@example.com', role: 'regular'},
{ email: 'moderator@example.com', role: 'moderator'}]
@@ -169,6 +183,7 @@ describe Wizard::StepUpdater do
updater.update
expect(updater).to be_success
expect(wizard.completed_steps?('invites')).to eq(true)
expect(Invite.where(email: 'regular@example.com')).to be_present
expect(Invite.where(email: 'moderator@example.com')).to be_present

View File

@@ -0,0 +1,30 @@
require 'rails_helper'
require 'wizard'
require 'wizard/builder'
describe Wizard::Builder do
let(:moderator) { Fabricate.build(:moderator) }
it "returns a wizard with steps when enabled" do
SiteSetting.wizard_enabled = true
wizard = Wizard::Builder.new(moderator).build
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
wizard = Wizard::Builder.new(moderator).build
expect(wizard).to be_present
expect(wizard.steps).to be_blank
end
end

View File

@@ -2,18 +2,21 @@ require 'rails_helper'
require 'wizard'
describe Wizard do
before do
SiteSetting.wizard_enabled = true
end
let(:user) { Fabricate.build(:user) }
let(:wizard) { Wizard.new(user) }
it "has default values" do
expect(wizard.start).to be_blank
expect(wizard.steps).to be_empty
expect(wizard.user).to be_present
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') }
@@ -54,4 +57,96 @@ describe Wizard do
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 "is false for a developer" do
developer = Fabricate(:admin)
Developer.create!(user_id: developer.id)
expect(build_simple(developer).requires_completion?).to eq(false)
end
it "it's false when the wizard is disabled" do
SiteSetting.wizard_enabled = false
admin = Fabricate(:admin)
expect(build_simple(admin).requires_completion?).to eq(false)
end
it "it's true for the first admin" do
admin = Fabricate(:admin)
expect(build_simple(admin).requires_completion?).to eq(true)
second_admin = Fabricate(:admin)
expect(build_simple(second_admin).requires_completion?).to eq(false)
end
it "is false for staff when complete" do
wizard = build_simple(Fabricate(: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(Fabricate(:admin))
expect(wizard.requires_completion?).to eq(false)
end
end
end

View File

@@ -2,6 +2,10 @@ require 'rails_helper'
describe StepsController do
before do
SiteSetting.wizard_enabled = true
end
it 'needs you to be logged in' do
expect {
xhr :put, :update, id: 'made-up-id', fields: { forum_title: "updated title" }
@@ -19,6 +23,12 @@ describe StepsController do
log_in(:admin)
end
it "raises an error if the wizard is disabled" do
SiteSetting.wizard_enabled = false
xhr :put, :update, id: 'contact', fields: { contact_email: "eviltrout@example.com" }
expect(response).to be_forbidden
end
it "updates properly if you are staff" do
xhr :put, :update, id: 'contact', fields: { contact_email: "eviltrout@example.com" }
expect(response).to be_success

View File

@@ -2,9 +2,13 @@ require 'rails_helper'
describe WizardController do
context 'index' do
context 'wizard enabled' do
render_views
before do
SiteSetting.wizard_enabled = true
end
it 'needs you to be logged in' do
expect { xhr :get, :index }.to raise_error(Discourse::NotLoggedIn)
end
@@ -15,6 +19,13 @@ describe WizardController do
expect(response).to be_forbidden
end
it "raises an error if the wizard is disabled" do
SiteSetting.wizard_enabled = false
log_in(:admin)
xhr :get, :index
expect(response).to be_forbidden
end
it "renders the wizard if you are an admin" do
log_in(:admin)
xhr :get, :index
@@ -27,7 +38,6 @@ describe WizardController do
expect(response).to be_success
expect(::JSON.parse(response.body).has_key?('wizard')).to eq(true)
end
end
end