Skip to content
Open
Prev Previous commit
Next Next commit
Refactor ActionPolicyAdapter tests for clarity and error handling
- Updated the test for differentiating between `:new` and `:create` abilities to improve clarity.
- Added tests to ensure proper authorization behavior for new post instances.
- Introduced a context to handle cases where the default policy does not exist, ensuring appropriate error handling.

These changes enhance the robustness of the ActionPolicyAdapter tests and improve error management.
  • Loading branch information
amkisko committed Dec 18, 2025
commit ea1d31e3b636192d0ea2d36b3308b650405ab505
19 changes: 17 additions & 2 deletions spec/unit/action_policy_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
expect(auth.authorized?(:update, Post)).to eq false
end

it "should treat :new ability the same as :create" do
it "should allow differentiating between new and create" do
expect(auth.authorized?(:new, Post)).to eq true
expect(auth.authorized?(:create, Post)).to eq true
expect(auth.authorized?(ActiveAdmin::Auth::NEW, Post)).to eq true

announcement_category = Category.new(name: "Announcements")
announcement_post = Post.new(title: "Big announcement", category: announcement_category)
expect(auth.authorized?(:create, announcement_post)).to eq false
expect(auth.authorized?(ActiveAdmin::Auth::CREATE, announcement_post)).to eq false
end

it "should scope the collection" do
Expand Down Expand Up @@ -54,6 +59,16 @@
end

it("should return default policy instance") { is_expected.to be_instance_of(ActionPolicy::ApplicationPolicy) }

context "and default policy doesn't exist" do
before do
allow(ActiveAdmin.application).to receive(:action_policy_default_policy).and_return nil
end

it "raises the error" do
expect { subject }.to raise_error ActionPolicy::NotFound
end
end
end
end
end