diff options
| author | Zog | 2018-04-27 15:07:25 +0200 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-27 15:13:51 +0200 |
| commit | e1425294874b4cd4d71668e7669220252ea2e936 (patch) | |
| tree | 89d2877305ad5e066e47f3692b1d0b619c381eb1 | |
| parent | 3ed5ca0e97c287977646b7af6a41632c9dd3ad49 (diff) | |
| download | chouette-core-e1425294874b4cd4d71668e7669220252ea2e936.tar.bz2 | |
Refs #6572; Fix specs
| -rw-r--r-- | app/policies/application_policy.rb | 2 | ||||
| -rw-r--r-- | app/policies/referential_policy.rb | 4 | ||||
| -rw-r--r-- | db/schema.rb | 27 | ||||
| -rw-r--r-- | spec/decorators/referential_decorator_spec.rb | 30 | ||||
| -rw-r--r-- | spec/features/workbenches/workbenches_show_spec.rb | 36 | ||||
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 2 | ||||
| -rw-r--r-- | spec/models/referential_spec.rb | 4 | ||||
| -rw-r--r-- | spec/policies/referential_policy_spec.rb | 225 | ||||
| -rw-r--r-- | spec/support/pundit/shared_examples.rb | 5 | ||||
| -rw-r--r-- | spec/views/referentials/show.html.erb_spec.rb | 6 |
10 files changed, 184 insertions, 157 deletions
diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 05bb39ee4..36f14437a 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -100,7 +100,7 @@ class ApplicationPolicy # ------- def referential - @referential ||= record.is_a?(Referential) && record || current_referential || record_referential + @referential ||= current_referential || record_referential end def record_referential diff --git a/app/policies/referential_policy.rb b/app/policies/referential_policy.rb index f20b336fc..14f81cf5c 100644 --- a/app/policies/referential_policy.rb +++ b/app/policies/referential_policy.rb @@ -41,4 +41,8 @@ class ReferentialPolicy < ApplicationPolicy # TODO: Replace with correct BL ASA available, c.f. https://projects.af83.io/issues/2692 true end + + def referential_read_only? + record.referential_read_only? + end end diff --git a/db/schema.rb b/db/schema.rb index eeab0102f..a39cb1689 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180418070400) do +ActiveRecord::Schema.define(version: 20180425160730) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -103,8 +103,8 @@ ActiveRecord::Schema.define(version: 20180418070400) do create_table "clean_up_results", id: :bigserial, force: :cascade do |t| t.string "message_key" - t.hstore "message_attributs" - t.integer "clean_up_id", limit: 8 + t.hstore "message_attributes" + t.integer "clean_up_id", limit: 8 t.datetime "created_at" t.datetime "updated_at" end @@ -772,7 +772,7 @@ ActiveRecord::Schema.define(version: 20180418070400) do t.string "objectid", null: false t.integer "object_version", limit: 8 t.integer "route_id", limit: 8 - t.integer "stop_point_ids", limit: 8, array: true + t.integer "stop_point_ids", array: true t.string "checksum" t.text "checksum_source" t.string "data_source_ref" @@ -1035,28 +1035,29 @@ ActiveRecord::Schema.define(version: 20180418070400) do add_index "vehicle_journey_at_stops", ["vehicle_journey_id"], name: "index_vehicle_journey_at_stops_on_vehicle_journey_id", using: :btree create_table "vehicle_journeys", id: :bigserial, force: :cascade do |t| - t.integer "route_id", limit: 8 - t.integer "journey_pattern_id", limit: 8 - t.integer "company_id", limit: 8 - t.string "objectid", null: false - t.integer "object_version", limit: 8 + t.integer "route_id", limit: 8 + t.integer "journey_pattern_id", limit: 8 + t.integer "company_id", limit: 8 + t.string "objectid", null: false + t.integer "object_version", limit: 8 t.string "comment" t.string "transport_mode" t.string "published_journey_name" t.string "published_journey_identifier" t.string "facility" t.string "vehicle_type_identifier" - t.integer "number", limit: 8 + t.integer "number", limit: 8 t.boolean "mobility_restricted_suitability" t.boolean "flexible_service" - t.integer "journey_category", default: 0, null: false + t.integer "journey_category", default: 0, null: false t.datetime "created_at" t.datetime "updated_at" t.string "checksum" t.text "checksum_source" t.string "data_source_ref" - t.jsonb "custom_field_values", default: {} - t.jsonb "metadata", default: {} + t.jsonb "custom_field_values", default: {} + t.jsonb "metadata", default: {} + t.integer "ignored_routing_contraint_zone_ids", default: [], array: true end add_index "vehicle_journeys", ["objectid"], name: "vehicle_journeys_objectid_key", unique: true, using: :btree diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb index 1224aaf75..98546f28b 100644 --- a/spec/decorators/referential_decorator_spec.rb +++ b/spec/decorators/referential_decorator_spec.rb @@ -41,12 +41,11 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do end end context 'all rights and same organisation' do - let( :user ){ build_stubbed :allmighty_user, organisation: referential.organisation } let( :action){ :index } context "on index" do it 'has corresponding actions' do - expect_action_link_elements(action).to eq ["Consulter", "Editer", "Calendriers", "Dupliquer", "Valider", "Conserver","<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_elements(action).to eq ["Consulter", "Editer", "Calendriers", "Dupliquer", "Valider", "Archiver","<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] expect_action_link_hrefs(action).to eq([ [object], [:edit, object], @@ -62,7 +61,7 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context "on show" do let( :action){ :show } it 'has corresponding actions' do - expect_action_link_elements(action).to eq ["Editer", "Calendriers", "Dupliquer", "Valider", "Conserver", "Purger", "<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_elements(action).to eq ["Editer", "Calendriers", "Dupliquer", "Valider", "Archiver", "Purger", "<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] expect_action_link_hrefs(action).to eq([ [:edit, object], referential_time_tables_path(object), @@ -74,13 +73,36 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do ]) end end + + context 'with a failed referential' do + before{ + referential.ready = false + referential.failed_at = Time.now + } + context "on index" do + it 'has corresponding actions' do + expect_action_link_elements(action).to eq ["Consulter"] + expect_action_link_hrefs(action).to eq([ + [object], + ]) + end + end + + context "on show" do + let( :action){ :show } + it 'has corresponding actions' do + expect_action_link_elements(action).to eq [] + expect_action_link_hrefs(action).to eq([]) + end + end + end end end context 'archived referential' do before { referential.archived_at = 42.seconds.ago } context 'no rights' do - it 'has only ahow and calendar actions' do + it 'has only show and calendar actions' do expect_action_link_hrefs.to eq([[object], referential_time_tables_path(object)]) end end diff --git a/spec/features/workbenches/workbenches_show_spec.rb b/spec/features/workbenches/workbenches_show_spec.rb index 405fdce82..441f829a1 100644 --- a/spec/features/workbenches/workbenches_show_spec.rb +++ b/spec/features/workbenches/workbenches_show_spec.rb @@ -15,10 +15,10 @@ RSpec.describe 'Workbenches', type: :feature do expect(page).to have_content(referential.name) end - it 'should not show unready referentials' do + it 'should show unready referentials' do referential.update_attribute(:ready, false) visit workbench_path(workbench) - expect(page).to_not have_content(referential.name) + expect(page).to have_content(referential.name) end end @@ -138,38 +138,34 @@ RSpec.describe 'Workbenches', type: :feature do context 'filter by status' do it 'should display archived referentials' do - other_referential.update_attribute(:archived_at, Date.today) - find("#q_archived_at_not_null").set(true) + other_referential.failed! + referential.archived! + find("input[type=checkbox][name='q[state[archived]]']").set(true) click_button I18n.t('actions.filter') - expect(page).to have_content(other_referential.name) - expect(page).to_not have_content(referential.name) + expect(page).to have_content(referential.name) + expect(page).to_not have_content(other_referential.name) end - it 'should display both archived and unarchived referentials' do - other_referential.update_attribute(:archived_at, Date.today) - find("#q_archived_at_not_null").set(true) - find("#q_archived_at_null").set(true) + it 'should display failed referentials' do + referential.failed! + other_referential.active! + find("input[type=checkbox][name='q[state[failed]]']").set(true) click_button I18n.t('actions.filter') expect(page).to have_content(referential.name) - expect(page).to have_content(other_referential.name) + expect(page).to_not have_content(other_referential.name) end - it 'should display unarchived referentials' do - other_referential.update_attribute(:archived_at, Date.today) - find("#q_archived_at_null").set(true) + it 'should display active referentials' do + referential.active! + other_referential.failed! + find("input[type=checkbox][name='q[state[active]]']").set(true) click_button I18n.t('actions.filter') expect(page).to have_content(referential.name) expect(page).to_not have_content(other_referential.name) end - - it 'should keep filter value on submit' do - find("#q_archived_at_null").set(true) - click_button I18n.t('actions.filter') - expect(find("#q_archived_at_null")).to be_checked - end end context 'filter by validity period' do diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 6cb7929ac..8482a848c 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -94,7 +94,7 @@ describe TableBuilderHelper, type: :helper do <li class=""><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> <li class=""><a href="/workbenches/#{workbench.id}/referentials/new?from=#{referential.id}">Dupliquer</a></li> <li class=""><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> - <li class=""><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> + <li class=""><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Archiver</a></li> </ul> <ul class="footer"> <li class=" delete-action"><a data-confirm="Etes vous sûr de vouloir supprimer ce jeu de données ?" rel="nofollow" data-method="delete" href="/referentials/#{referential.id}"><span class="fa fa-trash mr-xs"></span>Supprimer</a></li> diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb index 8b8cf4359..eb9c2e338 100644 --- a/spec/models/referential_spec.rb +++ b/spec/models/referential_spec.rb @@ -88,8 +88,8 @@ describe Referential, :type => :model do cleanup end - it "should return the cleanup" do - expect(operation).to eq cleanup + it "should return nothing" do + expect(operation).to be_nil end end end diff --git a/spec/policies/referential_policy_spec.rb b/spec/policies/referential_policy_spec.rb index 778e14901..f922ef379 100644 --- a/spec/policies/referential_policy_spec.rb +++ b/spec/policies/referential_policy_spec.rb @@ -1,160 +1,159 @@ # coding: utf-8 RSpec.describe ReferentialPolicy, type: :policy do - let( :record ){ build_stubbed :referential } - - # # Collection Based Permissions differ from standard as there is no referential yet # -------------------------------------------------------------------------------- - - permissions :create? do - it 'permissions present → allowed' do - add_permissions('referentials.create', to_user: user) - expect_it.to permit(user_context, record) - end - it 'permissions absent → forbidden' do - expect_it.not_to permit(user_context, record) + context "on a ready referential" do + let( :record ){ referential } + permissions :create? do + it 'permissions present → allowed' do + add_permissions('referentials.create', to_user: user) + expect_it.to permit(user_context, record) + end + it 'permissions absent → forbidden' do + expect_it.not_to permit(user_context, record) + end end - end - permissions :new? do - it 'permissions present → allowed' do - add_permissions('referentials.create', to_user: user) - expect_it.to permit(user_context, record) - end - it 'permissions absent → forbidden' do - expect_it.not_to permit(user_context, record) + permissions :new? do + it 'permissions present → allowed' do + add_permissions('referentials.create', to_user: user) + expect_it.to permit(user_context, record) + end + it 'permissions absent → forbidden' do + expect_it.not_to permit(user_context, record) + end end - end - - # - # Standard Destructive Action Permissions - # --------------------------------------- - permissions :destroy? do - it_behaves_like 'permitted policy and same organisation', 'referentials.destroy', archived_and_finalised: true - end - permissions :edit? do - it_behaves_like 'permitted policy and same organisation', 'referentials.update', archived_and_finalised: true - end - permissions :update? do - it_behaves_like 'permitted policy and same organisation', 'referentials.update', archived_and_finalised: true - end + # + # Standard Destructive Action Permissions + # --------------------------------------- - # - # Custom Permissions - # ------------------ + permissions :destroy? do + it_behaves_like 'permitted policy and same organisation', 'referentials.destroy', archived_and_finalised: true + end + permissions :edit? do + it_behaves_like 'permitted policy and same organisation', 'referentials.update', archived_and_finalised: true + end + permissions :update? do + it_behaves_like 'permitted policy and same organisation', 'referentials.update', archived_and_finalised: true + end - # permissions :clone? do - # it_behaves_like 'permitted policy', 'referentials.create', archived_and_finalised: true - # end + # + # Custom Permissions + # ------------------ - permissions :archive? do + # permissions :clone? do + # it_behaves_like 'permitted policy', 'referentials.create', archived_and_finalised: true + # end - context 'permission present →' do - before do - add_permissions('referentials.update', to_user: user) - end + permissions :archive? do - context 'same organisation →' do + context 'permission present →' do before do - user.organisation_id = referential.organisation_id - end - it "allows a user with the same organisation" do - expect_it.to permit(user_context, record) + add_permissions('referentials.update', to_user: user) end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'does remove permission for archived referentials' do - expect_it.not_to permit(user_context, record) - end - end - end - context 'different organisations →' do - it "forbids a user with a different organisation" do - expect_it.not_to permit(user_context, record) + context 'same organisation →' do + before do + user.organisation_id = referential.organisation_id + end + it "allows a user with the same organisation" do + expect_it.to permit(user_context, record) + end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'does remove permission for archived referentials' do + expect_it.not_to permit(user_context, record) + end + end end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'forbids for archived referentials' do + context 'different organisations →' do + it "forbids a user with a different organisation" do expect_it.not_to permit(user_context, record) end - end - end - end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'forbids for archived referentials' do + expect_it.not_to permit(user_context, record) + end + end - context 'permission absent →' do - context 'same organisation →' do - before do - user.organisation_id = referential.organisation_id end - it "forbids a user with the same organisation" do - expect_it.not_to permit(user_context, record) - end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'forbids for archived referentials' do + end + + context 'permission absent →' do + context 'same organisation →' do + before do + user.organisation_id = referential.organisation_id + end + it "forbids a user with the same organisation" do expect_it.not_to permit(user_context, record) end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'forbids for archived referentials' do + expect_it.not_to permit(user_context, record) + end + end end end end - end - permissions :unarchive? do - - context 'permission present →' do - before do - add_permissions('referentials.update', to_user: user) - end + permissions :unarchive? do - context 'same organisation →' do + context 'permission present →' do before do - user.organisation_id = referential.organisation_id - end - it "forbids a user with the same organisation" do - expect_it.not_to permit(user_context, record) - end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'adds permission for archived referentials' do - expect_it.to permit(user_context, record) - end + add_permissions('referentials.update', to_user: user) end - end - context 'different organisations →' do - it "forbids a user with a different organisation" do - expect_it.not_to permit(user_context, record) + context 'same organisation →' do + before do + user.organisation_id = referential.organisation_id + end + it "forbids a user with the same organisation" do + expect_it.not_to permit(user_context, record) + end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'adds permission for archived referentials' do + expect_it.to permit(user_context, record) + end + end end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'still forbids for archived referentials' do + context 'different organisations →' do + it "forbids a user with a different organisation" do expect_it.not_to permit(user_context, record) end - end - end - end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'still forbids for archived referentials' do + expect_it.not_to permit(user_context, record) + end + end - context 'permission absent →' do - context 'same organisation →' do - before do - user.organisation_id = referential.organisation_id end - it "forbids a user with a different rganisation" do - expect_it.not_to permit(user_context, record) - end - describe "archived" do - let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago } - it 'still forbids for archived referentials' do + end + + context 'permission absent →' do + context 'same organisation →' do + before do + user.organisation_id = referential.organisation_id + end + it "forbids a user with a different rganisation" do expect_it.not_to permit(user_context, record) end + describe "archived" do + let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago, ready: true } + it 'still forbids for archived referentials' do + expect_it.not_to permit(user_context, record) + end + end end end end diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb index 13f537c6d..8592ef29f 100644 --- a/spec/support/pundit/shared_examples.rb +++ b/spec/support/pundit/shared_examples.rb @@ -111,7 +111,10 @@ RSpec.shared_examples 'permitted policy and same organisation' do if archived_and_finalised it 'removes the permission for archived referentials' do user.organisation_id = referential.organisation_id - referential.archived_at = 42.seconds.ago + record.archived_at = 42.seconds.ago + record.ready = true + expect(record).to be_archived + expect(record).to be_referential_read_only expect_it.not_to permit(user_context, record) end diff --git a/spec/views/referentials/show.html.erb_spec.rb b/spec/views/referentials/show.html.erb_spec.rb index 82328cb8e..fb468d0ee 100644 --- a/spec/views/referentials/show.html.erb_spec.rb +++ b/spec/views/referentials/show.html.erb_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe "referentials/show", type: :view do - let!(:referential) do + let(:referential) do referential = create(:workbench_referential) assign :referential, referential.decorate(context: { current_organisation: referential.organisation @@ -15,6 +15,8 @@ describe "referentials/show", type: :view do let(:readonly){ false } before :each do + allow(referential.object).to receive(:referential_read_only?){ readonly } + assign :reflines, [] allow(view).to receive(:current_organisation).and_return(current_organisation) allow(view).to receive(:current_user).and_return(current_user) @@ -25,7 +27,6 @@ describe "referentials/show", type: :view do controller.request.path_parameters[:id] = referential.id allow(view).to receive(:params).and_return({action: :show}) - allow(referential).to receive(:referential_read_only?){ readonly } render template: "referentials/show", layout: "layouts/application" end @@ -41,6 +42,7 @@ describe "referentials/show", type: :view do context "with a readonly referential" do let(:readonly){ true } it "should not present edit button" do + p "HERE" expect(rendered).to_not have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") end end |
