aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/controller_spec_helper.rb18
-rw-r--r--spec/support/integration_spec_helper.rb49
-rw-r--r--spec/support/pundit/pundit_view_policy.rb20
3 files changed, 77 insertions, 10 deletions
diff --git a/spec/support/controller_spec_helper.rb b/spec/support/controller_spec_helper.rb
new file mode 100644
index 000000000..1d0288dea
--- /dev/null
+++ b/spec/support/controller_spec_helper.rb
@@ -0,0 +1,18 @@
+module ControllerSpecHelper
+ def with_permission permission, &block
+ context "with permission #{permission}" do
+ login_user
+ before(:each) do
+ @user.permissions << permission
+ @user.save!
+ sign_in @user
+ end
+ context('', &block) if block_given?
+ end
+ end
+
+end
+
+RSpec.configure do |config|
+ config.extend ControllerSpecHelper, type: :controller
+end
diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb
new file mode 100644
index 000000000..78efb9027
--- /dev/null
+++ b/spec/support/integration_spec_helper.rb
@@ -0,0 +1,49 @@
+module IntegrationSpecHelper
+
+ def paginate_collection klass, decorator, page=1
+ ModelDecorator.decorate( klass.page(page), with: decorator )
+ end
+
+ def build_paginated_collection factory, decorator, opts={}
+ count = opts.delete(:count) || 2
+ page = opts.delete(:page) || 1
+ klass = nil
+ count.times { klass ||= create(factory, opts).class }
+ paginate_collection klass, decorator, page
+ end
+
+ module Methods
+ def with_permission permission, &block
+ context "with permission #{permission}" do
+ let(:permissions){ [permission] }
+ context('', &block) if block_given?
+ end
+ end
+ end
+
+ def self.included into
+ into.extend Methods
+ end
+end
+
+RSpec.configure do |config|
+ config.include IntegrationSpecHelper, type: :view
+end
+
+RSpec::Matchers.define :have_link_for_each_item do |collection, name, href|
+ match do |actual|
+ collection.each do |item|
+ expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']", count: 1)
+ end
+ end
+ description { "have #{name} link for each item" }
+end
+
+RSpec::Matchers.define :have_the_right_number_of_links do |collection, count|
+ match do |actual|
+ collection.each do |item|
+ expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a", count: count)
+ end
+ end
+ description { "have #{count} links for each item" }
+end
diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb
index b8434cac0..91be0624c 100644
--- a/spec/support/pundit/pundit_view_policy.rb
+++ b/spec/support/pundit/pundit_view_policy.rb
@@ -1,16 +1,16 @@
module Pundit
module PunditViewPolicy
- extend ActiveSupport::Concern
+ def self.included into
+ into.let(:permissions){ nil }
+ into.let(:organisation){ referential.try(:organisation) }
+ into.let(:current_referential){ referential || build_stubbed(:referential) }
+ into.let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation }
+ into.let(:pundit_user){ UserContext.new(current_user, referential: current_referential) }
+ into.before do
+ allow(view).to receive(:pundit_user) { pundit_user }
- included do
- before do
- controller.singleton_class.class_eval do
- def policy(instance)
- Class.new do
- def method_missing(*args, &block); true; end
- end.new
- end
- helper_method :policy
+ allow(view).to receive(:policy) do |instance|
+ ::Pundit.policy pundit_user, instance
end
end
end