aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/concerns/feature_checker_spec.rb37
-rw-r--r--spec/controllers/referential_vehicle_journeys_controller_spec.rb43
-rw-r--r--spec/decorators/referential_decorator_spec.rb1
-rw-r--r--spec/decorators/stop_area_decorator_spec.rb25
-rw-r--r--spec/factories/chouette_purchase_windows.rb12
-rw-r--r--spec/features/purchase_windows_permission_spec.rb55
-rw-r--r--spec/models/chouette/purchase_window_spec.rb27
-rw-r--r--spec/models/chouette/stop_area_spec.rb25
-rw-r--r--spec/models/organisation_spec.rb22
-rw-r--r--spec/policies/purchase_window_policy_spec.rb15
-rw-r--r--spec/support/decorator_helpers.rb5
-rw-r--r--spec/views/stop_areas/edit.html.erb_spec.rb5
-rw-r--r--spec/views/stop_areas/new.html.erb_spec.rb4
13 files changed, 270 insertions, 6 deletions
diff --git a/spec/controllers/concerns/feature_checker_spec.rb b/spec/controllers/concerns/feature_checker_spec.rb
new file mode 100644
index 000000000..1d289bb15
--- /dev/null
+++ b/spec/controllers/concerns/feature_checker_spec.rb
@@ -0,0 +1,37 @@
+require "rails_helper"
+
+RSpec.describe "FeatureChecker", type: :controller do
+ login_user
+
+ controller do
+ include FeatureChecker
+ requires_feature :test, only: :protected
+
+ def protected; render text: "protected"; end
+ def not_protected; render text: "not protected"; end
+
+ def current_organisation
+ @organisation ||= Organisation.new
+ end
+ end
+
+ before do
+ routes.draw do
+ get "protected" => "anonymous#protected"
+ get "not_protected" => "anonymous#not_protected"
+ end
+ end
+
+ it "refuse access when organisation does not have the feature" do
+ expect{ get(:protected) }.to raise_error(FeatureChecker::NotAuthorizedError)
+ end
+
+ it "accept access on unprotected action" do
+ get :not_protected
+ end
+
+ it 'accept access when organisation has feature' do
+ controller.current_organisation.features << "test"
+ get :protected
+ end
+end
diff --git a/spec/controllers/referential_vehicle_journeys_controller_spec.rb b/spec/controllers/referential_vehicle_journeys_controller_spec.rb
new file mode 100644
index 000000000..842a6665e
--- /dev/null
+++ b/spec/controllers/referential_vehicle_journeys_controller_spec.rb
@@ -0,0 +1,43 @@
+require "rails_helper"
+
+RSpec.describe ReferentialVehicleJourneysController, type: :controller do
+ login_user
+
+ before do
+ @user.organisation.update features: %w{referential_vehicle_journeys}
+ end
+
+ describe 'GET #index' do
+ it 'should be successful' do
+ get :index, referential_id: referential
+ expect(response).to be_success
+ end
+
+ it "refuse access when organisation does not have the feature 'referential_vehicle_journeys'" do
+ @user.organisation.update features: []
+
+ expect do
+ get :index, referential_id: referential
+ end.to raise_error(FeatureChecker::NotAuthorizedError)
+ end
+
+ it 'define Ransack search (alias @q)' do
+ get :index, referential_id: referential
+ expect(assigns[:q]).to be_an_instance_of(Ransack::Search)
+ end
+
+ it 'define @vehicle_journeys collection' do
+ vehicle_journey = FactoryGirl.create :vehicle_journey
+ get :index, referential_id: referential
+ expect(assigns[:vehicle_journeys]).to include(vehicle_journey)
+ end
+
+ it 'paginage @vehicle_journeys collection' do
+ FactoryGirl.create :vehicle_journey
+
+ get :index, referential_id: referential
+ expect(assigns[:vehicle_journeys].total_entries).to be(1)
+ end
+ end
+
+end
diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb
index cbeaf2407..879ab7d4b 100644
--- a/spec/decorators/referential_decorator_spec.rb
+++ b/spec/decorators/referential_decorator_spec.rb
@@ -1,4 +1,5 @@
RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do
+ include Support::DecoratorHelpers
let( :object ){ build_stubbed :referential }
let( :referential ){ object }
diff --git a/spec/decorators/stop_area_decorator_spec.rb b/spec/decorators/stop_area_decorator_spec.rb
new file mode 100644
index 000000000..fd6aa207a
--- /dev/null
+++ b/spec/decorators/stop_area_decorator_spec.rb
@@ -0,0 +1,25 @@
+require "rails_helper"
+
+RSpec.describe StopAreaDecorator do
+
+ let(:stop_area) { Chouette::StopArea.new }
+ let(:decorator) { stop_area.decorate }
+
+ describe '#waiting_time_text' do
+ it "returns '-' when waiting_time is nil" do
+ stop_area.waiting_time = nil
+ expect(decorator.waiting_time_text).to eq('-')
+ end
+
+ it "returns '-' when waiting_time is zero" do
+ stop_area.waiting_time = 0
+ expect(decorator.waiting_time_text).to eq('-')
+ end
+
+ it "returns '120 minutes' when waiting_time is 120" do
+ stop_area.waiting_time = 120
+ expect(decorator.waiting_time_text).to eq('120 minutes')
+ end
+ end
+
+end
diff --git a/spec/factories/chouette_purchase_windows.rb b/spec/factories/chouette_purchase_windows.rb
new file mode 100644
index 000000000..2e2faf4d8
--- /dev/null
+++ b/spec/factories/chouette_purchase_windows.rb
@@ -0,0 +1,12 @@
+FactoryGirl.define do
+ factory :purchase_window, class: Chouette::PurchaseWindow do
+ sequence(:name) { |n| "Purchase Window #{n}" }
+ sequence(:objectid) { |n| "organisation:PurchaseWindow:#{n}:LOC" }
+ date_ranges { [generate(:periods)] }
+ end
+
+ sequence :periods do |n|
+ date = Date.today + 2*n
+ date..(date+1)
+ end
+end
diff --git a/spec/features/purchase_windows_permission_spec.rb b/spec/features/purchase_windows_permission_spec.rb
new file mode 100644
index 000000000..e74fb5c17
--- /dev/null
+++ b/spec/features/purchase_windows_permission_spec.rb
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+require 'spec_helper'
+
+describe "PurchaseWindows", :type => :feature do
+ login_user
+
+ let(:purchase_window) { create :purchase_window, referential: first_referential}
+
+ describe 'permissions' do
+ before do
+ allow_any_instance_of(PurchaseWindowPolicy).to receive(:create?).and_return permission
+ allow_any_instance_of(PurchaseWindowPolicy).to receive(:destroy?).and_return permission
+ allow_any_instance_of(PurchaseWindowPolicy).to receive(:update?).and_return permission
+ visit path
+ end
+
+ context 'on show view' do
+ let( :path ){ referential_purchase_window_path(first_referential, purchase_window) }
+
+ context 'if present → ' do
+ let( :permission ){ true }
+ it 'view shows the corresponding buttons' do
+ expect(page).to have_content(I18n.t('purchase_windows.actions.edit'))
+ expect(page).to have_content(I18n.t('purchase_windows.actions.destroy'))
+ end
+ end
+
+ context 'if absent → ' do
+ let( :permission ){ false }
+ it 'view does not show the corresponding buttons' do
+ expect(page).not_to have_content(I18n.t('purchase_windows.actions.edit'))
+ expect(page).not_to have_content(I18n.t('purchase_windows.actions.destroy'))
+ end
+ end
+ end
+
+ context 'on index view' do
+ let( :path ){ referential_purchase_windows_path(first_referential) }
+
+ context 'if present → ' do
+ let( :permission ){ true }
+ it 'index shows an edit button' do
+ expect(page).to have_content(I18n.t('purchase_windows.actions.new'))
+ end
+ end
+
+ context 'if absent → ' do
+ let( :permission ){ false }
+ it 'index does not show any edit button' do
+ expect(page).not_to have_content(I18n.t('purchase_windows.actions.new'))
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/chouette/purchase_window_spec.rb b/spec/models/chouette/purchase_window_spec.rb
new file mode 100644
index 000000000..702a44eeb
--- /dev/null
+++ b/spec/models/chouette/purchase_window_spec.rb
@@ -0,0 +1,27 @@
+RSpec.describe Chouette::PurchaseWindow, :type => :model do
+ let(:referential) {create(:referential)}
+ subject { create(:purchase_window, referential: referential) }
+
+ it { should belong_to(:referential) }
+ it { is_expected.to validate_presence_of(:name) }
+
+ describe 'validations' do
+ it 'validates and date_ranges do not overlap' do
+ expect(build(:purchase_window, referential: referential,date_ranges: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow])).to_not be_valid
+ # expect(build(periods: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow ])).to_not be_valid
+ end
+ end
+
+ describe 'before_validation' do
+ let(:purchase_window) { build(:purchase_window, referential: referential, date_ranges: []) }
+
+ it 'shoud fill date_ranges with date ranges' do
+ expected_range = Date.today..Date.tomorrow
+ purchase_window.date_ranges << expected_range
+ purchase_window.valid?
+
+ expect(purchase_window.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range])
+ end
+ end
+
+end
diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb
index c6aeafaf8..bec8c0868 100644
--- a/spec/models/chouette/stop_area_spec.rb
+++ b/spec/models/chouette/stop_area_spec.rb
@@ -426,5 +426,30 @@ describe Chouette::StopArea, :type => :model do
# end
# end
+ describe '#waiting_time' do
+
+ let(:stop_area) { FactoryGirl.build :stop_area }
+
+ it 'can be nil' do
+ stop_area.waiting_time = nil
+ expect(stop_area).to be_valid
+ end
+
+ it 'can be zero' do
+ stop_area.waiting_time = 0
+ expect(stop_area).to be_valid
+ end
+
+ it 'can be positive' do
+ stop_area.waiting_time = 120
+ expect(stop_area).to be_valid
+ end
+
+ it "can't be negative" do
+ stop_area.waiting_time = -1
+ expect(stop_area).to_not be_valid
+ end
+
+ end
end
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb
index 359417d88..595b08058 100644
--- a/spec/models/organisation_spec.rb
+++ b/spec/models/organisation_spec.rb
@@ -62,4 +62,26 @@ describe Organisation, :type => :model do
expect{Organisation.portail_sync}.to change{ Organisation.count }.by(4)
end
end
+
+ describe "#has_feature?" do
+
+ let(:organisation) { Organisation.new }
+
+ it 'return false if Organisation features is nil' do
+ organisation.features = nil
+ expect(organisation.has_feature?(:dummy)).to be_falsy
+ end
+
+ it 'return true if Organisation features contains given feature' do
+ organisation.features = %w{present}
+ expect(organisation.has_feature?(:present)).to be_truthy
+ end
+
+ it "return false if Organisation features doesn't contains given feature" do
+ organisation.features = %w{other}
+ expect(organisation.has_feature?(:absent)).to be_falsy
+ end
+
+ end
+
end
diff --git a/spec/policies/purchase_window_policy_spec.rb b/spec/policies/purchase_window_policy_spec.rb
new file mode 100644
index 000000000..f078bf288
--- /dev/null
+++ b/spec/policies/purchase_window_policy_spec.rb
@@ -0,0 +1,15 @@
+RSpec.describe PurchaseWindowPolicy, type: :policy do
+
+ let( :record ){ build_stubbed :purchase_window }
+ before { stub_policy_scope(record) }
+
+ permissions :create? do
+ it_behaves_like 'permitted policy and same organisation', "purchase_windows.create", archived: true
+ end
+ permissions :destroy? do
+ it_behaves_like 'permitted policy and same organisation', "purchase_windows.destroy", archived: true
+ end
+ permissions :update? do
+ it_behaves_like 'permitted policy and same organisation', "purchase_windows.update", archived: true
+ end
+end
diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb
index ffedd479b..9d450deb1 100644
--- a/spec/support/decorator_helpers.rb
+++ b/spec/support/decorator_helpers.rb
@@ -1,5 +1,4 @@
module Support
-
module DecoratorHelpers
def self.included(into)
into.instance_eval do
@@ -21,7 +20,3 @@ module Support
end
end
end
-
-RSpec.configure do | c |
- c.include Support::DecoratorHelpers, type: :decorator
-end
diff --git a/spec/views/stop_areas/edit.html.erb_spec.rb b/spec/views/stop_areas/edit.html.erb_spec.rb
index 5105bff4b..bfbb0bb55 100644
--- a/spec/views/stop_areas/edit.html.erb_spec.rb
+++ b/spec/views/stop_areas/edit.html.erb_spec.rb
@@ -6,6 +6,10 @@ describe "/stop_areas/edit", :type => :view do
let!(:stop_area) { assign(:stop_area, create(:stop_area)) }
let!(:map) { assign(:map, double(:to_html => '<div id="map"/>'.html_safe)) }
+ before do
+ allow(view).to receive(:has_feature?)
+ end
+
describe "form" do
it "should render input for name" do
render
@@ -13,6 +17,5 @@ describe "/stop_areas/edit", :type => :view do
with_tag "input[type=text][name='stop_area[name]'][value=?]", stop_area.name
end
end
-
end
end
diff --git a/spec/views/stop_areas/new.html.erb_spec.rb b/spec/views/stop_areas/new.html.erb_spec.rb
index 749782349..23f7387fa 100644
--- a/spec/views/stop_areas/new.html.erb_spec.rb
+++ b/spec/views/stop_areas/new.html.erb_spec.rb
@@ -5,6 +5,10 @@ describe "/stop_areas/new", :type => :view do
let!(:stop_area_referential) { assign :stop_area_referential, stop_area.stop_area_referential }
let!(:stop_area) { assign(:stop_area, build(:stop_area)) }
+ before do
+ allow(view).to receive(:has_feature?)
+ end
+
describe "form" do
it "should render input for name" do