aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuc Donnet2017-07-26 10:10:57 +0200
committerLuc Donnet2017-07-26 10:10:57 +0200
commita486dbe65187aade64c998e21dae139f8bccc6d1 (patch)
tree4f25b8c5c08b7acd784db197440d1cf27632e7be /spec
parenta692df1c4c336a971219f714bee01a156f4cf8e9 (diff)
parentcada0f02d732dce25492d7f7eb6d6232d56188dd (diff)
downloadchouette-core-a486dbe65187aade64c998e21dae139f8bccc6d1.tar.bz2
Merge branch 'master' into 4161_StopPoints_RouteEdit
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/devise/cas_sessions_controller_spec.rb26
-rw-r--r--spec/lib/stif/permission_translator_spec.rb39
-rw-r--r--spec/models/chouette/line_spec.rb11
-rw-r--r--spec/models/chouette/stop_area_spec.rb19
-rw-r--r--spec/models/chouette/time_table_spec.rb39
-rw-r--r--spec/models/time_table_combination_spec.rb76
-rw-r--r--spec/models/user_spec.rb8
-rw-r--r--spec/support/permissions.rb1
8 files changed, 173 insertions, 46 deletions
diff --git a/spec/controllers/devise/cas_sessions_controller_spec.rb b/spec/controllers/devise/cas_sessions_controller_spec.rb
index 950d141fd..c82fd2cdb 100644
--- a/spec/controllers/devise/cas_sessions_controller_spec.rb
+++ b/spec/controllers/devise/cas_sessions_controller_spec.rb
@@ -1,25 +1,37 @@
RSpec.describe Devise::CasSessionsController, type: :controller do
- login_user
+ before do
+ @user = signed_in_user
+ allow_any_instance_of(Warden::Proxy).to receive(:authenticate).and_return @user
+ allow_any_instance_of(Warden::Proxy).to receive(:authenticate!).and_return @user
+ @request.env["devise.mapping"] = Devise.mappings[:user]
+ end
+
context 'login is correctly redirected' do
+ let( :signed_in_user ){ build_stubbed :user }
it 'to #service' do
get :new
- expect(response).to redirect_to(unauthenticated_root_path)
+ expect( response ).to be_redirect
+ expect( response.redirect_url ).to eq("http://stif-portail-dev.af83.priv/sessions/login?service=http%3A%2F%2Ftest.host%2Fusers%2Fservice")
end
end
- context 'user does not have any boiv:.+ permission' do
- xit 'cannot login and will be redirected to the login page, with a corresponding message' do
+ context 'user does not have permission sessions:create' do
+ let( :signed_in_user ){ build_stubbed :user }
+
+ it 'cannot login and will be redirected to the login page, with a corresponding message' do
get :service
expect(controller).to set_flash[:alert].to(%r{IBOO})
- expect(response).to redirect_to("http://stif-portail-dev.af83.priv/sessions/login?service=http%3A%2F%2Ftest.host%2Fusers%2Fservice")
+ expect(response).to redirect_to "http://stif-portail-dev.af83.priv/sessions/logout?service=http%3A%2F%2Ftest.host%2Fusers%2Fservice"
end
end
- context 'user does have a boiv:.+ permission' do
+ context 'user does have permission sessions:create' do
+ let( :signed_in_user ){ build_stubbed :allmighty_user }
+
it 'can login and will be redirected to the referentials page' do
- @user.update_attribute :permissions, (@user.permissions << 'boiv:UnameIt')
+ @user.permissions << 'sessions:create'
get :service
expect(response).to redirect_to(authenticated_root_path)
end
diff --git a/spec/lib/stif/permission_translator_spec.rb b/spec/lib/stif/permission_translator_spec.rb
index 3672c7937..1af21364c 100644
--- a/spec/lib/stif/permission_translator_spec.rb
+++ b/spec/lib/stif/permission_translator_spec.rb
@@ -1,10 +1,45 @@
RSpec.describe Stif::PermissionTranslator do
- context "SSO Permission boiv:read:offer →" do
+ context "No SSO Permissions" do
+ it { expect(described_class.translate([])).to be_empty }
+ end
+
+ context "SSO Permission boiv:read-offer →" do
it "sessions:create only" do
- expect( described_class.translate(%w{boiv:read:offer}) ).to eq(%w{sessions:create})
+ expect( described_class.translate(%w{boiv:read-offer}) ).to eq(%w{sessions:create})
+ end
+
+ end
+
+ context "SSO Permission boiv:edit-offer →" do
+
+ it "all permissions" do
+ expect( described_class.translate(%w{boiv:edit-offer}) ).to eq(Support::Permissions.all_permissions)
+ end
+
+ it "all permissions, no doubletons" do
+ expect( described_class.translate(%w{boiv:edit-offer boiv:read-offer}) ).to eq(Support::Permissions.all_permissions)
end
+ it "all permissions, input order agnostic" do
+ expect( described_class.translate(%w{boiv:read-offer boiv:edit-offer}) ).to eq(Support::Permissions.all_permissions)
+ end
+ end
+
+ context "SSO Permission ignores garbage (no injection) →" do
+ it "remains empty" do
+ expect( described_class.translate(%w{referentials.create}) ).to be_empty
+ end
+
+ it "remains at boiv:read-offer level" do
+ expect( described_class.translate(%w{referentials.create boiv:read-offer calendars.delete}) ).to eq(%w{sessions:create})
+ end
+
+ it "does not add garbage or doubletons for boiv:edit-offer level" do
+ expect(
+ described_class.translate(%w{xxx boiv:read-offer lines.delete boiv:edit-offer footnotes.update})
+ ).to eq(Support::Permissions.all_permissions)
+ end
end
end
diff --git a/spec/models/chouette/line_spec.rb b/spec/models/chouette/line_spec.rb
index 5a339e7ed..2e5882012 100644
--- a/spec/models/chouette/line_spec.rb
+++ b/spec/models/chouette/line_spec.rb
@@ -1,17 +1,12 @@
require 'spec_helper'
describe Chouette::Line, :type => :model do
-
subject { create(:line) }
- it { is_expected.to belong_to(:line_referential) }
+ it { should belong_to(:line_referential) }
# it { is_expected.to validate_presence_of :network }
# it { is_expected.to validate_presence_of :company }
-
- it { is_expected.to validate_presence_of :name }
-
- # it { should validate_presence_of :objectid }
- it { is_expected.to validate_uniqueness_of :objectid }
+ it { should validate_presence_of :name }
describe '#display_name' do
it 'should display local_id, number, name and company name' do
@@ -22,7 +17,7 @@ describe Chouette::Line, :type => :model do
describe '#objectid' do
subject { super().objectid }
- it { is_expected.to be_kind_of(Chouette::NetexObjectId) }
+ it { is_expected.to be_kind_of(Chouette::StifCodifligneObjectid) }
end
# it { should validate_numericality_of :objectversion }
diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb
index 1a2ff8ede..293ae5202 100644
--- a/spec/models/chouette/stop_area_spec.rb
+++ b/spec/models/chouette/stop_area_spec.rb
@@ -7,16 +7,15 @@ describe Chouette::StopArea, :type => :model do
let!(:commercial_stop_point) { create :stop_area, :area_type => "lda" }
let!(:stop_place) { create :stop_area, :area_type => "zdlp" }
- # Refs #1627
- # describe '#objectid' do
- # subject { super().objectid }
- # it { is_expected.to be_kind_of(Chouette::ObjectId) }
- # end
-
- it { is_expected.to belong_to(:stop_area_referential) }
- it { is_expected.to validate_presence_of :name }
- it { is_expected.to validate_numericality_of :latitude }
- it { is_expected.to validate_numericality_of :longitude }
+ describe '#objectid' do
+ subject { super().objectid }
+ it { should be_kind_of(Chouette::StifReflexObjectid) }
+ end
+
+ it { should belong_to(:stop_area_referential) }
+ it { should validate_presence_of :name }
+ it { should validate_numericality_of :latitude }
+ it { should validate_numericality_of :longitude }
# describe ".latitude" do
# it "should accept -90 value" do
diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb
index bd74a2d4c..304cb0184 100644
--- a/spec/models/chouette/time_table_spec.rb
+++ b/spec/models/chouette/time_table_spec.rb
@@ -14,6 +14,8 @@ describe Chouette::TimeTable, :type => :model do
describe "#merge! with time_table" do
let(:another_tt) { create(:time_table) }
let(:another_tt_periods_to_range) { another_tt.periods.map{|p| p.period_start..p.period_end } }
+ let(:dates) { another_tt.dates.map(&:date) }
+ let(:continuous_dates) { another_tt.continuous_dates.flatten.map(&:date) }
# Make sur we don't have overlapping periods or dates
before do
@@ -22,12 +24,19 @@ describe Chouette::TimeTable, :type => :model do
p.period_end = p.period_end + 1.year
end
another_tt.dates.each{| d| d.date = d.date + 1.year }
+ another_tt.save
end
it 'should merge dates' do
subject.dates.clear
subject.merge!(another_tt)
- expect(subject.dates.map(&:date)).to include(*another_tt.dates.map(&:date))
+ expect(subject.dates.map(&:date)).to match_array(dates - continuous_dates)
+ end
+
+ it 'should not merge continuous dates' do
+ subject.dates.clear
+ subject.merge!(another_tt)
+ expect(subject.dates.map(&:date)).not_to include(*continuous_dates)
end
it 'should merge periods' do
@@ -50,28 +59,42 @@ describe Chouette::TimeTable, :type => :model do
subject.merge!(another_tt)
expect(subject.dates.map(&:date)).to include(another_tt.dates.last.date)
end
+
+ it 'should remove date in_out false if other tt doesnt have them' do
+ subject.dates.create(in_out: false, date: Date.today + 5.day + 1.year)
+
+ expect {
+ subject.merge!(another_tt)
+ }.to change {subject.reload.excluded_days.count}.by(-1)
+ end
end
context "#merge! with calendar" do
let(:calendar) { create(:calendar, date_ranges: [Date.today + 1.year..Date.tomorrow + 1.year]) }
+ let(:another_tt) { calendar.convert_to_time_table }
+ let(:dates) { subject.dates.map(&:date) }
+ let(:continuous_dates) { subject.continuous_dates.flatten.map(&:date) }
it 'should merge calendar dates' do
subject.dates.clear
- subject.merge!(calendar.convert_to_time_table)
- expect(subject.dates.map(&:date)).to include(*calendar.dates)
+ subject.merge!(another_tt)
+ expect(subject.dates.map(&:date)).to match_array(dates - continuous_dates)
+ end
+
+ it 'should not merge calendar continuous dates' do
+ subject.dates.clear
+ subject.merge!(another_tt)
+ expect(subject.dates.map(&:date)).not_to include(*continuous_dates)
end
it 'should merge calendar periods with no periods in source' do
subject.periods.clear
- another_tt = calendar.convert_to_time_table
subject.merge!(another_tt)
expect(subject_periods_to_range).to include(*calendar.date_ranges)
end
it 'should add calendar periods with existing periods in source' do
- another_tt = calendar.convert_to_time_table
subject.merge!(another_tt)
-
expect(subject_periods_to_range).to include(*calendar.date_ranges)
end
end
@@ -1109,7 +1132,7 @@ end
- describe "#optimize_periods" do
+ describe "#optimize_overlapping_periods" do
before do
subject.periods.clear
subject.periods << Chouette::TimeTablePeriod.new(
@@ -1127,7 +1150,7 @@ end
subject.int_day_types = 4|8|16
end
it "should return 2 ordered periods" do
- periods = subject.optimize_periods
+ periods = subject.optimize_overlapping_periods
expect(periods.size).to eq(2)
expect(periods[0].period_start).to eq(Date.new(2014, 6, 1))
expect(periods[0].period_end).to eq(Date.new(2014, 6, 14))
diff --git a/spec/models/time_table_combination_spec.rb b/spec/models/time_table_combination_spec.rb
index 3e60fa444..ee934f50d 100644
--- a/spec/models/time_table_combination_spec.rb
+++ b/spec/models/time_table_combination_spec.rb
@@ -8,30 +8,93 @@ describe TimeTableCombination, :type => :model do
describe '#continuous_dates' do
it 'should group continuous dates' do
dates = source.dates.where(in_out: true)
- expect(source.continuous_dates[0].count).to eq(dates.count)
+ expect(source.continuous_dates.flatten.count).to eq(dates.count)
- # 6 more continuous date, 1 isolated date
+ # 6 more continuous dates, 2 isolated dates
(10..15).each do |n|
source.dates.create(date: Date.today + n.day, in_out: true)
end
- source.dates.create(date: Date.today + 1.year, in_out: true)
+
+ (1..2).each do |n|
+ source.dates.create(date: Date.today + n.day + 1.year, in_out: true)
+ end
+
expect(source.reload.continuous_dates[1].count).to eq(6)
- expect(source.reload.continuous_dates[2].count).to eq(1)
+ expect(source.reload.continuous_dates[2].count).to eq(2)
end
end
describe '#convert_continuous_dates_to_periods' do
it 'should convert continuous dates to periods' do
+ source.dates.clear
+
(10..12).each do |n|
source.dates.create(date: Date.today + n.day, in_out: true)
end
- source.dates.create(date: Date.today + 1.year, in_out: true)
+
+ (1..3).each do |n|
+ source.dates.create(date: Date.today + n.day + 1.year, in_out: true)
+ end
expect {
source.reload.convert_continuous_dates_to_periods
}.to change {source.periods.count}.by(2)
- expect(source.reload.dates.where(in_out: true).count).to eq(1)
+ expect(source.reload.dates.where(in_out: true).count).to eq(0)
+ end
+ end
+
+ describe '#continuous_periods' do
+ it 'should group continuous periods' do
+ source.periods.clear
+
+ start_date = Date.today + 1.year
+ end_date = start_date + 10
+
+ # 6 more continuous dates, 2 isolated dates
+ 0.upto(4) do |i|
+ source.periods.create(period_start: start_date, period_end: end_date)
+ start_date = end_date + 1
+ end_date = start_date + 10
+ end
+
+ expect(source.reload.continuous_periods.flatten.count).to eq(5)
+ end
+ end
+
+ describe '#convert_continuous_periods_into_one' do
+ it 'should convert continuous periods into one' do
+ source.periods.clear
+
+ start_date = Date.today + 1.year
+ end_date = start_date + 10
+
+ # 6 more continuous dates, 2 isolated dates
+ 0.upto(4) do |i|
+ source.periods.create(period_start: start_date, period_end: end_date)
+ start_date = end_date + 1
+ end_date = start_date + 10
+ end
+
+ expect {
+ source.reload.convert_continuous_periods_into_one
+ }.to change {source.periods.count}.by(-4)
+ end
+ end
+
+ describe '#optimize_continuous_dates_and_periods' do
+ it 'should update period if timetable has in_date just before or after ' do
+ source.dates.clear
+ source.periods.clear
+
+ source.periods.create(period_start: Date.today, period_end: Date.today + 10.day)
+ source.dates.create(date: Date.today - 1.day, in_out: true)
+
+ expect {
+ source.periods = source.optimize_continuous_dates_and_periods
+ }.to change {source.dates.count}.by(-1)
+
+ expect(source.reload.periods.first.period_start).to eq(Date.today - 1.day)
end
end
@@ -129,4 +192,3 @@ describe TimeTableCombination, :type => :model do
end
end
end
-
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 6f98e5ce7..3a9ae37e9 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1,6 +1,4 @@
-require 'spec_helper'
-
-describe User, :type => :model do
+RSpec.describe User, :type => :model do
# it { should validate_uniqueness_of :email }
# it { should validate_presence_of :name }
@@ -116,9 +114,11 @@ describe User, :type => :model do
end
context 'permissions' do
+ let( :all_permissions ){ Stif::PermissionTranslator.translate(%w{boiv:edit-offer}) }
+
it 'should give edit permissions to user if user has "edit offer" permission in portail' do
User.portail_sync
- expect(User.find_by(username: 'vlatka.pavisic').permissions).to include_all(User.edit_offer_permissions)
+ expect(User.find_by(username: 'vlatka.pavisic').permissions).to eq(all_permissions)
expect(User.find_by(username: 'pierre.vabre').permissions).to be_empty
end
end
diff --git a/spec/support/permissions.rb b/spec/support/permissions.rb
index a13010f65..fcf9ae9c4 100644
--- a/spec/support/permissions.rb
+++ b/spec/support/permissions.rb
@@ -15,6 +15,7 @@ module Support
%w[
access_points
connection_links
+ calendars
footnotes
journey_patterns
referentials