Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class ApplicationController < ActionController::Base
flash[:alert] = exception.message.presence || "You are not authorized to perform this action."
redirect_back_or_to root_path
end

def default_authorization_policy_class
ApplicationPolicy
end
private

def after_sign_in_path_for(resource)
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class TagsController < ApplicationController
def index
authorize!
end

def sectors
authorize! Sector, to: :tags_index?
@sectors = Sector.published.order(:name)
end

def categories
authorize! Category, to: :tags_index?
@categories_by_type = Category
.published
.joins(:category_type)
Expand Down
1 change: 1 addition & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ApplicationPolicy < ActionPolicy::Base
authorize :user, optional: true, allow_nil: true
pre_check :verify_authenticated!

default_rule :manage?
alias_rule :new?, :create?, :edit?, :update?, :destroy?, to: :manage?

def manage?
Expand Down
9 changes: 9 additions & 0 deletions app/policies/category_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CategoryPolicy < ApplicationPolicy
# See https://actionpolicy.evilmartians.io/#/writing_policies
#
# override or add new rules here that are not defined in ApplicationPolicy

def tags_index?
true
end
end
9 changes: 9 additions & 0 deletions app/policies/sector_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SectorPolicy < ApplicationPolicy
# See https://actionpolicy.evilmartians.io/#/writing_policies
#
# override or add new rules here that are not defined in ApplicationPolicy

def tags_index?
true
end
end
30 changes: 30 additions & 0 deletions spec/policies/category_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "rails_helper"

RSpec.describe CategoryPolicy, type: :policy do
let(:admin_user) { build_stubbed :user, super_user: true }
let(:regular_user) { build_stubbed :user, super_user: false }

def policy_for(record: nil, user:)
described_class.new(record, user: user)
end

describe "#tags_index?" do
context "with admin user" do
subject { policy_for(user: admin_user) }

it { is_expected.to be_allowed_to(:tags_index?) }
end

context "with regular user" do
subject { policy_for(user: regular_user) }

it { is_expected.to be_allowed_to(:tags_index?) }
end

context "with no user" do
subject { policy_for(user: nil) }

it { is_expected.not_to be_allowed_to(:tags_index?) }
end
end
end
30 changes: 30 additions & 0 deletions spec/policies/sector_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "rails_helper"

RSpec.describe SectorPolicy, type: :policy do
let(:admin_user) { build_stubbed :user, super_user: true }
let(:regular_user) { build_stubbed :user, super_user: false }

def policy_for(record: nil, user:)
described_class.new(record, user: user)
end

describe "#tags_index?" do
context "with admin user" do
subject { policy_for(user: admin_user) }

it { is_expected.to be_allowed_to(:tags_index?) }
end

context "with regular user" do
subject { policy_for(user: regular_user) }

it { is_expected.to be_allowed_to(:tags_index?) }
end

context "with no user" do
subject { policy_for(user: nil) }

it { is_expected.not_to be_allowed_to(:tags_index?) }
end
end
end
7 changes: 7 additions & 0 deletions spec/requests/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

before { sign_in user }

it "checks authorization via ApplicationPolicy" do
expect_any_instance_of(ApplicationPolicy)
.to receive(:index?).and_call_original

get tags_path
end

it "renders Service Populations and Categories skeleton" do
get tags_path
expect(response).to have_http_status(:ok)
Expand Down