Skip to content

Commit

Permalink
Add a new ActiveJobExtensions which hooks into the serialization and …
Browse files Browse the repository at this point in the history
…deserialization of an ActiveJob to preserve the current tenant. (#319)
  • Loading branch information
tvongaza authored Dec 7, 2023
1 parent 69d2162 commit 6ecf5d2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/acts_as_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 @@ -161,3 +162,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

0 comments on commit 6ecf5d2

Please sign in to comment.