aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert2017-06-30 17:51:31 +0200
committerRobert2017-06-30 17:54:49 +0200
commit1a8b68be2a43ff20db73b9e9571ba3eff8d3c4ae (patch)
tree86eddefb1bdfd9969b317681c7aafe8cb4fa4805
parentba68d5ec06644e5d2d698a6a889f16e3e684705f (diff)
downloadchouette-core-1a8b68be2a43ff20db73b9e9571ba3eff8d3c4ae.tar.bz2
Spec fix and optimization
- Fixed bugs in Policy Specs (Shared methods were not using records) - Using build_stubbed instead of create for all Policy Methods (5.43s -> 3.75s)
-rw-r--r--spec/policies/application_policy_spec.rb2
-rw-r--r--spec/policies/boiv_policy_spec.rb2
-rw-r--r--spec/policies/line_policy_spec.rb3
-rw-r--r--spec/policies/route_policy_spec.rb2
-rw-r--r--spec/policies/routing_constraint_zone_policy_spec.rb3
-rw-r--r--spec/policies/time_table_policy_spec.rb3
-rw-r--r--spec/support/pundit/policies.rb4
-rw-r--r--spec/support/pundit/shared_examples.rb26
8 files changed, 29 insertions, 16 deletions
diff --git a/spec/policies/application_policy_spec.rb b/spec/policies/application_policy_spec.rb
index a7234461e..3ec177209 100644
--- a/spec/policies/application_policy_spec.rb
+++ b/spec/policies/application_policy_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe ApplicationPolicy, type: :policy do
end
it "allows a user with a different organisation" do
- user.update_attribute :organisation, referential.organisation
+ user.organisation = referential.organisation
expect_it.to permit(user_context, referential)
end
end
diff --git a/spec/policies/boiv_policy_spec.rb b/spec/policies/boiv_policy_spec.rb
index 514534adc..6787ab2ac 100644
--- a/spec/policies/boiv_policy_spec.rb
+++ b/spec/policies/boiv_policy_spec.rb
@@ -1,5 +1,7 @@
RSpec.describe BoivPolicy, type: :policy do
+ let( :record ){ nil }
+
permissions :index? do
it_behaves_like 'permitted policy and same organisation', 'boiv:read-offer'
end
diff --git a/spec/policies/line_policy_spec.rb b/spec/policies/line_policy_spec.rb
index ead5918aa..e720b2bc7 100644
--- a/spec/policies/line_policy_spec.rb
+++ b/spec/policies/line_policy_spec.rb
@@ -1,5 +1,8 @@
RSpec.describe LinePolicy, type: :policy do
+ let( :record ){ build_stubbed :line }
+
+
%w{create destroy edit}.each do | permission |
footnote_permission = "#{permission}_footnote"
permissions "#{footnote_permission}?".to_sym do
diff --git a/spec/policies/route_policy_spec.rb b/spec/policies/route_policy_spec.rb
index baf14c9fc..cc949ff45 100644
--- a/spec/policies/route_policy_spec.rb
+++ b/spec/policies/route_policy_spec.rb
@@ -1,5 +1,7 @@
RSpec.describe RoutePolicy, type: :policy do
+ let( :record ){ build_stubbed :route }
+
permissions :create? do
it_behaves_like 'permitted policy', 'routes.create', archived: true
end
diff --git a/spec/policies/routing_constraint_zone_policy_spec.rb b/spec/policies/routing_constraint_zone_policy_spec.rb
index 4b0f2cafe..2508b49f9 100644
--- a/spec/policies/routing_constraint_zone_policy_spec.rb
+++ b/spec/policies/routing_constraint_zone_policy_spec.rb
@@ -1,5 +1,8 @@
RSpec.describe RoutingConstraintZonePolicy, type: :policy do
+ let( :record ){ build_stubbed :routing_constraint_zone }
+
+
permissions :create? do
it_behaves_like 'permitted policy', 'routing_constraint_zones.create', archived: true
end
diff --git a/spec/policies/time_table_policy_spec.rb b/spec/policies/time_table_policy_spec.rb
index 1283a9fcf..90e6600ea 100644
--- a/spec/policies/time_table_policy_spec.rb
+++ b/spec/policies/time_table_policy_spec.rb
@@ -1,5 +1,8 @@
RSpec.describe TimeTablePolicy, type: :policy do
+ let( :record ){ build_stubbed :time_table }
+
+
permissions :duplicate? do
it_behaves_like 'permitted policy and same organisation', 'time_tables.create', archived: true
end
diff --git a/spec/support/pundit/policies.rb b/spec/support/pundit/policies.rb
index 56433b2ee..02fea2944 100644
--- a/spec/support/pundit/policies.rb
+++ b/spec/support/pundit/policies.rb
@@ -24,8 +24,8 @@ module Support
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 }
+ let( :referential ) { build_stubbed :referential }
+ let( :user ) { build_stubbed :user }
end
end
def with_user_permission(permission, &blk)
diff --git a/spec/support/pundit/shared_examples.rb b/spec/support/pundit/shared_examples.rb
index 4d14c46da..33ed1ffae 100644
--- a/spec/support/pundit/shared_examples.rb
+++ b/spec/support/pundit/shared_examples.rb
@@ -3,11 +3,11 @@ RSpec.shared_examples 'permitted policy and same organisation' do
context 'permission absent → ' do
it "denies a user with a different organisation" do
- expect_it.not_to permit(user_context, referential)
+ expect_it.not_to permit(user_context, record)
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)
+ user.organisation = referential.organisation
+ expect_it.not_to permit(user_context, record)
end
end
@@ -17,19 +17,19 @@ RSpec.shared_examples 'permitted policy and same organisation' do
end
it 'denies a user with a different organisation' do
- expect_it.not_to permit(user_context, referential)
+ expect_it.not_to permit(user_context, record)
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)
+ user.organisation = referential.organisation
+ expect_it.to permit(user_context, record)
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)
+ user.organisation = referential.organisation
+ referential.archived_at = 42.seconds.ago
+ expect_it.not_to permit(user_context, record)
end
end
end
@@ -39,7 +39,7 @@ 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)
+ expect_it.not_to permit(user_context, record)
end
end
context 'permission present → ' do
@@ -47,13 +47,13 @@ RSpec.shared_examples 'permitted policy' do
add_permissions(permission, for_user: user)
end
it 'allows a user with a different organisation' do
- expect_it.to permit(user_context, referential)
+ expect_it.to permit(user_context, record)
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)
+ referential.archived_at = 42.seconds.ago
+ expect_it.not_to permit(user_context, record)
end
end
end