diff options
Diffstat (limited to 'spec')
26 files changed, 601 insertions, 131 deletions
diff --git a/spec/controllers/autocomplete_stop_areas_controller_spec.rb b/spec/controllers/autocomplete_stop_areas_controller_spec.rb new file mode 100644 index 000000000..50fc877dd --- /dev/null +++ b/spec/controllers/autocomplete_stop_areas_controller_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe AutocompleteStopAreasController, type: :controller do + login_user + + let(:referential) { Referential.first } + let!(:stop_area) { create :stop_area, name: 'écolà militaire' } + + describe 'GET #index' do + it 'should be successful' do + get :index, referential_id: referential.id + expect(response).to be_success + end + + context 'search by name' do + it 'should be successful' do + get :index, referential_id: referential.id, q: 'écolà', :format => :json + expect(response).to be_success + expect(assigns(:stop_areas)).to eq([stop_area]) + end + + it 'should be accent insensitive' do + get :index, referential_id: referential.id, q: 'ecola', :format => :json + expect(response).to be_success + expect(assigns(:stop_areas)).to eq([stop_area]) + end + end + end +end diff --git a/spec/controllers/imports_controller_spec.rb b/spec/controllers/imports_controller_spec.rb index 22be9f6ed..08495ff47 100644 --- a/spec/controllers/imports_controller_spec.rb +++ b/spec/controllers/imports_controller_spec.rb @@ -17,6 +17,20 @@ RSpec.describe ImportsController, :type => :controller do end end + describe "POST #create" do + it "displays a flash message" do + post :create, workbench_id: workbench.id, + import: { + name: 'Offre', + file: fixture_file_upload('nozip.zip') + } + + expect(controller).to set_flash[:notice].to( + I18n.t('flash.imports.create.notice') + ) + end + end + describe 'GET #download' do it 'should be successful' do get :download, workbench_id: workbench.id, id: import.id, token: import.token_download diff --git a/spec/controllers/line_referentials_controller_spec.rb b/spec/controllers/line_referentials_controller_spec.rb index aee24b0fa..17ffb670d 100644 --- a/spec/controllers/line_referentials_controller_spec.rb +++ b/spec/controllers/line_referentials_controller_spec.rb @@ -1,3 +1,19 @@ RSpec.describe LineReferentialsController, :type => :controller do + login_user + let(:line_referential) { create :line_referential } + + describe 'PUT sync' do + let(:request){ put :sync, id: line_referential.id } + + it 'should redirect to 403' do + expect(request).to redirect_to "/403" + end + + with_permission "line_referentials.synchronize" do + it 'returns HTTP success' do + expect(request).to redirect_to [line_referential] + end + end + end end diff --git a/spec/controllers/referentials_controller_spec.rb b/spec/controllers/referentials_controller_spec.rb index fba063085..4050a8812 100644 --- a/spec/controllers/referentials_controller_spec.rb +++ b/spec/controllers/referentials_controller_spec.rb @@ -30,4 +30,33 @@ describe ReferentialsController, :type => :controller do expect(assigns[:compliance_control_sets]).to eq([compliance_control_set]) end end + + describe "POST #validate" do + it "displays a flash message" do + post :validate, id: referential.id, params: { + compliance_control_set: create(:compliance_control_set).id + } + + expect(controller).to set_flash[:notice].to( + I18n.t('notice.referentials.validate') + ) + end + end + + describe "POST #create" do + context "when duplicating" do + it "displays a flash message" do + post :create, + from: referential.id, + current_workbench_id: referential.workbench_id, + referential: { + name: 'Duplicated' + } + + expect(controller).to set_flash[:notice].to( + I18n.t('notice.referentials.duplicate') + ) + end + end + end end diff --git a/spec/controllers/stop_area_referentials_controller_spec.rb b/spec/controllers/stop_area_referentials_controller_spec.rb new file mode 100644 index 000000000..c8d7e1736 --- /dev/null +++ b/spec/controllers/stop_area_referentials_controller_spec.rb @@ -0,0 +1,17 @@ +RSpec.describe StopAreaReferentialsController, :type => :controller do + login_user + + let(:stop_area_referential) { create :stop_area_referential } + + describe 'PUT sync' do + let(:request){ put :sync, id: stop_area_referential.id } + + it { request.should redirect_to "/403" } + + with_permission "stop_area_referentials.synchronize" do + it 'returns HTTP success' do + expect(request).to redirect_to [stop_area_referential] + end + end + end +end diff --git a/spec/factories/chouette_stop_areas.rb b/spec/factories/chouette_stop_areas.rb index 8b64c227b..7f937e361 100644 --- a/spec/factories/chouette_stop_areas.rb +++ b/spec/factories/chouette_stop_areas.rb @@ -3,7 +3,7 @@ FactoryGirl.define do sequence(:objectid) { |n| "FR:#{n}:ZDE:#{n}:STIF" } sequence(:name) { |n| "stop_area_#{n}" } sequence(:registration_number) { |n| "test-#{n}" } - area_type { Chouette::StopArea.area_type.values.sample } + area_type { Chouette::AreaType.all.sample } latitude {10.0 * rand} longitude {10.0 * rand} diff --git a/spec/features/compliance_check_sets_spec.rb b/spec/features/compliance_check_sets_spec.rb index 7ba64b6b8..6b7d7a4f8 100644 --- a/spec/features/compliance_check_sets_spec.rb +++ b/spec/features/compliance_check_sets_spec.rb @@ -29,7 +29,7 @@ RSpec.describe "ComplianceCheckSets", type: :feature do it 'we can see the expected content' do # Breadcrumbs - expect_breadcrumb_links "Accueil", "Gestion de l'offre", "Liste des jeux de contrôles" + expect_breadcrumb_links "Accueil", "Gestion de l'offre", "Liste des rapports de contrôles" # Headline expect( page ).to have_content("Jeu de contrôles exécutés #{compliance_check_set.name}") @@ -49,10 +49,10 @@ RSpec.describe "ComplianceCheckSets", type: :feature do # Direct Children within(:xpath, xpath_for_div_of_block) do direct_checks.each do | direct_check | - expect( page ).to have_content( direct_check.code ) - expect( page ).to have_content( direct_check.name ) - expect( page ).to have_content( direct_check.criticity ) - expect( page ).to have_content( direct_check.comment ) + expect( page ).to have_content( direct_check.code ) + expect( page ).to have_content( direct_check.name ) + expect( page ).to have_content( direct_check.criticity ) + expect( page ).to have_content( direct_check.comment ) end end @@ -60,10 +60,10 @@ RSpec.describe "ComplianceCheckSets", type: :feature do compliance_check_set.compliance_check_blocks.each do | block | within(:xpath, xpath_for_div_of_block(block)) do block.compliance_checks.each do | check | - expect( page ).to have_content( check.code ) - expect( page ).to have_content( check.name ) - expect( page ).to have_content( check.criticity ) - expect( page ).to have_content( check.comment ) + expect( page ).to have_content( check.code ) + expect( page ).to have_content( check.name ) + expect( page ).to have_content( check.criticity ) + expect( page ).to have_content( check.comment ) end end end @@ -86,7 +86,7 @@ RSpec.describe "ComplianceCheckSets", type: :feature do all_checks.each do | check | expect( page ).to have_content(check.code) end - + end end diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 3b0a18379..83b746d4b 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -59,7 +59,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="referential-#{referential.id}"> <td> <div class="checkbox"><input type="checkbox" name="#{referential.id}" id="#{referential.id}" value="#{referential.id}" /><label for="#{referential.id}"></label></div> </td> @@ -213,7 +213,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="company-#{company.id}"> <td>#{company.get_objectid.local_id}</td> <td title="Voir"><a href="/referentials/#{referential.id}/companies/#{company.id}">#{company.name}</a></td> <td></td> @@ -326,7 +326,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="company-#{company.id}"> <td>#{company.get_objectid.local_id}</td> <td title="Voir"><a href="/referentials/#{referential.id}/companies/#{company.id}">#{company.name}</a></td> <td></td> @@ -381,5 +381,86 @@ describe TableBuilderHelper, type: :helper do expect(beautified_html).to eq(expected.chomp) end + + context "on a single row" do + let(:referential){ build_stubbed :referential } + let(:other_referential){ build_stubbed :referential } + let(:user_context){ + UserContext.new( + build_stubbed( + :user, + organisation: referential.organisation, + permissions: [ + 'referentials.create', + 'referentials.update', + 'referentials.destroy', + ] + ), + referential: referential + ) + } + let(:columns){ + [ + TableBuilderHelper::Column.new( + key: :name, + attribute: 'name' + ), + ] + } + let(:item){ referential.decorate } + let(:other_item){ other_referential.decorate } + let(:selectable){ false } + let(:links){ [:show] } + let(:overhead){ [] } + let(:model_name){ "referential" } + let(:other_tr){ helper.send(:tr, other_item, columns, selectable, links, overhead, model_name) } + let(:items){ [item, other_item] } + + before(:each){ + allow(helper).to receive(:current_user).and_return(user_context) + } + + context "with all rows non-selectable" do + let(:selectable){ false } + it "sets all rows as non selectable" do + items.each do |i| + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" + selector = "tr.#{klass} [type=checkbox]" + expect(tr).to_not have_selector selector + end + end + end + + context "with all rows selectable" do + let(:selectable){ true } + it "adds a checkbox in all rows" do + items.each do |i| + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" + selector = "tr.#{klass} [type=checkbox]" + expect(tr).to have_selector selector + end + end + end + + context "with THIS row non selectable" do + let(:selectable){ ->(i){ i.id != item.id } } + it "adds a checkbox in all rows" do + items.each do |i| + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" + selector = "tr.#{klass} [type=checkbox]" + expect(tr).to have_selector selector + end + end + it "disables this rows checkbox" do + tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name) + klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{item.id}" + selector = "tr.#{klass} [type=checkbox][disabled]" + expect(tr).to have_selector selector + end + end + end end end diff --git a/spec/lib/stif/permission_translator_spec.rb b/spec/lib/stif/permission_translator_spec.rb index ae1a2d1d5..9771af187 100644 --- a/spec/lib/stif/permission_translator_spec.rb +++ b/spec/lib/stif/permission_translator_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 RSpec.describe Stif::PermissionTranslator do context "No SSO Permissions" do @@ -42,4 +43,19 @@ RSpec.describe Stif::PermissionTranslator do ).to match_array(Support::Permissions.all_permissions) end end + + context "For the STIF organisation" do + let(:organisation){ build_stubbed :organisation, name: "STIF" } + let(:permissions){ %w{calendars.share stop_area_referentials.synchronize line_referentials.synchronize}.sort } + it "adds the STIF permission" do + expect(described_class.translate([], organisation).sort).to eq permissions + end + + context "with the case changed" do + let(:organisation){ build_stubbed :organisation, name: "StiF" } + it "adds the STIF permission" do + expect(described_class.translate([], organisation).sort).to eq permissions + end + end + end end diff --git a/spec/models/chouette/area_type_spec.rb b/spec/models/chouette/area_type_spec.rb new file mode 100644 index 000000000..470e3a066 --- /dev/null +++ b/spec/models/chouette/area_type_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +RSpec.describe Chouette::AreaType do + + describe "::ALL" do + it "includes all supported types" do + expect(Chouette::AreaType::ALL).to match_array( %i(zdep zder zdlp zdlr lda) ) + end + end + + describe ".find" do + it "returns nil if the given code is unknown" do + expect(Chouette::AreaType.find('dummy')).to be_nil + end + + it "returns an AreaType associated to the code" do + expect(Chouette::AreaType.find('zdep').code).to eq :zdep + end + end + + describe ".options" do + before do + Chouette::AreaType.reset_caches! + end + + it "returns an array with label and code for each type" do + allow(Chouette::AreaType).to receive(:all).and_return(%i{zdep lda}) + expect(Chouette::AreaType.options).to eq([["ZDEp", :zdep], ["LDA", :lda]]) + end + end + +end diff --git a/spec/models/referential_cloning_spec.rb b/spec/models/referential_cloning_spec.rb index 5acd433ec..4327c98aa 100644 --- a/spec/models/referential_cloning_spec.rb +++ b/spec/models/referential_cloning_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' RSpec.describe ReferentialCloning, :type => :model do + it 'should have a valid factory' do expect(FactoryGirl.build(:referential_cloning)).to be_valid end @@ -8,11 +9,69 @@ RSpec.describe ReferentialCloning, :type => :model do it { should belong_to :source_referential } it { should belong_to :target_referential } - describe "ReferentialCloningWorker" do + describe 'after commit' do + let(:referential_cloning) { FactoryGirl.create(:referential_cloning) } + + it 'invoke clone method' do + expect(referential_cloning).to receive(:clone) + referential_cloning.run_callbacks(:commit) + end + end + + describe '#clone' do let(:referential_cloning) { FactoryGirl.create(:referential_cloning) } it "should schedule a job in worker" do - expect{referential_cloning.run_callbacks(:commit)}.to change {ReferentialCloningWorker.jobs.count}.by(1) + expect{referential_cloning.clone}.to change {ReferentialCloningWorker.jobs.count}.by(1) + end + end + + describe '#clone!' do + let(:source_referential) { Referential.new slug: "source"} + let(:target_referential) { Referential.new slug: "target"} + let(:referential_cloning) do + ReferentialCloning.new source_referential: source_referential, + target_referential: target_referential + end + + let(:cloner) { double } + + before do + allow(AF83::SchemaCloner).to receive(:new).and_return cloner + allow(cloner).to receive(:clone_schema) end + + it 'creates a schema cloner with source and target schemas and clone schema' do + expect(AF83::SchemaCloner).to receive(:new).with(source_referential.slug, target_referential.slug).and_return(cloner) + expect(cloner).to receive(:clone_schema) + + referential_cloning.clone! + end + + context 'when clone_schema is performed without error' do + it "should have successful status" do + referential_cloning.clone! + expect(referential_cloning.status).to eq("successful") + end + end + + context 'when clone_schema raises an error' do + it "should have failed status" do + expect(cloner).to receive(:clone_schema).and_raise("#fail") + referential_cloning.clone! + expect(referential_cloning.status).to eq("failed") + end + end + + it "defines started_at" do + referential_cloning.clone! + expect(referential_cloning.started_at).not_to be_nil + end + + it "defines ended_at" do + referential_cloning.clone! + expect(referential_cloning.ended_at).not_to be_nil + end + end end diff --git a/spec/policies/calendar_policy_spec.rb b/spec/policies/calendar_policy_spec.rb index 294be8198..144a1c6bb 100644 --- a/spec/policies/calendar_policy_spec.rb +++ b/spec/policies/calendar_policy_spec.rb @@ -5,18 +5,24 @@ RSpec.describe CalendarPolicy, type: :policy do permissions :create? do - it_behaves_like 'permitted policy', 'calendars.create', archived: true + it_behaves_like 'permitted policy', 'calendars.create' + end + permissions :share? do + it_behaves_like 'permitted policy and same organisation', 'calendars.share' + end + permissions :share? do + it_behaves_like 'permitted policy and same organisation', 'calendars.share' end permissions :destroy? do - it_behaves_like 'permitted policy and same organisation', 'calendars.destroy', archived: true + it_behaves_like 'permitted policy and same organisation', 'calendars.destroy' end permissions :edit? do - it_behaves_like 'permitted policy and same organisation', 'calendars.update', archived: true + it_behaves_like 'permitted policy and same organisation', 'calendars.update' end permissions :new? do - it_behaves_like 'permitted policy', 'calendars.create', archived: true + it_behaves_like 'permitted policy', 'calendars.create' end permissions :update? do - it_behaves_like 'permitted policy and same organisation', 'calendars.update', archived: true + it_behaves_like 'permitted policy and same organisation', 'calendars.update' end end diff --git a/spec/policies/line_referential_policy_spec.rb b/spec/policies/line_referential_policy_spec.rb new file mode 100644 index 000000000..7e0a9da8e --- /dev/null +++ b/spec/policies/line_referential_policy_spec.rb @@ -0,0 +1,9 @@ +RSpec.describe LineReferentialPolicy, type: :policy do + + let( :record ){ build_stubbed :line_referential } + before { stub_policy_scope(record) } + + permissions :synchronize? do + it_behaves_like 'permitted policy', 'line_referentials.synchronize' + end +end diff --git a/spec/policies/sto_area_referential_policy_spec.rb b/spec/policies/sto_area_referential_policy_spec.rb new file mode 100644 index 000000000..5bd6da427 --- /dev/null +++ b/spec/policies/sto_area_referential_policy_spec.rb @@ -0,0 +1,9 @@ +RSpec.describe StopAreaReferentialPolicy, type: :policy do + + let( :record ){ build_stubbed :stop_area_referential } + before { stub_policy_scope(record) } + + permissions :synchronize? do + it_behaves_like 'permitted policy', 'stop_area_referentials.synchronize' + end +end diff --git a/spec/support/controller_spec_helper.rb b/spec/support/controller_spec_helper.rb new file mode 100644 index 000000000..1d0288dea --- /dev/null +++ b/spec/support/controller_spec_helper.rb @@ -0,0 +1,18 @@ +module ControllerSpecHelper + def with_permission permission, &block + context "with permission #{permission}" do + login_user + before(:each) do + @user.permissions << permission + @user.save! + sign_in @user + end + context('', &block) if block_given? + end + end + +end + +RSpec.configure do |config| + config.extend ControllerSpecHelper, type: :controller +end diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb new file mode 100644 index 000000000..78efb9027 --- /dev/null +++ b/spec/support/integration_spec_helper.rb @@ -0,0 +1,49 @@ +module IntegrationSpecHelper + + def paginate_collection klass, decorator, page=1 + ModelDecorator.decorate( klass.page(page), with: decorator ) + end + + def build_paginated_collection factory, decorator, opts={} + count = opts.delete(:count) || 2 + page = opts.delete(:page) || 1 + klass = nil + count.times { klass ||= create(factory, opts).class } + paginate_collection klass, decorator, page + end + + module Methods + def with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? + end + end + end + + def self.included into + into.extend Methods + end +end + +RSpec.configure do |config| + config.include IntegrationSpecHelper, type: :view +end + +RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| + match do |actual| + collection.each do |item| + expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']", count: 1) + end + end + description { "have #{name} link for each item" } +end + +RSpec::Matchers.define :have_the_right_number_of_links do |collection, count| + match do |actual| + collection.each do |item| + expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a", count: count) + end + end + description { "have #{count} links for each item" } +end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index b8434cac0..91be0624c 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -1,16 +1,16 @@ module Pundit module PunditViewPolicy - extend ActiveSupport::Concern + def self.included into + into.let(:permissions){ nil } + into.let(:organisation){ referential.try(:organisation) } + into.let(:current_referential){ referential || build_stubbed(:referential) } + into.let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } + into.let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } + into.before do + allow(view).to receive(:pundit_user) { pundit_user } - included do - before do - controller.singleton_class.class_eval do - def policy(instance) - Class.new do - def method_missing(*args, &block); true; end - end.new - end - helper_method :policy + allow(view).to receive(:policy) do |instance| + ::Pundit.policy pundit_user, instance end end end diff --git a/spec/views/connection_links/index.html.erb_spec.rb b/spec/views/connection_links/index.html.erb_spec.rb index a01380094..1f133e31e 100644 --- a/spec/views/connection_links/index.html.erb_spec.rb +++ b/spec/views/connection_links/index.html.erb_spec.rb @@ -17,9 +17,11 @@ describe "/connection_links/index", :type => :view do end end - it "should render a link to create a new group" do - render - expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_referential_connection_link_path(referential)}']") + with_permission "connection_links.create" do + it "should render a link to create a new group" do + render + expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_referential_connection_link_path(referential)}']") + end end end diff --git a/spec/views/connection_links/show.html.erb_spec.rb b/spec/views/connection_links/show.html.erb_spec.rb deleted file mode 100644 index c04a4f3f1..000000000 --- a/spec/views/connection_links/show.html.erb_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'spec_helper' - -describe "/connection_links/show", :type => :view do - - assign_referential - let!(:connection_link) { assign(:connection_link, create(:connection_link)) } - let!(:map) { assign(:map, double(:to_html => '<div id="map"/>'.html_safe)) } - - before do - allow(view).to receive_messages(current_organisation: referential.organisation) - end - - it "should render h2 with the connection_link name" do - render - expect(rendered).to have_selector("h2", :text => Regexp.new(connection_link.name)) - end - -# it "should display a map with class 'connection_link'" do -# pending ": map not yet implemented" -# render -# expect(rendered).to have_selector("#map", :class => 'connection_link') -# end - - it "should render a link to edit the connection_link" do - render - expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{view.edit_referential_connection_link_path(referential, connection_link)}']") - end - - it "should render a link to remove the connection_link" do - render - expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{view.referential_connection_link_path(referential, connection_link)}'][class='remove']") - end - -end - diff --git a/spec/views/connection_links/show.html.slim_spec.rb b/spec/views/connection_links/show.html.slim_spec.rb new file mode 100644 index 000000000..afe94fc6c --- /dev/null +++ b/spec/views/connection_links/show.html.slim_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe "/connection_links/show", :type => :view do + + assign_referential + let!(:connection_link) { assign(:connection_link, create(:connection_link)) } + let!(:map) { assign(:map, double(:to_html => '<div id="map"/>'.html_safe)) } + + before do + allow(view).to receive_messages(current_organisation: referential.organisation) + end + + it "should render h2 with the connection_link name" do + render + expect(rendered).to have_selector("h2", :text => Regexp.new(connection_link.name)) + end + + with_permission "connection_links.update" do + it "should render a link to edit the connection_link" do + render + expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{view.edit_referential_connection_link_path(referential, connection_link)}']") + end + end + + with_permission "connection_links.destroy" do + it "should render a link to remove the connection_link" do + render + expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{view.referential_connection_link_path(referential, connection_link)}'][class='remove']") + end + end + +end diff --git a/spec/views/line_referentials/show.html.slim_spec.rb b/spec/views/line_referentials/show.html.slim_spec.rb new file mode 100644 index 000000000..0516677cb --- /dev/null +++ b/spec/views/line_referentials/show.html.slim_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "/line_referentials/show", :type => :view do + + let!(:line_referential) { assign :line_referential, create(:line_referential) } + + before :each do + render + end + + it "should not present syncing infos and button" do + expect(view.content_for(:page_header_actions)).to_not have_selector("a[href=\"#{view.sync_line_referential_path(line_referential)}\"]") + expect(view.content_for(:page_header_meta)).to_not have_selector(".last-update") + end + + with_permission "line_referentials.synchronize" do + it "should present syncing infos and button" do + expect(view.content_for(:page_header_actions)).to have_selector("a[href=\"#{view.sync_line_referential_path(line_referential)}\"]", count: 1) + expect(view.content_for(:page_header_meta)).to have_selector(".last-update", count: 1) + end + end +end diff --git a/spec/views/line_referentials/stop_area_referentials/show.html.slim_spec.rb b/spec/views/line_referentials/stop_area_referentials/show.html.slim_spec.rb new file mode 100644 index 000000000..71a8d16f5 --- /dev/null +++ b/spec/views/line_referentials/stop_area_referentials/show.html.slim_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "/stop_area_referentials/show", :type => :view do + + let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } + + before :each do + render + end + + it "should not present syncing infos and button" do + expect(view.content_for(:page_header_actions)).to_not have_selector("a[href=\"#{view.sync_stop_area_referential_path(stop_area_referential)}\"]") + expect(view.content_for(:page_header_meta)).to_not have_selector(".last-update") + end + + with_permission "stop_area_referentials.synchronize" do + it "should present syncing infos and button" do + expect(view.content_for(:page_header_actions)).to have_selector("a[href=\"#{view.sync_stop_area_referential_path(stop_area_referential)}\"]", count: 1) + expect(view.content_for(:page_header_meta)).to have_selector(".last-update", count: 1) + end + end +end diff --git a/spec/views/offer_workbenches/show.html.erb_spec.rb b/spec/views/offer_workbenches/show.html.erb_spec.rb index 40b09268a..138a1560d 100644 --- a/spec/views/offer_workbenches/show.html.erb_spec.rb +++ b/spec/views/offer_workbenches/show.html.erb_spec.rb @@ -1,5 +1,56 @@ -require 'rails_helper' +require 'spec_helper' -RSpec.describe "workbenches/show.html.erb", :type => :view do +RSpec::Matchers.define :have_box_for_item do |item, disabled| + match do |actual| + klass = "#{TableBuilderHelper.item_row_class_name([item])}-#{item.id}" + if disabled + selector = "tr.#{klass} [type=checkbox][disabled][value='#{item.id}']" + else + selector = "tr.#{klass} [type=checkbox][value='#{item.id}']:not([disabled])" + end + expect(actual).to have_selector(selector, count: 1) + end + description { "have a #{disabled ? "disabled ": ""}box for the item ##{item.id}" } +end + +describe "workbenches/show", :type => :view do + let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } + let!(:lines) { + ids.map do |id| + create :line, objectid: id, line_referential: workbench.line_referential + end + } + let!(:workbench){ assign :workbench, create(:workbench) } + let!(:same_organisation_referential){ create :workbench_referential, workbench: workbench, metadatas: [create(:referential_metadata, lines: lines)] } + let!(:different_organisation_referential){ create :workbench_referential, metadatas: [create(:referential_metadata, lines: lines)] } + let!(:referentials){ + same_organisation_referential && different_organisation_referential + assign :wbench_refs, paginate_collection(Referential, ReferentialDecorator) + } + let!(:q) { assign :q_for_form, Ransack::Search.new(Referential) } + before :each do + lines + controller.request.path_parameters[:id] = workbench.id + expect(workbench.referentials).to include same_organisation_referential + expect(workbench.referentials).to_not include different_organisation_referential + expect(workbench.all_referentials).to include same_organisation_referential + expect(workbench.all_referentials).to include different_organisation_referential + render + end + + it { should have_link_for_each_item(referentials, "show", -> (referential){ view.referential_path(referential) }) } + + context "without permission" do + it "should disable all the checkboxes" do + expect(rendered).to have_box_for_item same_organisation_referential, false + expect(rendered).to have_box_for_item different_organisation_referential, true + end + end + with_permission "referentials.destroy" do + it "should enable the checkbox for the referential which belongs to the same organisation and disable the other one" do + expect(rendered).to have_box_for_item same_organisation_referential, false + expect(rendered).to have_box_for_item different_organisation_referential, true + end + end end diff --git a/spec/views/stop_areas/index.html.erb_spec.rb b/spec/views/stop_areas/index.html.erb_spec.rb deleted file mode 100644 index 2dfae1bfd..000000000 --- a/spec/views/stop_areas/index.html.erb_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'spec_helper' - -describe "/stop_areas/index", :type => :view do - - let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } - let!(:stop_areas) { assign :stop_areas, Array.new(2) { create(:stop_area, stop_area_referential: stop_area_referential) }.paginate } - let!(:q) { assign :q, Ransack::Search.new(Chouette::StopArea) } - - before :each do - allow(view).to receive(:link_with_search).and_return("#") - end - - # it "should render a show link for each group" do - # render - # stop_areas.each do |stop_area| - # expect(rendered).to have_selector(".stop_area a[href='#{view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}']", :text => stop_area.name) - # end - # end - # - # it "should render a link to create a new group" do - # render - # expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_stop_area_referential_stop_area_path(stop_area_referential)}']") - # end - -end diff --git a/spec/views/stop_areas/index.html.slim_spec.rb b/spec/views/stop_areas/index.html.slim_spec.rb new file mode 100644 index 000000000..8daa5eb4b --- /dev/null +++ b/spec/views/stop_areas/index.html.slim_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe "/stop_areas/index", :type => :view do + + let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } + let!(:stop_areas) do + assign :stop_areas, build_paginated_collection(:stop_area, StopAreaDecorator, stop_area_referential: stop_area_referential) + end + let!(:q) { assign :q, Ransack::Search.new(Chouette::StopArea) } + + before :each do + allow(view).to receive(:link_with_search).and_return("#") + allow(view).to receive(:collection).and_return(stop_areas) + allow(view).to receive(:current_referential).and_return(stop_area_referential) + controller.request.path_parameters[:stop_area_referential_id] = stop_area_referential.id + render + end + + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_the_right_number_of_links(stop_areas, 1) } + + with_permission "stop_areas.create" do + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should_not have_link_for_each_item(stop_areas, "create", -> (stop_area){ view.new_stop_area_referential_stop_area_path(stop_area_referential) }) } + it { should have_the_right_number_of_links(stop_areas, 1) } + end + + with_permission "stop_areas.update" do + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_link_for_each_item(stop_areas, "edit", -> (stop_area){ view.edit_stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_the_right_number_of_links(stop_areas, 2) } + end + +end diff --git a/spec/workers/referential_cloning_worker_spec.rb b/spec/workers/referential_cloning_worker_spec.rb index 7e4a2357a..2b9a54805 100644 --- a/spec/workers/referential_cloning_worker_spec.rb +++ b/spec/workers/referential_cloning_worker_spec.rb @@ -2,52 +2,35 @@ require 'spec_helper' require 'ostruct' RSpec.describe ReferentialCloningWorker do + alias_method :worker, :subject context "given a referential cloning" do + let(:id) { double } + let(:referential_cloning) { double } - let( :id ){ double } + it "invokes the clone! method of the associated ReferentialCloning" do + expect(ReferentialCloning).to receive(:find).with(id).and_return(referential_cloning) + expect(referential_cloning).to receive(:clone!) - let( :worker ){ described_class.new } - - def make_referential(schema_name) - return OpenStruct.new( slug: schema_name ) - end - - let( :source_schema ){ "source_schema" } - let( :target_schema ){ "target_schema" } - let( :referential_cloning ){ OpenStruct.new(source_referential: make_referential(source_schema), - target_referential: make_referential(target_schema)) } - let( :cloner ){ 'cloner' } - - - before do - expect( ReferentialCloning ).to receive(:find).with(id).and_return(referential_cloning) - expect( AF83::SchemaCloner ).to receive(:new).with( source_schema, target_schema ).and_return(cloner) - expect( cloner ).to receive(:clone_schema) - - expect( referential_cloning ).to receive(:run!) - end - - it "invokes the correct stored procedure, updates the database and the AASM" do - expect( referential_cloning ).to receive(:successful!) worker.perform(id) end end - it "should clone an existing Referential" do - source_referential = create :referential - - source_referential.switch - source_time_table = create :time_table + context 'with existing Referential' do + it "preserve existing data" do + source_referential = create :referential - target_referential = create :referential, created_from: source_referential + source_referential.switch + source_time_table = create :time_table - cloning = ReferentialCloning.create source_referential: source_referential, target_referential: target_referential - ReferentialCloningWorker.new.perform(cloning) + target_referential = create :referential, created_from: source_referential - target_referential.switch - expect(Chouette::TimeTable.where(objectid: source_time_table.objectid).exists?) - end + cloning = ReferentialCloning.create source_referential: source_referential, target_referential: target_referential + worker.perform(cloning.id) + target_referential.switch + expect(Chouette::TimeTable.where(objectid: source_time_table.objectid).exists?) + end + end end |
