Skip to content

Commit

Permalink
Test that themes registered in disabled features cannot be used
Browse files Browse the repository at this point in the history
  • Loading branch information
aviav committed May 19, 2017
1 parent 9268728 commit 6217402
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/pageflow/revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Revision < ActiveRecord::Base
validate :published_until_unchanged, :if => :published_until_was_in_past?
validate :published_until_blank, :if => :published_at_blank?

validates_inclusion_of(:theme_name, in: lambda do |revision|
Pageflow.config_for(revision.entry.account).themes.names
end)

def main_storyline_chapters
main_storyline = storylines.first
main_storyline ? main_storyline.chapters : Chapter.none
Expand Down
4 changes: 3 additions & 1 deletion app/models/pageflow/theming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class Theming < ActiveRecord::Base
scope :for_request, ->(request) { Pageflow.config.theming_request_scope.call(all, request) }

validates :account, :presence => true
validates_inclusion_of :theme_name, :in => ->(theming) { Pageflow.config_for(theming.account).themes.names }
validates_inclusion_of(:theme_name, in: lambda do |theming|
Pageflow.config_for(theming.account).themes.names
end)

def resolve_widgets(options = {})
widgets.resolve(Pageflow.config_for(account), options)
Expand Down
30 changes: 30 additions & 0 deletions spec/models/pageflow/revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,35 @@ def revision_components
expect(revision.locale).to eq('fr')
end
end

describe '#theme_name' do
it 'allows setting theme_name included in account config' do
pageflow_configure do |config|
config.features.register('red_theme') do |feature_config|
feature_config.themes.register(:red)
end
end

account = create(:account, feature_states: {red_theme: true})
entry = create(:entry, account: account)
revision = build(:revision, entry: entry, theme_name: 'red')

expect(revision).to be_valid
end

it 'does not allow setting theme_name not included in account config' do
pageflow_configure do |config|
config.features.register('glitter_theme') do |feature_config|
feature_config.themes.register(:glitter)
end
end

account = create(:account, feature_states: {glitter_theme: false})
entry = create(:entry, account: account)
revision = build(:revision, entry: entry, theme_name: 'glitter')

expect(revision).not_to be_valid
end
end
end
end
28 changes: 28 additions & 0 deletions spec/models/pageflow/theming_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
require 'spec_helper'

module Pageflow
Expand All @@ -11,6 +12,20 @@ module Pageflow
expect(theming.errors).to include(:theme_name)
end

it 'is invalid if disabled for account' do
pageflow_configure do |config|
config.features.register('glitter_theme') do |feature_config|
feature_config.themes.register(:glitter)
end
end

account = create(:account, feature_states: {glitter_theme: false})
theming = build(:theming, account: account, theme_name: 'glitter')

theming.valid?
expect(theming.errors).to include(:theme_name)
end

it 'is valid if registered for usage in theming' do
pageflow_configure do |config|
config.themes.register(:custom)
Expand All @@ -20,6 +35,19 @@ module Pageflow

expect(theming).to be_valid
end

it 'is valid if enabled for account' do
pageflow_configure do |config|
config.features.register('glitter_theme') do |feature_config|
feature_config.themes.register(:glitter)
end
end

account = create(:account, feature_states: {glitter_theme: true})
theming = build(:theming, account: account, theme_name: 'glitter')

expect(theming).to be_valid
end
end

describe '#theme' do
Expand Down

0 comments on commit 6217402

Please sign in to comment.