aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-12-07 15:25:43 +0100
committerXinhui2017-12-07 15:25:43 +0100
commit3f2575c44c6a059ae03412f38b7263450fb58024 (patch)
treeff6e6cb9a9468c3cb3c8fd1aa30713f08cde7ec6
parenta022425cf375387338020549698863d2112b61f6 (diff)
downloadchouette-core-5150_decorator_policy.tar.bz2
TimeTable decorator policy spec5150_decorator_policy
Refs #5150
-rw-r--r--app/decorators/time_table_decorator.rb22
-rw-r--r--spec/decorators/time_table_decorator_spec.rb66
2 files changed, 77 insertions, 11 deletions
diff --git a/app/decorators/time_table_decorator.rb b/app/decorators/time_table_decorator.rb
index c6eeac176..720ea30d8 100644
--- a/app/decorators/time_table_decorator.rb
+++ b/app/decorators/time_table_decorator.rb
@@ -10,18 +10,18 @@ class TimeTableDecorator < Draper::Decorator
def action_links
links = []
- if object.calendar
- links << Link.new(
- content: h.t('actions.actualize'),
- href: h.actualize_referential_time_table_path(
- context[:referential],
- object
- ),
- method: :post
- )
- end
+ if h.policy(object).actualize? && object.calendar
+ links << Link.new(
+ content: h.t('actions.actualize'),
+ href: h.actualize_referential_time_table_path(
+ context[:referential],
+ object
+ ),
+ method: :post
+ )
+ end
- if h.policy(object).edit?
+ if h.policy(object).update?
links << Link.new(
content: h.t('actions.combine'),
href: h.new_referential_time_table_time_table_combination_path(
diff --git a/spec/decorators/time_table_decorator_spec.rb b/spec/decorators/time_table_decorator_spec.rb
new file mode 100644
index 000000000..d89b27ffa
--- /dev/null
+++ b/spec/decorators/time_table_decorator_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+describe TimeTableDecorator do
+ let(:object) { build_stubbed :time_table, calendar: build_stubbed(:calendar) }
+ let(:user) { build_stubbed :user }
+
+ describe 'action links' do
+ before do
+ allow(helpers).to receive(:actualize_referential_time_table_path).and_return('/actualize_path')
+ allow(helpers).to receive(:new_referential_time_table_time_table_combination_path).and_return('/combine_path')
+ allow(helpers).to receive(:duplicate_referential_time_table_path).and_return('/duplicate_path')
+ allow(helpers).to receive(:referential_time_table_path).and_return('/show_path')
+ allow(helpers).to receive(:destroy_link_content).and_return('destroy_link_content')
+ end
+
+ context 'for authorized user' do
+ before do
+ stub_policy([actualize?: true, duplicate?: true, update?: true, destroy?: true])
+ end
+
+ it 'should render actualize link' do
+ expect(subject.action_links.map(&:content)).to include(I18n.t('actions.actualize'))
+ expect_action_link_hrefs.to include('/actualize_path')
+ end
+
+ it 'should render combine link' do
+ expect(subject.action_links.map(&:content)).to include(I18n.t('actions.combine'))
+ expect_action_link_hrefs.to include('/combine_path')
+ end
+
+ it 'should not render duplicate link' do
+ expect(subject.action_links.map(&:content)).to include(I18n.t('actions.duplicate'))
+ expect_action_link_hrefs.to include('/duplicate_path')
+ end
+
+ it 'should render destroy link' do
+ expect(subject.action_links.map(&:content)).to include('destroy_link_content')
+ end
+ end
+
+ context 'for unauthorized user' do
+ before do
+ stub_policy([actualize?: false, duplicate?: false, update?: false, destroy?: false])
+ end
+
+ it 'should not render actualize link' do
+ expect(subject.action_links.map(&:content)).to_not include(I18n.t('actions.actualize'))
+ expect_action_link_hrefs.to_not include('/actualize_path')
+ end
+
+ it 'should not render combine link' do
+ expect(subject.action_links.map(&:content)).to_not include(I18n.t('actions.combine'))
+ expect_action_link_hrefs.to_not include('/combine_path')
+ end
+
+ it 'should not render duplicate link' do
+ expect(subject.action_links.map(&:content)).to_not include(I18n.t('actions.duplicate'))
+ expect_action_link_hrefs.to_not include('/duplicate_path')
+ end
+
+ it 'should not render destroy link' do
+ expect(subject.action_links.map(&:content)).to_not include('destroy_link_content')
+ end
+ end
+ end
+end