Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠️ Neighborhood: Expose sidekiq-web ui to Operators #1317

Merged
merged 2 commits into from
Apr 6, 2023
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
10 changes: 10 additions & 0 deletions app/models/operator/route_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Operator
# Restricts a route to Operators
# @see https://guides.rubyonrails.org/routing.html#specifying-constraints
class RouteConstraint
def matches?(request)
return false unless request.session[:person_id]
Person.find_by(id: request.session[:person_id])&.operator?
end
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "sidekiq/web"

Rails.application.routes.draw do
mount Rswag::Ui::Engine => "/api-docs"
mount Rswag::Api::Engine => "/api-docs"
Expand Down Expand Up @@ -28,4 +30,5 @@
end

root "neighborhoods#show"
mount Sidekiq::Web => "/sidekiq", :constraints => Operator::RouteConstraint.new
end
14 changes: 14 additions & 0 deletions spec/models/operator/route_constraint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "rails_helper"

RSpec.describe Operator::RouteConstraint do
subject(:route_constraint) { described_class.new }

before do
allow(Person).to receive(:find_by).with(id: "operator-1234").and_return(instance_double(Operator, operator?: true))
allow(Person).to receive(:find_by).with(id: "neighbor-1234").and_return(instance_double(Person, operator?: false))
end

it { is_expected.not_to be_matches(instance_double(Rack::Request, session: {})) }
it { is_expected.to be_matches(instance_double(Rack::Request, session: {person_id: "operator-1234"})) }
it { is_expected.not_to be_matches(instance_double(Rack::Request, session: {person_id: "neighbor-1234"})) }
end