ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.56k stars 264 forks source link

Saving wrong tentant when using factory bot #317

Open leoncruz opened 1 year ago

leoncruz commented 1 year ago

This is how I configure the tenant for request specs on rails_helper.rb:

config.before(:each, type: :request) do |spec|
  unless spec.metadata[:without_agency]
    agency = create(:agency, name: 'Default Agency')

    ActsAsTenant.test_tenant = agency

    host! "#{agency.subdomain}.example.com"
  end
end

config.after(:each, type: :request) do
   ActsAsTenant.test_tenant = nil
end

This is my test:

let!(:agency) { create(:agency) }

let!(:user) { create(:user, agency: agency) }

let!(:access_token) { create(:access_token, user: user) }

let(:headers) do
  {
    'Authorization' => "Bearer #{access_token.unhashed_access_token}"
  }
end

context 'when agency from subdomain exists' do
  it do
    host! "#{agency.subdomain}.example.com"

    get '/dummies', headers: headers

    expect(Current.agency).to be agency
  end
end

In the controller, the current_tenant is the agency created on test, but the user agency is the created on rails_helper.rb:

image

image

image

I'm probably missing out something. What I doing wrong?