aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/data_modifier.rb53
-rw-r--r--spec/support/data_modifier/bool.rb18
-rw-r--r--spec/support/data_modifier/box.rb21
-rw-r--r--spec/support/data_modifier/enum.rb19
-rw-r--r--spec/support/data_modifier/hash.rb16
-rw-r--r--spec/support/permissions.rb28
-rw-r--r--spec/support/pundit/policies.rb17
-rw-r--r--spec/support/pundit/shared_examples.rb8
-rw-r--r--spec/support/random.rb30
-rw-r--r--spec/support/referential.rb17
-rw-r--r--spec/support/shared_examples/compliance_control_class_level_defaults.rb42
-rw-r--r--spec/support/shared_examples/compliance_control_validation.rb43
12 files changed, 286 insertions, 26 deletions
diff --git a/spec/support/data_modifier.rb b/spec/support/data_modifier.rb
new file mode 100644
index 000000000..2b3b00ac5
--- /dev/null
+++ b/spec/support/data_modifier.rb
@@ -0,0 +1,53 @@
+require_relative 'data_modifier/enum'
+require_relative 'data_modifier/hash'
+module Support
+ module DataModifier
+ module InstanceMethods
+ CannotModify = Class.new RuntimeError
+
+ def advance_values(atts, *keys)
+ keys.inject(atts){ |h, k| h.merge( k => atts[k].next) }
+ end
+
+ # return array of atts wich each value modified, unboxing
+ # values if needed
+ def modify_atts(base_atts)
+ base_atts.keys.map do | key |
+ modify_att base_atts, key
+ end.compact
+ end
+
+ private
+ def modify_att atts, key
+ atts.merge(key => modify_value(atts[key]))
+ rescue CannotModify
+ nil
+ end
+ def modify_value value
+ case value
+ when String
+ "#{value}."
+ when Fixnum
+ value + 1
+ when TrueClass
+ false
+ when FalseClass
+ true
+ when Float
+ value * 1.1
+ when Date
+ value + 1.day
+ when Box
+ value.next.value
+ else
+ raise CannotModify
+ end
+ end
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::DataModifier::InstanceMethods, type: :checksum
+ c.include Support::DataModifier::InstanceMethods, type: :model
+end
diff --git a/spec/support/data_modifier/bool.rb b/spec/support/data_modifier/bool.rb
new file mode 100644
index 000000000..f4be9bd89
--- /dev/null
+++ b/spec/support/data_modifier/bool.rb
@@ -0,0 +1,18 @@
+require_relative 'box'
+
+module Support
+ module DataModifier
+
+ class BoolBox
+ include Box
+ attr_reader :value
+
+ def initialize value
+ @value = value
+ end
+ def next
+ self.class.new(!value)
+ end
+ end
+ end
+end
diff --git a/spec/support/data_modifier/box.rb b/spec/support/data_modifier/box.rb
new file mode 100644
index 000000000..0847b628c
--- /dev/null
+++ b/spec/support/data_modifier/box.rb
@@ -0,0 +1,21 @@
+require_relative 'hash'
+module Support
+ module DataModifier
+ module Box
+ def next
+ raise "Need to implement #{__method__} in #{self.class}"
+ end
+
+ class << self
+ def unbox atts
+ Hash.map_values(atts, method(:value_of))
+ end
+ def value_of v
+ self === v ? v.value : v
+ end
+ end
+ end
+
+ end
+end
+
diff --git a/spec/support/data_modifier/enum.rb b/spec/support/data_modifier/enum.rb
new file mode 100644
index 000000000..c8a6fe573
--- /dev/null
+++ b/spec/support/data_modifier/enum.rb
@@ -0,0 +1,19 @@
+require_relative 'box'
+
+module Support
+ module DataModifier
+
+ class EnumBox
+ include Box
+ attr_reader :value, :values
+
+ def initialize *enum_values
+ @values = enum_values
+ @value = @values.first
+ end
+ def next
+ self.class.new(*(@values[1..-1] << @values.first))
+ end
+ end
+ end
+end
diff --git a/spec/support/data_modifier/hash.rb b/spec/support/data_modifier/hash.rb
new file mode 100644
index 000000000..05da3cf4f
--- /dev/null
+++ b/spec/support/data_modifier/hash.rb
@@ -0,0 +1,16 @@
+module Support
+ module DataModifier
+ module Hash extend self
+ def map_values hashy, f=nil, &fn
+ raise ArgumentError, "need block or function arg" unless f = fn || f
+ hashy.inject({}){ |h, (k,v)| h.merge(k => f.(v)) }
+ end
+ def first_values ary_hash
+ map_values(ary_hash, &:first)
+ end
+ def last_values ary_hash
+ map_values(ary_hash, &:last)
+ end
+ end
+ end
+end
diff --git a/spec/support/permissions.rb b/spec/support/permissions.rb
index 467c07a32..7d09e16cb 100644
--- a/spec/support/permissions.rb
+++ b/spec/support/permissions.rb
@@ -13,18 +13,22 @@ module Support
def _permitted_resources
%w[
- api_keys
- access_points
- connection_links
- calendars
- footnotes
- journey_patterns
- referentials
- routes
- routing_constraint_zones
- time_tables
- vehicle_journeys
- compliance_controls
+ access_points
+ connection_links
+ calendars
+ footnotes
+ imports
+ journey_patterns
+ referentials
+ routes
+ routing_constraint_zones
+ time_tables
+ vehicle_journeys
+ api_keys
+ compliance_controls
+ compliance_controls_sets
+ compliance_controls_blocks
+ compliance_check_sets
]
end
end
diff --git a/spec/support/pundit/policies.rb b/spec/support/pundit/policies.rb
index d5bb63243..a3489d9db 100644
--- a/spec/support/pundit/policies.rb
+++ b/spec/support/pundit/policies.rb
@@ -3,18 +3,18 @@ require 'pundit/rspec'
module Support
module Pundit
module Policies
- def add_permissions(*permissions, for_user:)
- for_user.permissions ||= []
- for_user.permissions += permissions.flatten
+ def add_permissions(*permissions, to_user:)
+ to_user.permissions ||= []
+ to_user.permissions += permissions.flatten
end
def create_user_context(user:, referential:)
UserContext.new(user, referential: referential)
end
- def add_permissions(*permissions, for_user:)
- for_user.permissions ||= []
- for_user.permissions += permissions.flatten
+ def remove_permissions(*permissions, from_user:, save: false)
+ from_user.permissions -= permissions.flatten
+ from_user.save! if save
end
end
@@ -30,7 +30,7 @@ module Support
end
def with_user_permission(permission, &blk)
it "with user permission #{permission.inspect}" do
- add_permissions(permission, for_user: user)
+ add_permissions(permission, to_user: user)
blk.()
end
end
@@ -41,7 +41,7 @@ module Support
perms, options = permissions.partition{|x| String === x}
context "with permissions #{perms.inspect}...", *options do
before do
- add_permissions(*permissions, for_user: @user)
+ add_permissions(*permissions, to_user: @user)
end
instance_eval(&blk)
end
@@ -51,6 +51,7 @@ module Support
end
RSpec.configure do | c |
+ c.include Support::Pundit::Policies, type: :controller
c.include Support::Pundit::Policies, type: :policy
c.extend Support::Pundit::PoliciesMacros, type: :policy
c.include Support::Pundit::Policies, type: :feature
diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb
index 63a106759..49c6845da 100644
--- a/spec/support/pundit/shared_examples.rb
+++ b/spec/support/pundit/shared_examples.rb
@@ -18,7 +18,7 @@ RSpec.shared_examples 'always allowed' do
context 'different organisations →' do
before do
- add_permissions(permission, for_user: user)
+ add_permissions(permission, to_user: user)
end
it "allows a user with a different organisation" do
expect_it.to permit(user_context, record)
@@ -51,7 +51,7 @@ RSpec.shared_examples 'always forbidden' do
context 'different organisations →' do
before do
- add_permissions(permission, for_user: user)
+ add_permissions(permission, to_user: user)
end
it "denies a user with a different organisation" do
expect_it.not_to permit(user_context, record)
@@ -80,7 +80,7 @@ RSpec.shared_examples 'permitted policy and same organisation' do
context 'permission present → ' do
before do
- add_permissions(permission, for_user: user)
+ add_permissions(permission, to_user: user)
end
it 'denies a user with a different organisation' do
@@ -113,7 +113,7 @@ RSpec.shared_examples 'permitted policy' do
context 'permission present → ' do
before do
- add_permissions(permission, for_user: user)
+ add_permissions(permission, to_user: user)
end
it 'allows user' do
diff --git a/spec/support/random.rb b/spec/support/random.rb
new file mode 100644
index 000000000..59e1a1475
--- /dev/null
+++ b/spec/support/random.rb
@@ -0,0 +1,30 @@
+module Support
+ module Random
+
+ PRETTY_LARGE_INT = 1 << 30
+
+ def random_hex
+ SecureRandom.hex
+ end
+
+ def random_element from
+ from[random_int(from.size)]
+ end
+
+ def random_int max_plus_one=PRETTY_LARGE_INT
+ (random_number * max_plus_one).to_i
+ end
+
+ def random_number
+ SecureRandom.random_number
+ end
+
+ def random_string
+ SecureRandom.urlsafe_base64
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::Random
+end
diff --git a/spec/support/referential.rb b/spec/support/referential.rb
index 3b74cb639..6f60bd86b 100644
--- a/spec/support/referential.rb
+++ b/spec/support/referential.rb
@@ -52,8 +52,21 @@ RSpec.configure do |config|
referential.add_member organisation, owner: true
end
- workbench = Workbench.create!(:name => "Gestion de l'offre", organisation: organisation, line_referential: line_referential, stop_area_referential: stop_area_referential)
- referential = Referential.create! prefix: "first", name: "first", slug: "first", organisation: organisation, workbench: workbench
+ workbench = FactoryGirl.create(
+ :workbench,
+ name: "Gestion de l'offre",
+ organisation: organisation,
+ line_referential: line_referential,
+ stop_area_referential: stop_area_referential
+ )
+ referential = FactoryGirl.create(
+ :referential,
+ prefix: "first",
+ name: "first",
+ slug: "first",
+ organisation: organisation,
+ workbench: workbench
+ )
end
config.before(:each) do
diff --git a/spec/support/shared_examples/compliance_control_class_level_defaults.rb b/spec/support/shared_examples/compliance_control_class_level_defaults.rb
new file mode 100644
index 000000000..276ac1ca5
--- /dev/null
+++ b/spec/support/shared_examples/compliance_control_class_level_defaults.rb
@@ -0,0 +1,42 @@
+require_relative '../data_modifier'
+
+H = Support::DataModifier::Hash
+RSpec.shared_examples_for 'ComplianceControl Class Level Defaults' do
+ context 'class attributes' do
+ it 'are correctly set' do
+ expect( described_class.default_code).to eq(default_code)
+ end
+ end
+ context 'are used in instantiation' do
+ let( :record ){ create factory }
+ let( :default_att_names ){%w[ code origin_code ]}
+
+ it 'all defaults' do
+ expect( record.attributes.values_at(*default_att_names ))
+ .to eq([ default_code, default_code])
+ end
+ it 'but provided values are not overwritten by defaults' do
+ code = random_string
+ origin_code = random_string
+ # Remove each of the attributes from explicit initialisation to see
+ # its value provided by CCCWDA.
+
+ # atts :: Map(String, (explicit_value, default_value))
+ atts = {
+ 'code' => [code, default_code],
+ 'origin_code' => [origin_code, default_code],
+ }
+ atts.keys.each do |key|
+ # Replace key to be tested by default value
+ expected = H.first_values(atts).merge(key => atts[key].last)
+ expected_values = expected.values_at(*default_att_names)
+ # Remove key to be tested from atts passed to `#create`
+ construction_atts = H.first_values(atts).merge(key => nil).compact
+ explicit = create factory, construction_atts
+
+ expect( explicit.attributes.values_at(*default_att_names ))
+ .to eq(expected_values)
+ end
+ end
+ end
+ end
diff --git a/spec/support/shared_examples/compliance_control_validation.rb b/spec/support/shared_examples/compliance_control_validation.rb
new file mode 100644
index 000000000..d4ab9f41d
--- /dev/null
+++ b/spec/support/shared_examples/compliance_control_validation.rb
@@ -0,0 +1,43 @@
+RSpec.shared_examples_for 'has min_max_values' do
+
+ context "is valid" do
+ it 'if no value is provided' do
+ expect_it.to be_valid
+ end
+ it 'if minimum is provided alone' do
+ subject.minimum = 42
+ expect_it.to be_valid
+ end
+ it 'if maximum is provided alone' do
+ subject.maximum = 42
+ expect_it.to be_valid
+ end
+
+ it 'if maximum is not smaller than minimum' do
+ 100.times do
+ min = random_int
+ max = min + random_int(20)
+ subject.assign_attributes maximum: max, minimum: min
+ subject.assign_attributes maximum: min, minimum: min
+ expect_it.to be_valid
+ end
+ end
+ end
+
+ context "is invalid" do
+ it 'if maximum is smaller than minimum' do
+ 100.times do
+ min = random_int
+ max = min - random_int(20) - 1
+ subject.assign_attributes maximum: max, minimum: min
+ expect_it.not_to be_valid
+ end
+ end
+
+ it 'and has a correct error message' do
+ subject.assign_attributes maximum: 1, minimum: 2
+ expect_it.not_to be_valid
+ expect( subject.errors.messages[:min_max_values].first ).to match("la valeur de minimum (2) ne doit pas être superieur à la valuer du maximum (1)")
+ end
+ end
+end