aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/autocomplete_stop_areas_controller_spec.rb29
-rw-r--r--spec/controllers/imports_controller_spec.rb14
-rw-r--r--spec/controllers/referentials_controller_spec.rb29
-rw-r--r--spec/helpers/table_builder_helper_spec.rb6
-rw-r--r--spec/models/referential_cloning_spec.rb63
-rw-r--r--spec/models/workbench_spec.rb2
-rw-r--r--spec/support/integration_spec_helper.rb12
-rw-r--r--spec/support/pundit/pundit_view_policy.rb20
-rw-r--r--spec/views/connection_links/index.html.erb_spec.rb8
-rw-r--r--spec/views/connection_links/show.html.erb_spec.rb35
-rw-r--r--spec/views/connection_links/show.html.slim_spec.rb32
-rw-r--r--spec/views/stop_areas/index.html.erb_spec.rb25
-rw-r--r--spec/views/stop_areas/index.html.slim_spec.rb53
-rw-r--r--spec/workers/referential_cloning_worker_spec.rb53
14 files changed, 267 insertions, 114 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/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/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb
index 3b0a18379..3b3504c60 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>
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/models/workbench_spec.rb b/spec/models/workbench_spec.rb
index 3b9ed6b07..caff00ae4 100644
--- a/spec/models/workbench_spec.rb
+++ b/spec/models/workbench_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe Workbench, :type => :model do
let(:workbench) { create :workbench, organisation: organisation }
it 'should filter lines based on my organisation functional_scope' do
- ids.insert('STIF:CODIFLIGNE:Line:0000').each do |id|
+ (ids + ['STIF:CODIFLIGNE:Line:0000']).each do |id|
create :line, objectid: id, line_referential: workbench.line_referential
end
lines = workbench.lines
diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb
new file mode 100644
index 000000000..182cadf24
--- /dev/null
+++ b/spec/support/integration_spec_helper.rb
@@ -0,0 +1,12 @@
+module IntegrationSpecHelper
+ 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.extend IntegrationSpecHelper, type: :view
+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/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..64c958879
--- /dev/null
+++ b/spec/views/stop_areas/index.html.slim_spec.rb
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+RSpec::Matchers.define :have_link_for_each_stop_area do |stop_areas, name, href|
+ match do |actual|
+ stop_areas.each do |stop_area|
+ expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{href.call(stop_area)}']", count: 1)
+ end
+ end
+ description { "have #{name} link for each stop area" }
+end
+
+RSpec::Matchers.define :have_the_right_number_of_links do |stop_areas, count|
+ match do |actual|
+ stop_areas.each do |stop_area|
+ expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a", count: count)
+ end
+ end
+ description { "have #{count} links for each stop area" }
+end
+
+describe "/stop_areas/index", :type => :view do
+
+ let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) }
+ let!(:stop_areas) do
+ 2.times { create(:stop_area, stop_area_referential: stop_area_referential) }
+ assign :stop_areas, ModelDecorator.decorate( Chouette::StopArea.page(1), with: StopAreaDecorator )
+ 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_stop_area(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_stop_area(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) }
+ it { should_not have_link_for_each_stop_area(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_stop_area(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) }
+ it { should have_link_for_each_stop_area(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