diff options
| author | Xinhui | 2017-05-29 16:29:32 +0200 |
|---|---|---|
| committer | Xinhui | 2017-05-29 16:29:32 +0200 |
| commit | 6886441ce86bcd720b27cdd089567def5b9d771a (patch) | |
| tree | bda614b5fce39f51a794304615a7252ef86b012e /spec | |
| parent | 4f5cc7d35777f3b4bfa1c63c1223c679f713424e (diff) | |
| parent | 54bf18da9a74295c327e39c659ef3a28719a2631 (diff) | |
| download | chouette-core-6886441ce86bcd720b27cdd089567def5b9d771a.tar.bz2 | |
Merge branch 'master' into staging
Diffstat (limited to 'spec')
20 files changed, 326 insertions, 140 deletions
diff --git a/spec/features/line_footnotes_spec.rb b/spec/features/line_footnotes_spec.rb index 4d77cba41..6a359ad50 100644 --- a/spec/features/line_footnotes_spec.rb +++ b/spec/features/line_footnotes_spec.rb @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -require 'spec_helper' - describe 'Line Footnotes', type: :feature do login_user diff --git a/spec/features/time_tables_spec.rb b/spec/features/time_tables_spec.rb index 06ae9bac3..58a1dc98f 100644 --- a/spec/features/time_tables_spec.rb +++ b/spec/features/time_tables_spec.rb @@ -67,6 +67,10 @@ describe "TimeTables", :type => :feature do expect(page).to have_content(time_tables.first.comment) end + it 'should not show actualize link on time_tabl without calendar' do + expect(page).not_to have_content(I18n.t('time_tables.actions.actualize')) + end + # context 'user has permission to create time tables' do # it 'shows a create link for time tables' do # expect(page).to have_content(I18n.t('time_tables.actions.new')) diff --git a/spec/javascripts/time_table/reducers/metas_spec.js b/spec/javascripts/time_table/reducers/metas_spec.js index 79dbe1ea3..5ec7a0034 100644 --- a/spec/javascripts/time_table/reducers/metas_spec.js +++ b/spec/javascripts/time_table/reducers/metas_spec.js @@ -30,7 +30,7 @@ describe('metas reducer', () => { type: 'UPDATE_DAY_TYPES', dayTypes: arr }) - ).toEqual(Object.assign({}, state, {day_types: arr, calendar: {name: 'Aucun'}})) + ).toEqual(Object.assign({}, state, {day_types: arr, calendar: null})) }) it('should handle UPDATE_COMMENT', () => { diff --git a/spec/javascripts/vehicle_journeys/reducers/vehicle_journeys_spec.js b/spec/javascripts/vehicle_journeys/reducers/vehicle_journeys_spec.js index f805852c7..662c3d82f 100644 --- a/spec/javascripts/vehicle_journeys/reducers/vehicle_journeys_spec.js +++ b/spec/javascripts/vehicle_journeys/reducers/vehicle_journeys_spec.js @@ -88,7 +88,8 @@ describe('vehicleJourneys reducer', () => { dummy: true }] let fakeData = { - published_journey_name: {value: 'test'} + published_journey_name: {value: 'test'}, + published_journey_identifier: {value : ''} } let fakeSelectedJourneyPattern = {id: "1"} let fakeSelectedCompany = {name: "ALBATRANS"} @@ -104,6 +105,7 @@ describe('vehicleJourneys reducer', () => { journey_pattern: fakeSelectedJourneyPattern, company: fakeSelectedCompany, published_journey_name: 'test', + published_journey_identifier: '', objectid: '', footnotes: [], time_tables: [], diff --git a/spec/lib/time_duration_spec.rb b/spec/lib/time_duration_spec.rb new file mode 100644 index 000000000..1cba1f6d5 --- /dev/null +++ b/spec/lib/time_duration_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe TimeDuration do + describe ".exceeds_gap?" do + context "when duration is 4.hours" do + it "should return false if gap < 1.hour" do + t1 = Time.now + t2 = Time.now + 3.minutes + expect(TimeDuration.exceeds_gap?(4.hours, t1, t2)).to be_falsey + end + + it "should return true if gap > 4.hour" do + t1 = Time.now + t2 = Time.now + (4.hours + 1.minutes) + expect(TimeDuration.exceeds_gap?(4.hours, t1, t2)).to be_truthy + end + + it "returns true when `earlier` is later than `later`" do + earlier = Time.new(2000, 1, 1, 11, 0, 0, 0) + later = Time.new(2000, 1, 1, 12, 0, 0, 0) + + expect(TimeDuration.exceeds_gap?(4.hours, later, earlier)).to be true + end + + it "returns false when `earlier` == `later`" do + earlier = Time.new(2000, 1, 1, 1, 0, 0, 0) + later = earlier + + expect(TimeDuration.exceeds_gap?(4.hours, later, earlier)).to be false + end + end + end +end diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb index 505ca12be..3d45bd346 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -51,6 +51,15 @@ describe Chouette::TimeTable, :type => :model do }.to change {subject.periods.count}.by(-1) end + it 'should update caldendar association' do + subject.calendar = create(:calendar) + subject.save + state['calendar'] = nil + + subject.state_update state + expect(subject.reload.calendar).to eq(nil) + end + it 'should update color' do state['color'] = '#FFA070' subject.state_update state diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb deleted file mode 100644 index fbd544a28..000000000 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'spec_helper' -require 'pp' - -describe Chouette::VehicleJourneyAtStop, :type => :model do - let!(:vehicle_journey) { create(:vehicle_journey_odd)} - subject { vehicle_journey.vehicle_journey_at_stops.first } - - describe "#exceeds_gap?" do - it "should return false if gap < 1.hour" do - t1 = Time.now - t2 = Time.now + 3.minutes - expect(subject.exceeds_gap?(t1, t2)).to be_falsey - end - it "should return true if gap > 4.hour" do - t1 = Time.now - t2 = Time.now + (4.hours + 1.minutes) - expect(subject.exceeds_gap?(t1, t2)).to be_truthy - end - end - - describe "#increasing_times_validate" do - let(:vjas1){ vehicle_journey.vehicle_journey_at_stops[0]} - let(:vjas2){ vehicle_journey.vehicle_journey_at_stops[1]} - context "when vjas#arrival_time exceeds gap" do - it "should add errors on arrival_time" do - vjas1.arrival_time = vjas2.arrival_time - 5.hour - expect(vjas2.increasing_times_validate(vjas1)).to be_falsey - expect(vjas2.errors).not_to be_empty - expect(vjas2.errors[:arrival_time]).not_to be_blank - end - end - context "when vjas#departure_time exceeds gap" do - it "should add errors on departure_time" do - vjas1.departure_time = vjas2.departure_time - 5.hour - expect(vjas2.increasing_times_validate(vjas1)).to be_falsey - expect(vjas2.errors).not_to be_empty - expect(vjas2.errors[:departure_time]).not_to be_blank - end - end - context "when vjas does'nt exceed gap" do - it "should not add errors" do - expect(vjas2.increasing_times_validate(vjas1)).to be_truthy - expect(vjas2.errors).to be_empty - end - end - end -end diff --git a/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb new file mode 100644 index 000000000..c30e0dbd8 --- /dev/null +++ b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do + subject { create(:vehicle_journey_odd) } + + describe "#validate" do + before(:each) do + subject.vehicle_journey_at_stops[0].departure_time = + subject.vehicle_journey_at_stops[1].departure_time - 5.hour + subject.vehicle_journey_at_stops[0].arrival_time = + subject.vehicle_journey_at_stops[0].departure_time + subject.vehicle_journey_at_stops[1].arrival_time = + subject.vehicle_journey_at_stops[1].departure_time + end + + it "should make instance invalid if departure time exceeds gap" do + subject.validate + + expect( + subject.vehicle_journey_at_stops[1].errors[:departure_time] + ).not_to be_blank + expect(subject).not_to be_valid + end + end + + describe ".validate_at_stop_times_must_increase" do + let!(:vehicle_journey) { create(:vehicle_journey_odd) } + subject { vehicle_journey.vehicle_journey_at_stops.first } + + let(:vjas1) { vehicle_journey.vehicle_journey_at_stops[0] } + let(:vjas2) { vehicle_journey.vehicle_journey_at_stops[1] } + + context "when vjas#arrival_time exceeds gap" do + it "should add errors on arrival_time" do + vjas1.arrival_time = vjas2.arrival_time - 5.hour + expect( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .validate_at_stop_times_must_increase(vjas2, vjas1) + ).to be_falsey + expect(vjas2.errors).not_to be_empty + expect(vjas2.errors[:arrival_time]).not_to be_blank + end + end + + context "when vjas#departure_time exceeds gap" do + it "should add errors on departure_time" do + vjas1.departure_time = vjas2.departure_time - 5.hour + expect( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .validate_at_stop_times_must_increase(vjas2, vjas1) + ).to be_falsey + expect(vjas2.errors).not_to be_empty + expect(vjas2.errors[:departure_time]).not_to be_blank + end + end + + context "when vjas doesn't exceed gap" do + it "should not add errors" do + expect( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .validate_at_stop_times_must_increase(vjas2, vjas1) + ).to be_truthy + expect(vjas2.errors).to be_empty + end + end + end +end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index ed76c278c..4a108d7c0 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -466,18 +466,6 @@ describe Chouette::VehicleJourney, :type => :model do end context "when following departure times exceeds gap" do - describe "#increasing_times" do - before(:each) do - subject.vehicle_journey_at_stops[0].departure_time = subject.vehicle_journey_at_stops[1].departure_time - 5.hour - subject.vehicle_journey_at_stops[0].arrival_time = subject.vehicle_journey_at_stops[0].departure_time - subject.vehicle_journey_at_stops[1].arrival_time = subject.vehicle_journey_at_stops[1].departure_time - end - it "should make instance invalid" do - subject.increasing_times - expect(subject.vehicle_journey_at_stops[1].errors[:departure_time]).not_to be_blank - expect(subject).not_to be_valid - end - end describe "#update_attributes" do let!(:params){ {"vehicle_journey_at_stops_attributes" => { "0"=>{"id" => subject.vehicle_journey_at_stops[0].id ,"arrival_time" => 1.minutes.ago,"departure_time" => 1.minutes.ago}, diff --git a/spec/policies/application_policy_spec.rb b/spec/policies/application_policy_spec.rb index d7e8e5e27..a7234461e 100644 --- a/spec/policies/application_policy_spec.rb +++ b/spec/policies/application_policy_spec.rb @@ -1,11 +1,6 @@ RSpec.describe ApplicationPolicy, type: :policy do - subject { described_class } - permissions :organisation_match? do - let( :user_context ) { create_user_context(user: user, referential: referential) } - let( :referentail ) { create :referential } - let( :user ) { create :user } it "denies a user with a different organisation" do expect_it.not_to permit(user_context, referential) @@ -16,4 +11,5 @@ RSpec.describe ApplicationPolicy, type: :policy do expect_it.to permit(user_context, referential) end end + end diff --git a/spec/policies/boiv_policy_spec.rb b/spec/policies/boiv_policy_spec.rb new file mode 100644 index 000000000..bf09cdcd9 --- /dev/null +++ b/spec/policies/boiv_policy_spec.rb @@ -0,0 +1,15 @@ +RSpec.describe BoivPolicy, type: :policy do + + permissions :index? do + it_behaves_like 'permitted policy and same organisation', 'boiv:read-offer' + end + + permissions :boiv_read_offer? do + it_behaves_like 'permitted policy and same organisation', 'boiv:read-offer' + end + + permissions :show? do + it_behaves_like 'permitted policy and same organisation', 'boiv:read-offer' + end + +end diff --git a/spec/policies/line_policy_spec.rb b/spec/policies/line_policy_spec.rb new file mode 100644 index 000000000..ead5918aa --- /dev/null +++ b/spec/policies/line_policy_spec.rb @@ -0,0 +1,18 @@ +RSpec.describe LinePolicy, type: :policy do + + %w{create destroy edit}.each do | permission | + footnote_permission = "#{permission}_footnote" + permissions "#{footnote_permission}?".to_sym do + it_behaves_like 'permitted policy', "footnotes.#{permission}", archived: true + end + end + + permissions :new_footnote? do + it_behaves_like 'permitted policy', 'footnotes.create', archived: true + end + + permissions :update_footnote? do + it_behaves_like 'permitted policy', 'footnotes.edit', archived: true + end + +end diff --git a/spec/policies/route_policy_spec.rb b/spec/policies/route_policy_spec.rb new file mode 100644 index 000000000..baf14c9fc --- /dev/null +++ b/spec/policies/route_policy_spec.rb @@ -0,0 +1,22 @@ +RSpec.describe RoutePolicy, type: :policy do + + permissions :create? do + it_behaves_like 'permitted policy', 'routes.create', archived: true + end + + permissions :destroy? do + it_behaves_like 'permitted policy and same organisation', 'routes.destroy', archived: true + end + + permissions :edit? do + it_behaves_like 'permitted policy and same organisation', 'routes.edit', archived: true + end + + permissions :new? do + it_behaves_like 'permitted policy', 'routes.create', archived: true + end + + permissions :update? do + it_behaves_like 'permitted policy and same organisation', 'routes.edit', archived: true + end +end diff --git a/spec/policies/routing_constraint_zone_policy_spec.rb b/spec/policies/routing_constraint_zone_policy_spec.rb new file mode 100644 index 000000000..4b0f2cafe --- /dev/null +++ b/spec/policies/routing_constraint_zone_policy_spec.rb @@ -0,0 +1,22 @@ +RSpec.describe RoutingConstraintZonePolicy, type: :policy do + + permissions :create? do + it_behaves_like 'permitted policy', 'routing_constraint_zones.create', archived: true + end + + permissions :destroy? do + it_behaves_like 'permitted policy and same organisation', 'routing_constraint_zones.destroy', archived: true + end + + permissions :edit? do + it_behaves_like 'permitted policy and same organisation', 'routing_constraint_zones.edit', archived: true + end + + permissions :new? do + it_behaves_like 'permitted policy', 'routing_constraint_zones.create', archived: true + end + + permissions :update? do + it_behaves_like 'permitted policy and same organisation', 'routing_constraint_zones.edit', archived: true + end +end diff --git a/spec/policies/time_table_policy_spec.rb b/spec/policies/time_table_policy_spec.rb index 63bd316e4..1283a9fcf 100644 --- a/spec/policies/time_table_policy_spec.rb +++ b/spec/policies/time_table_policy_spec.rb @@ -1,26 +1,18 @@ RSpec.describe TimeTablePolicy, type: :policy do permissions :duplicate? do - context "user of a different organisation" do - it "is denied" do - expect_it.not_to permit(user_context, referential) - end - it "even if she has the time_tables.create permission" do - add_permissions 'time_tables.create', for_user: user - expect_it.not_to permit(user_context, referential) - end - end - context "user of the same organisation" do - before do - user.update_attribute :organisation, referential.organisation - end - it "is denied" do - expect_it.not_to permit(user_context, referential) - end - it "unless she has the time_tables.create permission" do - add_permissions 'time_tables.create', for_user: user - expect_it.to permit(user_context, referential) - end + it_behaves_like 'permitted policy and same organisation', 'time_tables.create', archived: true + end + + %w{destroy edit}.each do | permission | + permissions "#{permission}?".to_sym do + it_behaves_like 'permitted policy and same organisation', "time_tables.#{permission}", archived: true end end + + permissions :create? do + it_behaves_like 'permitted policy', 'time_tables.create', archived: true + end + + end diff --git a/spec/support/pundit.rb b/spec/support/pundit.rb deleted file mode 100644 index d818ce754..000000000 --- a/spec/support/pundit.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'pundit/rspec' - -module Support - module ApplicationPolicy - def add_permissions(*permissions, for_user:) - for_user.permissions ||= [] - for_user.permissions += permissions.flatten - end - - def create_user_context(user:, referential:) - OpenStruct.new(user: user, context: {referential: referential}) - end - end - - module ApplicationPolicyMacros - def self.extended into - into.module_eval do - subject { described_class } - let( :user_context ) { create_user_context(user: user, referential: referential) } - let( :referentail ) { create :referential } - let( :user ) { create :user } - end - end - - end -end - -RSpec.configure do | c | - c.include Support::ApplicationPolicy, type: :policy - c.extend Support::ApplicationPolicyMacros, type: :policy -end diff --git a/spec/support/pundit/policies.rb b/spec/support/pundit/policies.rb new file mode 100644 index 000000000..637a2a528 --- /dev/null +++ b/spec/support/pundit/policies.rb @@ -0,0 +1,37 @@ +require 'pundit/rspec' + +module Support + module Pundit + module Policies + def add_permissions(*permissions, for_user:) + for_user.permissions ||= [] + for_user.permissions += permissions.flatten + end + + def create_user_context(user:, referential:) + OpenStruct.new(user: user, context: {referential: referential}) + end + + def add_permissions(*permissions, for_user:) + for_user.permissions ||= [] + for_user.permissions += permissions.flatten + end + end + + module PoliciesMacros + def self.extended into + into.module_eval do + subject { described_class } + let( :user_context ) { create_user_context(user: user, referential: referential) } + let( :referentail ) { create :referential } + let( :user ) { create :user } + end + end + end + end +end + +RSpec.configure do | c | + c.include Support::Pundit::Policies, type: :policy + c.extend Support::Pundit::PoliciesMacros, type: :policy +end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb new file mode 100644 index 000000000..b8434cac0 --- /dev/null +++ b/spec/support/pundit/pundit_view_policy.rb @@ -0,0 +1,22 @@ +module Pundit + module PunditViewPolicy + extend ActiveSupport::Concern + + 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 + end + end + end + end +end + +RSpec.configure do |config| + config.include Pundit::PunditViewPolicy, type: :view +end diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb new file mode 100644 index 000000000..4d14c46da --- /dev/null +++ b/spec/support/pundit/shared_examples.rb @@ -0,0 +1,60 @@ +RSpec.shared_examples 'permitted policy and same organisation' do + | permission, archived: false| + + context 'permission absent → ' do + it "denies a user with a different organisation" do + expect_it.not_to permit(user_context, referential) + end + it 'and also a user with the same organisation' do + user.update_attribute :organisation, referential.organisation + expect_it.not_to permit(user_context, referential) + end + end + + context 'permission present → ' do + before do + add_permissions(permission, for_user: user) + end + + it 'denies a user with a different organisation' do + expect_it.not_to permit(user_context, referential) + end + + it 'but allows it for a user with the same organisation' do + user.update_attribute :organisation, referential.organisation + expect_it.to permit(user_context, referential) + end + + if archived + it 'removes the permission for archived referentials' do + user.update_attribute :organisation, referential.organisation + referential.update_attribute :archived_at, 42.seconds.ago + expect_it.not_to permit(user_context, referential) + end + end + end +end + +RSpec.shared_examples 'permitted policy' do + | permission, archived: false| + context 'permission absent → ' do + it "denies a user with a different organisation" do + expect_it.not_to permit(user_context, referential) + end + end + context 'permission present → ' do + before do + add_permissions(permission, for_user: user) + end + it 'allows a user with a different organisation' do + expect_it.to permit(user_context, referential) + end + + if archived + it 'removes the permission for archived referentials' do + referential.update_attribute :archived_at, 42.seconds.ago + expect_it.not_to permit(user_context, referential) + end + end + end +end diff --git a/spec/support/pundit_view_policy.rb b/spec/support/pundit_view_policy.rb deleted file mode 100644 index 2945b9aac..000000000 --- a/spec/support/pundit_view_policy.rb +++ /dev/null @@ -1,20 +0,0 @@ -module PunditViewPolicy - extend ActiveSupport::Concern - - 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 - end - end - end -end - -RSpec.configure do |config| - config.include PunditViewPolicy, type: :view -end |
