-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new ActiveJobExtensions which hooks into the serialization and …
…deserialization of an ActiveJob to preserve the current tenant. (#319)
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |