From e9b778d44f0dc54d4a92804448065c20f7f4ff69 Mon Sep 17 00:00:00 2001 From: Greg Fisher Date: Fri, 7 Apr 2017 10:03:05 -0300 Subject: [PATCH 1/5] Add scenario for missing `replace_?` policy action on `replace_fields` --- .../authorization/default_pundit_authorizer_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb index 63599ab5..6c85858f 100644 --- a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb +++ b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb @@ -189,6 +189,10 @@ before { stub_policy_actions(source_record, replace_comments?: false, update?: true) } it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } end + + context 'were replace_? is undefined' do + it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } + end end context 'unauthorized for update? on source record' do @@ -208,6 +212,10 @@ before { stub_policy_actions(source_record, replace_comments?: false, update?: false) } it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } end + + context 'were replace_? is undefined' do + it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + end end end From e7fc0b374c6b4bb60bde4813aef5c4650c71c841 Mon Sep 17 00:00:00 2001 From: Greg Fisher Date: Wed, 12 Apr 2017 16:10:40 -0300 Subject: [PATCH 2/5] Rearrange specs for replace_fields and add coverage --- .../default_pundit_authorizer_spec.rb | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb index 6c85858f..9f66d9ae 100644 --- a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb +++ b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb @@ -168,52 +168,51 @@ records: related_records }] end + subject(:method_call) do -> { authorizer.replace_fields(source_record, related_records_with_context) } end - context 'authorized for update? on source record' do + context 'authorized for update? on source record and related records is empty' do before { allow_action(source_record, 'update?') } - - context 'related records is empty' do - let(:related_records) { [] } - it { is_expected.not_to raise_error } - end - - context 'authorized for replace_comments? on source record' do - before { stub_policy_actions(source_record, replace_comments?: true, update?: true) } - it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } - end - - context 'unauthorized for replace_comments? on source record' do - before { stub_policy_actions(source_record, replace_comments?: false, update?: true) } - it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } - end - - context 'were replace_? is undefined' do - it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } - end + let(:related_records) { [] } + it { is_expected.not_to raise_error } end - context 'unauthorized for update? on source record' do + context 'unauthorized for update? on source record and related records is empty' do before { disallow_action(source_record, 'update?') } + let(:related_records) { [] } + it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + end - context 'related records is empty' do - let(:related_records) { [] } - it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } - end + context 'authorized for replace_comments? and authorized for update? on source record' do + before { stub_policy_actions(source_record, replace_comments?: true, update?: true) } + it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } + end - context 'authorized for replace_comments? on source record' do - before { stub_policy_actions(source_record, replace_comments?: true, update?: false) } - it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } - end + context 'unauthorized for replace_comments? and authorized for update? on source record' do + before { stub_policy_actions(source_record, replace_comments?: false, update?: true) } + it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + end + + context 'authorized for replace_comments? and unauthorized for update? on source record' do + before { stub_policy_actions(source_record, replace_comments?: true, update?: false) } + it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + end - context 'unauthorized for replace_comments? on source record' do - before { stub_policy_actions(source_record, replace_comments?: false, update?: false) } - it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + context 'unauthorized for replace_comments? and unauthorized for update? on source record' do + before { stub_policy_actions(source_record, replace_comments?: false, update?: false) } + it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } + end + + context 'where replace_? is undefined' do + context 'authorized for update? on source record' do + before { stub_policy_actions(source_record, update?: true) } + it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } end - context 'were replace_? is undefined' do + context 'unauthorized for update? on source record' do + before { stub_policy_actions(source_record, update?: false) } it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } end end From fdbcff77ff096f0ab0a020648f55f646b1b951da Mon Sep 17 00:00:00 2001 From: Greg Fisher Date: Wed, 12 Apr 2017 16:58:18 -0300 Subject: [PATCH 3/5] Remove whitespace --- spec/jsonapi/authorization/default_pundit_authorizer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb index 9f66d9ae..b27919cd 100644 --- a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb +++ b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb @@ -194,7 +194,7 @@ before { stub_policy_actions(source_record, replace_comments?: false, update?: true) } it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } end - + context 'authorized for replace_comments? and unauthorized for update? on source record' do before { stub_policy_actions(source_record, replace_comments?: true, update?: false) } it { is_expected.to raise_error(::Pundit::NotAuthorizedError) } From 4d947a888ef7e745b9a311fffa3b5475f5412655 Mon Sep 17 00:00:00 2001 From: Greg Fisher Date: Thu, 13 Apr 2017 20:49:24 -0300 Subject: [PATCH 4/5] Fix spec, dont expect specific error not to be raised --- spec/jsonapi/authorization/default_pundit_authorizer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb index b27919cd..61946c6e 100644 --- a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb +++ b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb @@ -187,7 +187,7 @@ context 'authorized for replace_comments? and authorized for update? on source record' do before { stub_policy_actions(source_record, replace_comments?: true, update?: true) } - it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } + it { is_expected.not_to raise_error } end context 'unauthorized for replace_comments? and authorized for update? on source record' do From 3fce5962e66ed4cc856b956aca235ca7b5bb33e3 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Sat, 15 Apr 2017 11:55:26 +0300 Subject: [PATCH 5/5] Fix too specific not_to raise_error assert --- spec/jsonapi/authorization/default_pundit_authorizer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb index 61946c6e..b6e180b4 100644 --- a/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb +++ b/spec/jsonapi/authorization/default_pundit_authorizer_spec.rb @@ -208,7 +208,7 @@ context 'where replace_? is undefined' do context 'authorized for update? on source record' do before { stub_policy_actions(source_record, update?: true) } - it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } + it { is_expected.not_to raise_error } end context 'unauthorized for update? on source record' do