From 5fa02104cacdd2fcf6fa5c1ce6477eada0a7e9ef Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 29 Nov 2017 18:29:18 +0100 Subject: spec/support/referential: Rename `:js` filter to `:truncation` This `before` hook allows tests to use the "truncation" database cleanup strategy. It doesn't do anything explicitly JavaScript-related. Rather, it was named JS because it was intended to be used for feature tests with JavaScript. However, now that I need to use it for a couple tests in `spec/models/referential_spec.rb`, the `:js` name doesn't make sense. Rename the filter to something that says what it does, not how it should be used. Refs #5024 --- spec/support/referential.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/referential.rb b/spec/support/referential.rb index b615491da..c0ae35779 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -78,7 +78,7 @@ RSpec.configure do |config| first_referential.switch end - config.before(:each, :js => true) do + config.before(:each, truncation: true) do DatabaseCleaner.strategy = :truncation, { except: %w[spatial_ref_sys] } end -- cgit v1.2.3 From 985d9a673a79f2b304119999dc76d418225332c7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 13 Dec 2017 16:16:44 +0100 Subject: spec/support/referential: Eliminate data errors in truncation tests After the first `truncation: true` test ran, all tables would be cleared. This caused errors as our tests rely on a referential like this existing: Referential.create(name: 'first') It also caused problems with `LineReferential` as the `:chouette_line` factory depends on an existing `LineReferential` associated to the `first` Referential. Add the `referentials` and `line_referentials` tables to the exclusion list when truncating so these tables don't get cleared and the 'first' records persist across test runs like before. To prevent errors trying to unnecessarily switch to the 'first' referential in `truncation: true` tests, disable that functionality by adding a filter to exclude these tests from the normal transactional `before(:each)` config block. Refs #5024 --- spec/support/referential.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/referential.rb b/spec/support/referential.rb index c0ae35779..d00a9ba7f 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -72,14 +72,20 @@ RSpec.configure do |config| ) end - config.before(:each) do + config.before(:each, truncation: false) do DatabaseCleaner.strategy = :transaction # Switch into the default tenant first_referential.switch end config.before(:each, truncation: true) do - DatabaseCleaner.strategy = :truncation, { except: %w[spatial_ref_sys] } + DatabaseCleaner.strategy = :truncation, { + except: %w[ + spatial_ref_sys + referentials + line_referentials + ] + } end config.before(:each) do -- cgit v1.2.3 From bd8a999b12963b7303500ce93449bb7932afeec3 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 13 Dec 2017 17:01:15 +0100 Subject: referential_lock_during_creation_spec: Make truncation strategy stick The `truncation: true` strategy wasn't sticking. The config `before` block only ran once for this test file. Instead of attaching the filter to the `it` blocks, put it on the `context`. This correctly gets each test to run in a transaction. Remove the `truncation: false` filter I had added previously as this didn't do what I wanted it to: it prevented the `before` block from running instead of filtering out tests with `truncation: true`. Refs #5024 --- spec/support/referential.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/referential.rb b/spec/support/referential.rb index d00a9ba7f..3a41b2e70 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -72,7 +72,7 @@ RSpec.configure do |config| ) end - config.before(:each, truncation: false) do + config.before(:each) do DatabaseCleaner.strategy = :transaction # Switch into the default tenant first_referential.switch -- cgit v1.2.3 From d2c0d80c7aaeb61d373f084f13bb007e1f41527e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 13 Dec 2017 18:25:08 +0100 Subject: spec/support/referential: Restore old exclusions for truncation Since the tests I was trying to use `truncation` for are now skipped, remove these exceptions and revert this line back to what it was before all this mess. Refs #5024 --- spec/support/referential.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'spec/support') diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 3a41b2e70..c0ae35779 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -79,13 +79,7 @@ RSpec.configure do |config| end config.before(:each, truncation: true) do - DatabaseCleaner.strategy = :truncation, { - except: %w[ - spatial_ref_sys - referentials - line_referentials - ] - } + DatabaseCleaner.strategy = :truncation, { except: %w[spatial_ref_sys] } end config.before(:each) do -- cgit v1.2.3 From e82600d2efcc90f327b90d04239fe41b80031ad4 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 27 Nov 2017 10:25:43 +0100 Subject: Refs: #5006@3h; Zip Support for Specs, allowing to create zip archives in memory * create zip archive from hash or directory tree * Metaspecs -- commented out - for potential refactoring of Zip Support - as readable documentation on how to use Zip Support --- spec/support/helpers/tree_walker.rb | 15 +++++++ spec/support/zip_support.rb | 25 +++++++++++ spec/support/zip_support/create_zip_data.rb | 70 +++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 spec/support/helpers/tree_walker.rb create mode 100644 spec/support/zip_support.rb create mode 100644 spec/support/zip_support/create_zip_data.rb (limited to 'spec/support') 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/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 -- cgit v1.2.3 From 443de38df52143f72d97308c28ac68f99e009757 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 15 Dec 2017 08:24:24 +0100 Subject: Fixes: #5281@1.2h; View specs and minor CR change from #5006 - CR change from #5006, severity of zip file messages :warning -> :error - View specs for showing import_messages --- spec/support/referential.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/referential.rb b/spec/support/referential.rb index b615491da..7f56e5143 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -29,7 +29,6 @@ module ReferentialHelper end end - end end -- cgit v1.2.3 From 88b8e30a415f300108be4a19a53ca9768671d21e Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 09:11:04 +0100 Subject: Refs #5287; Add specs for the view. Refactor to come --- spec/support/integration_spec_helper.rb | 17 +++++++++++++++++ spec/support/pundit/pundit_view_policy.rb | 16 +++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 spec/support/integration_spec_helper.rb (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb new file mode 100644 index 000000000..958aab9d5 --- /dev/null +++ b/spec/support/integration_spec_helper.rb @@ -0,0 +1,17 @@ +module IntegrationSpecHelper + extend ActiveSupport::Concern + + included do + def self.with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? + end + end + end +end + + +RSpec.configure do |config| + config.include IntegrationSpecHelper, type: :view +end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index b8434cac0..02a78a4e0 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -3,14 +3,16 @@ module Pundit extend ActiveSupport::Concern included do + + let(:permissions){ nil } + let(:current_referential){ build_stubbed :referential } + let(:current_user){ build_stubbed :user, permissions: permissions } + let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } 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(:pundit_user) { pundit_user } + + allow(view).to receive(:policy) do |instance| + ::Pundit.policy pundit_user, instance end end end -- cgit v1.2.3 From cd92a6e07393b1b6ee57a8d980f072aa8a034874 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 11:27:14 +0100 Subject: - Fix specs on connections_links/index - Fix specs on connections_links/show - Update pundit view specs helper to use the current referential when it has already been defined --- spec/support/pundit/pundit_view_policy.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 02a78a4e0..6a663a471 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -5,8 +5,9 @@ module Pundit included do let(:permissions){ nil } - let(:current_referential){ build_stubbed :referential } - let(:current_user){ build_stubbed :user, permissions: permissions } + let(:organisation){ referential.try(:organisation) } + let(:current_referential){ referential || build_stubbed(:referential) } + let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } before do allow(view).to receive(:pundit_user) { pundit_user } -- cgit v1.2.3 From 1b2c50949d827d8f82a40d591489394f5dc2f993 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 18 Dec 2017 09:25:10 +0100 Subject: Refs #5287; CR 1 - Get rid of ActiveSupport::Concern in the spec helpers - Rename misnamed spec files --- spec/support/integration_spec_helper.rb | 15 +++++---------- spec/support/pundit/pundit_view_policy.rb | 17 +++++++---------- 2 files changed, 12 insertions(+), 20 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 958aab9d5..182cadf24 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,17 +1,12 @@ module IntegrationSpecHelper - extend ActiveSupport::Concern - - included do - def self.with_permission permission, &block - context "with permission #{permission}" do - let(:permissions){ [permission] } - context('', &block) if block_given? - end + def with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? end end end - RSpec.configure do |config| - config.include IntegrationSpecHelper, type: :view + config.extend IntegrationSpecHelper, type: :view end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 6a663a471..91be0624c 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -1,15 +1,12 @@ module Pundit module PunditViewPolicy - extend ActiveSupport::Concern - - included do - - let(:permissions){ nil } - let(:organisation){ referential.try(:organisation) } - let(:current_referential){ referential || build_stubbed(:referential) } - let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } - let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } - before do + 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 } allow(view).to receive(:policy) do |instance| -- cgit v1.2.3 From 74a601f6f35e0a547d3cde0501081e6e9f509959 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 17:01:49 +0100 Subject: Ref #5291@2h; Specs setup - Refactor specs helper to be more generic - Write missing specs for the Workbenches#show view We now have failing tests highlighting the bug --- spec/support/integration_spec_helper.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 182cadf24..5bcf0bd3a 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -4,9 +4,39 @@ module IntegrationSpecHelper let(:permissions){ [permission] } context('', &block) if block_given? end + + 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 end end RSpec.configure do |config| config.extend 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 -- cgit v1.2.3 From 3aa5ff0d1f5e74b4c46c14e6240e7def82f0451d Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 18 Dec 2017 09:34:15 +0100 Subject: Refs #5291@0.1h; Fix rebase Fix integration_spec_helper after rebase --- spec/support/integration_spec_helper.rb | 37 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 5bcf0bd3a..78efb9027 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,26 +1,33 @@ module IntegrationSpecHelper - def with_permission permission, &block - context "with permission #{permission}" do - let(:permissions){ [permission] } - context('', &block) if block_given? - end - def paginate_collection klass, decorator, page=1 - ModelDecorator.decorate( klass.page(page), with: decorator ) - end + 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 + 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.extend IntegrationSpecHelper, type: :view + config.include IntegrationSpecHelper, type: :view end RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| -- cgit v1.2.3 From 98c08c6fae5b0bf59231b3e402ca91307c200297 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 18 Dec 2017 17:18:25 +0100 Subject: Refs #5333@1.5h; Use permissions to sync StopAreas and Lines - add missing policies - update permissions translator - update views to check for the permission - update views helper to check for the permission - uipdate controllers to check for the permission --- spec/support/controller_spec_helper.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 spec/support/controller_spec_helper.rb (limited to 'spec/support') 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 -- cgit v1.2.3 From 676f45e8fd85c1422345d4d27ba2385e4bd536fe Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 09:11:04 +0100 Subject: Refs #5287; Add specs for the view. Refactor to come --- spec/support/integration_spec_helper.rb | 17 +++++++++++++++++ spec/support/pundit/pundit_view_policy.rb | 16 +++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 spec/support/integration_spec_helper.rb (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb new file mode 100644 index 000000000..958aab9d5 --- /dev/null +++ b/spec/support/integration_spec_helper.rb @@ -0,0 +1,17 @@ +module IntegrationSpecHelper + extend ActiveSupport::Concern + + included do + def self.with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? + end + end + end +end + + +RSpec.configure do |config| + config.include IntegrationSpecHelper, type: :view +end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index b8434cac0..02a78a4e0 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -3,14 +3,16 @@ module Pundit extend ActiveSupport::Concern included do + + let(:permissions){ nil } + let(:current_referential){ build_stubbed :referential } + let(:current_user){ build_stubbed :user, permissions: permissions } + let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } 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(:pundit_user) { pundit_user } + + allow(view).to receive(:policy) do |instance| + ::Pundit.policy pundit_user, instance end end end -- cgit v1.2.3 From 357563bbf408aa2000097ee200dfbfba1f677121 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 11:27:14 +0100 Subject: - Fix specs on connections_links/index - Fix specs on connections_links/show - Update pundit view specs helper to use the current referential when it has already been defined --- spec/support/pundit/pundit_view_policy.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 02a78a4e0..6a663a471 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -5,8 +5,9 @@ module Pundit included do let(:permissions){ nil } - let(:current_referential){ build_stubbed :referential } - let(:current_user){ build_stubbed :user, permissions: permissions } + let(:organisation){ referential.try(:organisation) } + let(:current_referential){ referential || build_stubbed(:referential) } + let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } before do allow(view).to receive(:pundit_user) { pundit_user } -- cgit v1.2.3 From 548fe7f13d698fe9a6620ef60fc239f28347af79 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 18 Dec 2017 09:25:10 +0100 Subject: Refs #5287; CR 1 - Get rid of ActiveSupport::Concern in the spec helpers - Rename misnamed spec files --- spec/support/integration_spec_helper.rb | 15 +++++---------- spec/support/pundit/pundit_view_policy.rb | 17 +++++++---------- 2 files changed, 12 insertions(+), 20 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 958aab9d5..182cadf24 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,17 +1,12 @@ module IntegrationSpecHelper - extend ActiveSupport::Concern - - included do - def self.with_permission permission, &block - context "with permission #{permission}" do - let(:permissions){ [permission] } - context('', &block) if block_given? - end + def with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? end end end - RSpec.configure do |config| - config.include IntegrationSpecHelper, type: :view + config.extend IntegrationSpecHelper, type: :view end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 6a663a471..91be0624c 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -1,15 +1,12 @@ module Pundit module PunditViewPolicy - extend ActiveSupport::Concern - - included do - - let(:permissions){ nil } - let(:organisation){ referential.try(:organisation) } - let(:current_referential){ referential || build_stubbed(:referential) } - let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } - let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } - before do + 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 } allow(view).to receive(:policy) do |instance| -- cgit v1.2.3 From 2736ff5e010586266176a9a0fb0ac8dc6e227f63 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 17:01:49 +0100 Subject: Ref #5291@2h; Specs setup - Refactor specs helper to be more generic - Write missing specs for the Workbenches#show view We now have failing tests highlighting the bug --- spec/support/integration_spec_helper.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 182cadf24..5bcf0bd3a 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -4,9 +4,39 @@ module IntegrationSpecHelper let(:permissions){ [permission] } context('', &block) if block_given? end + + 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 end end RSpec.configure do |config| config.extend 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 -- cgit v1.2.3 From c3ce5e25c3596329bd1ff9542d73354e1a2bb368 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 17:01:49 +0100 Subject: Ref #5291@2h; Specs setup - Refactor specs helper to be more generic - Write missing specs for the Workbenches#show view We now have failing tests highlighting the bug --- spec/support/integration_spec_helper.rb | 36 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 5bcf0bd3a..6edec2a05 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,26 +1,32 @@ module IntegrationSpecHelper - def with_permission permission, &block - context "with permission #{permission}" do - let(:permissions){ [permission] } - context('', &block) if block_given? - end + def paginate_collection klass, decorator, page=1 + ModelDecorator.decorate( klass.page(page), with: decorator ) + end - 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 - 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 + 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.extend IntegrationSpecHelper, type: :view + config.include IntegrationSpecHelper, type: :view end RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| -- cgit v1.2.3 From 12924f238cc02a671a4c535b49320cd3b0ce7302 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 18 Dec 2017 09:34:15 +0100 Subject: Refs #5291@0.1h; Fix rebase Fix integration_spec_helper after rebase --- spec/support/integration_spec_helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 6edec2a05..78efb9027 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,4 +1,5 @@ module IntegrationSpecHelper + def paginate_collection klass, decorator, page=1 ModelDecorator.decorate( klass.page(page), with: decorator ) end -- cgit v1.2.3 From ffa7c137f0d06f37f67561f09472b304efa90069 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Wed, 20 Dec 2017 20:52:58 +0100 Subject: Add StopArea#waiting_time with support in stop_areas#index/show. Refs #5351 --- spec/support/decorator_helpers.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'spec/support') 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 -- cgit v1.2.3 From 4eadd762a0005e3e452aa757495e02dc244db3dc Mon Sep 17 00:00:00 2001 From: Zog Date: Thu, 21 Dec 2017 11:14:23 +0100 Subject: Refs #5355 @0.5h; Add opposite route button on timetables --- spec/support/integration_spec_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 78efb9027..1bf211fe1 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,7 +1,11 @@ module IntegrationSpecHelper def paginate_collection klass, decorator, page=1 - ModelDecorator.decorate( klass.page(page), with: decorator ) + coll = klass.page(page) + if decorator + coll = ModelDecorator.decorate( coll, with: decorator ) + end + coll end def build_paginated_collection factory, decorator, opts={} -- cgit v1.2.3 From 4918ffcd7492661f36a763cdf248757993f9bff4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 19 Dec 2017 16:35:09 +0100 Subject: Remove `hstore_accessor` usage The places we were using `hstore_accessor` have now been converted to Postgres JSON fields. Thus we no longer need `hstore_accessor` in order to add accessors for the fields. Instead we can use Rails' built-in support for Postgres JSON fields and create accessors for our JSON hash keys with `store_accessor`. We get rid of the data types because `store_accessor` doesn't work like that and JSON already types our values with primitive data types. Finally, add some extra validation tests now that the vaildation for our JSON fields should work. Refs #5316 --- spec/support/shared_examples/compliance_control_validation.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'spec/support') 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 -- cgit v1.2.3 From 9a34cb48f49df574ae1e7c599713ed246e2938cf Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 20 Dec 2017 12:58:20 +0100 Subject: Refs #5430 @2h; Deactivate lines instead of destroying them - Add `activate` and `deactivate` actions in `LinesController`, as well as corresponding routes - Add `activate!` and `deactivate!` methods in `Chouette::Line`, as well as `activated?` - Add `activate?` and `deactivate?` permissions in `LinePolicy` - Add corresponding `action_links`in the Decorator - Create helper for these actions - Add an optional `'extra_class` to the Links - Update styles for ".delete-action" to handle the case where there are several - Add I18n keys accordingly --- spec/support/integration_spec_helper.rb | 34 +++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 1bf211fe1..36306559d 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,19 +1,20 @@ module IntegrationSpecHelper - def paginate_collection klass, decorator, page=1 - coll = klass.page(page) + def paginate_collection klass, decorator, page=1, context={} + collection = klass.page(page) if decorator - coll = ModelDecorator.decorate( coll, with: decorator ) + collection = ModelDecorator.decorate(collection, with: decorator, context: context) end - coll + 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 + count.times { klass = create(factory, opts).class } + paginate_collection klass, decorator, page, context end module Methods @@ -34,20 +35,33 @@ RSpec.configure do |config| config.include IntegrationSpecHelper, type: :view end -RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| +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| - expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']", count: 1) + @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 |actual| + match do collection.each do |item| - expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a", count: count) + @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 -- cgit v1.2.3 From d3445d085b607544dc6355956cd228835258d612 Mon Sep 17 00:00:00 2001 From: Zog Date: Tue, 26 Dec 2017 11:23:53 +0100 Subject: Refs #5382 @1h; Enable all area types for Stopareas in Routes Given that the organisation has the matching feature. --- spec/support/controller_spec_helper.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'spec/support') diff --git a/spec/support/controller_spec_helper.rb b/spec/support/controller_spec_helper.rb index 1d0288dea..dbc7d582b 100644 --- a/spec/support/controller_spec_helper.rb +++ b/spec/support/controller_spec_helper.rb @@ -11,6 +11,21 @@ module ControllerSpecHelper 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| -- cgit v1.2.3 From 71ea30a8621ad00f83826d46955d5768094bcae7 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Mon, 18 Dec 2017 12:03:48 +0100 Subject: Fix permission specs by including merges in Support::Permissions. Refs #5299 --- spec/support/permissions.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/support') diff --git a/spec/support/permissions.rb b/spec/support/permissions.rb index dde530871..95afd6c1c 100644 --- a/spec/support/permissions.rb +++ b/spec/support/permissions.rb @@ -18,6 +18,7 @@ module Support calendars footnotes imports + merges journey_patterns referentials routes -- cgit v1.2.3 From fb9eb367b9c56c19df7f2e2301f4a417f5409d6d Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 5 Jan 2018 17:34:07 +0100 Subject: Refs: #5413@4h; Setting up db (chore) and specing desired behavior [skip-ci] - Adapted shared pundit examples to allow to check with archieved and finalised referentials - Speced desired behavior with this enhancement - finalise_referential helper in policy spec's support --- spec/support/pundit/policies.rb | 5 +++- spec/support/pundit/shared_examples.rb | 51 ++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 13 deletions(-) (limited to 'spec/support') diff --git a/spec/support/pundit/policies.rb b/spec/support/pundit/policies.rb index a3489d9db..d8d12d735 100644 --- a/spec/support/pundit/policies.rb +++ b/spec/support/pundit/policies.rb @@ -12,11 +12,14 @@ module Support UserContext.new(user, referential: referential) end + def finalise_referential + referential.referential_suite_id = random_int + end + def remove_permissions(*permissions, from_user:, save: false) from_user.permissions -= permissions.flatten from_user.save! if save end - end module PoliciesMacros diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb index 49f915626..13f537c6d 100644 --- a/spec/support/pundit/shared_examples.rb +++ b/spec/support/pundit/shared_examples.rb @@ -1,6 +1,6 @@ RSpec.shared_examples 'always allowed' do - | permission, archived: false| + | permission, archived_and_finalised: false | context 'same organisation →' do before do user.organisation_id = referential.organisation_id @@ -8,11 +8,16 @@ RSpec.shared_examples 'always allowed' do it "allows a user with the same organisation" do expect_it.to permit(user_context, record) end - if archived + if archived_and_finalised it 'does not remove permission for archived referentials' do referential.archived_at = 42.seconds.ago expect_it.to permit(user_context, record) end + + it 'does not remove permission for finalised referentials' do + finalise_referential + expect_it.to permit(user_context, record) + end end end @@ -23,27 +28,33 @@ RSpec.shared_examples 'always allowed' do it "allows a user with a different organisation" do expect_it.to permit(user_context, record) end - if archived + if archived_and_finalised it 'does not remove permission for archived referentials' do referential.archived_at = 42.seconds.ago expect_it.to permit(user_context, record) end + it 'does not remove permission for finalised referentials' do + finalise_referential + expect_it.to permit(user_context, record) + end end end end RSpec.shared_examples 'always forbidden' do - | permission, archived: false| + | permission, archived_and_finalised: false| context 'same organisation →' do before do user.organisation_id = referential.organisation_id end + it "allows a user with the same organisation" do expect_it.not_to permit(user_context, record) end - if archived + + if archived_and_finalised it 'still no permission for archived referentials' do - referential.archived_at = 42.seconds.ago + finalise_referential expect_it.not_to permit(user_context, record) end end @@ -56,17 +67,22 @@ RSpec.shared_examples 'always forbidden' do it "denies a user with a different organisation" do expect_it.not_to permit(user_context, record) end - if archived + if archived_and_finalised it 'still no permission for archived referentials' do referential.archived_at = 42.seconds.ago expect_it.not_to permit(user_context, record) end + + it 'still no permission for finalised referentials' do + finalise_referential + expect_it.not_to permit(user_context, record) + end end end end RSpec.shared_examples 'permitted policy and same organisation' do - | permission, archived: false| + | permission, archived_and_finalised: false | context 'permission absent → ' do it "denies a user with a different organisation" do @@ -92,18 +108,24 @@ RSpec.shared_examples 'permitted policy and same organisation' do expect_it.to permit(user_context, record) end - if archived + if archived_and_finalised it 'removes the permission for archived referentials' do user.organisation_id = referential.organisation_id referential.archived_at = 42.seconds.ago expect_it.not_to permit(user_context, record) end + + it 'removes the permission for finalised referentials' do + user.organisation_id = referential.organisation_id + finalise_referential + expect_it.not_to permit(user_context, record) + end end end end RSpec.shared_examples 'permitted policy' do - | permission, archived: false| + | permission, archived_and_finalised: false| context 'permission absent → ' do it "denies user" do @@ -120,12 +142,17 @@ RSpec.shared_examples 'permitted policy' do expect_it.to permit(user_context, record) end - if archived + if archived_and_finalised it 'removes the permission for archived referentials' do user.organisation_id = referential.organisation_id referential.archived_at = 42.seconds.ago expect_it.not_to permit(user_context, record) end + it 'removes the permission for finalised referentials' do + user.organisation_id = referential.organisation_id + finalise_referential + expect_it.not_to permit(user_context, record) + end end end end @@ -148,4 +175,4 @@ RSpec.shared_examples 'permitted policy outside referential' do expect_it.to permit(user_context, record) end end -end \ No newline at end of file +end -- cgit v1.2.3 From 5c3abf69c50f00e0caab1517be2beccca10e635b Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 10 Jan 2018 16:39:08 +0100 Subject: Refs #5535 @0.25h; Adds a `full_schedule?` on `JourneyPattern` To know if all the costs between the stops of the pattern are set. --- spec/support/journey_pattern_helper.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/support/journey_pattern_helper.rb (limited to 'spec/support') diff --git a/spec/support/journey_pattern_helper.rb b/spec/support/journey_pattern_helper.rb new file mode 100644 index 000000000..d16120acf --- /dev/null +++ b/spec/support/journey_pattern_helper.rb @@ -0,0 +1,19 @@ +module Support + module JourneyPatternHelper + def generate_journey_pattern_costs distance, time + costs = {} + (journey_pattern.stop_points.size - 1).times do |i| + start, finish = journey_pattern.stop_points[i..i+1] + costs["#{start.id}-#{finish.id}"] = { + distance: (distance.respond_to?(:call) ? distance.call(i) : distance), + time: (time.respond_to?(:call) ? time.call(i) : time) + } + end + costs + end + end +end + +RSpec.configure do | config | + config.include Support::JourneyPatternHelper, type: :model +end -- cgit v1.2.3 From c8a2e2ce194d3d051bb96522c40c4d34392bdf8e Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 10 Jan 2018 18:24:54 +0100 Subject: Refs #5535 @2H; Automatically fill VJs tilmes when possible --- spec/support/journey_pattern_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/journey_pattern_helper.rb b/spec/support/journey_pattern_helper.rb index d16120acf..3ba1c501b 100644 --- a/spec/support/journey_pattern_helper.rb +++ b/spec/support/journey_pattern_helper.rb @@ -4,7 +4,7 @@ module Support costs = {} (journey_pattern.stop_points.size - 1).times do |i| start, finish = journey_pattern.stop_points[i..i+1] - costs["#{start.id}-#{finish.id}"] = { + costs["#{start.stop_area_id}-#{finish.stop_area_id}"] = { distance: (distance.respond_to?(:call) ? distance.call(i) : distance), time: (time.respond_to?(:call) ? time.call(i) : time) } -- cgit v1.2.3 From 41135059b9e9b1649b9001a512a0cb9c3e370127 Mon Sep 17 00:00:00 2001 From: Luc Donnet Date: Thu, 11 Jan 2018 13:50:09 +0100 Subject: Fix vehicle_journey_control for min max values Refs #5543 @2 --- .../compliance_control_validation.rb | 45 +++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'spec/support') diff --git a/spec/support/shared_examples/compliance_control_validation.rb b/spec/support/shared_examples/compliance_control_validation.rb index c76712c4c..b23c2984f 100644 --- a/spec/support/shared_examples/compliance_control_validation.rb +++ b/spec/support/shared_examples/compliance_control_validation.rb @@ -4,43 +4,44 @@ RSpec.shared_examples_for 'has min_max_values' do it { should validate_numericality_of(:minimum) } it { should validate_numericality_of(:maximum) } - it 'if no value is provided' do + it 'if maximum is greater than minimum' do + min = random_int + max = min + 100 + subject.assign_attributes maximum: max, minimum: min expect_it.to be_valid end + end + + context "is invalid" do + it 'if no value is provided' do + subject.minimum = nil + subject.maximum = nil + expect_it.not_to be_valid + end + it 'if minimum is provided alone' do subject.minimum = 42 - expect_it.to be_valid + subject.maximum = nil + expect_it.not_to be_valid end + it 'if maximum is provided alone' do + subject.minimum = nil 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 + expect_it.not_to be_valid 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 + min = random_int + max = min - 1 + subject.assign_attributes maximum: max, minimum: min + expect_it.not_to be_valid 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)") + expect( subject.errors.messages[:minimum].first ).to match(I18n.t("compliance_controls.min_max_values", min: 2, max: 1)) end end end -- cgit v1.2.3 From ff5d96b530a41669c394b7b0dc138e29da48e97d Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 22 Jan 2018 09:54:20 +0100 Subject: Refs #5586 @2h; Fix specs --- spec/support/decorator_helpers.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'spec/support') diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb index 9d450deb1..b2c41e842 100644 --- a/spec/support/decorator_helpers.rb +++ b/spec/support/decorator_helpers.rb @@ -5,18 +5,31 @@ module Support subject{ object.decorate } let( :policy ){ ::Pundit.policy(user_context, object) } let( :user_context ){ UserContext.new(user, referential: referential) } - + let( :features ){ [] } + let( :filtered_action_links){} before do allow_any_instance_of(Draper::HelperProxy).to receive(:policy).and_return policy + allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| + features.include?(f) + } end end end - def expect_action_link_hrefs - expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + def expect_action_link_hrefs(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:href) ) + else + expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + end end - def expect_action_link_elements - expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + + def expect_action_link_elements(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:content) ) + else + expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + end end end end -- cgit v1.2.3 From fe7915ffac359db41c7737a3847f31a728f502e6 Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 31 Jan 2018 11:39:39 +0100 Subject: Refs #5683 @2H; Fix specs and refactor action_links Note: Did not fix the missing workgroup in the calendar mailer --- spec/support/pundit/pundit_view_policy.rb | 5 ++--- spec/support/referential.rb | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'spec/support') diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 91be0624c..330209049 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -2,9 +2,8 @@ module Pundit module PunditViewPolicy 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(:current_referential){ referential || build_stubbed(:referential, organisation: organisation) } + into.let(:current_user){ create :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 } diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 497ff47a8..9acdce73a 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -11,8 +11,8 @@ module ReferentialHelper def self.included(base) base.class_eval do extend ClassMethods - alias_method :referential, :first_referential - alias_method :organisation, :first_organisation + base.let(:referential){ first_referential } + base.let(:organisation){ first_organisation } end end -- cgit v1.2.3 From 4f58c4eaca46dc975aa907840cdbe067f6225d5e Mon Sep 17 00:00:00 2001 From: Zog Date: Tue, 6 Feb 2018 12:48:57 +0100 Subject: Fix checksum-related specs --- spec/support/checksum_support.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index 14ea3c55e..e02d9f9f3 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -42,12 +42,12 @@ shared_examples 'checksum support' do |factory_name| end it 'should trigger set_current_checksum_source on save' do - expect(instance).to receive(:set_current_checksum_source) + expect(instance).to receive(:set_current_checksum_source).at_least(:once) instance.save end it 'should trigger update_checksum on save' do - expect(instance).to receive(:update_checksum) + expect(instance).to receive(:update_checksum).at_least(:once) instance.save end end -- cgit v1.2.3 From a6134639f361629726283f0e2544fd948d552e0b Mon Sep 17 00:00:00 2001 From: Zog Date: Tue, 6 Feb 2018 12:48:57 +0100 Subject: Fix checksum-related specs --- spec/support/checksum_support.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index 14ea3c55e..e02d9f9f3 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -42,12 +42,12 @@ shared_examples 'checksum support' do |factory_name| end it 'should trigger set_current_checksum_source on save' do - expect(instance).to receive(:set_current_checksum_source) + expect(instance).to receive(:set_current_checksum_source).at_least(:once) instance.save end it 'should trigger update_checksum on save' do - expect(instance).to receive(:update_checksum) + expect(instance).to receive(:update_checksum).at_least(:once) instance.save end end -- cgit v1.2.3 From 31b7b91b46d2e8f740d2cfd493836219f5a4f42d Mon Sep 17 00:00:00 2001 From: Xinhui Date: Fri, 5 Jan 2018 16:43:37 +0100 Subject: Refactoring use subject as factory Refs #5416 --- spec/support/checksum_support.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index e02d9f9f3..9a4475e23 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -1,17 +1,15 @@ -shared_examples 'checksum support' do |factory_name| - let(:instance) { create(factory_name) } - +shared_examples 'checksum support' do describe '#current_checksum_source' do let(:attributes) { ['code_value', 'label_value'] } let(:seperator) { ChecksumSupport::SEPARATOR } let(:nil_value) { ChecksumSupport::VALUE_FOR_NIL_ATTRIBUTE } before do - allow_any_instance_of(instance.class).to receive(:checksum_attributes).and_return(attributes) + allow_any_instance_of(subject.class).to receive(:checksum_attributes).and_return(attributes) end it 'should separate attribute by seperator' do - expect(instance.current_checksum_source).to eq("code_value#{seperator}label_value") + expect(subject.current_checksum_source).to eq("code_value#{seperator}label_value") end context 'nil value' do @@ -19,7 +17,7 @@ shared_examples 'checksum support' do |factory_name| it 'should replace nil attributes by default value' do source = "code_value#{seperator}#{nil_value}" - expect(instance.current_checksum_source).to eq(source) + expect(subject.current_checksum_source).to eq(source) end end @@ -28,26 +26,26 @@ shared_examples 'checksum support' do |factory_name| it 'should convert to nil' do source = "code_value#{seperator}#{nil_value}" - expect(instance.current_checksum_source).to eq(source) + expect(subject.current_checksum_source).to eq(source) end end end it 'should save checksum on create' do - expect(instance.checksum).to_not be_nil + expect(subject.checksum).to_not be_nil end it 'should save checksum_source' do - expect(instance.checksum_source).to_not be_nil + expect(subject.checksum_source).to_not be_nil end it 'should trigger set_current_checksum_source on save' do - expect(instance).to receive(:set_current_checksum_source).at_least(:once) - instance.save + expect(subject).to receive(:set_current_checksum_source).at_least(:once) + subject.save end it 'should trigger update_checksum on save' do - expect(instance).to receive(:update_checksum).at_least(:once) - instance.save + expect(subject).to receive(:update_checksum).at_least(:once) + subject.save end end -- cgit v1.2.3 From f3769c9d9fa0d4bf3c620a9c95e0c7533da9801c Mon Sep 17 00:00:00 2001 From: Xinhui Date: Tue, 9 Jan 2018 14:46:40 +0100 Subject: Refactoring spec checksum support Refs #5416 --- spec/support/checksum_support.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index 9a4475e23..3d88d1d57 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -29,6 +29,33 @@ shared_examples 'checksum support' do expect(subject.current_checksum_source).to eq(source) end end + + context 'array value' do + let(:attributes) { [['v1', 'v2', 'v3'], 'code_value'] } + + it 'should convert to list' do + source = "v1,v2,v3#{seperator}code_value" + expect(subject.current_checksum_source).to eq(source) + end + end + + context 'array of array value' do + let(:attributes) { [[['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']], 'code_value'] } + + it 'should convert to list' do + source = "(a1,a2,a3),(b1,b2,b3)#{seperator}code_value" + expect(subject.current_checksum_source).to eq(source) + end + end + + context 'array of array value, with empty array' do + let(:attributes) { [[['a1', 'a2', 'a3'], []], 'code_value'] } + + it 'should convert to list' do + source = "(a1,a2,a3),-#{seperator}code_value" + expect(subject.current_checksum_source).to eq(source) + end + end end it 'should save checksum on create' do -- cgit v1.2.3 From f9e8dd00ff0a8ed935789e83d0cdc2ffdfb7d1bb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 8 Feb 2018 11:56:35 +0100 Subject: checksum_support.rb: Rename `seperator` to `separator` Fix spelling. Refs #5416 --- spec/support/checksum_support.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index 3d88d1d57..760955274 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -1,22 +1,22 @@ shared_examples 'checksum support' do describe '#current_checksum_source' do let(:attributes) { ['code_value', 'label_value'] } - let(:seperator) { ChecksumSupport::SEPARATOR } + let(:separator) { ChecksumSupport::SEPARATOR } let(:nil_value) { ChecksumSupport::VALUE_FOR_NIL_ATTRIBUTE } before do allow_any_instance_of(subject.class).to receive(:checksum_attributes).and_return(attributes) end - it 'should separate attribute by seperator' do - expect(subject.current_checksum_source).to eq("code_value#{seperator}label_value") + it 'should separate attribute by separator' do + expect(subject.current_checksum_source).to eq("code_value#{separator}label_value") end context 'nil value' do let(:attributes) { ['code_value', nil] } it 'should replace nil attributes by default value' do - source = "code_value#{seperator}#{nil_value}" + source = "code_value#{separator}#{nil_value}" expect(subject.current_checksum_source).to eq(source) end end @@ -25,7 +25,7 @@ shared_examples 'checksum support' do let(:attributes) { ['code_value', []] } it 'should convert to nil' do - source = "code_value#{seperator}#{nil_value}" + source = "code_value#{separator}#{nil_value}" expect(subject.current_checksum_source).to eq(source) end end @@ -34,7 +34,7 @@ shared_examples 'checksum support' do let(:attributes) { [['v1', 'v2', 'v3'], 'code_value'] } it 'should convert to list' do - source = "v1,v2,v3#{seperator}code_value" + source = "v1,v2,v3#{separator}code_value" expect(subject.current_checksum_source).to eq(source) end end @@ -43,7 +43,7 @@ shared_examples 'checksum support' do let(:attributes) { [[['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']], 'code_value'] } it 'should convert to list' do - source = "(a1,a2,a3),(b1,b2,b3)#{seperator}code_value" + source = "(a1,a2,a3),(b1,b2,b3)#{separator}code_value" expect(subject.current_checksum_source).to eq(source) end end @@ -52,7 +52,7 @@ shared_examples 'checksum support' do let(:attributes) { [[['a1', 'a2', 'a3'], []], 'code_value'] } it 'should convert to list' do - source = "(a1,a2,a3),-#{seperator}code_value" + source = "(a1,a2,a3),-#{separator}code_value" expect(subject.current_checksum_source).to eq(source) end end -- cgit v1.2.3 From 8b6e358e3e59c507eac738a38b54f45a133164cc Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 22 Jan 2018 16:45:19 +0100 Subject: Refs #5669 @2h; Add a snapshot mechanism in the specs --- spec/support/integration_spec_helper.rb | 7 +++++++ spec/support/pundit/pundit_view_policy.rb | 7 ++++++- spec/support/snapshot_support.rb | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spec/support/snapshot_support.rb (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 36306559d..a9a31f232 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -24,6 +24,13 @@ module IntegrationSpecHelper context('', &block) if block_given? end end + + def with_feature feature, &block + context "with feature #{feature}" do + let(:features){ [feature] } + context('', &block) if block_given? + end + end end def self.included into diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 330209049..058f1f6ed 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -5,9 +5,14 @@ module Pundit into.let(:current_referential){ referential || build_stubbed(:referential, organisation: organisation) } into.let(:current_user){ create :user, permissions: permissions, organisation: organisation } into.let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } + into.let(:current_offer_workbench) { create :workbench, organisation: organisation} into.before do allow(view).to receive(:pundit_user) { pundit_user } - + allow(view).to receive(:current_user) { current_user } + allow(view).to receive(:current_organisation).and_return(organisation) + allow(view).to receive(:current_offer_workbench).and_return(current_offer_workbench) + allow(view).to receive(:has_feature?){ |f| features.include?(f)} + allow(view).to receive(:user_signed_in?).and_return true allow(view).to receive(:policy) do |instance| ::Pundit.policy pundit_user, instance end diff --git a/spec/support/snapshot_support.rb b/spec/support/snapshot_support.rb new file mode 100644 index 000000000..f1a6b1469 --- /dev/null +++ b/spec/support/snapshot_support.rb @@ -0,0 +1,23 @@ +RSpec::Matchers.define :match_actions_links_snapshot do |name| + match do |actual| + @content = Capybara::Node::Simple.new(rendered).find('.page_header').native.inner_html + expect(@content).to match_snapshot(name) + end + + failure_message do |actual| + out = ["Snapshots did not match."] + expected = File.read(File.dirname(method_missing(:class).metadata[:file_path]) + "/__snapshots__/#{name}.snap") + out << "Expected: #{expected}" + out << "Actual: #{@content}" + out << "\n\n --- DIFF ---" + out << differ.diff_as_string(@content, expected) + out.join("\n") + end + + def differ + RSpec::Support::Differ.new( + :object_preparer => lambda { |object| RSpec::Matchers::Composable.surface_descriptions_in(object) }, + :color => RSpec::Matchers.configuration.color? + ) + end +end -- cgit v1.2.3 From 590ff858cf0fe71b420f8a59d393319e190f68aa Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 22 Jan 2018 21:11:28 +0100 Subject: Refs #5669; Add a preview for snapshots for easier debugging --- spec/support/snapshot_support.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/snapshot_support.rb b/spec/support/snapshot_support.rb index f1a6b1469..c37bb075c 100644 --- a/spec/support/snapshot_support.rb +++ b/spec/support/snapshot_support.rb @@ -6,14 +6,27 @@ RSpec::Matchers.define :match_actions_links_snapshot do |name| failure_message do |actual| out = ["Snapshots did not match."] - expected = File.read(File.dirname(method_missing(:class).metadata[:file_path]) + "/__snapshots__/#{name}.snap") + snap_path = File.dirname(method_missing(:class).metadata[:file_path]) + "/__snapshots__/#{name}.snap" + temp_path = Pathname.new "#{Rails.root}/tmp/__snapshots__/#{name}.failed.snap" + FileUtils.mkdir_p temp_path.dirname + tmp = File.new temp_path, "w" + tmp.write @content + tmp.close() + expected = File.read snap_path out << "Expected: #{expected}" out << "Actual: #{@content}" out << "\n\n --- DIFF ---" out << differ.diff_as_string(@content, expected) + out << "\n\n --- Previews : ---" + out << "Expected: \n" + snapshot_url(snap: snap_path, layout: :actions_links) + out << " \nActual: \n" + snapshot_url(snap: tmp.path, layout: :actions_links) out.join("\n") end + def snapshot_url snap:, layout: + "http://localhost:3000/snap/?snap=#{URI.encode(snap.to_s)}&layout=#{URI.encode(layout.to_s)}" + end + def differ RSpec::Support::Differ.new( :object_preparer => lambda { |object| RSpec::Matchers::Composable.surface_descriptions_in(object) }, -- cgit v1.2.3 From f114485e22c17239f3d7be06eb3bc1abffbbfe4b Mon Sep 17 00:00:00 2001 From: Zog Date: Tue, 23 Jan 2018 13:35:26 +0100 Subject: Refs #5669 @1h; Set invariant values in the snapshots --- spec/support/snapshot_support.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'spec/support') diff --git a/spec/support/snapshot_support.rb b/spec/support/snapshot_support.rb index c37bb075c..b1ade5288 100644 --- a/spec/support/snapshot_support.rb +++ b/spec/support/snapshot_support.rb @@ -1,3 +1,27 @@ +module SnaphostSpecHelper + + module Methods + def set_invariant expr, val=nil + val ||= expr + chain = expr.split(".") + method = chain.pop + + before(:each) do + allow(eval(chain.join('.'))).to receive(method){ val } + end + end + end + + def self.included into + into.extend Methods + end +end + +RSpec.configure do |config| + config.include SnaphostSpecHelper, type: :view +end + + RSpec::Matchers.define :match_actions_links_snapshot do |name| match do |actual| @content = Capybara::Node::Simple.new(rendered).find('.page_header').native.inner_html -- cgit v1.2.3 From 708c2fbe2f579426972a45f9a716d844a8e825ae Mon Sep 17 00:00:00 2001 From: Zog Date: Thu, 8 Feb 2018 13:48:13 +0100 Subject: Refs #5669 @2h; Fix Specs --- spec/support/integration_spec_helper.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index a9a31f232..7ba7e9f92 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -3,7 +3,11 @@ module IntegrationSpecHelper def paginate_collection klass, decorator, page=1, context={} collection = klass.page(page) if decorator - collection = ModelDecorator.decorate(collection, with: decorator, context: context) + if decorator < AF83::Decorator + collection = decorator.decorate(collection, context: context) + else + collection = ModelDecorator.decorate(collection, with: decorator, context: context) + end end collection end @@ -55,7 +59,7 @@ RSpec::Matchers.define :have_link_for_each_item do |collection, name, opts| end description { "have #{name} link for each item" } failure_message do - "expected view to have #{name} link for each item, failed with selector: \"#{@selector}\"" + "expected view to have one #{name} link for each item, failed with selector: \"#{@selector}\"" end end -- cgit v1.2.3 From 34dca4bbfc60b17a1648d8af34d592ec7c58c0ca Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 9 Feb 2018 10:52:01 +0100 Subject: Refs 5669; Fix specs --- spec/support/pundit/pundit_view_policy.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/support') diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 058f1f6ed..63970de02 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -11,6 +11,7 @@ module Pundit allow(view).to receive(:current_user) { current_user } allow(view).to receive(:current_organisation).and_return(organisation) allow(view).to receive(:current_offer_workbench).and_return(current_offer_workbench) + allow(view).to receive(:current_workgroup).and_return(current_offer_workbench.workgroup) allow(view).to receive(:has_feature?){ |f| features.include?(f)} allow(view).to receive(:user_signed_in?).and_return true allow(view).to receive(:policy) do |instance| -- cgit v1.2.3 From 42cff5c818140b193eb90fbb40b31e8a0c708207 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 9 Feb 2018 12:21:30 +0100 Subject: checksum_support.rb: Add test that checksum is the same on update Ensure the checksum doesn't change when we save it (and thus call `#update_checksum`) if the source hasn't changed. Refs #5416 --- spec/support/checksum_support.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'spec/support') diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index 760955274..f8dffb1b7 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -75,4 +75,12 @@ shared_examples 'checksum support' do expect(subject).to receive(:update_checksum).at_least(:once) subject.save end + + it "doesn't change the checksum on save if the source hasn't been changed" do + checksum = subject.checksum + + subject.save + + expect(subject.checksum).to eq(checksum) + end end -- cgit v1.2.3