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

Preserve current tenant through active job #319

Merged
merged 1 commit into from
Dec 7, 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
5 changes: 5 additions & 0 deletions lib/acts_as_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module ActsAsTenant
autoload :ControllerExtensions, "acts_as_tenant/controller_extensions"
autoload :ModelExtensions, "acts_as_tenant/model_extensions"
autoload :TenantHelper, "acts_as_tenant/tenant_helper"
autoload :ActiveJobExtensions, "acts_as_tenant/active_job_extensions"

@@configuration = nil
@@tenant_klass = nil
Expand Down Expand Up @@ -158,3 +159,7 @@ def self.should_require_tenant?
ActiveSupport.on_load(:action_view) do |base|
base.include ActsAsTenant::TenantHelper
end

ActiveSupport.on_load(:active_job) do |base|
base.prepend ActsAsTenant::ActiveJobExtensions
end
13 changes: 13 additions & 0 deletions lib/acts_as_tenant/active_job_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ActsAsTenant
module ActiveJobExtensions
def serialize
super.merge("current_tenant" => ActsAsTenant.current_tenant&.to_global_id)
end

def deserialize(job_data)
tenant_global_id = job_data.delete("current_tenant")
ActsAsTenant.current_tenant = tenant_global_id ? GlobalID::Locator.locate(tenant_global_id) : nil
super
end
end
end
49 changes: 49 additions & 0 deletions spec/jobs/active_job_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "spec_helper"

class ApplicationTestJob < ApplicationJob
def perform(expected_tenant:)
raise ApplicationTestJobTenantError unless ActsAsTenant.current_tenant == expected_tenant
Project.all
end
end

class ApplicationTestJobTenantError < StandardError; end

RSpec.describe ApplicationTestJob, type: :job do
include ActiveJob::TestHelper

let(:account) { accounts(:foo) }

describe "#perform_later" do
context "when tenant is required" do
before { allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true) }

it "raises ApplicationTestJobTenantError when expected_tenant does not match current_tenant" do
ActsAsTenant.current_tenant = account
expect { described_class.perform_later(expected_tenant: nil) }.to have_enqueued_job.on_queue("default")
expect { perform_enqueued_jobs }.to raise_error(ApplicationTestJobTenantError)
end

it "when tenant is set, successfully queues and performs job" do
ActsAsTenant.current_tenant = account
expect { described_class.perform_later(expected_tenant: account) }.to have_enqueued_job.on_queue("default")
expect { perform_enqueued_jobs }.not_to raise_error
end

it "when tenant is not set, successfully queues but fails to perform job" do
ActsAsTenant.current_tenant = nil
expect { described_class.perform_later(expected_tenant: nil) }.to have_enqueued_job.on_queue("default")
expect { perform_enqueued_jobs }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
end
end

context "when tenant is not required" do
before { allow(ActsAsTenant.configuration).to receive_messages(require_tenant: false) }
it "when tenant is not set, queues and performs job" do
ActsAsTenant.current_tenant = nil
expect { described_class.perform_later(expected_tenant: nil) }.to have_enqueued_job.on_queue("default")
expect { perform_enqueued_jobs }.not_to raise_error
end
end
end
end