aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/controller_spec_helper.rb33
-rw-r--r--spec/support/decorator_helpers.rb5
-rw-r--r--spec/support/helpers/tree_walker.rb15
-rw-r--r--spec/support/integration_spec_helper.rb67
-rw-r--r--spec/support/pundit/pundit_view_policy.rb20
-rw-r--r--spec/support/referential.rb1
-rw-r--r--spec/support/shared_examples/compliance_control_validation.rb10
-rw-r--r--spec/support/zip_support.rb25
-rw-r--r--spec/support/zip_support/create_zip_data.rb70
9 files changed, 230 insertions, 16 deletions
diff --git a/spec/support/controller_spec_helper.rb b/spec/support/controller_spec_helper.rb
new file mode 100644
index 000000000..dbc7d582b
--- /dev/null
+++ b/spec/support/controller_spec_helper.rb
@@ -0,0 +1,33 @@
+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
+
+ def with_feature feature, &block
+ context "with feature #{feature}" do
+ login_user
+ before(:each) do
+ organisation = @user.organisation
+ unless organisation.has_feature?(feature)
+ organisation.features << feature
+ organisation.save!
+ end
+ 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/decorator_helpers.rb b/spec/support/decorator_helpers.rb
index ffedd479b..9d450deb1 100644
--- a/spec/support/decorator_helpers.rb
+++ b/spec/support/decorator_helpers.rb
@@ -1,5 +1,4 @@
module Support
-
module DecoratorHelpers
def self.included(into)
into.instance_eval do
@@ -21,7 +20,3 @@ module Support
end
end
end
-
-RSpec.configure do | c |
- c.include Support::DecoratorHelpers, type: :decorator
-end
diff --git a/spec/support/helpers/tree_walker.rb b/spec/support/helpers/tree_walker.rb
new file mode 100644
index 000000000..b86c3a8e1
--- /dev/null
+++ b/spec/support/helpers/tree_walker.rb
@@ -0,0 +1,15 @@
+module TreeWalker extend self
+ MAX_LEVEL = 5
+ def walk_tree path, max_level: MAX_LEVEL, level: 0, yield_dirs: :no, &blk
+ raise RuntimeError, "too many levels in tree walk, > #{max_level}" if level > max_level
+ Dir.glob(File.join(path, '*')) do | file |
+ if File.directory?( file )
+ blk.(:dir, file) if yield_dirs == :before
+ walk_tree(file, max_level: max_level, level: level.succ, yield_dirs: yield_dirs, &blk)
+ blk.(:dir, file) if yield_dirs == :after
+ else
+ blk.(:file, file)
+ end
+ end
+ end
+end
diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb
new file mode 100644
index 000000000..36306559d
--- /dev/null
+++ b/spec/support/integration_spec_helper.rb
@@ -0,0 +1,67 @@
+module IntegrationSpecHelper
+
+ def paginate_collection klass, decorator, page=1, context={}
+ collection = klass.page(page)
+ if decorator
+ collection = ModelDecorator.decorate(collection, with: decorator, context: context)
+ end
+ collection
+ end
+
+ def build_paginated_collection factory, decorator, opts={}
+ context = opts.delete(:context) || {}
+ count = opts.delete(:count) || 2
+ page = opts.delete(:page) || 1
+ klass = nil
+ count.times { klass = create(factory, opts).class }
+ paginate_collection klass, decorator, page, context
+ 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, opts|
+ opts = {href: opts} unless opts.is_a? Hash
+ href = opts[:href]
+ method = opts[:method]
+ method_selector = method.present? ? "[data-method='#{method.downcase}']": ""
+ match do |actual|
+ collection.each do |item|
+ @selector = "tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']#{method_selector}"
+ expect(rendered).to have_selector(@selector, count: 1)
+ end
+ end
+ description { "have #{name} link for each item" }
+ failure_message do
+ "expected view to have #{name} link for each item, failed with selector: \"#{@selector}\""
+ end
+end
+
+RSpec::Matchers.define :have_the_right_number_of_links do |collection, count|
+ match do
+ collection.each do |item|
+ @selector = "tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a"
+ expect(rendered).to have_selector(@selector, count: count)
+ end
+ end
+ description { "have #{count} links for each item" }
+ failure_message do
+ actual = Capybara::Node::Simple.new(rendered).all(@selector).count
+ "expected #{count} links for each item, got #{actual} for \"#{@selector}\""
+ end
+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
diff --git a/spec/support/referential.rb b/spec/support/referential.rb
index c0ae35779..497ff47a8 100644
--- a/spec/support/referential.rb
+++ b/spec/support/referential.rb
@@ -29,7 +29,6 @@ module ReferentialHelper
end
end
-
end
end
diff --git a/spec/support/shared_examples/compliance_control_validation.rb b/spec/support/shared_examples/compliance_control_validation.rb
index d4ab9f41d..c76712c4c 100644
--- a/spec/support/shared_examples/compliance_control_validation.rb
+++ b/spec/support/shared_examples/compliance_control_validation.rb
@@ -1,6 +1,9 @@
RSpec.shared_examples_for 'has min_max_values' do
context "is valid" do
+ it { should validate_numericality_of(:minimum) }
+ it { should validate_numericality_of(:maximum) }
+
it 'if no value is provided' do
expect_it.to be_valid
end
@@ -41,3 +44,10 @@ RSpec.shared_examples_for 'has min_max_values' do
end
end
end
+
+
+RSpec.shared_examples_for "has target attribute" do
+ context "is valid" do
+ it { should validate_presence_of(:target) }
+ end
+end
diff --git a/spec/support/zip_support.rb b/spec/support/zip_support.rb
new file mode 100644
index 000000000..3d9b2f97c
--- /dev/null
+++ b/spec/support/zip_support.rb
@@ -0,0 +1,25 @@
+require_relative 'helpers/tree_walker'
+module ZipSupport
+
+ module Helper extend self
+ def remove
+ -> filetype, path do
+ filetype == :file ? File.unlink(path) : Dir.unlink(path)
+ end
+ end
+ end
+
+ def zip_fixtures_path(file_name)
+ fixtures_path(File.join('zip', file_name))
+ end
+
+ def clear_all_zip_fixtures! relpath = ''
+ raise ArgumentError, 'up dir not allowed (..)' if %r{\.\.} === relpath
+ TreeWalker.walk_tree zip_fixtures_path(relpath), yield_dirs: :after, &Helper.remove
+ end
+end
+
+RSpec.configure do |conf|
+ conf.include ZipSupport, type: :zip
+end
+
diff --git a/spec/support/zip_support/create_zip_data.rb b/spec/support/zip_support/create_zip_data.rb
new file mode 100644
index 000000000..250d67f74
--- /dev/null
+++ b/spec/support/zip_support/create_zip_data.rb
@@ -0,0 +1,70 @@
+require_relative '../helpers/tree_walker'
+module ZipSupport
+ module CreateZipData
+
+ class ZipData < Struct.new(:name, :data)
+
+ def write_to file
+ File.write(file, data)
+ end
+
+ end
+
+ class Implementation
+
+ attr_reader :name, :prefix, :zip
+
+ def initialize name
+ @name = name
+ @prefix = "#{name}/"
+ @zip = ZipData.new(name, '')
+ end
+
+ def make_from names_to_content_map
+ os = Zip::OutputStream.write_buffer do | zio |
+ names_to_content_map.each(&add_entries(zio))
+ end
+ zip.data = os.string
+ zip
+ end
+
+ def make_from_tree
+ os = Zip::OutputStream.write_buffer do | zio |
+ TreeWalker.walk_tree(name, &add_entry(zio))
+ end
+ zip.data = os.string
+ zip
+ end
+
+ private
+
+ def add_entry zio
+ -> _, path do
+ rel_path = path.sub(prefix, '')
+ zio.put_next_entry(rel_path)
+ zio.write(File.read(path))
+ end
+ end
+
+ def add_entries zio
+ -> name, content do
+ zio.put_next_entry(name)
+ zio.write(content)
+ end
+ end
+ end
+
+
+ def make_zip(name, names_to_content_map = {})
+ Implementation.new(name).make_from(names_to_content_map)
+ end
+
+ def make_zip_from_tree(dir)
+ Implementation.new(dir).make_from_tree
+ end
+ end
+end
+
+RSpec.configure do |conf|
+ conf.include ZipSupport::CreateZipData, type: :zip
+end