aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuc Donnet2017-07-10 23:35:07 +0200
committerLuc Donnet2017-07-10 23:35:07 +0200
commitcd813e7397269bcc2709178f9fd23fe2b986502a (patch)
treecd01cff2b848492004c81fa3be1b86637aabe48b /spec
parent272359b13aba516c8fa752d35adaac4981e7c5a6 (diff)
parent8fc8aca735de2002a4299125a8ba2edcc532a1f8 (diff)
downloadchouette-core-cd813e7397269bcc2709178f9fd23fe2b986502a.tar.bz2
Merge branch 'staging' of github.com:AF83/stif-boiv into staging
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/users.rb12
-rw-r--r--spec/features/calendars_permissions_spec.rb26
-rw-r--r--spec/features/connection_links_spec.rb5
-rw-r--r--spec/jobs/mailer_job_spec.rb5
-rw-r--r--spec/lib/stif/permission_translator_spec.rb10
-rw-r--r--spec/mailers/calendar_mailer_spec.rb2
-rw-r--r--spec/mailers/previews/calendar_mailer_preview.rb9
-rw-r--r--spec/policies/calendar_policy_spec.rb4
-rw-r--r--spec/support/devise.rb32
-rw-r--r--spec/support/permissions.rb28
-rw-r--r--spec/support/pundit/shared_examples.rb30
11 files changed, 109 insertions, 54 deletions
diff --git a/spec/factories/users.rb b/spec/factories/users.rb
index d532cbafc..8f620c3a1 100644
--- a/spec/factories/users.rb
+++ b/spec/factories/users.rb
@@ -1,12 +1,4 @@
-all_permissions = %w[
- footnotes
- journey_patterns
- referentials
- routes
- routing_constraint_zones
- time_tables
- vehicle_journeys
- ].product( %w{create destroy update} ).map{ |model_action| model_action.join('.') }
+require_relative '../support/permissions'
FactoryGirl.define do
factory :user do
@@ -17,7 +9,7 @@ FactoryGirl.define do
password "secret"
password_confirmation "secret"
factory :allmighty_user do
- permissions all_permissions
+ permissions Support::Permissions.all_permissions
end
end
end
diff --git a/spec/features/calendars_permissions_spec.rb b/spec/features/calendars_permissions_spec.rb
index 6eb0ea08e..9b47ab2bb 100644
--- a/spec/features/calendars_permissions_spec.rb
+++ b/spec/features/calendars_permissions_spec.rb
@@ -1,15 +1,13 @@
-# -*- coding: utf-8 -*-
-require 'spec_helper'
-
-describe 'Calendars', type: :feature do
+RSpec.describe 'Calendars', type: :feature do
login_user
let(:calendar) { create :calendar, organisation_id: 1 }
describe 'permissions' do
before do
- allow_any_instance_of(CalendarPolicy).to receive(:edit?).and_return permission
+ allow_any_instance_of(CalendarPolicy).to receive(:create?).and_return permission
allow_any_instance_of(CalendarPolicy).to receive(:destroy?).and_return permission
+ allow_any_instance_of(CalendarPolicy).to receive(:edit?).and_return permission
allow_any_instance_of(CalendarPolicy).to receive(:share?).and_return permission
visit path
end
@@ -51,5 +49,23 @@ describe 'Calendars', type: :feature do
end
end
end
+
+ context 'on index view' do
+ let( :path ){ calendars_path }
+
+ context 'if present → ' do
+ let( :permission ){ true }
+ it 'index shows an edit button' do
+ expect(page).to have_css('a.btn.btn-default', text: 'Créer')
+ end
+ end
+
+ context 'if absent → ' do
+ let( :permission ){ false }
+ it 'index does not show any edit button' do
+ expect(page).not_to have_css('a.btn.btn-default', text: 'Créer')
+ end
+ end
+ end
end
end
diff --git a/spec/features/connection_links_spec.rb b/spec/features/connection_links_spec.rb
index 524fbb89a..0325e6e1c 100644
--- a/spec/features/connection_links_spec.rb
+++ b/spec/features/connection_links_spec.rb
@@ -1,7 +1,4 @@
-# -*- coding: utf-8 -*-
-require 'spec_helper'
-
-describe "ConnectionLinks", :type => :feature do
+RSpec.describe "ConnectionLinks", :type => :feature do
login_user
let!(:connection_links) { Array.new(2) { create(:connection_link) } }
diff --git a/spec/jobs/mailer_job_spec.rb b/spec/jobs/mailer_job_spec.rb
deleted file mode 100644
index 363b8724a..000000000
--- a/spec/jobs/mailer_job_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe MailerJob, type: :job do
- pending "add some examples to (or delete) #{__FILE__}"
-end
diff --git a/spec/lib/stif/permission_translator_spec.rb b/spec/lib/stif/permission_translator_spec.rb
new file mode 100644
index 000000000..3672c7937
--- /dev/null
+++ b/spec/lib/stif/permission_translator_spec.rb
@@ -0,0 +1,10 @@
+RSpec.describe Stif::PermissionTranslator do
+
+ 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})
+ end
+
+ end
+end
diff --git a/spec/mailers/calendar_mailer_spec.rb b/spec/mailers/calendar_mailer_spec.rb
index d41092461..49cc3cce8 100644
--- a/spec/mailers/calendar_mailer_spec.rb
+++ b/spec/mailers/calendar_mailer_spec.rb
@@ -20,7 +20,7 @@ RSpec.describe CalendarMailer, type: :mailer do
end
it 'should have correct body' do
- key = I18n.t("mailers.calendar_mailer.#{type}.body")
+ key = I18n.t("mailers.calendar_mailer.#{type}.body", cal_name: calendar.name, cal_index_url: calendars_url)
expect(email).to have_body_text /#{key}/
end
end
diff --git a/spec/mailers/previews/calendar_mailer_preview.rb b/spec/mailers/previews/calendar_mailer_preview.rb
index 572c6c667..5fa108924 100644
--- a/spec/mailers/previews/calendar_mailer_preview.rb
+++ b/spec/mailers/previews/calendar_mailer_preview.rb
@@ -1,4 +1,13 @@
# Preview all emails at http://localhost:3000/rails/mailers/calendar_mailer
class CalendarMailerPreview < ActionMailer::Preview
+ def created
+ cal = Calendar.new(name: 'test calendar', shared: true)
+ CalendarMailer.created(cal, User.take)
+ end
+
+ def updated
+ cal = Calendar.new(name: 'test calendar', shared: true)
+ CalendarMailer.updated(cal, User.take)
+ end
end
diff --git a/spec/policies/calendar_policy_spec.rb b/spec/policies/calendar_policy_spec.rb
index 57f771c54..294be8198 100644
--- a/spec/policies/calendar_policy_spec.rb
+++ b/spec/policies/calendar_policy_spec.rb
@@ -5,7 +5,7 @@ RSpec.describe CalendarPolicy, type: :policy do
permissions :create? do
- it_behaves_like 'permitted policy and same organisation', 'calendars.create', archived: true
+ it_behaves_like 'permitted policy', 'calendars.create', archived: true
end
permissions :destroy? do
it_behaves_like 'permitted policy and same organisation', 'calendars.destroy', archived: true
@@ -14,7 +14,7 @@ RSpec.describe CalendarPolicy, type: :policy do
it_behaves_like 'permitted policy and same organisation', 'calendars.update', archived: true
end
permissions :new? do
- it_behaves_like 'permitted policy and same organisation', 'calendars.create', archived: true
+ it_behaves_like 'permitted policy', 'calendars.create', archived: true
end
permissions :update? do
it_behaves_like 'permitted policy and same organisation', 'calendars.update', archived: true
diff --git a/spec/support/devise.rb b/spec/support/devise.rb
index 28703c072..46249fef2 100644
--- a/spec/support/devise.rb
+++ b/spec/support/devise.rb
@@ -3,13 +3,11 @@ module DeviseRequestHelper
def login_user
organisation = Organisation.where(:code => "first").first_or_create(attributes_for(:organisation))
- @user ||= create(:user, :organisation => organisation,
- :permissions => ['routes.create', 'routes.update', 'routes.destroy', 'journey_patterns.create', 'journey_patterns.update', 'journey_patterns.destroy',
- 'vehicle_journeys.create', 'vehicle_journeys.update', 'vehicle_journeys.destroy', 'time_tables.create', 'time_tables.update', 'time_tables.destroy',
- 'footnotes.update', 'footnotes.create', 'footnotes.destroy', 'routing_constraint_zones.create', 'routing_constraint_zones.update', 'routing_constraint_zones.destroy',
- 'access_points.create', 'access_points.update', 'access_points.destroy', 'access_links.create', 'access_links.update', 'access_links.destroy',
- 'connection_links.create', 'connection_links.update', 'connection_links.destroy', 'route_sections.create', 'route_sections.update', 'route_sections.destroy',
- 'referentials.create', 'referentials.update', 'referentials.destroy'])
+ @user ||=
+ create(:user,
+ :organisation => organisation,
+ :permissions => Support::Permissions.all_permissions)
+
login_as @user, :scope => :user
# post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
end
@@ -38,28 +36,12 @@ end
module DeviseControllerHelper
def setup_user
- _all_actions = %w{create destroy update}
- _all_resources = %w{ access_links
- access_points
- connection_links
- footnotes
- journey_patterns
- referentials
- route_sections
- routes
- routing_constraint_zones
- time_tables
- vehicle_journeys }
- join_with = -> (separator) do
- -> (ary) { ary.join(separator) }
- end
-
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
organisation = Organisation.where(:code => "first").first_or_create(attributes_for(:organisation))
@user = create(:user,
organisation: organisation,
- permissions: _all_resources.product( _all_actions ).map(&join_with.('.')))
+ permissions: Support::Permissions.all_permissions)
end
end
@@ -70,8 +52,6 @@ module DeviseControllerHelper
end
end
- private
-
end
RSpec.configure do |config|
diff --git a/spec/support/permissions.rb b/spec/support/permissions.rb
new file mode 100644
index 000000000..a13010f65
--- /dev/null
+++ b/spec/support/permissions.rb
@@ -0,0 +1,28 @@
+module Support
+ module Permissions extend self
+
+ def all_permissions
+ @__all_permissions__ ||= _destructive_permissions << 'sessions:create'
+ end
+
+ private
+
+ def _destructive_permissions
+ _permitted_resources.product( %w{create destroy update} ).map{ |model_action| model_action.join('.') }
+ end
+
+ def _permitted_resources
+ %w[
+ access_points
+ connection_links
+ footnotes
+ journey_patterns
+ referentials
+ routes
+ routing_constraint_zones
+ time_tables
+ vehicle_journeys
+ ]
+ end
+ end
+end
diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb
index b91caa479..63a106759 100644
--- a/spec/support/pundit/shared_examples.rb
+++ b/spec/support/pundit/shared_examples.rb
@@ -64,7 +64,7 @@ RSpec.shared_examples 'always forbidden' do
end
end
end
-j
+
RSpec.shared_examples 'permitted policy and same organisation' do
| permission, archived: false|
@@ -101,3 +101,31 @@ RSpec.shared_examples 'permitted policy and same organisation' do
end
end
end
+
+RSpec.shared_examples 'permitted policy' do
+ | permission, archived: false|
+
+ context 'permission absent → ' do
+ it "denies user" do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
+
+ context 'permission present → ' do
+ before do
+ add_permissions(permission, for_user: user)
+ end
+
+ it 'allows user' do
+ expect_it.to permit(user_context, record)
+ end
+
+ if archived
+ it 'removes the permission for archived referentials' do
+ user.organisation_id = referential.organisation_id
+ referential.archived_at = 42.seconds.ago
+ expect_it.not_to permit(user_context, record)
+ end
+ end
+ end
+end