Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevschmid committed Mar 25, 2024
1 parent a9da2a5 commit d568709
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/rls/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

module RLS
class Railtie < Rails::Railtie
config.rls = ActiveSupport::OrderedOptions.new
config.rls.role = "app_rls"

config.to_prepare do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(RLS::Extensions::PostgreSQLAdapter)
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(RLS::Extensions::SchemaDumper)
Expand All @@ -21,6 +18,11 @@ class Railtie < Rails::Railtie
ActiveRecord::Migration.include(RLS::Migration)
end

initializer "rls.configure" do |app|
app.config.rls = ActiveSupport::OrderedOptions.new
app.config.rls.role = "#{app.class.module_parent_name.underscore}_rls_#{Rails.env}"
end

rake_tasks do
load "tasks/rls.rake"
end
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/rls.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rake.application.top_level_tasks.each do |task_name|
Rake::Task.tasks.each do |task|
task_name = task.name
next unless task_name.start_with?("db:")

# disable RLS role before running the task
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/postgresql_adapter_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

specify do
role = connection.query_value("SHOW ROLE")
expect(role).to eq "app_rls"
expect(role).to eq "dummy_rls_test"
end

context "when admin" do
Expand Down
17 changes: 14 additions & 3 deletions spec/integration/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

it "limits posts to the tenant specified by the subdomain" do
host! "acme.rls.test"
post "/posts", params: { post: { title: "ACME Post", content: "This is content", tenant_id: acme.id} }
post "/posts", params: {
post: {
title: "ACME Post",
content: "This is content",
tenant_id: acme.id,
},
}
expect(response).to have_http_status(:found)

get "/posts"
Expand All @@ -19,8 +25,13 @@
get "/posts"
expect(response.body).not_to include("ACME Post")

post "/posts",
params: { post: { title: "New G-Virus", content: "This is classified information", tenant_id: umbrella_corp.id } }
post "/posts", params: {
post: {
title: "New G-Virus",
content: "This is classified information",
tenant_id: umbrella_corp.id,
},
}
expect(response).to have_http_status(:found)

get "/posts"
Expand Down
4 changes: 2 additions & 2 deletions spec/tasks/rls_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

specify do
expect(RLS).to receive(:disable!).ordered
expect(RLS.connection).to receive(:execute).with(/CREATE ROLE "app_rls"/).ordered
expect(RLS.connection).to receive(:execute).with(/CREATE ROLE "dummy_rls_test"/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
end
Expand All @@ -38,7 +38,7 @@

specify do
expect(RLS).to receive(:disable!).ordered
expect(RLS.connection).to receive(:execute).with(/DROP ROLE "app_rls"/).ordered
expect(RLS.connection).to receive(:execute).with(/DROP ROLE "dummy_rls_test"/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/rls/extensions/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def clear_query_cache

describe "#initialize" do
it "sets the role" do
expect_any_instance_of(MyAdapter).to receive(:execute).with("SET ROLE 'app_rls'")
expect_any_instance_of(MyAdapter).to receive(:execute).with("SET ROLE 'dummy_rls_test'")
connection # initialize
end

context "when admin" do
before { RLS.admin = true }

it "does not set the role" do
expect_any_instance_of(MyAdapter).not_to receive(:execute).with("SET ROLE 'app_rls'")
expect_any_instance_of(MyAdapter).not_to receive(:execute).with("SET ROLE 'dummy_rls_test'")
connection # initialize
end
end
Expand Down

0 comments on commit d568709

Please sign in to comment.